JSAST.cpp 20 KB

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