JSAST.h 21 KB

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