JSAST.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <rapidjson/document.h>
  8. #include <rapidjson/stringbuffer.h>
  9. #include <rapidjson/prettywriter.h>
  10. #include <Atomic/IO/Log.h>
  11. #include "JSAST.h"
  12. #include "JSASTVisitor.h"
  13. using namespace rapidjson;
  14. namespace AtomicEditor
  15. {
  16. JSASTNode::JSASTNode(JSASTType type) : type_(type), rangeStart_(0), rangeEnd_(0)
  17. {
  18. }
  19. bool JSASTNode::Parse(const rapidjson::Value& value)
  20. {
  21. assert(value.IsObject());
  22. for (Value::ConstMemberIterator itr = value.MemberBegin();
  23. itr != value.MemberEnd(); ++itr)
  24. {
  25. String name = itr->name.GetString();
  26. if (name == "loc")
  27. {
  28. ParseLoc(itr->value);
  29. }
  30. else if (name == "type")
  31. {
  32. // TODO: Verify type
  33. }
  34. else if (name == "range")
  35. {
  36. ParseRange(itr->value);
  37. }
  38. }
  39. return true;
  40. }
  41. bool JSASTNode::ParseLoc(const rapidjson::Value& value)
  42. {
  43. // SpiderMonkey can have this for loc :/
  44. if (value.IsNull())
  45. return true;
  46. assert(value.IsObject());
  47. const Value::Member* mstart = value.FindMember("start");
  48. assert(mstart);
  49. const Value::Member* mend = value.FindMember("end");
  50. assert(mend);
  51. loc_.startLine_ = mstart->value["line"].GetInt();
  52. loc_.startColumn_ = mstart->value["column"].GetInt();
  53. loc_.endLine_ = mend->value["line"].GetInt();
  54. loc_.endColumn_ = mend->value["column"].GetInt();
  55. return true;
  56. }
  57. bool JSASTNode::ParseRange(const rapidjson::Value& value)
  58. {
  59. assert(value.IsArray() && value.Size() == 2);
  60. rangeStart_ = value[SizeType(0)].GetInt();
  61. rangeEnd_ = value[SizeType(1)].GetInt();
  62. return true;
  63. }
  64. JSASTStatement* JSASTNode::ParseStatement(const rapidjson::Value &value)
  65. {
  66. assert(value.IsObject());
  67. JSASTStatement* statement = NULL;
  68. const Value::Member* mtype = value.FindMember("type");
  69. assert(mtype);
  70. String type = mtype->value.GetString();
  71. if (type == "ExpressionStatement")
  72. {
  73. statement = new JSASTExpressionStatement();
  74. }
  75. else if (type == "VariableDeclaration")
  76. {
  77. statement = new JSASTVariableDeclaration();
  78. }
  79. else if (type == "ReturnStatement")
  80. {
  81. statement = new JSASTReturnStatement();
  82. }
  83. else if (type == "FunctionDeclaration")
  84. {
  85. statement = new JSASTFunctionDeclaration();
  86. }
  87. else if (type == "IfStatement")
  88. {
  89. statement = new JSASTIfStatement();
  90. }
  91. else if (type == "BlockStatement")
  92. {
  93. statement = new JSASTBlockStatement();
  94. }
  95. else if (type == "EmptyStatement")
  96. {
  97. statement = new JSASTBlockStatement();
  98. }
  99. else if (type == "ForStatement")
  100. {
  101. statement = new JSASTForStatement();
  102. }
  103. else if (type == "LabeledStatement")
  104. {
  105. statement = new JSASTLabeledStatement();
  106. }
  107. if (!statement)
  108. {
  109. LOGWARNINGF("Unknown JSASTStatement: %s", type.CString());
  110. }
  111. else
  112. {
  113. statement->Parse(value);
  114. }
  115. return statement;
  116. }
  117. JSASTExpression* JSASTNode::ParseExpression(const rapidjson::Value &value, JSASTType astType)
  118. {
  119. if (!value.IsObject())
  120. return NULL;
  121. JSASTExpression* expr = NULL;
  122. const Value::Member* mtype = value.FindMember("type");
  123. assert(mtype);
  124. String type = mtype->value.GetString();
  125. if (type == "Identifier" && (astType == JSAST_UNDEFINED || astType == JSAST_IDENTIFIER))
  126. {
  127. expr = new JSASTIdentifier();
  128. }
  129. else if (type == "Literal" && (astType == JSAST_UNDEFINED || astType == JSAST_LITERAL))
  130. {
  131. expr = new JSASTLiteral();
  132. }
  133. else if (type == "UnaryExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_UNARYEXPRESSION))
  134. {
  135. expr = new JSASTUnaryExpression();
  136. }
  137. else if (type == "UpdateExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_UPDATEEXPRESSION))
  138. {
  139. expr = new JSASTUpdateExpression();
  140. }
  141. else if (type == "NewExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_NEWEXPRESSION))
  142. {
  143. expr = new JSASTNewExpression();
  144. }
  145. else if (type == "FunctionExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_FUNCTIONEXPRESSION))
  146. {
  147. expr = new JSASTFunctionExpression();
  148. }
  149. else if (type == "BinaryExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_BINARYEXPRESSION))
  150. {
  151. expr = new JSASTBinaryExpression();
  152. }
  153. else if (type == "AssignmentExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_ASSIGNMENTEXPRESSION))
  154. {
  155. expr = new JSASTAssignmentExpression();
  156. }
  157. else if (type == "LogicalExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_LOGICALEXPRESSION))
  158. {
  159. expr = new JSASTAssignmentExpression();
  160. }
  161. else if (type == "CallExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_CALLEXPRESSION))
  162. {
  163. expr = new JSASTCallExpression();
  164. }
  165. else if (type == "VariableDeclarator" && (astType == JSAST_UNDEFINED || astType == JSAST_VARIABLEDECLARATOR))
  166. {
  167. expr = new JSASTVariableDeclarator();
  168. }
  169. else if (type == "MemberExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_MEMBEREXPRESSION))
  170. {
  171. expr = new JSASTMemberExpression();
  172. }
  173. else if (type == "ArrayExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_ARRAYEXPRESSION))
  174. {
  175. expr = new JSASTArrayExpression();
  176. }
  177. else if (type == "ObjectExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_OBJECTEXPRESSION))
  178. {
  179. expr = new JSASTObjectExpression();
  180. }
  181. else if (type == "ConditionalExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_CONDITIONALEXPRESSION))
  182. {
  183. expr = new JSASTConditionalExpression();
  184. }
  185. else if (type == "ThisExpression" && (astType == JSAST_UNDEFINED || astType == JSAST_THISEXPRESSION))
  186. {
  187. expr = new JSASTThisExpression();
  188. }
  189. if (!expr)
  190. {
  191. LOGWARNINGF("Unknown JSASTExpression: %s", type.CString());
  192. }
  193. else
  194. {
  195. expr->Parse(value);
  196. }
  197. return expr;
  198. }
  199. bool JSASTNode::ParseExpressionArray(const rapidjson::Value &value, Vector<JSASTExpression *> &expressions)
  200. {
  201. assert(value.IsArray());
  202. for (Value::ConstValueIterator itr = value.Begin(); itr != value.End(); itr++)
  203. {
  204. JSASTExpression* expr = ParseExpression(*itr);
  205. //assert(expr);
  206. expressions.Push(expr);
  207. }
  208. return true;
  209. }
  210. bool JSASTNode::ParseStatementArray(const rapidjson::Value &value, Vector<JSASTStatement *> &statements)
  211. {
  212. assert(value.IsArray());
  213. for (Value::ConstValueIterator itr = value.Begin(); itr != value.End(); itr++)
  214. {
  215. JSASTStatement* stmt = ParseStatement(*itr);
  216. //assert(stmt);
  217. statements.Push(stmt);
  218. }
  219. return true;
  220. }
  221. bool JSASTThisExpression::Parse(const rapidjson::Value& value)
  222. {
  223. JSASTExpression::Parse(value);
  224. return true;
  225. }
  226. void JSASTThisExpression::Accept(JSASTVisitor* visitor)
  227. {
  228. visitor->visit(this);
  229. }
  230. bool JSASTIdentifier::Parse(const rapidjson::Value& value)
  231. {
  232. JSASTExpression::Parse(value);
  233. if (value.FindMember("name"))
  234. name_ = value["name"].GetString();
  235. return true;
  236. }
  237. void JSASTIdentifier::Accept(JSASTVisitor* visitor)
  238. {
  239. visitor->visit(this);
  240. }
  241. bool JSASTAssignmentExpression::Parse(const rapidjson::Value& value)
  242. {
  243. JSASTExpression::Parse(value);
  244. operator_ = value["operator"].GetString();
  245. leftExpression_ = ParseExpression(value["left"]);
  246. rightExpression_ = ParseExpression(value["right"]);
  247. return true;
  248. }
  249. void JSASTAssignmentExpression::Accept(JSASTVisitor* visitor)
  250. {
  251. visitor->visit(this);
  252. }
  253. bool JSASTLogicalExpression::Parse(const rapidjson::Value& value)
  254. {
  255. JSASTExpression::Parse(value);
  256. operator_ = value["operator"].GetString();
  257. leftExpression_ = ParseExpression(value["left"]);
  258. rightExpression_ = ParseExpression(value["right"]);
  259. return true;
  260. }
  261. void JSASTLogicalExpression::Accept(JSASTVisitor* visitor)
  262. {
  263. visitor->visit(this);
  264. }
  265. bool JSASTBinaryExpression::Parse(const rapidjson::Value& value)
  266. {
  267. JSASTExpression::Parse(value);
  268. operator_ = value["operator"].GetString();
  269. leftExpression_ = ParseExpression(value["left"]);
  270. rightExpression_ = ParseExpression(value["right"]);
  271. return true;
  272. }
  273. void JSASTBinaryExpression::Accept(JSASTVisitor* visitor)
  274. {
  275. visitor->visit(this);
  276. }
  277. bool JSASTLiteral::Parse(const rapidjson::Value& value)
  278. {
  279. JSASTExpression::Parse(value);
  280. if (!value.FindMember("value") || !value.FindMember("raw"))
  281. {
  282. literalType_ = LITERAL_UNKNOWN;
  283. return false;
  284. }
  285. const rapidjson::Value& v = value["value"];
  286. raw_ = value["raw"].GetString();;
  287. if (v.IsNull())
  288. literalType_ = LITERAL_NULL;
  289. else if (v.IsString())
  290. literalType_ = LITERAL_STRING;
  291. else if (v.IsNumber())
  292. literalType_ = LITERAL_NUMBER;
  293. else if (v.IsBool())
  294. literalType_ = LITERAL_BOOLEAN;
  295. return true;
  296. }
  297. void JSASTLiteral::Accept(JSASTVisitor* visitor)
  298. {
  299. visitor->visit(this);
  300. }
  301. bool JSASTArrayExpression::Parse(const rapidjson::Value& value)
  302. {
  303. JSASTExpression::Parse(value);
  304. ParseExpressionArray(value["elements"], elements_);
  305. return true;
  306. }
  307. void JSASTArrayExpression::Accept(JSASTVisitor* visitor)
  308. {
  309. visitor->visit(this);
  310. }
  311. bool JSASTProperty::Parse(const rapidjson::Value& value)
  312. {
  313. key_ = ParseExpression(value["key"]);
  314. assert(key_);
  315. value_ = ParseExpression(value["value"]);
  316. assert(value_);
  317. return true;
  318. }
  319. void JSASTProperty::Accept(JSASTVisitor* visitor)
  320. {
  321. visitor->visit(this);
  322. }
  323. bool JSASTObjectExpression::Parse(const rapidjson::Value& value)
  324. {
  325. JSASTExpression::Parse(value);
  326. const rapidjson::Value& jprops = value["properties"];
  327. assert(jprops.IsArray());
  328. for (Value::ConstValueIterator itr = jprops.Begin(); itr != jprops.End(); itr++)
  329. {
  330. JSASTProperty* property = new JSASTProperty();
  331. property->Parse(*itr);
  332. properties_.Push(property);
  333. }
  334. return true;
  335. }
  336. void JSASTObjectExpression::Accept(JSASTVisitor* visitor)
  337. {
  338. visitor->visit(this);
  339. }
  340. bool JSASTMemberExpression::Parse(const rapidjson::Value& value)
  341. {
  342. JSASTExpression::Parse(value);
  343. computed_ = value["computed"].GetBool();
  344. object_ = ParseExpression(value["object"]);
  345. property_ = ParseExpression(value["property"]);
  346. return true;
  347. }
  348. void JSASTMemberExpression::Accept(JSASTVisitor* visitor)
  349. {
  350. visitor->visit(this);
  351. }
  352. bool JSASTCallExpression::Parse(const rapidjson::Value& value)
  353. {
  354. JSASTExpression::Parse(value);
  355. callee_ = ParseExpression(value["callee"]);
  356. ParseExpressionArray(value["arguments"], arguments_);
  357. return true;
  358. }
  359. void JSASTCallExpression::Accept(JSASTVisitor* visitor)
  360. {
  361. visitor->visit(this);
  362. }
  363. bool JSASTNewExpression::Parse(const rapidjson::Value& value)
  364. {
  365. JSASTExpression::Parse(value);
  366. callee_ = ParseExpression(value["callee"]);
  367. ParseExpressionArray(value["arguments"], arguments_);
  368. return true;
  369. }
  370. void JSASTNewExpression::Accept(JSASTVisitor* visitor)
  371. {
  372. visitor->visit(this);
  373. }
  374. bool JSASTVariableDeclarator::Parse(const rapidjson::Value& value)
  375. {
  376. JSASTExpression::Parse(value);
  377. id_ = ParseExpression(value["id"]);
  378. const Value::Member* init = value.FindMember("init");
  379. if (init && init->value.IsObject())
  380. {
  381. init_ = ParseExpression(init->value);
  382. }
  383. return true;
  384. }
  385. void JSASTVariableDeclarator::Accept(JSASTVisitor* visitor)
  386. {
  387. visitor->visit(this);
  388. }
  389. bool JSASTUnaryExpression::Parse(const rapidjson::Value& value)
  390. {
  391. JSASTExpression::Parse(value);
  392. argument_ = ParseExpression(value["argument"]);
  393. prefix_ = value["prefix"].IsTrue();
  394. operator_ = value["operator"].GetString();
  395. return true;
  396. }
  397. void JSASTUnaryExpression::Accept(JSASTVisitor* visitor)
  398. {
  399. visitor->visit(this);
  400. }
  401. bool JSASTUpdateExpression::Parse(const rapidjson::Value& value)
  402. {
  403. JSASTExpression::Parse(value);
  404. argument_ = ParseExpression(value["argument"]);
  405. prefix_ = value["prefix"].IsTrue();
  406. operator_ = value["operator"].GetString();
  407. return true;
  408. }
  409. void JSASTUpdateExpression::Accept(JSASTVisitor* visitor)
  410. {
  411. visitor->visit(this);
  412. }
  413. bool JSASTConditionalExpression::Parse(const rapidjson::Value& value)
  414. {
  415. JSASTExpression::Parse(value);
  416. test_ = ParseExpression(value["test"]);
  417. consequent_ = ParseExpression(value["consequent"]);
  418. alternate_ = ParseExpression(value["alternate"]);
  419. return true;
  420. }
  421. void JSASTConditionalExpression::Accept(JSASTVisitor* visitor)
  422. {
  423. visitor->visit(this);
  424. }
  425. bool JSASTFunctionExpression::Parse(const rapidjson::Value& value)
  426. {
  427. JSASTExpression::Parse(value);
  428. if (!value["id"].IsNull())
  429. {
  430. id_ = (JSASTIdentifier*) ParseExpression(value["id"], JSAST_IDENTIFIER);
  431. }
  432. ParseExpressionArray(value["params"], params_);
  433. const rapidjson::Value& body = value["body"];
  434. String type = body["type"].GetString();
  435. if (type == "BlockStatement")
  436. {
  437. body_ = (JSASTBlockStatement*) ParseStatement(body);
  438. }
  439. else
  440. {
  441. return true;
  442. }
  443. return true;
  444. }
  445. void JSASTFunctionExpression::Accept(JSASTVisitor* visitor)
  446. {
  447. visitor->visit(this);
  448. }
  449. JSASTFunctionExpression::~JSASTFunctionExpression()
  450. {
  451. if (id_)
  452. delete id_;
  453. if (rest_)
  454. delete rest_;
  455. if (body_)
  456. delete body_;
  457. for (unsigned i = 0; i < params_.Size(); i++)
  458. delete params_[i];
  459. for (unsigned i = 0; i < defaults_.Size(); i++)
  460. delete defaults_[i];
  461. params_.Clear();
  462. defaults_.Clear();
  463. }
  464. // STATEMENTS
  465. bool JSASTExpressionStatement::Parse(const rapidjson::Value& value)
  466. {
  467. JSASTStatement::Parse(value);
  468. const Value::Member* expr = value.FindMember("expression");
  469. assert(expr);
  470. expression_ = ParseExpression(expr->value);
  471. return expression_ != NULL;
  472. }
  473. void JSASTLabeledStatement::Accept(JSASTVisitor* visitor)
  474. {
  475. visitor->visit(this);
  476. }
  477. bool JSASTLabeledStatement::Parse(const rapidjson::Value& value)
  478. {
  479. JSASTStatement::Parse(value);
  480. const Value::Member* body = value.FindMember("body");
  481. assert(body);
  482. const Value::Member* label = value.FindMember("label");
  483. assert(label);
  484. body_ = ParseStatement(body->value);
  485. label_ = (JSASTIdentifier*) ParseExpression(label->value, JSAST_IDENTIFIER);
  486. return body_ && label_;
  487. }
  488. void JSASTExpressionStatement::Accept(JSASTVisitor* visitor)
  489. {
  490. visitor->visit(this);
  491. }
  492. bool JSASTReturnStatement::Parse(const rapidjson::Value& value)
  493. {
  494. JSASTStatement::Parse(value);
  495. if (!value["argument"].IsNull())
  496. {
  497. argument_ = ParseExpression(value["argument"]);
  498. }
  499. return true;
  500. }
  501. void JSASTReturnStatement::Accept(JSASTVisitor* visitor)
  502. {
  503. visitor->visit(this);
  504. }
  505. bool JSASTVariableDeclaration::Parse(const rapidjson::Value& value)
  506. {
  507. JSASTDeclaration::Parse(value);
  508. kind_ = value["kind"].GetString();
  509. ParseExpressionArray(value["declarations"], declarations_);
  510. return true;
  511. }
  512. void JSASTVariableDeclaration::Accept(JSASTVisitor* visitor)
  513. {
  514. visitor->visit(this);
  515. }
  516. JSASTComment::JSASTComment() : JSASTNode(JSAST_COMMENT) , isBlock_(false)
  517. {
  518. }
  519. JSASTComment::~JSASTComment()
  520. {
  521. }
  522. bool JSASTComment::Parse(const rapidjson::Value& value)
  523. {
  524. JSASTNode::Parse(value);
  525. // Block or Line
  526. if (value.FindMember("type"))
  527. {
  528. String type = value["type"].GetString();
  529. isBlock_ = type == "Block";
  530. }
  531. if (value.FindMember("value"))
  532. {
  533. value_ = value["value"].GetString();
  534. }
  535. return true;
  536. }
  537. void JSASTComment::Accept(JSASTVisitor *visitor)
  538. {
  539. visitor->visit(this);
  540. }
  541. JSASTProgram::JSASTProgram(const String &path, const String &json) : JSASTNode(JSAST_PROGRAM)
  542. , document_(new Document())
  543. {
  544. if (document_->Parse<0>(json.CString()).HasParseError())
  545. {
  546. LOGERRORF("Could not parse JSON data from %s", path.CString());
  547. return;
  548. }
  549. else
  550. {
  551. //StringBuffer buffer;
  552. //PrettyWriter<StringBuffer> writer(buffer, &(document_->GetAllocator()));
  553. //writer.SetIndent(' ', 3);
  554. //document_->Accept(writer);
  555. //LOGINFOF("%s", buffer.GetString());
  556. }
  557. Parse(*document_);
  558. }
  559. bool JSASTProgram::Parse(const rapidjson::Value& value)
  560. {
  561. JSASTNode::Parse(value);
  562. for (Value::ConstMemberIterator itr = value.MemberBegin();
  563. itr != value.MemberEnd(); ++itr)
  564. {
  565. String name = itr->name.GetString();
  566. if (name == "body")
  567. {
  568. ParseStatementArray(itr->value, body_);
  569. }
  570. if (name == "comments")
  571. {
  572. if (itr->value.IsArray())
  573. {
  574. for (Value::ConstValueIterator citr = itr->value.Begin();
  575. citr != itr->value.End();
  576. citr++)
  577. {
  578. JSASTComment* comment = new JSASTComment();
  579. assert(citr->IsObject());
  580. comment->Parse(*citr);
  581. comments_.Push(comment);
  582. }
  583. }
  584. }
  585. }
  586. return true;
  587. }
  588. JSASTProgram::~JSASTProgram()
  589. {
  590. for (unsigned i = 0; i < comments_.Size(); i++)
  591. delete comments_[i];
  592. comments_.Clear();
  593. delete document_;
  594. document_ = 0;
  595. }
  596. void JSASTProgram::Accept(JSASTVisitor *visitor)
  597. {
  598. visitor->visit(this);
  599. }
  600. JSASTProgram* JSASTProgram::ParseFromJSON(const String& path, const String& json)
  601. {
  602. JSASTProgram* cunit = new JSASTProgram(path, json);
  603. return cunit;
  604. }
  605. bool JSASTIfStatement::Parse(const rapidjson::Value& value)
  606. {
  607. JSASTStatement::Parse(value);
  608. test_ = ParseExpression(value["test"]);
  609. consequent_ = ParseStatement(value["consequent"]);
  610. if (!value["alternate"].IsNull())
  611. alternate_ = ParseStatement(value["alternate"]);
  612. return true;
  613. }
  614. void JSASTIfStatement::Accept(JSASTVisitor* visitor)
  615. {
  616. visitor->visit(this);
  617. }
  618. bool JSASTFunctionDeclaration::Parse(const rapidjson::Value& value)
  619. {
  620. JSASTDeclaration::Parse(value);
  621. if (!value["id"].IsNull())
  622. {
  623. id_ = (JSASTIdentifier*) ParseExpression(value["id"], JSAST_IDENTIFIER);
  624. }
  625. ParseExpressionArray(value["params"], params_);
  626. const rapidjson::Value& body = value["body"];
  627. String type = body["type"].GetString();
  628. if (type == "BlockStatement")
  629. {
  630. body_ = (JSASTBlockStatement*) ParseStatement(body);
  631. }
  632. else
  633. {
  634. }
  635. return true;
  636. }
  637. void JSASTFunctionDeclaration::Accept(JSASTVisitor* visitor)
  638. {
  639. visitor->visit(this);
  640. }
  641. JSASTFunctionDeclaration::~JSASTFunctionDeclaration()
  642. {
  643. if (id_)
  644. delete id_;
  645. if (rest_)
  646. delete rest_;
  647. if (body_)
  648. delete body_;
  649. for (unsigned i = 0; i < params_.Size(); i++)
  650. delete params_[i];
  651. for (unsigned i = 0; i < defaults_.Size(); i++)
  652. delete defaults_[i];
  653. params_.Clear();
  654. defaults_.Clear();
  655. }
  656. bool JSASTBlockStatement::Parse(const rapidjson::Value& value)
  657. {
  658. if (value.FindMember("body"))
  659. {
  660. ParseStatementArray(value["body"], body_);
  661. }
  662. return true;
  663. }
  664. void JSASTBlockStatement::Accept(JSASTVisitor* visitor)
  665. {
  666. visitor->visit(this);
  667. }
  668. bool JSASTForStatement::Parse(const rapidjson::Value& value)
  669. {
  670. JSASTStatement::Parse(value);
  671. if (value.FindMember("init") && !value.FindMember("init")->value.IsNull())
  672. {
  673. const Value::Member* member = value.FindMember("init");
  674. String type = member->value.FindMember("type")->value.GetString();
  675. if (type == "VariableDeclaration")
  676. {
  677. initVariable_ = (JSASTVariableDeclaration*) ParseStatement(member->value);
  678. }
  679. else
  680. {
  681. initExpression_ = ParseExpression(member->value);
  682. }
  683. }
  684. if (value.FindMember("test") && !value.FindMember("test")->value.IsNull())
  685. {
  686. test_ = ParseExpression(value["test"]);
  687. }
  688. if (value.FindMember("update") && !value.FindMember("update")->value.IsNull())
  689. {
  690. update_ = ParseExpression(value["update"]);
  691. }
  692. if (value.FindMember("body"))
  693. {
  694. body_ = ParseStatement(value["body"]);
  695. }
  696. return true;
  697. }
  698. void JSASTForStatement::Accept(JSASTVisitor* visitor)
  699. {
  700. visitor->visit(this);
  701. }
  702. }