JSAST.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. #pragma once
  5. // see https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API
  6. #include <Atomic/Container/Str.h>
  7. #include <Atomic/Container/Vector.h>
  8. using namespace Atomic;
  9. namespace rapidjson
  10. {
  11. template<typename CharType> struct UTF8;
  12. class CrtAllocator;
  13. template <typename BaseAllocator> class MemoryPoolAllocator;
  14. template <typename Encoding, typename Allocator> class GenericValue;
  15. typedef GenericValue<UTF8<char>, MemoryPoolAllocator<CrtAllocator> > Value;
  16. template <typename Encoding, typename Allocator> class GenericDocument;
  17. typedef GenericDocument<UTF8<char>, MemoryPoolAllocator<CrtAllocator> > Document;
  18. }
  19. namespace AtomicEditor
  20. {
  21. enum JSASTType
  22. {
  23. JSAST_UNDEFINED,
  24. JSAST_COMMENT,
  25. JSAST_PROGRAM,
  26. JSAST_PROPERTY,
  27. // Expression
  28. JSAST_ASSIGNMENTEXPRESSION,
  29. JSAST_LOGICALEXPRESSION,
  30. JSAST_IDENTIFIER,
  31. JSAST_CALLEXPRESSION,
  32. JSAST_VARIABLEDECLARATOR,
  33. JSAST_BINARYEXPRESSION,
  34. JSAST_MEMBEREXPRESSION,
  35. JSAST_LITERAL,
  36. JSAST_ARRAYEXPRESSION,
  37. JSAST_FUNCTIONEXPRESSION,
  38. JSAST_NEWEXPRESSION,
  39. JSAST_UNARYEXPRESSION,
  40. JSAST_UPDATEEXPRESSION,
  41. JSAST_CONDITIONALEXPRESSION,
  42. JSAST_OBJECTEXPRESSION,
  43. JSAST_THISEXPRESSION,
  44. // Statements
  45. JSAST_RETURNSTATEMENT,
  46. JSAST_EMPTYSTATEMENT,
  47. JSAST_EXPRESSIONSTATEMENT,
  48. JSAST_LABELEDSTATEMENT,
  49. JSAST_VARIABLEDECLARATION,
  50. JSAST_FUNCTIONDECLARATION,
  51. JSAST_IFSTATEMENT,
  52. JSAST_BLOCKSTATEMENT
  53. };
  54. class JSASTProgram;
  55. class JSASTStatement;
  56. class JSASTEmptyStatement;
  57. class JSASTExpressionStatement;
  58. class JSASTExpression;
  59. class JSASTVisitor;
  60. class JSASTBlockStatement;
  61. class JSASTNode
  62. {
  63. public:
  64. struct Loc
  65. {
  66. int startLine_;
  67. int startColumn_;
  68. int endLine_;
  69. int endColumn_;
  70. Loc()
  71. {
  72. startLine_ = startColumn_ = -1;
  73. endLine_ = endColumn_ = -1;
  74. }
  75. bool Valid() const { return startLine_ != -1 && startColumn_ != -1 &&
  76. endLine_ != -1 && endColumn_ != -1; }
  77. };
  78. JSASTNode(JSASTType type);
  79. virtual bool Parse(const rapidjson::Value& value);
  80. JSASTType GetType() { return type_; }
  81. const Loc& GetLoc() { return loc_; }
  82. virtual void Accept(JSASTVisitor* visitor) = 0;
  83. protected:
  84. bool ParseLoc(const rapidjson::Value &value);
  85. bool ParseRange(const rapidjson::Value &value);
  86. JSASTStatement* ParseStatement(const rapidjson::Value &value);
  87. JSASTExpression* ParseExpression(const rapidjson::Value &value, JSASTType astType = JSAST_UNDEFINED);
  88. bool ParseExpressionArray(const rapidjson::Value &value, Vector<JSASTExpression *> &expressions);
  89. bool ParseStatementArray(const rapidjson::Value &value, Vector<JSASTStatement *> &statements);
  90. private:
  91. Loc loc_;
  92. JSASTType type_;
  93. // character range
  94. int rangeStart_;
  95. int rangeEnd_;
  96. };
  97. class JSASTComment: public JSASTNode
  98. {
  99. public:
  100. JSASTComment();
  101. virtual ~JSASTComment();
  102. virtual bool Parse(const rapidjson::Value& value);
  103. virtual void Accept(JSASTVisitor* visitor);
  104. const String& GetValue() { return value_; }
  105. bool IsBlock() { return isBlock_; }
  106. private:
  107. bool isBlock_;
  108. String value_;
  109. };
  110. class JSASTProgram : public JSASTNode
  111. {
  112. public:
  113. JSASTProgram(const String& path, const String& json);
  114. virtual ~JSASTProgram();
  115. virtual bool Parse(const rapidjson::Value& value);
  116. virtual void Accept(JSASTVisitor* visitor);
  117. static JSASTProgram* ParseFromJSON(const String& path, const String& json);
  118. unsigned GetStatementCount() { return body_.Size(); }
  119. JSASTStatement* GetStatement(unsigned idx) { return body_[idx]; }
  120. unsigned GetCommentCount() { return comments_.Size(); }
  121. JSASTComment* GetComment(unsigned idx) { return comments_[idx]; }
  122. protected:
  123. private:
  124. Vector<JSASTStatement*> body_;
  125. rapidjson::Document* document_;
  126. Vector<JSASTComment*> comments_;
  127. };
  128. class JSASTExpression : public JSASTNode
  129. {
  130. public:
  131. virtual ~JSASTExpression() {}
  132. virtual bool Parse(const rapidjson::Value& value)
  133. {
  134. return JSASTNode::Parse(value);
  135. }
  136. protected:
  137. JSASTExpression(JSASTType type) : JSASTNode(type) {}
  138. };
  139. class JSASTIdentifier : public JSASTExpression
  140. {
  141. public:
  142. JSASTIdentifier() : JSASTExpression(JSAST_IDENTIFIER) {}
  143. virtual bool Parse(const rapidjson::Value& value);
  144. virtual void Accept(JSASTVisitor* visitor);
  145. private:
  146. String name_;
  147. };
  148. class JSASTThisExpression : public JSASTExpression
  149. {
  150. public:
  151. JSASTThisExpression() : JSASTExpression(JSAST_THISEXPRESSION) {}
  152. virtual bool Parse(const rapidjson::Value& value);
  153. virtual void Accept(JSASTVisitor* visitor);
  154. private:
  155. };
  156. class JSASTLiteral : public JSASTExpression
  157. {
  158. public:
  159. enum LiteralType
  160. {
  161. LITERAL_UNKNOWN,
  162. LITERAL_NULL,
  163. LITERAL_STRING,
  164. LITERAL_NUMBER,
  165. LITERAL_BOOLEAN,
  166. LITERAL_REGEX
  167. };
  168. JSASTLiteral() : JSASTExpression(JSAST_LITERAL), literalType_(LITERAL_UNKNOWN) {}
  169. virtual bool Parse(const rapidjson::Value& value);
  170. virtual void Accept(JSASTVisitor* visitor);
  171. LiteralType GetLiteralType() { return literalType_; }
  172. private:
  173. //value: string | boolean | null | number | RegExp;
  174. LiteralType literalType_;
  175. String raw_;
  176. };
  177. class JSASTArrayExpression : public JSASTExpression
  178. {
  179. public:
  180. JSASTArrayExpression() : JSASTExpression(JSAST_ARRAYEXPRESSION) {}
  181. virtual ~JSASTArrayExpression()
  182. {
  183. for (unsigned i = 0; i < elements_.Size(); i++)
  184. delete elements_[i];
  185. elements_.Clear();
  186. }
  187. virtual bool Parse(const rapidjson::Value& value);
  188. virtual void Accept(JSASTVisitor* visitor);
  189. unsigned GetElementCount() { return elements_.Size(); }
  190. JSASTExpression* GetElement(unsigned idx) { return elements_[idx]; }
  191. private:
  192. Vector<JSASTExpression*> elements_;
  193. };
  194. class JSASTProperty: public JSASTNode
  195. {
  196. public:
  197. JSASTProperty() : JSASTNode(JSAST_PROPERTY), key_(0), value_(0) {}
  198. virtual ~JSASTProperty()
  199. {
  200. if (key_)
  201. delete key_;
  202. if (value_)
  203. delete value_;
  204. }
  205. virtual bool Parse(const rapidjson::Value& value);
  206. virtual void Accept(JSASTVisitor* visitor);
  207. JSASTExpression* GetKey() { return key_; }
  208. JSASTExpression* GetValue() { return value_; }
  209. private:
  210. JSASTExpression* key_;
  211. JSASTExpression* value_;
  212. };
  213. class JSASTObjectExpression : public JSASTExpression
  214. {
  215. public:
  216. JSASTObjectExpression() : JSASTExpression(JSAST_OBJECTEXPRESSION) {}
  217. virtual ~JSASTObjectExpression()
  218. {
  219. for (unsigned i = 0; i < properties_.Size(); i++)
  220. delete properties_[i];
  221. properties_.Clear();
  222. }
  223. unsigned GetPropertyCount() { return properties_.Size(); }
  224. JSASTProperty* GetProperty(unsigned idx) { return properties_[idx]; }
  225. virtual bool Parse(const rapidjson::Value& value);
  226. virtual void Accept(JSASTVisitor* visitor);
  227. private:
  228. Vector<JSASTProperty*> properties_;
  229. };
  230. class JSASTAssignmentExpression : public JSASTExpression
  231. {
  232. public:
  233. JSASTAssignmentExpression() : JSASTExpression(JSAST_ASSIGNMENTEXPRESSION),
  234. leftExpression_(0), rightExpression_(0) {}
  235. virtual ~JSASTAssignmentExpression()
  236. {
  237. if (leftExpression_)
  238. delete leftExpression_;
  239. if (rightExpression_)
  240. delete rightExpression_;
  241. }
  242. virtual bool Parse(const rapidjson::Value& value);
  243. virtual void Accept(JSASTVisitor* visitor);
  244. const String& GetOperator() { return operator_; }
  245. JSASTExpression* GetLeft() { return leftExpression_; }
  246. JSASTExpression* GetRight() { return rightExpression_; }
  247. private:
  248. String operator_;
  249. JSASTExpression* leftExpression_;
  250. JSASTExpression* rightExpression_;
  251. };
  252. class JSASTLogicalExpression : public JSASTExpression
  253. {
  254. public:
  255. JSASTLogicalExpression() : JSASTExpression(JSAST_LOGICALEXPRESSION),
  256. leftExpression_(0), rightExpression_(0) {}
  257. virtual ~JSASTLogicalExpression()
  258. {
  259. if (leftExpression_)
  260. delete leftExpression_;
  261. if (rightExpression_)
  262. delete rightExpression_;
  263. }
  264. virtual bool Parse(const rapidjson::Value& value);
  265. virtual void Accept(JSASTVisitor* visitor);
  266. const String& GetOperator() { return operator_; }
  267. JSASTExpression* GetLeft() { return leftExpression_; }
  268. JSASTExpression* GetRight() { return rightExpression_; }
  269. private:
  270. String operator_;
  271. JSASTExpression* leftExpression_;
  272. JSASTExpression* rightExpression_;
  273. };
  274. class JSASTUnaryExpression : public JSASTExpression
  275. {
  276. public:
  277. JSASTUnaryExpression() : JSASTExpression(JSAST_UNARYEXPRESSION),
  278. argument_(0), prefix_(false) {}
  279. virtual ~JSASTUnaryExpression()
  280. {
  281. if (argument_)
  282. delete argument_;
  283. }
  284. virtual bool Parse(const rapidjson::Value& value);
  285. virtual void Accept(JSASTVisitor* visitor);
  286. const String& GetOperator() { return operator_;}
  287. JSASTExpression* GetArgument() { return argument_; }
  288. private:
  289. String operator_;
  290. bool prefix_;
  291. JSASTExpression* argument_;
  292. };
  293. class JSASTUpdateExpression : public JSASTExpression
  294. {
  295. public:
  296. JSASTUpdateExpression() : JSASTExpression(JSAST_UPDATEEXPRESSION),
  297. argument_(0), prefix_(false) {}
  298. virtual ~JSASTUpdateExpression()
  299. {
  300. if (argument_)
  301. delete argument_;
  302. }
  303. virtual bool Parse(const rapidjson::Value& value);
  304. virtual void Accept(JSASTVisitor* visitor);
  305. const String& GetOperator() { return operator_;}
  306. JSASTExpression* GetArgument() { return argument_; }
  307. private:
  308. String operator_;
  309. bool prefix_;
  310. JSASTExpression* argument_;
  311. };
  312. class JSASTFunctionExpression : public JSASTExpression
  313. {
  314. public:
  315. JSASTFunctionExpression() : JSASTExpression(JSAST_FUNCTIONEXPRESSION),
  316. id_(0), rest_(0), body_(0), generator_(false), expression_(false) {}
  317. virtual ~JSASTFunctionExpression();
  318. virtual bool Parse(const rapidjson::Value& value);
  319. virtual void Accept(JSASTVisitor* visitor);
  320. JSASTIdentifier* GetID() { return id_; }
  321. JSASTBlockStatement* GetBodyStatement() { return body_; }
  322. unsigned GetParamsCount() { return params_.Size(); }
  323. JSASTExpression* GetParam(unsigned idx) { return params_[idx]; }
  324. private:
  325. JSASTIdentifier* id_;
  326. Vector<JSASTExpression*> params_;
  327. Vector<JSASTExpression*> defaults_;
  328. JSASTIdentifier* rest_;
  329. JSASTBlockStatement* body_;
  330. bool generator_;
  331. bool expression_;
  332. };
  333. class JSASTBinaryExpression : public JSASTExpression
  334. {
  335. public:
  336. JSASTBinaryExpression() : JSASTExpression(JSAST_BINARYEXPRESSION),
  337. leftExpression_(0), rightExpression_(0) {}
  338. virtual ~JSASTBinaryExpression()
  339. {
  340. if (leftExpression_)
  341. delete leftExpression_;
  342. if (rightExpression_)
  343. delete rightExpression_;
  344. }
  345. virtual bool Parse(const rapidjson::Value& value);
  346. virtual void Accept(JSASTVisitor* visitor);
  347. JSASTExpression* GetLeft() { return leftExpression_; }
  348. JSASTExpression* GetRight() { return rightExpression_; }
  349. const String& GetOperator() { return operator_;}
  350. private:
  351. String operator_;
  352. JSASTExpression* leftExpression_;
  353. JSASTExpression* rightExpression_;
  354. };
  355. class JSASTMemberExpression : public JSASTExpression
  356. {
  357. public:
  358. JSASTMemberExpression() : JSASTExpression(JSAST_MEMBEREXPRESSION),
  359. object_(0), property_(0), computed_(false) {}
  360. virtual ~JSASTMemberExpression()
  361. {
  362. if (object_)
  363. delete object_;
  364. if (property_)
  365. delete property_;
  366. }
  367. virtual bool Parse(const rapidjson::Value& value);
  368. virtual void Accept(JSASTVisitor* visitor);
  369. JSASTExpression* GetObject() { return object_;}
  370. JSASTExpression* GetProperty() { return property_; }
  371. private:
  372. JSASTExpression* object_;
  373. JSASTExpression* property_;
  374. bool computed_;
  375. };
  376. class JSASTConditionalExpression : public JSASTExpression
  377. {
  378. public:
  379. JSASTConditionalExpression() : JSASTExpression(JSAST_CONDITIONALEXPRESSION), test_(0), consequent_(0), alternate_(0) {}
  380. virtual ~JSASTConditionalExpression()
  381. {
  382. if (test_)
  383. delete test_;
  384. if (consequent_)
  385. delete consequent_;
  386. if (alternate_)
  387. delete alternate_;
  388. }
  389. virtual bool Parse(const rapidjson::Value& value);
  390. virtual void Accept(JSASTVisitor* visitor);
  391. JSASTExpression* GetTest() { return test_; }
  392. JSASTExpression* GetConsequent() { return consequent_; }
  393. JSASTExpression* GetAlternate() { return alternate_; }
  394. private:
  395. JSASTExpression* test_;
  396. JSASTExpression* consequent_;
  397. JSASTExpression* alternate_;
  398. };
  399. class JSASTCallExpression : public JSASTExpression
  400. {
  401. public:
  402. JSASTCallExpression() : JSASTExpression(JSAST_CALLEXPRESSION),
  403. callee_(0){}
  404. virtual ~JSASTCallExpression()
  405. {
  406. if (callee_)
  407. delete callee_;
  408. for (unsigned i = 0; i < arguments_.Size(); i++)
  409. {
  410. delete arguments_[i];
  411. }
  412. arguments_.Clear();
  413. }
  414. virtual bool Parse(const rapidjson::Value& value);
  415. virtual void Accept(JSASTVisitor* visitor);
  416. JSASTExpression* GetCallee() { return callee_; }
  417. unsigned GetArgumentCount() { return arguments_.Size(); }
  418. JSASTExpression* GetArgument(unsigned idx) { return arguments_[idx]; }
  419. private:
  420. JSASTExpression* callee_;
  421. Vector<JSASTExpression*> arguments_;
  422. };
  423. class JSASTNewExpression : public JSASTExpression
  424. {
  425. public:
  426. JSASTNewExpression() : JSASTExpression(JSAST_NEWEXPRESSION),
  427. callee_(0){}
  428. virtual ~JSASTNewExpression()
  429. {
  430. if (callee_)
  431. delete callee_;
  432. for (unsigned i = 0; i < arguments_.Size(); i++)
  433. {
  434. delete arguments_[i];
  435. }
  436. arguments_.Clear();
  437. }
  438. virtual bool Parse(const rapidjson::Value& value);
  439. virtual void Accept(JSASTVisitor* visitor);
  440. JSASTExpression* GetCallee() { return callee_; }
  441. unsigned GetArgumentCount() { return arguments_.Size(); }
  442. JSASTExpression* GetArgument(unsigned idx) { return arguments_[idx]; }
  443. private:
  444. JSASTExpression* callee_;
  445. Vector<JSASTExpression*> arguments_;
  446. };
  447. class JSASTVariableDeclarator : public JSASTExpression
  448. {
  449. public:
  450. JSASTVariableDeclarator() : JSASTExpression(JSAST_VARIABLEDECLARATOR), id_(0), init_(0) {}
  451. virtual ~JSASTVariableDeclarator()
  452. {
  453. if (id_)
  454. delete id_;
  455. if (init_)
  456. delete init_;
  457. }
  458. virtual bool Parse(const rapidjson::Value& value);
  459. virtual void Accept(JSASTVisitor* visitor);
  460. JSASTExpression* GetID() { return id_; }
  461. JSASTExpression* GetInit() { return init_; }
  462. private:
  463. JSASTExpression* id_;
  464. JSASTExpression* init_;
  465. };
  466. class JSASTStatement : public JSASTNode
  467. {
  468. public:
  469. virtual ~JSASTStatement() {}
  470. virtual bool Parse(const rapidjson::Value& value)
  471. {
  472. return JSASTNode::Parse(value);
  473. }
  474. protected:
  475. JSASTStatement(JSASTType type) : JSASTNode(type) {}
  476. };
  477. class JSASTEmptyStatement : public JSASTStatement
  478. {
  479. public:
  480. JSASTEmptyStatement() : JSASTStatement(JSAST_EMPTYSTATEMENT) {}
  481. virtual bool Parse(const rapidjson::Value& value)
  482. {
  483. return JSASTStatement::Parse(value);
  484. }
  485. virtual void Accept(JSASTVisitor* visitor);
  486. };
  487. class JSASTExpressionStatement : public JSASTStatement
  488. {
  489. public:
  490. JSASTExpressionStatement(JSASTType type = JSAST_EXPRESSIONSTATEMENT) : JSASTStatement(type),
  491. expression_(0) {}
  492. virtual ~JSASTExpressionStatement()
  493. {
  494. if (expression_)
  495. delete expression_;
  496. }
  497. virtual bool Parse(const rapidjson::Value& value);
  498. virtual void Accept(JSASTVisitor* visitor);
  499. JSASTExpression* GetExpression() { return expression_; }
  500. protected:
  501. JSASTExpression* expression_;
  502. };
  503. class JSASTLabeledStatement : public JSASTStatement
  504. {
  505. public:
  506. JSASTLabeledStatement(JSASTType type = JSAST_LABELEDSTATEMENT) : JSASTStatement(type),
  507. label_(0), body_(0) {}
  508. virtual ~JSASTLabeledStatement()
  509. {
  510. if (label_)
  511. delete label_;
  512. if (body_)
  513. delete body_;
  514. }
  515. JSASTIdentifier* GetLabel() { return label_; }
  516. JSASTStatement* GetBody() { return body_; }
  517. virtual bool Parse(const rapidjson::Value& value);
  518. virtual void Accept(JSASTVisitor* visitor);
  519. protected:
  520. JSASTIdentifier* label_;
  521. JSASTStatement* body_;
  522. };
  523. class JSASTReturnStatement : public JSASTStatement
  524. {
  525. public:
  526. JSASTReturnStatement(JSASTType type = JSAST_RETURNSTATEMENT) : JSASTStatement(type),
  527. argument_(0) {}
  528. virtual ~JSASTReturnStatement()
  529. {
  530. if (argument_)
  531. delete argument_;
  532. }
  533. virtual bool Parse(const rapidjson::Value& value);
  534. virtual void Accept(JSASTVisitor* visitor);
  535. JSASTExpression* GetArgument() { return argument_; }
  536. protected:
  537. JSASTExpression* argument_;
  538. };
  539. class JSASTDeclaration : public JSASTStatement
  540. {
  541. public:
  542. virtual bool Parse(const rapidjson::Value& value) { return JSASTStatement::Parse(value); }
  543. protected:
  544. JSASTDeclaration(JSASTType type) : JSASTStatement(type) {}
  545. };
  546. class JSASTBlockStatement : public JSASTStatement
  547. {
  548. public:
  549. JSASTBlockStatement() : JSASTStatement(JSAST_BLOCKSTATEMENT) {}
  550. virtual ~JSASTBlockStatement()
  551. {
  552. for (unsigned i = 0; i < body_.Size(); i++)
  553. delete body_[i];
  554. body_.Clear();
  555. }
  556. virtual bool Parse(const rapidjson::Value& value);
  557. virtual void Accept(JSASTVisitor* visitor);
  558. unsigned GetStatementCount() { return body_.Size(); }
  559. JSASTStatement* GetStatement(unsigned idx) { return body_[idx]; }
  560. private:
  561. Vector<JSASTStatement*> body_;
  562. };
  563. class JSASTIfStatement : public JSASTStatement
  564. {
  565. public:
  566. JSASTIfStatement() : JSASTStatement(JSAST_IFSTATEMENT), test_(0), consequent_(0), alternate_(0) {}
  567. virtual ~JSASTIfStatement()
  568. {
  569. if (test_)
  570. delete test_;
  571. if (consequent_)
  572. delete consequent_;
  573. if (alternate_)
  574. delete alternate_;
  575. }
  576. virtual bool Parse(const rapidjson::Value& value);
  577. virtual void Accept(JSASTVisitor* visitor);
  578. JSASTExpression* GetTest() { return test_; }
  579. JSASTStatement* GetConsequent() { return consequent_; }
  580. JSASTStatement* GetAlternate() { return alternate_; }
  581. private:
  582. JSASTExpression* test_;
  583. JSASTStatement* consequent_;
  584. JSASTStatement* alternate_;
  585. };
  586. class JSASTVariableDeclaration : public JSASTDeclaration
  587. {
  588. public:
  589. JSASTVariableDeclaration() : JSASTDeclaration(JSAST_VARIABLEDECLARATION) {}
  590. virtual bool Parse(const rapidjson::Value& value);
  591. virtual void Accept(JSASTVisitor* visitor);
  592. const String& GetKind() { return kind_; }
  593. unsigned GetDeclarationsCount() { return declarations_.Size(); }
  594. JSASTExpression* GetDeclaration(unsigned idx) { return declarations_[idx]; }
  595. protected:
  596. // "var" | "let" | "const";
  597. String kind_;
  598. // Array of JSASTVariableDeclarator
  599. Vector<JSASTExpression*> declarations_;
  600. };
  601. class JSASTFunctionDeclaration : public JSASTDeclaration
  602. {
  603. public:
  604. JSASTFunctionDeclaration() : JSASTDeclaration(JSAST_FUNCTIONDECLARATION),
  605. id_(0), rest_(0), body_(0), generator_(false), expression_(false) {}
  606. virtual ~JSASTFunctionDeclaration();
  607. virtual bool Parse(const rapidjson::Value& value);
  608. virtual void Accept(JSASTVisitor* visitor);
  609. JSASTIdentifier* GetID() { return id_; }
  610. JSASTBlockStatement* GetBodyStatement() { return body_; }
  611. unsigned GetParamsCount() { return params_.Size(); }
  612. JSASTExpression* GetParam(unsigned idx) { return params_[idx]; }
  613. private:
  614. JSASTIdentifier* id_;
  615. Vector<JSASTExpression*> params_;
  616. Vector<JSASTExpression*> defaults_;
  617. JSASTIdentifier* rest_;
  618. JSASTBlockStatement* body_;
  619. bool generator_;
  620. bool expression_;
  621. };
  622. /*
  623. interface ForStatement <: Statement {
  624. type: "ForStatement";
  625. init: VariableDeclaration | Expression | null;
  626. test: Expression | null;
  627. update: Expression | null;
  628. body: Statement;
  629. }
  630. */
  631. class JSASTForStatement : public JSASTStatement
  632. {
  633. public:
  634. JSASTForStatement() : JSASTStatement(JSAST_IFSTATEMENT),
  635. initVariable_(0), initExpression_(0), test_(0), update_(0), body_(0)
  636. {
  637. }
  638. virtual ~JSASTForStatement()
  639. {
  640. if (initVariable_)
  641. delete initVariable_;
  642. if (initExpression_)
  643. delete initExpression_;
  644. if (test_)
  645. delete(test_);
  646. if(update_)
  647. delete update_;
  648. if (body_)
  649. delete body_;
  650. }
  651. virtual bool Parse(const rapidjson::Value& value);
  652. virtual void Accept(JSASTVisitor* visitor);
  653. JSASTExpression* GetTest() { return test_; }
  654. JSASTVariableDeclaration* GetInitVariable() { return initVariable_; }
  655. JSASTExpression* GetInitExpression() { return initExpression_; }
  656. JSASTExpression* GetUpdate() { return update_; }
  657. JSASTStatement* GetBody() { return body_; }
  658. private:
  659. JSASTVariableDeclaration* initVariable_;
  660. JSASTExpression* initExpression_;
  661. JSASTExpression* test_;
  662. JSASTExpression* update_;
  663. JSASTStatement* body_;
  664. };
  665. }