JSAST.cpp 21 KB

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