JSASTVisitor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #include "JSAST.h"
  24. namespace AtomicEditor
  25. {
  26. class JSASTVisitor
  27. {
  28. public:
  29. virtual JSASTProgram* visit(JSASTProgram *program) = 0;
  30. virtual JSASTStatement* visit(JSASTBlockStatement *stmt) = 0;
  31. virtual JSASTStatement* visit(JSASTExpressionStatement *stmt) = 0;
  32. virtual JSASTStatement* visit(JSASTIfStatement *stmt) = 0;
  33. virtual JSASTStatement* visit(JSASTFunctionDeclaration *stmt) = 0;
  34. virtual JSASTStatement* visit(JSASTVariableDeclaration *stmt) = 0;
  35. virtual JSASTStatement* visit(JSASTReturnStatement *stmt) = 0;
  36. virtual JSASTStatement* visit(JSASTEmptyStatement *stmt) = 0;
  37. virtual JSASTStatement* visit(JSASTForStatement *stmt) = 0;
  38. virtual JSASTExpression* visit(JSASTAssignmentExpression* expr) = 0;
  39. virtual JSASTExpression* visit(JSASTLogicalExpression* expr) = 0;
  40. virtual JSASTExpression* visit(JSASTConditionalExpression* expr) = 0;
  41. virtual JSASTExpression* visit(JSASTThisExpression* expr) = 0;
  42. virtual JSASTExpression* visit(JSASTIdentifier* expr) = 0;
  43. virtual JSASTExpression* visit(JSASTCallExpression* expr) = 0;
  44. virtual JSASTExpression* visit(JSASTBinaryExpression* expr) = 0;
  45. virtual JSASTExpression* visit(JSASTMemberExpression* expr) = 0;
  46. virtual JSASTExpression* visit(JSASTLiteral* expr) = 0;
  47. virtual JSASTExpression* visit(JSASTArrayExpression* expr) = 0;
  48. virtual JSASTExpression* visit(JSASTObjectExpression* expr) = 0;
  49. virtual JSASTExpression* visit(JSASTFunctionExpression* expr) = 0;
  50. virtual JSASTExpression* visit(JSASTNewExpression* expr) = 0;
  51. virtual JSASTExpression* visit(JSASTUnaryExpression* expr) = 0;
  52. virtual JSASTExpression* visit(JSASTUpdateExpression* expr) = 0;
  53. virtual JSASTExpression* visit(JSASTVariableDeclarator* expr) = 0;
  54. virtual JSASTProperty* visit(JSASTProperty* property) = 0;
  55. virtual JSASTStatement* visit(JSASTLabeledStatement* property) = 0;
  56. virtual JSASTComment* visit(JSASTComment* expr) = 0;
  57. };
  58. class JSASTTraversalVisitor : public JSASTVisitor
  59. {
  60. public:
  61. virtual JSASTProgram* visit(JSASTProgram *program)
  62. {
  63. // this would cause recursion
  64. // program->Accept(this);
  65. unsigned count = program->GetCommentCount();
  66. for (unsigned i = 0; i < count; i++)
  67. {
  68. JSASTComment* cmt = program->GetComment(i);
  69. if (cmt)
  70. cmt->Accept(this);
  71. }
  72. count = program->GetStatementCount();
  73. for (unsigned i = 0; i < count; i++)
  74. {
  75. JSASTStatement* stmt = program->GetStatement(i);
  76. if (stmt)
  77. stmt->Accept(this);
  78. }
  79. return program;
  80. }
  81. virtual JSASTStatement* visit(JSASTForStatement *stmt)
  82. {
  83. if (stmt->GetInitVariable())
  84. stmt->GetInitVariable()->Accept(this);
  85. if (stmt->GetInitExpression())
  86. stmt->GetInitExpression()->Accept(this);
  87. if (stmt->GetTest())
  88. stmt->GetTest()->Accept(this);
  89. if (stmt->GetUpdate())
  90. stmt->GetUpdate()->Accept(this);
  91. if (stmt->GetBody())
  92. stmt->GetBody()->Accept(this);
  93. return stmt;
  94. }
  95. virtual JSASTStatement* visit(JSASTBlockStatement *block)
  96. {
  97. unsigned count = block->GetStatementCount();
  98. for (unsigned i = 0; i < count; i++)
  99. {
  100. JSASTStatement* stmt = block->GetStatement(i);
  101. if (stmt)
  102. stmt->Accept(this);
  103. }
  104. return block;
  105. }
  106. virtual JSASTStatement* visit(JSASTExpressionStatement *stmt)
  107. {
  108. if (stmt->GetExpression())
  109. stmt->GetExpression()->Accept(this);
  110. return stmt;
  111. }
  112. virtual JSASTStatement* visit(JSASTIfStatement *stmt)
  113. {
  114. if (stmt->GetTest())
  115. stmt->GetTest()->Accept(this);
  116. if (stmt->GetConsequent())
  117. stmt->GetConsequent()->Accept(this);
  118. if (stmt->GetAlternate())
  119. stmt->GetAlternate()->Accept(this);
  120. return stmt;
  121. }
  122. virtual JSASTStatement* visit(JSASTFunctionDeclaration *stmt)
  123. {
  124. if (stmt->GetID())
  125. stmt->GetID()->Accept(this);
  126. for (unsigned i = 0; i < stmt->GetParamsCount(); i++)
  127. stmt->GetParam(i)->Accept(this);
  128. JSASTBlockStatement* body = stmt->GetBodyStatement();
  129. if (body)
  130. body->Accept(this);
  131. return stmt;
  132. }
  133. virtual JSASTStatement* visit(JSASTVariableDeclaration *stmt)
  134. {
  135. for (unsigned i = 0; i < stmt->GetDeclarationsCount(); i++)
  136. stmt->GetDeclaration(i)->Accept(this);
  137. return stmt;
  138. }
  139. virtual JSASTStatement* visit(JSASTReturnStatement *stmt)
  140. {
  141. if (stmt->GetArgument())
  142. stmt->GetArgument()->Accept(this);
  143. return stmt;
  144. }
  145. virtual JSASTStatement* visit(JSASTEmptyStatement *stmt)
  146. {
  147. return stmt;
  148. }
  149. virtual JSASTExpression* visit(JSASTAssignmentExpression* expr)
  150. {
  151. if (expr->GetLeft())
  152. expr->GetLeft()->Accept(this);
  153. if (expr->GetRight())
  154. expr->GetRight()->Accept(this);
  155. return expr;
  156. }
  157. virtual JSASTExpression* visit(JSASTConditionalExpression *expr)
  158. {
  159. if (expr->GetTest())
  160. expr->GetTest()->Accept(this);
  161. if (expr->GetConsequent())
  162. expr->GetConsequent()->Accept(this);
  163. if (expr->GetAlternate())
  164. expr->GetAlternate()->Accept(this);
  165. return expr;
  166. }
  167. virtual JSASTExpression* visit(JSASTLogicalExpression* expr)
  168. {
  169. if (expr->GetLeft())
  170. expr->GetLeft()->Accept(this);
  171. if (expr->GetRight())
  172. expr->GetRight()->Accept(this);
  173. return expr;
  174. }
  175. virtual JSASTExpression* visit(JSASTIdentifier* expr)
  176. {
  177. return expr;
  178. }
  179. virtual JSASTExpression* visit(JSASTCallExpression* expr)
  180. {
  181. if (expr->GetCallee())
  182. expr->GetCallee()->Accept(this);
  183. for (unsigned i = 0; i < expr->GetArgumentCount(); i++)
  184. { if (expr->GetArgument(i))
  185. expr->GetArgument(i)->Accept(this);
  186. }
  187. return expr;
  188. }
  189. virtual JSASTExpression* visit(JSASTBinaryExpression* expr)
  190. {
  191. if (expr->GetLeft())
  192. expr->GetLeft()->Accept(this);
  193. if (expr->GetRight())
  194. expr->GetRight()->Accept(this);
  195. return expr;
  196. }
  197. virtual JSASTExpression* visit(JSASTMemberExpression* expr)
  198. {
  199. if (expr->GetObject())
  200. expr->GetObject()->Accept(this);
  201. if (expr->GetProperty())
  202. expr->GetProperty()->Accept(this);
  203. return expr;
  204. }
  205. virtual JSASTExpression* visit(JSASTLiteral* expr)
  206. {
  207. return expr;
  208. }
  209. virtual JSASTExpression* visit(JSASTArrayExpression* expr)
  210. {
  211. for (unsigned i = 0; i < expr->GetElementCount(); i++)
  212. {
  213. JSASTExpression* arrayElement = expr->GetElement(i);
  214. if (arrayElement)
  215. arrayElement->Accept(this);
  216. }
  217. return expr;
  218. }
  219. virtual JSASTExpression* visit(JSASTObjectExpression* expr)
  220. {
  221. for (unsigned i = 0; i < expr->GetPropertyCount(); i++)
  222. {
  223. JSASTProperty* property = expr->GetProperty(i);
  224. if (property)
  225. property->Accept(this);
  226. }
  227. return expr;
  228. }
  229. virtual JSASTExpression* visit(JSASTFunctionExpression* expr)
  230. {
  231. if (expr->GetID())
  232. expr->GetID()->Accept(this);
  233. for (unsigned i = 0; i < expr->GetParamsCount(); i++)
  234. expr->GetParam(i)->Accept(this);
  235. JSASTBlockStatement* body = expr->GetBodyStatement();
  236. if (body)
  237. body->Accept(this);
  238. return expr;
  239. }
  240. virtual JSASTExpression* visit(JSASTNewExpression* expr)
  241. {
  242. if (expr->GetCallee())
  243. expr->GetCallee()->Accept(this);
  244. for (unsigned i = 0; i < expr->GetArgumentCount(); i++)
  245. {
  246. if (expr->GetArgument(i))
  247. expr->GetArgument(i)->Accept(this);
  248. }
  249. return expr;
  250. }
  251. virtual JSASTExpression* visit(JSASTUnaryExpression* expr)
  252. {
  253. if (expr->GetArgument())
  254. expr->GetArgument()->Accept(this);
  255. return expr;
  256. }
  257. virtual JSASTExpression* visit(JSASTUpdateExpression* expr)
  258. {
  259. if (expr->GetArgument())
  260. expr->GetArgument()->Accept(this);
  261. return expr;
  262. }
  263. virtual JSASTExpression* visit(JSASTVariableDeclarator* expr)
  264. {
  265. if (expr->GetID())
  266. expr->GetID()->Accept(this);
  267. if (expr->GetInit())
  268. expr->GetInit()->Accept(this);
  269. return expr;
  270. }
  271. virtual JSASTComment* visit(JSASTComment* comment)
  272. {
  273. return comment;
  274. }
  275. virtual JSASTProperty* visit(JSASTProperty* property)
  276. {
  277. if (property->GetKey())
  278. property->GetKey()->Accept(this);
  279. if (property->GetValue())
  280. property->GetValue()->Accept(this);
  281. return property;
  282. }
  283. virtual JSASTStatement* visit(JSASTLabeledStatement* labeledStatement)
  284. {
  285. if (labeledStatement->GetLabel())
  286. labeledStatement->GetLabel()->Accept(this);
  287. if (labeledStatement->GetBody())
  288. labeledStatement->GetBody()->Accept(this);
  289. return labeledStatement;
  290. }
  291. virtual JSASTExpression* visit(JSASTThisExpression* expr)
  292. {
  293. return expr;
  294. }
  295. };
  296. }