JSAST.cpp 20 KB

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