JSAST.h 22 KB

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