ast.js 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. "use strict";
  34. function DEFNODE(type, props, methods, base) {
  35. if (typeof base === "undefined") base = AST_Node;
  36. props = props ? props.split(/\s+/) : [];
  37. var self_props = props;
  38. if (base && base.PROPS) props = props.concat(base.PROPS);
  39. var code = [
  40. "return function AST_", type, "(props){",
  41. "if(props){",
  42. ];
  43. props.forEach(function(prop) {
  44. code.push("this.", prop, "=props.", prop, ";");
  45. });
  46. code.push("}");
  47. var proto = base && new base;
  48. if (proto && proto.initialize || methods && methods.initialize) code.push("this.initialize();");
  49. code.push("}");
  50. var ctor = new Function(code.join(""))();
  51. if (proto) {
  52. ctor.prototype = proto;
  53. ctor.BASE = base;
  54. }
  55. if (base) base.SUBCLASSES.push(ctor);
  56. ctor.prototype.CTOR = ctor;
  57. ctor.PROPS = props || null;
  58. ctor.SELF_PROPS = self_props;
  59. ctor.SUBCLASSES = [];
  60. if (type) {
  61. ctor.prototype.TYPE = ctor.TYPE = type;
  62. }
  63. if (methods) for (var name in methods) if (HOP(methods, name)) {
  64. if (/^\$/.test(name)) {
  65. ctor[name.substr(1)] = methods[name];
  66. } else {
  67. ctor.prototype[name] = methods[name];
  68. }
  69. }
  70. ctor.DEFMETHOD = function(name, method) {
  71. this.prototype[name] = method;
  72. };
  73. if (typeof exports !== "undefined") {
  74. exports["AST_" + type] = ctor;
  75. }
  76. return ctor;
  77. }
  78. var AST_Token = DEFNODE("Token", "type value line col pos endline endcol endpos nlb comments_before comments_after file raw", {
  79. }, null);
  80. var AST_Node = DEFNODE("Node", "start end", {
  81. _clone: function(deep) {
  82. if (deep) {
  83. var self = this.clone();
  84. return self.transform(new TreeTransformer(function(node) {
  85. if (node !== self) {
  86. return node.clone(true);
  87. }
  88. }));
  89. }
  90. return new this.CTOR(this);
  91. },
  92. clone: function(deep) {
  93. return this._clone(deep);
  94. },
  95. $documentation: "Base class of all AST nodes",
  96. $propdoc: {
  97. start: "[AST_Token] The first token of this node",
  98. end: "[AST_Token] The last token of this node"
  99. },
  100. walk: function(visitor) {
  101. visitor.visit(this);
  102. },
  103. _validate: function() {
  104. if (this.TYPE == "Node") throw new Error("should not instantiate AST_Node");
  105. },
  106. validate: function() {
  107. var ctor = this.CTOR;
  108. do {
  109. ctor.prototype._validate.call(this);
  110. } while (ctor = ctor.BASE);
  111. },
  112. validate_ast: function() {
  113. var marker = {};
  114. this.walk(new TreeWalker(function(node) {
  115. if (node.validate_visited === marker) {
  116. throw new Error(string_template("cannot reuse {type} from [{file}:{line},{col}]", {
  117. type: "AST_" + node.TYPE,
  118. file: node.start.file,
  119. line: node.start.line,
  120. col: node.start.col,
  121. }));
  122. }
  123. node.validate_visited = marker;
  124. }));
  125. },
  126. }, null);
  127. (AST_Node.log_function = function(fn, verbose) {
  128. if (typeof fn != "function") {
  129. AST_Node.info = AST_Node.warn = noop;
  130. return;
  131. }
  132. var printed = Object.create(null);
  133. AST_Node.info = verbose ? function(text, props) {
  134. log("INFO: " + string_template(text, props));
  135. } : noop;
  136. AST_Node.warn = function(text, props) {
  137. log("WARN: " + string_template(text, props));
  138. };
  139. function log(msg) {
  140. if (printed[msg]) return;
  141. printed[msg] = true;
  142. fn(msg);
  143. }
  144. })();
  145. var restore_transforms = [];
  146. AST_Node.enable_validation = function() {
  147. AST_Node.disable_validation();
  148. (function validate_transform(ctor) {
  149. ctor.SUBCLASSES.forEach(validate_transform);
  150. if (!HOP(ctor.prototype, "transform")) return;
  151. var transform = ctor.prototype.transform;
  152. ctor.prototype.transform = function(tw, in_list) {
  153. var node = transform.call(this, tw, in_list);
  154. if (node instanceof AST_Node) {
  155. node.validate();
  156. } else if (!(node === null || in_list && List.is_op(node))) {
  157. throw new Error("invalid transformed value: " + node);
  158. }
  159. return node;
  160. };
  161. restore_transforms.push(function() {
  162. ctor.prototype.transform = transform;
  163. });
  164. })(this);
  165. };
  166. AST_Node.disable_validation = function() {
  167. var restore;
  168. while (restore = restore_transforms.pop()) restore();
  169. };
  170. /* -----[ statements ]----- */
  171. var AST_Statement = DEFNODE("Statement", null, {
  172. $documentation: "Base class of all statements",
  173. _validate: function() {
  174. if (this.TYPE == "Statement") throw new Error("should not instantiate AST_Statement");
  175. },
  176. });
  177. var AST_Debugger = DEFNODE("Debugger", null, {
  178. $documentation: "Represents a debugger statement",
  179. }, AST_Statement);
  180. var AST_Directive = DEFNODE("Directive", "quote value", {
  181. $documentation: "Represents a directive, like \"use strict\";",
  182. $propdoc: {
  183. quote: "[string?] the original quote character",
  184. value: "[string] The value of this directive as a plain string (it's not an AST_String!)",
  185. },
  186. _validate: function() {
  187. if (this.quote != null) {
  188. if (typeof this.quote != "string") throw new Error("quote must be string");
  189. if (!/^["']$/.test(this.quote)) throw new Error("invalid quote: " + this.quote);
  190. }
  191. if (typeof this.value != "string") throw new Error("value must be string");
  192. },
  193. }, AST_Statement);
  194. var AST_EmptyStatement = DEFNODE("EmptyStatement", null, {
  195. $documentation: "The empty statement (empty block or simply a semicolon)"
  196. }, AST_Statement);
  197. function is_statement(node) {
  198. return node instanceof AST_Statement
  199. && !(node instanceof AST_ClassExpression)
  200. && !(node instanceof AST_LambdaExpression);
  201. }
  202. function validate_expression(value, prop, multiple, allow_spread, allow_hole) {
  203. multiple = multiple ? "contain" : "be";
  204. if (!(value instanceof AST_Node)) throw new Error(prop + " must " + multiple + " AST_Node");
  205. if (value instanceof AST_DefaultValue) throw new Error(prop + " cannot " + multiple + " AST_DefaultValue");
  206. if (value instanceof AST_Destructured) throw new Error(prop + " cannot " + multiple + " AST_Destructured");
  207. if (value instanceof AST_Hole && !allow_hole) throw new Error(prop + " cannot " + multiple + " AST_Hole");
  208. if (value instanceof AST_Spread && !allow_spread) throw new Error(prop + " cannot " + multiple + " AST_Spread");
  209. if (is_statement(value)) throw new Error(prop + " cannot " + multiple + " AST_Statement");
  210. if (value instanceof AST_SymbolDeclaration) {
  211. throw new Error(prop + " cannot " + multiple + " AST_SymbolDeclaration");
  212. }
  213. }
  214. function must_be_expression(node, prop) {
  215. validate_expression(node[prop], prop);
  216. }
  217. var AST_SimpleStatement = DEFNODE("SimpleStatement", "body", {
  218. $documentation: "A statement consisting of an expression, i.e. a = 1 + 2",
  219. $propdoc: {
  220. body: "[AST_Node] an expression node (should not be instanceof AST_Statement)",
  221. },
  222. walk: function(visitor) {
  223. var node = this;
  224. visitor.visit(node, function() {
  225. node.body.walk(visitor);
  226. });
  227. },
  228. _validate: function() {
  229. must_be_expression(this, "body");
  230. },
  231. }, AST_Statement);
  232. var AST_BlockScope = DEFNODE("BlockScope", "enclosed functions make_def parent_scope variables", {
  233. $documentation: "Base class for all statements introducing a lexical scope",
  234. $propdoc: {
  235. enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes",
  236. functions: "[Dictionary/S] like `variables`, but only lists function declarations",
  237. parent_scope: "[AST_Scope?/S] link to the parent scope",
  238. variables: "[Dictionary/S] a map of name ---> SymbolDef for all variables/functions defined in this scope",
  239. },
  240. clone: function(deep) {
  241. var node = this._clone(deep);
  242. if (this.enclosed) node.enclosed = this.enclosed.slice();
  243. if (this.functions) node.functions = this.functions.clone();
  244. if (this.variables) node.variables = this.variables.clone();
  245. return node;
  246. },
  247. pinned: function() {
  248. return this.resolve().pinned();
  249. },
  250. resolve: function() {
  251. return this.parent_scope.resolve();
  252. },
  253. _validate: function() {
  254. if (this.TYPE == "BlockScope") throw new Error("should not instantiate AST_BlockScope");
  255. if (this.parent_scope == null) return;
  256. if (!(this.parent_scope instanceof AST_BlockScope)) throw new Error("parent_scope must be AST_BlockScope");
  257. if (!(this.resolve() instanceof AST_Scope)) throw new Error("must be contained within AST_Scope");
  258. },
  259. }, AST_Statement);
  260. function walk_body(node, visitor) {
  261. node.body.forEach(function(node) {
  262. node.walk(visitor);
  263. });
  264. }
  265. var AST_Block = DEFNODE("Block", "body", {
  266. $documentation: "A body of statements (usually braced)",
  267. $propdoc: {
  268. body: "[AST_Statement*] an array of statements"
  269. },
  270. walk: function(visitor) {
  271. var node = this;
  272. visitor.visit(node, function() {
  273. walk_body(node, visitor);
  274. });
  275. },
  276. _validate: function() {
  277. if (this.TYPE == "Block") throw new Error("should not instantiate AST_Block");
  278. this.body.forEach(function(node) {
  279. if (!is_statement(node)) throw new Error("body must contain AST_Statement");
  280. });
  281. },
  282. }, AST_BlockScope);
  283. var AST_BlockStatement = DEFNODE("BlockStatement", null, {
  284. $documentation: "A block statement",
  285. }, AST_Block);
  286. var AST_StatementWithBody = DEFNODE("StatementWithBody", "body", {
  287. $documentation: "Base class for all statements that contain one nested body: `For`, `ForIn`, `Do`, `While`, `With`",
  288. $propdoc: {
  289. body: "[AST_Statement] the body; this should always be present, even if it's an AST_EmptyStatement"
  290. },
  291. _validate: function() {
  292. if (this.TYPE == "StatementWithBody") throw new Error("should not instantiate AST_StatementWithBody");
  293. if (!is_statement(this.body)) throw new Error("body must be AST_Statement");
  294. },
  295. }, AST_BlockScope);
  296. var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
  297. $documentation: "Statement with a label",
  298. $propdoc: {
  299. label: "[AST_Label] a label definition"
  300. },
  301. walk: function(visitor) {
  302. var node = this;
  303. visitor.visit(node, function() {
  304. node.label.walk(visitor);
  305. node.body.walk(visitor);
  306. });
  307. },
  308. clone: function(deep) {
  309. var node = this._clone(deep);
  310. if (deep) {
  311. var label = node.label;
  312. var def = this.label;
  313. node.walk(new TreeWalker(function(node) {
  314. if (node instanceof AST_LoopControl) {
  315. if (!node.label || node.label.thedef !== def) return;
  316. node.label.thedef = label;
  317. label.references.push(node);
  318. return true;
  319. }
  320. if (node instanceof AST_Scope) return true;
  321. }));
  322. }
  323. return node;
  324. },
  325. _validate: function() {
  326. if (!(this.label instanceof AST_Label)) throw new Error("label must be AST_Label");
  327. },
  328. }, AST_StatementWithBody);
  329. var AST_IterationStatement = DEFNODE("IterationStatement", null, {
  330. $documentation: "Internal class. All loops inherit from it.",
  331. _validate: function() {
  332. if (this.TYPE == "IterationStatement") throw new Error("should not instantiate AST_IterationStatement");
  333. },
  334. }, AST_StatementWithBody);
  335. var AST_DWLoop = DEFNODE("DWLoop", "condition", {
  336. $documentation: "Base class for do/while statements",
  337. $propdoc: {
  338. condition: "[AST_Node] the loop condition. Should not be instanceof AST_Statement"
  339. },
  340. _validate: function() {
  341. if (this.TYPE == "DWLoop") throw new Error("should not instantiate AST_DWLoop");
  342. must_be_expression(this, "condition");
  343. },
  344. }, AST_IterationStatement);
  345. var AST_Do = DEFNODE("Do", null, {
  346. $documentation: "A `do` statement",
  347. walk: function(visitor) {
  348. var node = this;
  349. visitor.visit(node, function() {
  350. node.body.walk(visitor);
  351. node.condition.walk(visitor);
  352. });
  353. }
  354. }, AST_DWLoop);
  355. var AST_While = DEFNODE("While", null, {
  356. $documentation: "A `while` statement",
  357. walk: function(visitor) {
  358. var node = this;
  359. visitor.visit(node, function() {
  360. node.condition.walk(visitor);
  361. node.body.walk(visitor);
  362. });
  363. }
  364. }, AST_DWLoop);
  365. var AST_For = DEFNODE("For", "init condition step", {
  366. $documentation: "A `for` statement",
  367. $propdoc: {
  368. init: "[AST_Node?] the `for` initialization code, or null if empty",
  369. condition: "[AST_Node?] the `for` termination clause, or null if empty",
  370. step: "[AST_Node?] the `for` update clause, or null if empty"
  371. },
  372. walk: function(visitor) {
  373. var node = this;
  374. visitor.visit(node, function() {
  375. if (node.init) node.init.walk(visitor);
  376. if (node.condition) node.condition.walk(visitor);
  377. if (node.step) node.step.walk(visitor);
  378. node.body.walk(visitor);
  379. });
  380. },
  381. _validate: function() {
  382. if (this.init != null) {
  383. if (!(this.init instanceof AST_Node)) throw new Error("init must be AST_Node");
  384. if (is_statement(this.init) && !(this.init instanceof AST_Definitions)) {
  385. throw new Error("init cannot be AST_Statement");
  386. }
  387. }
  388. if (this.condition != null) must_be_expression(this, "condition");
  389. if (this.step != null) must_be_expression(this, "step");
  390. },
  391. }, AST_IterationStatement);
  392. var AST_ForEnumeration = DEFNODE("ForEnumeration", "init object", {
  393. $documentation: "Base class for enumeration loops, i.e. `for ... in`, `for ... of` & `for await ... of`",
  394. $propdoc: {
  395. init: "[AST_Node] the assignment target during iteration",
  396. object: "[AST_Node] the object to iterate over"
  397. },
  398. walk: function(visitor) {
  399. var node = this;
  400. visitor.visit(node, function() {
  401. node.init.walk(visitor);
  402. node.object.walk(visitor);
  403. node.body.walk(visitor);
  404. });
  405. },
  406. _validate: function() {
  407. if (this.TYPE == "ForEnumeration") throw new Error("should not instantiate AST_ForEnumeration");
  408. if (this.init instanceof AST_Definitions) {
  409. if (this.init.definitions.length != 1) throw new Error("init must have single declaration");
  410. } else {
  411. validate_destructured(this.init, function(node) {
  412. if (!(node instanceof AST_PropAccess || node instanceof AST_SymbolRef)) {
  413. throw new Error("init must be assignable: " + node.TYPE);
  414. }
  415. });
  416. }
  417. must_be_expression(this, "object");
  418. },
  419. }, AST_IterationStatement);
  420. var AST_ForIn = DEFNODE("ForIn", null, {
  421. $documentation: "A `for ... in` statement",
  422. }, AST_ForEnumeration);
  423. var AST_ForOf = DEFNODE("ForOf", null, {
  424. $documentation: "A `for ... of` statement",
  425. }, AST_ForEnumeration);
  426. var AST_ForAwaitOf = DEFNODE("ForAwaitOf", null, {
  427. $documentation: "A `for await ... of` statement",
  428. }, AST_ForOf);
  429. var AST_With = DEFNODE("With", "expression", {
  430. $documentation: "A `with` statement",
  431. $propdoc: {
  432. expression: "[AST_Node] the `with` expression"
  433. },
  434. walk: function(visitor) {
  435. var node = this;
  436. visitor.visit(node, function() {
  437. node.expression.walk(visitor);
  438. node.body.walk(visitor);
  439. });
  440. },
  441. _validate: function() {
  442. must_be_expression(this, "expression");
  443. },
  444. }, AST_StatementWithBody);
  445. /* -----[ scope and functions ]----- */
  446. var AST_Scope = DEFNODE("Scope", "uses_eval uses_with", {
  447. $documentation: "Base class for all statements introducing a lexical scope",
  448. $propdoc: {
  449. uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`",
  450. uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
  451. },
  452. pinned: function() {
  453. return this.uses_eval || this.uses_with;
  454. },
  455. resolve: return_this,
  456. _validate: function() {
  457. if (this.TYPE == "Scope") throw new Error("should not instantiate AST_Scope");
  458. },
  459. }, AST_Block);
  460. var AST_Toplevel = DEFNODE("Toplevel", "globals", {
  461. $documentation: "The toplevel scope",
  462. $propdoc: {
  463. globals: "[Dictionary/S] a map of name ---> SymbolDef for all undeclared names",
  464. },
  465. wrap: function(name) {
  466. var body = this.body;
  467. return parse([
  468. "(function(exports){'$ORIG';})(typeof ",
  469. name,
  470. "=='undefined'?(",
  471. name,
  472. "={}):",
  473. name,
  474. ");"
  475. ].join(""), {
  476. filename: "wrap=" + JSON.stringify(name)
  477. }).transform(new TreeTransformer(function(node) {
  478. if (node instanceof AST_Directive && node.value == "$ORIG") {
  479. return List.splice(body);
  480. }
  481. }));
  482. },
  483. enclose: function(args_values) {
  484. if (typeof args_values != "string") args_values = "";
  485. var index = args_values.indexOf(":");
  486. if (index < 0) index = args_values.length;
  487. var body = this.body;
  488. return parse([
  489. "(function(",
  490. args_values.slice(0, index),
  491. '){"$ORIG"})(',
  492. args_values.slice(index + 1),
  493. ")"
  494. ].join(""), {
  495. filename: "enclose=" + JSON.stringify(args_values)
  496. }).transform(new TreeTransformer(function(node) {
  497. if (node instanceof AST_Directive && node.value == "$ORIG") {
  498. return List.splice(body);
  499. }
  500. }));
  501. }
  502. }, AST_Scope);
  503. var AST_Lambda = DEFNODE("Lambda", "argnames length_read rest uses_arguments", {
  504. $documentation: "Base class for functions",
  505. $propdoc: {
  506. argnames: "[(AST_DefaultValue|AST_Destructured|AST_SymbolFunarg)*] array of function arguments and/or destructured literals",
  507. length_read: "[boolean/S] whether length property of this function is accessed",
  508. rest: "[(AST_Destructured|AST_SymbolFunarg)?] rest parameter, or null if absent",
  509. uses_arguments: "[boolean/S] whether this function accesses the arguments array",
  510. },
  511. each_argname: function(visit) {
  512. var tw = new TreeWalker(function(node) {
  513. if (node instanceof AST_DefaultValue) {
  514. node.name.walk(tw);
  515. return true;
  516. }
  517. if (node instanceof AST_DestructuredKeyVal) {
  518. node.value.walk(tw);
  519. return true;
  520. }
  521. if (node instanceof AST_SymbolFunarg) visit(node);
  522. });
  523. this.argnames.forEach(function(argname) {
  524. argname.walk(tw);
  525. });
  526. if (this.rest) this.rest.walk(tw);
  527. },
  528. walk: function(visitor) {
  529. var node = this;
  530. visitor.visit(node, function() {
  531. if (node.name) node.name.walk(visitor);
  532. node.argnames.forEach(function(argname) {
  533. argname.walk(visitor);
  534. });
  535. if (node.rest) node.rest.walk(visitor);
  536. walk_body(node, visitor);
  537. });
  538. },
  539. _validate: function() {
  540. if (this.TYPE == "Lambda") throw new Error("should not instantiate AST_Lambda");
  541. this.argnames.forEach(function(node) {
  542. validate_destructured(node, function(node) {
  543. if (!(node instanceof AST_SymbolFunarg)) throw new Error("argnames must be AST_SymbolFunarg[]");
  544. }, true);
  545. });
  546. if (this.rest != null) validate_destructured(this.rest, function(node) {
  547. if (!(node instanceof AST_SymbolFunarg)) throw new Error("rest must be AST_SymbolFunarg");
  548. });
  549. },
  550. }, AST_Scope);
  551. var AST_Accessor = DEFNODE("Accessor", null, {
  552. $documentation: "A getter/setter function",
  553. _validate: function() {
  554. if (this.name != null) throw new Error("name must be null");
  555. },
  556. }, AST_Lambda);
  557. var AST_LambdaExpression = DEFNODE("LambdaExpression", "inlined", {
  558. $documentation: "Base class for function expressions",
  559. $propdoc: {
  560. inlined: "[boolean/S] whether this function has been inlined",
  561. },
  562. _validate: function() {
  563. if (this.TYPE == "LambdaExpression") throw new Error("should not instantiate AST_LambdaExpression");
  564. },
  565. }, AST_Lambda);
  566. function is_arrow(node) {
  567. return node instanceof AST_Arrow || node instanceof AST_AsyncArrow;
  568. }
  569. function is_async(node) {
  570. return node instanceof AST_AsyncArrow
  571. || node instanceof AST_AsyncDefun
  572. || node instanceof AST_AsyncFunction
  573. || node instanceof AST_AsyncGeneratorDefun
  574. || node instanceof AST_AsyncGeneratorFunction;
  575. }
  576. function is_generator(node) {
  577. return node instanceof AST_AsyncGeneratorDefun
  578. || node instanceof AST_AsyncGeneratorFunction
  579. || node instanceof AST_GeneratorDefun
  580. || node instanceof AST_GeneratorFunction;
  581. }
  582. function walk_lambda(node, tw) {
  583. if (is_arrow(node) && node.value) {
  584. node.value.walk(tw);
  585. } else {
  586. walk_body(node, tw);
  587. }
  588. }
  589. var AST_Arrow = DEFNODE("Arrow", "value", {
  590. $documentation: "An arrow function expression",
  591. $propdoc: {
  592. value: "[AST_Node?] simple return expression, or null if using function body.",
  593. },
  594. walk: function(visitor) {
  595. var node = this;
  596. visitor.visit(node, function() {
  597. node.argnames.forEach(function(argname) {
  598. argname.walk(visitor);
  599. });
  600. if (node.rest) node.rest.walk(visitor);
  601. if (node.value) {
  602. node.value.walk(visitor);
  603. } else {
  604. walk_body(node, visitor);
  605. }
  606. });
  607. },
  608. _validate: function() {
  609. if (this.name != null) throw new Error("name must be null");
  610. if (this.uses_arguments) throw new Error("uses_arguments must be false");
  611. if (this.value != null) {
  612. must_be_expression(this, "value");
  613. if (this.body.length) throw new Error("body must be empty if value exists");
  614. }
  615. },
  616. }, AST_LambdaExpression);
  617. var AST_AsyncArrow = DEFNODE("AsyncArrow", "value", {
  618. $documentation: "An asynchronous arrow function expression",
  619. $propdoc: {
  620. value: "[AST_Node?] simple return expression, or null if using function body.",
  621. },
  622. walk: function(visitor) {
  623. var node = this;
  624. visitor.visit(node, function() {
  625. node.argnames.forEach(function(argname) {
  626. argname.walk(visitor);
  627. });
  628. if (node.rest) node.rest.walk(visitor);
  629. if (node.value) {
  630. node.value.walk(visitor);
  631. } else {
  632. walk_body(node, visitor);
  633. }
  634. });
  635. },
  636. _validate: function() {
  637. if (this.name != null) throw new Error("name must be null");
  638. if (this.uses_arguments) throw new Error("uses_arguments must be false");
  639. if (this.value != null) {
  640. must_be_expression(this, "value");
  641. if (this.body.length) throw new Error("body must be empty if value exists");
  642. }
  643. },
  644. }, AST_LambdaExpression);
  645. var AST_AsyncFunction = DEFNODE("AsyncFunction", "name", {
  646. $documentation: "An asynchronous function expression",
  647. $propdoc: {
  648. name: "[AST_SymbolLambda?] the name of this function, or null if not specified",
  649. },
  650. _validate: function() {
  651. if (this.name != null) {
  652. if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
  653. }
  654. },
  655. }, AST_LambdaExpression);
  656. var AST_AsyncGeneratorFunction = DEFNODE("AsyncGeneratorFunction", "name", {
  657. $documentation: "An asynchronous generator function expression",
  658. $propdoc: {
  659. name: "[AST_SymbolLambda?] the name of this function, or null if not specified",
  660. },
  661. _validate: function() {
  662. if (this.name != null) {
  663. if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
  664. }
  665. },
  666. }, AST_LambdaExpression);
  667. var AST_Function = DEFNODE("Function", "name", {
  668. $documentation: "A function expression",
  669. $propdoc: {
  670. name: "[AST_SymbolLambda?] the name of this function, or null if not specified",
  671. },
  672. _validate: function() {
  673. if (this.name != null) {
  674. if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
  675. }
  676. },
  677. }, AST_LambdaExpression);
  678. var AST_GeneratorFunction = DEFNODE("GeneratorFunction", "name", {
  679. $documentation: "A generator function expression",
  680. $propdoc: {
  681. name: "[AST_SymbolLambda?] the name of this function, or null if not specified",
  682. },
  683. _validate: function() {
  684. if (this.name != null) {
  685. if (!(this.name instanceof AST_SymbolLambda)) throw new Error("name must be AST_SymbolLambda");
  686. }
  687. },
  688. }, AST_LambdaExpression);
  689. var AST_LambdaDefinition = DEFNODE("LambdaDefinition", "inlined name", {
  690. $documentation: "Base class for function definitions",
  691. $propdoc: {
  692. inlined: "[boolean/S] whether this function has been inlined",
  693. name: "[AST_SymbolDefun] the name of this function",
  694. },
  695. _validate: function() {
  696. if (this.TYPE == "LambdaDefinition") throw new Error("should not instantiate AST_LambdaDefinition");
  697. if (!(this.name instanceof AST_SymbolDefun)) throw new Error("name must be AST_SymbolDefun");
  698. },
  699. }, AST_Lambda);
  700. var AST_AsyncDefun = DEFNODE("AsyncDefun", null, {
  701. $documentation: "An asynchronous function definition",
  702. }, AST_LambdaDefinition);
  703. var AST_AsyncGeneratorDefun = DEFNODE("AsyncGeneratorDefun", null, {
  704. $documentation: "An asynchronous generator function definition",
  705. }, AST_LambdaDefinition);
  706. var AST_Defun = DEFNODE("Defun", null, {
  707. $documentation: "A function definition",
  708. }, AST_LambdaDefinition);
  709. var AST_GeneratorDefun = DEFNODE("GeneratorDefun", null, {
  710. $documentation: "A generator function definition",
  711. }, AST_LambdaDefinition);
  712. /* -----[ classes ]----- */
  713. var AST_Class = DEFNODE("Class", "extends name properties", {
  714. $documentation: "Base class for class literals",
  715. $propdoc: {
  716. extends: "[AST_Node?] the super class, or null if not specified",
  717. properties: "[AST_ClassProperty*] array of class properties",
  718. },
  719. walk: function(visitor) {
  720. var node = this;
  721. visitor.visit(node, function() {
  722. if (node.name) node.name.walk(visitor);
  723. if (node.extends) node.extends.walk(visitor);
  724. node.properties.forEach(function(prop) {
  725. prop.walk(visitor);
  726. });
  727. });
  728. },
  729. _validate: function() {
  730. if (this.TYPE == "Class") throw new Error("should not instantiate AST_Class");
  731. if (this.extends != null) must_be_expression(this, "extends");
  732. this.properties.forEach(function(node) {
  733. if (!(node instanceof AST_ClassProperty)) throw new Error("properties must contain AST_ClassProperty");
  734. });
  735. },
  736. }, AST_BlockScope);
  737. var AST_DefClass = DEFNODE("DefClass", null, {
  738. $documentation: "A class definition",
  739. $propdoc: {
  740. name: "[AST_SymbolDefClass] the name of this class",
  741. },
  742. _validate: function() {
  743. if (!(this.name instanceof AST_SymbolDefClass)) throw new Error("name must be AST_SymbolDefClass");
  744. },
  745. }, AST_Class);
  746. var AST_ClassExpression = DEFNODE("ClassExpression", null, {
  747. $documentation: "A class expression",
  748. $propdoc: {
  749. name: "[AST_SymbolClass?] the name of this class, or null if not specified",
  750. },
  751. _validate: function() {
  752. if (this.name != null) {
  753. if (!(this.name instanceof AST_SymbolClass)) throw new Error("name must be AST_SymbolClass");
  754. }
  755. },
  756. }, AST_Class);
  757. var AST_ClassProperty = DEFNODE("ClassProperty", "key private static value", {
  758. $documentation: "Base class for `class` properties",
  759. $propdoc: {
  760. key: "[string|AST_Node] property name (AST_Node for computed property)",
  761. private: "[boolean] whether this is a private property",
  762. static: "[boolean] whether this is a static property",
  763. value: "[AST_Node?] property value (AST_Accessor for getters/setters, AST_LambdaExpression for methods, null if not specified for fields)",
  764. },
  765. walk: function(visitor) {
  766. var node = this;
  767. visitor.visit(node, function() {
  768. if (node.key instanceof AST_Node) node.key.walk(visitor);
  769. if (node.value) node.value.walk(visitor);
  770. });
  771. },
  772. _validate: function() {
  773. if (this.TYPE == "ClassProperty") throw new Error("should not instantiate AST_ClassProperty");
  774. if (typeof this.key != "string") {
  775. if (!(this.key instanceof AST_Node)) throw new Error("key must be string or AST_Node");
  776. must_be_expression(this, "key");
  777. }
  778. if(this.value != null) {
  779. if (!(this.value instanceof AST_Node)) throw new Error("value must be AST_Node");
  780. }
  781. },
  782. });
  783. var AST_ClassField = DEFNODE("ClassField", null, {
  784. $documentation: "A `class` field",
  785. _validate: function() {
  786. if(this.value != null) must_be_expression(this, "value");
  787. },
  788. }, AST_ClassProperty);
  789. var AST_ClassGetter = DEFNODE("ClassGetter", null, {
  790. $documentation: "A `class` getter",
  791. _validate: function() {
  792. if (!(this.value instanceof AST_Accessor)) throw new Error("value must be AST_Accessor");
  793. },
  794. }, AST_ClassProperty);
  795. var AST_ClassSetter = DEFNODE("ClassSetter", null, {
  796. $documentation: "A `class` setter",
  797. _validate: function() {
  798. if (!(this.value instanceof AST_Accessor)) throw new Error("value must be AST_Accessor");
  799. },
  800. }, AST_ClassProperty);
  801. var AST_ClassMethod = DEFNODE("ClassMethod", null, {
  802. $documentation: "A `class` method",
  803. _validate: function() {
  804. if (!(this.value instanceof AST_LambdaExpression)) throw new Error("value must be AST_LambdaExpression");
  805. if (is_arrow(this.value)) throw new Error("value cannot be AST_Arrow or AST_AsyncArrow");
  806. if (this.value.name != null) throw new Error("name of class method's lambda must be null");
  807. },
  808. }, AST_ClassProperty);
  809. /* -----[ JUMPS ]----- */
  810. var AST_Jump = DEFNODE("Jump", null, {
  811. $documentation: "Base class for “jumps” (for now that's `return`, `throw`, `break` and `continue`)",
  812. _validate: function() {
  813. if (this.TYPE == "Jump") throw new Error("should not instantiate AST_Jump");
  814. },
  815. }, AST_Statement);
  816. var AST_Exit = DEFNODE("Exit", "value", {
  817. $documentation: "Base class for “exits” (`return` and `throw`)",
  818. $propdoc: {
  819. value: "[AST_Node?] the value returned or thrown by this statement; could be null for AST_Return"
  820. },
  821. walk: function(visitor) {
  822. var node = this;
  823. visitor.visit(node, function() {
  824. if (node.value) node.value.walk(visitor);
  825. });
  826. },
  827. _validate: function() {
  828. if (this.TYPE == "Exit") throw new Error("should not instantiate AST_Exit");
  829. },
  830. }, AST_Jump);
  831. var AST_Return = DEFNODE("Return", null, {
  832. $documentation: "A `return` statement",
  833. _validate: function() {
  834. if (this.value != null) must_be_expression(this, "value");
  835. },
  836. }, AST_Exit);
  837. var AST_Throw = DEFNODE("Throw", null, {
  838. $documentation: "A `throw` statement",
  839. _validate: function() {
  840. must_be_expression(this, "value");
  841. },
  842. }, AST_Exit);
  843. var AST_LoopControl = DEFNODE("LoopControl", "label", {
  844. $documentation: "Base class for loop control statements (`break` and `continue`)",
  845. $propdoc: {
  846. label: "[AST_LabelRef?] the label, or null if none",
  847. },
  848. walk: function(visitor) {
  849. var node = this;
  850. visitor.visit(node, function() {
  851. if (node.label) node.label.walk(visitor);
  852. });
  853. },
  854. _validate: function() {
  855. if (this.TYPE == "LoopControl") throw new Error("should not instantiate AST_LoopControl");
  856. if (this.label != null) {
  857. if (!(this.label instanceof AST_LabelRef)) throw new Error("label must be AST_LabelRef");
  858. }
  859. },
  860. }, AST_Jump);
  861. var AST_Break = DEFNODE("Break", null, {
  862. $documentation: "A `break` statement"
  863. }, AST_LoopControl);
  864. var AST_Continue = DEFNODE("Continue", null, {
  865. $documentation: "A `continue` statement"
  866. }, AST_LoopControl);
  867. /* -----[ IF ]----- */
  868. var AST_If = DEFNODE("If", "condition alternative", {
  869. $documentation: "A `if` statement",
  870. $propdoc: {
  871. condition: "[AST_Node] the `if` condition",
  872. alternative: "[AST_Statement?] the `else` part, or null if not present"
  873. },
  874. walk: function(visitor) {
  875. var node = this;
  876. visitor.visit(node, function() {
  877. node.condition.walk(visitor);
  878. node.body.walk(visitor);
  879. if (node.alternative) node.alternative.walk(visitor);
  880. });
  881. },
  882. _validate: function() {
  883. must_be_expression(this, "condition");
  884. if (this.alternative != null) {
  885. if (!is_statement(this.alternative)) throw new Error("alternative must be AST_Statement");
  886. }
  887. },
  888. }, AST_StatementWithBody);
  889. /* -----[ SWITCH ]----- */
  890. var AST_Switch = DEFNODE("Switch", "expression", {
  891. $documentation: "A `switch` statement",
  892. $propdoc: {
  893. expression: "[AST_Node] the `switch` “discriminant”"
  894. },
  895. walk: function(visitor) {
  896. var node = this;
  897. visitor.visit(node, function() {
  898. node.expression.walk(visitor);
  899. walk_body(node, visitor);
  900. });
  901. },
  902. _validate: function() {
  903. must_be_expression(this, "expression");
  904. this.body.forEach(function(node) {
  905. if (!(node instanceof AST_SwitchBranch)) throw new Error("body must be AST_SwitchBranch[]");
  906. });
  907. },
  908. }, AST_Block);
  909. var AST_SwitchBranch = DEFNODE("SwitchBranch", null, {
  910. $documentation: "Base class for `switch` branches",
  911. _validate: function() {
  912. if (this.TYPE == "SwitchBranch") throw new Error("should not instantiate AST_SwitchBranch");
  913. },
  914. }, AST_Block);
  915. var AST_Default = DEFNODE("Default", null, {
  916. $documentation: "A `default` switch branch",
  917. }, AST_SwitchBranch);
  918. var AST_Case = DEFNODE("Case", "expression", {
  919. $documentation: "A `case` switch branch",
  920. $propdoc: {
  921. expression: "[AST_Node] the `case` expression"
  922. },
  923. walk: function(visitor) {
  924. var node = this;
  925. visitor.visit(node, function() {
  926. node.expression.walk(visitor);
  927. walk_body(node, visitor);
  928. });
  929. },
  930. _validate: function() {
  931. must_be_expression(this, "expression");
  932. },
  933. }, AST_SwitchBranch);
  934. /* -----[ EXCEPTIONS ]----- */
  935. var AST_Try = DEFNODE("Try", "bcatch bfinally", {
  936. $documentation: "A `try` statement",
  937. $propdoc: {
  938. bcatch: "[AST_Catch?] the catch block, or null if not present",
  939. bfinally: "[AST_Finally?] the finally block, or null if not present"
  940. },
  941. walk: function(visitor) {
  942. var node = this;
  943. visitor.visit(node, function() {
  944. walk_body(node, visitor);
  945. if (node.bcatch) node.bcatch.walk(visitor);
  946. if (node.bfinally) node.bfinally.walk(visitor);
  947. });
  948. },
  949. _validate: function() {
  950. if (this.bcatch != null) {
  951. if (!(this.bcatch instanceof AST_Catch)) throw new Error("bcatch must be AST_Catch");
  952. }
  953. if (this.bfinally != null) {
  954. if (!(this.bfinally instanceof AST_Finally)) throw new Error("bfinally must be AST_Finally");
  955. }
  956. },
  957. }, AST_Block);
  958. var AST_Catch = DEFNODE("Catch", "argname", {
  959. $documentation: "A `catch` node; only makes sense as part of a `try` statement",
  960. $propdoc: {
  961. argname: "[(AST_Destructured|AST_SymbolCatch)?] symbol for the exception, or null if not present",
  962. },
  963. walk: function(visitor) {
  964. var node = this;
  965. visitor.visit(node, function() {
  966. if (node.argname) node.argname.walk(visitor);
  967. walk_body(node, visitor);
  968. });
  969. },
  970. _validate: function() {
  971. if (this.argname != null) validate_destructured(this.argname, function(node) {
  972. if (!(node instanceof AST_SymbolCatch)) throw new Error("argname must be AST_SymbolCatch");
  973. });
  974. },
  975. }, AST_Block);
  976. var AST_Finally = DEFNODE("Finally", null, {
  977. $documentation: "A `finally` node; only makes sense as part of a `try` statement"
  978. }, AST_Block);
  979. /* -----[ VAR ]----- */
  980. var AST_Definitions = DEFNODE("Definitions", "definitions", {
  981. $documentation: "Base class for `var` nodes (variable declarations/initializations)",
  982. $propdoc: {
  983. definitions: "[AST_VarDef*] array of variable definitions"
  984. },
  985. walk: function(visitor) {
  986. var node = this;
  987. visitor.visit(node, function() {
  988. node.definitions.forEach(function(defn) {
  989. defn.walk(visitor);
  990. });
  991. });
  992. },
  993. _validate: function() {
  994. if (this.TYPE == "Definitions") throw new Error("should not instantiate AST_Definitions");
  995. if (this.definitions.length < 1) throw new Error("must have at least one definition");
  996. },
  997. }, AST_Statement);
  998. var AST_Const = DEFNODE("Const", null, {
  999. $documentation: "A `const` statement",
  1000. _validate: function() {
  1001. this.definitions.forEach(function(node) {
  1002. if (!(node instanceof AST_VarDef)) throw new Error("definitions must be AST_VarDef[]");
  1003. validate_destructured(node.name, function(node) {
  1004. if (!(node instanceof AST_SymbolConst)) throw new Error("name must be AST_SymbolConst");
  1005. });
  1006. });
  1007. },
  1008. }, AST_Definitions);
  1009. var AST_Let = DEFNODE("Let", null, {
  1010. $documentation: "A `let` statement",
  1011. _validate: function() {
  1012. this.definitions.forEach(function(node) {
  1013. if (!(node instanceof AST_VarDef)) throw new Error("definitions must be AST_VarDef[]");
  1014. validate_destructured(node.name, function(node) {
  1015. if (!(node instanceof AST_SymbolLet)) throw new Error("name must be AST_SymbolLet");
  1016. });
  1017. });
  1018. },
  1019. }, AST_Definitions);
  1020. var AST_Var = DEFNODE("Var", null, {
  1021. $documentation: "A `var` statement",
  1022. _validate: function() {
  1023. this.definitions.forEach(function(node) {
  1024. if (!(node instanceof AST_VarDef)) throw new Error("definitions must be AST_VarDef[]");
  1025. validate_destructured(node.name, function(node) {
  1026. if (!(node instanceof AST_SymbolVar)) throw new Error("name must be AST_SymbolVar");
  1027. });
  1028. });
  1029. },
  1030. }, AST_Definitions);
  1031. var AST_VarDef = DEFNODE("VarDef", "name value", {
  1032. $documentation: "A variable declaration; only appears in a AST_Definitions node",
  1033. $propdoc: {
  1034. name: "[AST_Destructured|AST_SymbolVar] name of the variable",
  1035. value: "[AST_Node?] initializer, or null of there's no initializer",
  1036. },
  1037. walk: function(visitor) {
  1038. var node = this;
  1039. visitor.visit(node, function() {
  1040. node.name.walk(visitor);
  1041. if (node.value) node.value.walk(visitor);
  1042. });
  1043. },
  1044. _validate: function() {
  1045. if (this.value != null) must_be_expression(this, "value");
  1046. },
  1047. });
  1048. /* -----[ OTHER ]----- */
  1049. var AST_ExportDeclaration = DEFNODE("ExportDeclaration", "body", {
  1050. $documentation: "An `export` statement",
  1051. $propdoc: {
  1052. body: "[AST_DefClass|AST_Definitions|AST_LambdaDefinition] the statement to export",
  1053. },
  1054. walk: function(visitor) {
  1055. var node = this;
  1056. visitor.visit(node, function() {
  1057. node.body.walk(visitor);
  1058. });
  1059. },
  1060. _validate: function() {
  1061. if (!(this.body instanceof AST_DefClass
  1062. || this.body instanceof AST_Definitions
  1063. || this.body instanceof AST_LambdaDefinition)) {
  1064. throw new Error("body must be AST_DefClass, AST_Definitions or AST_LambdaDefinition");
  1065. }
  1066. },
  1067. }, AST_Statement);
  1068. var AST_ExportDefault = DEFNODE("ExportDefault", "body", {
  1069. $documentation: "An `export default` statement",
  1070. $propdoc: {
  1071. body: "[AST_Node] the default export",
  1072. },
  1073. walk: function(visitor) {
  1074. var node = this;
  1075. visitor.visit(node, function() {
  1076. node.body.walk(visitor);
  1077. });
  1078. },
  1079. _validate: function() {
  1080. if (!(this.body instanceof AST_DefClass || this.body instanceof AST_LambdaDefinition)) {
  1081. must_be_expression(this, "body");
  1082. }
  1083. },
  1084. }, AST_Statement);
  1085. var AST_ExportForeign = DEFNODE("ExportForeign", "aliases keys path quote", {
  1086. $documentation: "An `export ... from '...'` statement",
  1087. $propdoc: {
  1088. aliases: "[string*] array of aliases to export",
  1089. keys: "[string*] array of keys to import",
  1090. path: "[string] the path to import module",
  1091. quote: "[string?] the original quote character",
  1092. },
  1093. _validate: function() {
  1094. if (this.aliases.length != this.keys.length) {
  1095. throw new Error("aliases:key length mismatch: " + this.aliases.length + " != " + this.keys.length);
  1096. }
  1097. this.aliases.forEach(function(name) {
  1098. if (typeof name != "string") throw new Error("aliases must contain string");
  1099. });
  1100. this.keys.forEach(function(name) {
  1101. if (typeof name != "string") throw new Error("keys must contain string");
  1102. });
  1103. if (typeof this.path != "string") throw new Error("path must be string");
  1104. if (this.quote != null) {
  1105. if (typeof this.quote != "string") throw new Error("quote must be string");
  1106. if (!/^["']$/.test(this.quote)) throw new Error("invalid quote: " + this.quote);
  1107. }
  1108. },
  1109. }, AST_Statement);
  1110. var AST_ExportReferences = DEFNODE("ExportReferences", "properties", {
  1111. $documentation: "An `export { ... }` statement",
  1112. $propdoc: {
  1113. properties: "[AST_SymbolExport*] array of aliases to export",
  1114. },
  1115. walk: function(visitor) {
  1116. var node = this;
  1117. visitor.visit(node, function() {
  1118. node.properties.forEach(function(prop) {
  1119. prop.walk(visitor);
  1120. });
  1121. });
  1122. },
  1123. _validate: function() {
  1124. this.properties.forEach(function(prop) {
  1125. if (!(prop instanceof AST_SymbolExport)) throw new Error("properties must contain AST_SymbolExport");
  1126. });
  1127. },
  1128. }, AST_Statement);
  1129. var AST_Import = DEFNODE("Import", "all default path properties quote", {
  1130. $documentation: "An `import` statement",
  1131. $propdoc: {
  1132. all: "[AST_SymbolImport?] the imported namespace, or null if not specified",
  1133. default: "[AST_SymbolImport?] the alias for default `export`, or null if not specified",
  1134. path: "[string] the path to import module",
  1135. properties: "[(AST_SymbolImport*)?] array of aliases, or null if not specified",
  1136. quote: "[string?] the original quote character",
  1137. },
  1138. walk: function(visitor) {
  1139. var node = this;
  1140. visitor.visit(node, function() {
  1141. if (node.all) node.all.walk(visitor);
  1142. if (node.default) node.default.walk(visitor);
  1143. if (node.properties) node.properties.forEach(function(prop) {
  1144. prop.walk(visitor);
  1145. });
  1146. });
  1147. },
  1148. _validate: function() {
  1149. if (this.all != null) {
  1150. if (!(this.all instanceof AST_SymbolImport)) throw new Error("all must be AST_SymbolImport");
  1151. if (this.properties != null) throw new Error("cannot import both * and {} in the same statement");
  1152. }
  1153. if (this.default != null) {
  1154. if (!(this.default instanceof AST_SymbolImport)) throw new Error("default must be AST_SymbolImport");
  1155. if (this.default.key !== "") throw new Error("invalid default key: " + this.default.key);
  1156. }
  1157. if (typeof this.path != "string") throw new Error("path must be string");
  1158. if (this.properties != null) this.properties.forEach(function(node) {
  1159. if (!(node instanceof AST_SymbolImport)) throw new Error("properties must contain AST_SymbolImport");
  1160. });
  1161. if (this.quote != null) {
  1162. if (typeof this.quote != "string") throw new Error("quote must be string");
  1163. if (!/^["']$/.test(this.quote)) throw new Error("invalid quote: " + this.quote);
  1164. }
  1165. },
  1166. }, AST_Statement);
  1167. var AST_DefaultValue = DEFNODE("DefaultValue", "name value", {
  1168. $documentation: "A default value declaration",
  1169. $propdoc: {
  1170. name: "[AST_Destructured|AST_SymbolDeclaration] name of the variable",
  1171. value: "[AST_Node] value to assign if variable is `undefined`",
  1172. },
  1173. walk: function(visitor) {
  1174. var node = this;
  1175. visitor.visit(node, function() {
  1176. node.name.walk(visitor);
  1177. node.value.walk(visitor);
  1178. });
  1179. },
  1180. _validate: function() {
  1181. must_be_expression(this, "value");
  1182. },
  1183. });
  1184. function must_be_expressions(node, prop, allow_spread, allow_hole) {
  1185. node[prop].forEach(function(node) {
  1186. validate_expression(node, prop, true, allow_spread, allow_hole);
  1187. });
  1188. }
  1189. var AST_Call = DEFNODE("Call", "args expression optional pure", {
  1190. $documentation: "A function call expression",
  1191. $propdoc: {
  1192. args: "[AST_Node*] array of arguments",
  1193. expression: "[AST_Node] expression to invoke as function",
  1194. optional: "[boolean] whether the expression is optional chaining",
  1195. pure: "[string/S] marker for side-effect-free call expression",
  1196. },
  1197. walk: function(visitor) {
  1198. var node = this;
  1199. visitor.visit(node, function() {
  1200. node.expression.walk(visitor);
  1201. node.args.forEach(function(arg) {
  1202. arg.walk(visitor);
  1203. });
  1204. });
  1205. },
  1206. _validate: function() {
  1207. must_be_expression(this, "expression");
  1208. must_be_expressions(this, "args", true);
  1209. },
  1210. });
  1211. var AST_New = DEFNODE("New", null, {
  1212. $documentation: "An object instantiation. Derives from a function call since it has exactly the same properties",
  1213. _validate: function() {
  1214. if (this.optional) throw new Error("optional must be false");
  1215. },
  1216. }, AST_Call);
  1217. var AST_Sequence = DEFNODE("Sequence", "expressions", {
  1218. $documentation: "A sequence expression (comma-separated expressions)",
  1219. $propdoc: {
  1220. expressions: "[AST_Node*] array of expressions (at least two)"
  1221. },
  1222. walk: function(visitor) {
  1223. var node = this;
  1224. visitor.visit(node, function() {
  1225. node.expressions.forEach(function(expr) {
  1226. expr.walk(visitor);
  1227. });
  1228. });
  1229. },
  1230. _validate: function() {
  1231. if (this.expressions.length < 2) throw new Error("expressions must contain multiple elements");
  1232. must_be_expressions(this, "expressions");
  1233. },
  1234. });
  1235. var AST_PropAccess = DEFNODE("PropAccess", "expression optional property", {
  1236. $documentation: "Base class for property access expressions, i.e. `a.foo` or `a[\"foo\"]`",
  1237. $propdoc: {
  1238. expression: "[AST_Node] the “container” expression",
  1239. optional: "[boolean] whether the expression is optional chaining",
  1240. property: "[AST_Node|string] the property to access. For AST_Dot this is always a plain string, while for AST_Sub it's an arbitrary AST_Node",
  1241. },
  1242. getProperty: function() {
  1243. var p = this.property;
  1244. if (p instanceof AST_Constant) {
  1245. return p.value;
  1246. }
  1247. if (p instanceof AST_UnaryPrefix
  1248. && p.operator == "void"
  1249. && p.expression instanceof AST_Constant) {
  1250. return;
  1251. }
  1252. return p;
  1253. },
  1254. _validate: function() {
  1255. if (this.TYPE == "PropAccess") throw new Error("should not instantiate AST_PropAccess");
  1256. must_be_expression(this, "expression");
  1257. },
  1258. });
  1259. var AST_Dot = DEFNODE("Dot", null, {
  1260. $documentation: "A dotted property access expression",
  1261. walk: function(visitor) {
  1262. var node = this;
  1263. visitor.visit(node, function() {
  1264. node.expression.walk(visitor);
  1265. });
  1266. },
  1267. _validate: function() {
  1268. if (typeof this.property != "string") throw new Error("property must be string");
  1269. },
  1270. }, AST_PropAccess);
  1271. var AST_Sub = DEFNODE("Sub", null, {
  1272. $documentation: "Index-style property access, i.e. `a[\"foo\"]`",
  1273. walk: function(visitor) {
  1274. var node = this;
  1275. visitor.visit(node, function() {
  1276. node.expression.walk(visitor);
  1277. node.property.walk(visitor);
  1278. });
  1279. },
  1280. _validate: function() {
  1281. must_be_expression(this, "property");
  1282. },
  1283. }, AST_PropAccess);
  1284. var AST_Spread = DEFNODE("Spread", "expression", {
  1285. $documentation: "Spread expression in array/object literals or function calls",
  1286. $propdoc: {
  1287. expression: "[AST_Node] expression to be expanded",
  1288. },
  1289. walk: function(visitor) {
  1290. var node = this;
  1291. visitor.visit(node, function() {
  1292. node.expression.walk(visitor);
  1293. });
  1294. },
  1295. _validate: function() {
  1296. must_be_expression(this, "expression");
  1297. },
  1298. });
  1299. var AST_Unary = DEFNODE("Unary", "operator expression", {
  1300. $documentation: "Base class for unary expressions",
  1301. $propdoc: {
  1302. operator: "[string] the operator",
  1303. expression: "[AST_Node] expression that this unary operator applies to"
  1304. },
  1305. walk: function(visitor) {
  1306. var node = this;
  1307. visitor.visit(node, function() {
  1308. node.expression.walk(visitor);
  1309. });
  1310. },
  1311. _validate: function() {
  1312. if (this.TYPE == "Unary") throw new Error("should not instantiate AST_Unary");
  1313. if (typeof this.operator != "string") throw new Error("operator must be string");
  1314. must_be_expression(this, "expression");
  1315. },
  1316. });
  1317. var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, {
  1318. $documentation: "Unary prefix expression, i.e. `typeof i` or `++i`"
  1319. }, AST_Unary);
  1320. var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, {
  1321. $documentation: "Unary postfix expression, i.e. `i++`"
  1322. }, AST_Unary);
  1323. var AST_Binary = DEFNODE("Binary", "operator left right", {
  1324. $documentation: "Binary expression, i.e. `a + b`",
  1325. $propdoc: {
  1326. left: "[AST_Node] left-hand side expression",
  1327. operator: "[string] the operator",
  1328. right: "[AST_Node] right-hand side expression"
  1329. },
  1330. walk: function(visitor) {
  1331. var node = this;
  1332. visitor.visit(node, function() {
  1333. node.left.walk(visitor);
  1334. node.right.walk(visitor);
  1335. });
  1336. },
  1337. _validate: function() {
  1338. if (!(this instanceof AST_Assign)) must_be_expression(this, "left");
  1339. if (typeof this.operator != "string") throw new Error("operator must be string");
  1340. must_be_expression(this, "right");
  1341. },
  1342. });
  1343. var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", {
  1344. $documentation: "Conditional expression using the ternary operator, i.e. `a ? b : c`",
  1345. $propdoc: {
  1346. condition: "[AST_Node]",
  1347. consequent: "[AST_Node]",
  1348. alternative: "[AST_Node]"
  1349. },
  1350. walk: function(visitor) {
  1351. var node = this;
  1352. visitor.visit(node, function() {
  1353. node.condition.walk(visitor);
  1354. node.consequent.walk(visitor);
  1355. node.alternative.walk(visitor);
  1356. });
  1357. },
  1358. _validate: function() {
  1359. must_be_expression(this, "condition");
  1360. must_be_expression(this, "consequent");
  1361. must_be_expression(this, "alternative");
  1362. },
  1363. });
  1364. var AST_Assign = DEFNODE("Assign", null, {
  1365. $documentation: "An assignment expression — `a = b + 5`",
  1366. _validate: function() {
  1367. if (this.operator.indexOf("=") < 0) throw new Error('operator must contain "="');
  1368. if (this.left instanceof AST_Destructured) {
  1369. if (this.operator != "=") throw new Error("invalid destructuring operator: " + this.operator);
  1370. validate_destructured(this.left, function(node) {
  1371. if (!(node instanceof AST_PropAccess || node instanceof AST_SymbolRef)) {
  1372. throw new Error("left must be assignable: " + node.TYPE);
  1373. }
  1374. });
  1375. }
  1376. },
  1377. }, AST_Binary);
  1378. var AST_Await = DEFNODE("Await", "expression", {
  1379. $documentation: "An await expression",
  1380. $propdoc: {
  1381. expression: "[AST_Node] expression with Promise to resolve on",
  1382. },
  1383. walk: function(visitor) {
  1384. var node = this;
  1385. visitor.visit(node, function() {
  1386. node.expression.walk(visitor);
  1387. });
  1388. },
  1389. _validate: function() {
  1390. must_be_expression(this, "expression");
  1391. },
  1392. });
  1393. var AST_Yield = DEFNODE("Yield", "expression nested", {
  1394. $documentation: "A yield expression",
  1395. $propdoc: {
  1396. expression: "[AST_Node?] return value for iterator, or null if undefined",
  1397. nested: "[boolean] whether to iterate over expression as generator",
  1398. },
  1399. walk: function(visitor) {
  1400. var node = this;
  1401. visitor.visit(node, function() {
  1402. if (node.expression) node.expression.walk(visitor);
  1403. });
  1404. },
  1405. _validate: function() {
  1406. if (this.expression != null) {
  1407. must_be_expression(this, "expression");
  1408. } else if (this.nested) {
  1409. throw new Error("yield* must contain expression");
  1410. }
  1411. },
  1412. });
  1413. /* -----[ LITERALS ]----- */
  1414. var AST_Array = DEFNODE("Array", "elements", {
  1415. $documentation: "An array literal",
  1416. $propdoc: {
  1417. elements: "[AST_Node*] array of elements"
  1418. },
  1419. walk: function(visitor) {
  1420. var node = this;
  1421. visitor.visit(node, function() {
  1422. node.elements.forEach(function(element) {
  1423. element.walk(visitor);
  1424. });
  1425. });
  1426. },
  1427. _validate: function() {
  1428. must_be_expressions(this, "elements", true, true);
  1429. },
  1430. });
  1431. var AST_Destructured = DEFNODE("Destructured", "rest", {
  1432. $documentation: "Base class for destructured literal",
  1433. $propdoc: {
  1434. rest: "[(AST_Destructured|AST_SymbolDeclaration|AST_SymbolRef)?] rest parameter, or null if absent",
  1435. },
  1436. _validate: function() {
  1437. if (this.TYPE == "Destructured") throw new Error("should not instantiate AST_Destructured");
  1438. },
  1439. });
  1440. function validate_destructured(node, check, allow_default) {
  1441. if (node instanceof AST_DefaultValue && allow_default) return validate_destructured(node.name, check);
  1442. if (node instanceof AST_Destructured) {
  1443. if (node.rest != null) validate_destructured(node.rest, check);
  1444. if (node instanceof AST_DestructuredArray) return node.elements.forEach(function(node) {
  1445. if (!(node instanceof AST_Hole)) validate_destructured(node, check, true);
  1446. });
  1447. if (node instanceof AST_DestructuredObject) return node.properties.forEach(function(prop) {
  1448. validate_destructured(prop.value, check, true);
  1449. });
  1450. }
  1451. check(node);
  1452. }
  1453. var AST_DestructuredArray = DEFNODE("DestructuredArray", "elements", {
  1454. $documentation: "A destructured array literal",
  1455. $propdoc: {
  1456. elements: "[(AST_DefaultValue|AST_Destructured|AST_SymbolDeclaration|AST_SymbolRef)*] array of elements",
  1457. },
  1458. walk: function(visitor) {
  1459. var node = this;
  1460. visitor.visit(node, function() {
  1461. node.elements.forEach(function(element) {
  1462. element.walk(visitor);
  1463. });
  1464. if (node.rest) node.rest.walk(visitor);
  1465. });
  1466. },
  1467. }, AST_Destructured);
  1468. var AST_DestructuredKeyVal = DEFNODE("DestructuredKeyVal", "key value", {
  1469. $documentation: "A key: value destructured property",
  1470. $propdoc: {
  1471. key: "[string|AST_Node] property name. For computed property this is an AST_Node.",
  1472. value: "[AST_DefaultValue|AST_Destructured|AST_SymbolDeclaration|AST_SymbolRef] property value",
  1473. },
  1474. walk: function(visitor) {
  1475. var node = this;
  1476. visitor.visit(node, function() {
  1477. if (node.key instanceof AST_Node) node.key.walk(visitor);
  1478. node.value.walk(visitor);
  1479. });
  1480. },
  1481. _validate: function() {
  1482. if (typeof this.key != "string") {
  1483. if (!(this.key instanceof AST_Node)) throw new Error("key must be string or AST_Node");
  1484. must_be_expression(this, "key");
  1485. }
  1486. if (!(this.value instanceof AST_Node)) throw new Error("value must be AST_Node");
  1487. },
  1488. });
  1489. var AST_DestructuredObject = DEFNODE("DestructuredObject", "properties", {
  1490. $documentation: "A destructured object literal",
  1491. $propdoc: {
  1492. properties: "[AST_DestructuredKeyVal*] array of properties",
  1493. },
  1494. walk: function(visitor) {
  1495. var node = this;
  1496. visitor.visit(node, function() {
  1497. node.properties.forEach(function(prop) {
  1498. prop.walk(visitor);
  1499. });
  1500. if (node.rest) node.rest.walk(visitor);
  1501. });
  1502. },
  1503. _validate: function() {
  1504. this.properties.forEach(function(node) {
  1505. if (!(node instanceof AST_DestructuredKeyVal)) throw new Error("properties must be AST_DestructuredKeyVal[]");
  1506. });
  1507. },
  1508. }, AST_Destructured);
  1509. var AST_Object = DEFNODE("Object", "properties", {
  1510. $documentation: "An object literal",
  1511. $propdoc: {
  1512. properties: "[(AST_ObjectProperty|AST_Spread)*] array of properties"
  1513. },
  1514. walk: function(visitor) {
  1515. var node = this;
  1516. visitor.visit(node, function() {
  1517. node.properties.forEach(function(prop) {
  1518. prop.walk(visitor);
  1519. });
  1520. });
  1521. },
  1522. _validate: function() {
  1523. this.properties.forEach(function(node) {
  1524. if (!(node instanceof AST_ObjectProperty || node instanceof AST_Spread)) {
  1525. throw new Error("properties must contain AST_ObjectProperty and/or AST_Spread only");
  1526. }
  1527. });
  1528. },
  1529. });
  1530. var AST_ObjectProperty = DEFNODE("ObjectProperty", "key value", {
  1531. $documentation: "Base class for literal object properties",
  1532. $propdoc: {
  1533. key: "[string|AST_Node] property name. For computed property this is an AST_Node.",
  1534. value: "[AST_Node] property value. For getters and setters this is an AST_Accessor.",
  1535. },
  1536. walk: function(visitor) {
  1537. var node = this;
  1538. visitor.visit(node, function() {
  1539. if (node.key instanceof AST_Node) node.key.walk(visitor);
  1540. node.value.walk(visitor);
  1541. });
  1542. },
  1543. _validate: function() {
  1544. if (this.TYPE == "ObjectProperty") throw new Error("should not instantiate AST_ObjectProperty");
  1545. if (typeof this.key != "string") {
  1546. if (!(this.key instanceof AST_Node)) throw new Error("key must be string or AST_Node");
  1547. must_be_expression(this, "key");
  1548. }
  1549. if (!(this.value instanceof AST_Node)) throw new Error("value must be AST_Node");
  1550. },
  1551. });
  1552. var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", null, {
  1553. $documentation: "A key: value object property",
  1554. _validate: function() {
  1555. must_be_expression(this, "value");
  1556. },
  1557. }, AST_ObjectProperty);
  1558. var AST_ObjectMethod = DEFNODE("ObjectMethod", null, {
  1559. $documentation: "A key(){} object property",
  1560. _validate: function() {
  1561. if (!(this.value instanceof AST_LambdaExpression)) throw new Error("value must be AST_LambdaExpression");
  1562. if (is_arrow(this.value)) throw new Error("value cannot be AST_Arrow or AST_AsyncArrow");
  1563. if (this.value.name != null) throw new Error("name of class method's lambda must be null");
  1564. },
  1565. }, AST_ObjectKeyVal);
  1566. var AST_ObjectSetter = DEFNODE("ObjectSetter", null, {
  1567. $documentation: "An object setter property",
  1568. _validate: function() {
  1569. if (!(this.value instanceof AST_Accessor)) throw new Error("value must be AST_Accessor");
  1570. },
  1571. }, AST_ObjectProperty);
  1572. var AST_ObjectGetter = DEFNODE("ObjectGetter", null, {
  1573. $documentation: "An object getter property",
  1574. _validate: function() {
  1575. if (!(this.value instanceof AST_Accessor)) throw new Error("value must be AST_Accessor");
  1576. },
  1577. }, AST_ObjectProperty);
  1578. var AST_Symbol = DEFNODE("Symbol", "scope name thedef", {
  1579. $documentation: "Base class for all symbols",
  1580. $propdoc: {
  1581. name: "[string] name of this symbol",
  1582. scope: "[AST_Scope/S] the current scope (not necessarily the definition scope)",
  1583. thedef: "[SymbolDef/S] the definition of this symbol"
  1584. },
  1585. _validate: function() {
  1586. if (this.TYPE == "Symbol") throw new Error("should not instantiate AST_Symbol");
  1587. if (typeof this.name != "string") throw new Error("name must be string");
  1588. },
  1589. });
  1590. var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", {
  1591. $documentation: "A declaration symbol (symbol in var, function name or argument, symbol in catch)",
  1592. }, AST_Symbol);
  1593. var AST_SymbolConst = DEFNODE("SymbolConst", null, {
  1594. $documentation: "Symbol defining a constant",
  1595. }, AST_SymbolDeclaration);
  1596. var AST_SymbolImport = DEFNODE("SymbolImport", "key", {
  1597. $documentation: "Symbol defined by an `import` statement",
  1598. $propdoc: {
  1599. key: "[string] the original `export` name",
  1600. },
  1601. _validate: function() {
  1602. if (typeof this.key != "string") throw new Error("key must be string");
  1603. },
  1604. }, AST_SymbolConst);
  1605. var AST_SymbolLet = DEFNODE("SymbolLet", null, {
  1606. $documentation: "Symbol defining a lexical-scoped variable",
  1607. }, AST_SymbolDeclaration);
  1608. var AST_SymbolVar = DEFNODE("SymbolVar", null, {
  1609. $documentation: "Symbol defining a variable",
  1610. }, AST_SymbolDeclaration);
  1611. var AST_SymbolFunarg = DEFNODE("SymbolFunarg", null, {
  1612. $documentation: "Symbol naming a function argument",
  1613. }, AST_SymbolVar);
  1614. var AST_SymbolDefun = DEFNODE("SymbolDefun", null, {
  1615. $documentation: "Symbol defining a function",
  1616. }, AST_SymbolDeclaration);
  1617. var AST_SymbolLambda = DEFNODE("SymbolLambda", null, {
  1618. $documentation: "Symbol naming a function expression",
  1619. }, AST_SymbolDeclaration);
  1620. var AST_SymbolDefClass = DEFNODE("SymbolDefClass", null, {
  1621. $documentation: "Symbol defining a class",
  1622. }, AST_SymbolLet);
  1623. var AST_SymbolClass = DEFNODE("SymbolClass", null, {
  1624. $documentation: "Symbol naming a class expression",
  1625. }, AST_SymbolLet);
  1626. var AST_SymbolCatch = DEFNODE("SymbolCatch", null, {
  1627. $documentation: "Symbol naming the exception in catch",
  1628. }, AST_SymbolDeclaration);
  1629. var AST_Label = DEFNODE("Label", "references", {
  1630. $documentation: "Symbol naming a label (declaration)",
  1631. $propdoc: {
  1632. references: "[AST_LoopControl*] a list of nodes referring to this label"
  1633. },
  1634. initialize: function() {
  1635. this.references = [];
  1636. this.thedef = this;
  1637. }
  1638. }, AST_Symbol);
  1639. var AST_SymbolRef = DEFNODE("SymbolRef", "fixed in_arg redef", {
  1640. $documentation: "Reference to some symbol (not definition/declaration)",
  1641. }, AST_Symbol);
  1642. var AST_SymbolExport = DEFNODE("SymbolExport", "alias", {
  1643. $documentation: "Reference in an `export` statement",
  1644. $propdoc: {
  1645. alias: "[string] the `export` alias",
  1646. },
  1647. _validate: function() {
  1648. if (typeof this.alias != "string") throw new Error("alias must be string");
  1649. },
  1650. }, AST_SymbolRef);
  1651. var AST_LabelRef = DEFNODE("LabelRef", null, {
  1652. $documentation: "Reference to a label symbol",
  1653. }, AST_Symbol);
  1654. var AST_ObjectIdentity = DEFNODE("ObjectIdentity", null, {
  1655. $documentation: "Base class for `super` & `this`",
  1656. _validate: function() {
  1657. if (this.TYPE == "ObjectIdentity") throw new Error("should not instantiate AST_ObjectIdentity");
  1658. },
  1659. }, AST_Symbol);
  1660. var AST_Super = DEFNODE("Super", null, {
  1661. $documentation: "The `super` symbol",
  1662. _validate: function() {
  1663. if (this.name !== "super") throw new Error('name must be "super"');
  1664. },
  1665. }, AST_ObjectIdentity);
  1666. var AST_This = DEFNODE("This", null, {
  1667. $documentation: "The `this` symbol",
  1668. _validate: function() {
  1669. if (this.TYPE == "This" && this.name !== "this") throw new Error('name must be "this"');
  1670. },
  1671. }, AST_ObjectIdentity);
  1672. var AST_NewTarget = DEFNODE("NewTarget", null, {
  1673. $documentation: "The `new.target` symbol",
  1674. initialize: function() {
  1675. this.name = "new.target";
  1676. },
  1677. _validate: function() {
  1678. if (this.name !== "new.target") throw new Error('name must be "new.target": ' + this.name);
  1679. },
  1680. }, AST_This);
  1681. var AST_Template = DEFNODE("Template", "expressions strings tag", {
  1682. $documentation: "A template literal, i.e. tag`str1${expr1}...strN${exprN}strN+1`",
  1683. $propdoc: {
  1684. expressions: "[AST_Node*] the placeholder expressions",
  1685. strings: "[string*] the raw text segments",
  1686. tag: "[AST_Node] tag function, or null if absent",
  1687. },
  1688. walk: function(visitor) {
  1689. var node = this;
  1690. visitor.visit(node, function() {
  1691. if (node.tag) node.tag.walk(visitor);
  1692. node.expressions.forEach(function(expr) {
  1693. expr.walk(visitor);
  1694. });
  1695. });
  1696. },
  1697. _validate: function() {
  1698. if (this.expressions.length + 1 != this.strings.length) {
  1699. throw new Error("malformed template with " + this.expressions.length + " placeholder(s) but " + this.strings.length + " text segment(s)");
  1700. }
  1701. must_be_expressions(this, "expressions");
  1702. this.strings.forEach(function(string) {
  1703. if (typeof string != "string") throw new Error("strings must contain string");
  1704. });
  1705. if (this.tag != null) must_be_expression(this, "tag");
  1706. },
  1707. });
  1708. var AST_Constant = DEFNODE("Constant", null, {
  1709. $documentation: "Base class for all constants",
  1710. _validate: function() {
  1711. if (this.TYPE == "Constant") throw new Error("should not instantiate AST_Constant");
  1712. },
  1713. });
  1714. var AST_String = DEFNODE("String", "quote value", {
  1715. $documentation: "A string literal",
  1716. $propdoc: {
  1717. quote: "[string?] the original quote character",
  1718. value: "[string] the contents of this string",
  1719. },
  1720. _validate: function() {
  1721. if (this.quote != null) {
  1722. if (typeof this.quote != "string") throw new Error("quote must be string");
  1723. if (!/^["']$/.test(this.quote)) throw new Error("invalid quote: " + this.quote);
  1724. }
  1725. if (typeof this.value != "string") throw new Error("value must be string");
  1726. },
  1727. }, AST_Constant);
  1728. var AST_Number = DEFNODE("Number", "value", {
  1729. $documentation: "A number literal",
  1730. $propdoc: {
  1731. value: "[number] the numeric value",
  1732. },
  1733. _validate: function() {
  1734. if (typeof this.value != "number") throw new Error("value must be number");
  1735. if (!isFinite(this.value)) throw new Error("value must be finite");
  1736. if (this.value < 0) throw new Error("value cannot be negative");
  1737. },
  1738. }, AST_Constant);
  1739. var AST_BigInt = DEFNODE("BigInt", "value", {
  1740. $documentation: "A BigInt literal",
  1741. $propdoc: {
  1742. value: "[string] the numeric representation",
  1743. },
  1744. _validate: function() {
  1745. if (typeof this.value != "string") throw new Error("value must be string");
  1746. if (this.value[0] == "-") throw new Error("value cannot be negative");
  1747. },
  1748. }, AST_Constant);
  1749. var AST_RegExp = DEFNODE("RegExp", "value", {
  1750. $documentation: "A regexp literal",
  1751. $propdoc: {
  1752. value: "[RegExp] the actual regexp"
  1753. },
  1754. _validate: function() {
  1755. if (!(this.value instanceof RegExp)) throw new Error("value must be RegExp");
  1756. },
  1757. }, AST_Constant);
  1758. var AST_Atom = DEFNODE("Atom", null, {
  1759. $documentation: "Base class for atoms",
  1760. _validate: function() {
  1761. if (this.TYPE == "Atom") throw new Error("should not instantiate AST_Atom");
  1762. },
  1763. }, AST_Constant);
  1764. var AST_Null = DEFNODE("Null", null, {
  1765. $documentation: "The `null` atom",
  1766. value: null
  1767. }, AST_Atom);
  1768. var AST_NaN = DEFNODE("NaN", null, {
  1769. $documentation: "The impossible value",
  1770. value: 0/0
  1771. }, AST_Atom);
  1772. var AST_Undefined = DEFNODE("Undefined", null, {
  1773. $documentation: "The `undefined` value",
  1774. value: function(){}()
  1775. }, AST_Atom);
  1776. var AST_Hole = DEFNODE("Hole", null, {
  1777. $documentation: "A hole in an array",
  1778. value: function(){}()
  1779. }, AST_Atom);
  1780. var AST_Infinity = DEFNODE("Infinity", null, {
  1781. $documentation: "The `Infinity` value",
  1782. value: 1/0
  1783. }, AST_Atom);
  1784. var AST_Boolean = DEFNODE("Boolean", null, {
  1785. $documentation: "Base class for booleans",
  1786. _validate: function() {
  1787. if (this.TYPE == "Boolean") throw new Error("should not instantiate AST_Boolean");
  1788. },
  1789. }, AST_Atom);
  1790. var AST_False = DEFNODE("False", null, {
  1791. $documentation: "The `false` atom",
  1792. value: false
  1793. }, AST_Boolean);
  1794. var AST_True = DEFNODE("True", null, {
  1795. $documentation: "The `true` atom",
  1796. value: true
  1797. }, AST_Boolean);
  1798. /* -----[ TreeWalker ]----- */
  1799. function TreeWalker(callback) {
  1800. this.callback = callback;
  1801. this.directives = Object.create(null);
  1802. this.stack = [];
  1803. }
  1804. TreeWalker.prototype = {
  1805. visit: function(node, descend) {
  1806. this.push(node);
  1807. var done = this.callback(node, descend || noop);
  1808. if (!done && descend) descend();
  1809. this.pop();
  1810. },
  1811. parent: function(n) {
  1812. return this.stack[this.stack.length - 2 - (n || 0)];
  1813. },
  1814. push: function(node) {
  1815. if (node instanceof AST_Lambda) {
  1816. this.directives = Object.create(this.directives);
  1817. } else if (node instanceof AST_Directive && !this.directives[node.value]) {
  1818. this.directives[node.value] = node;
  1819. }
  1820. this.stack.push(node);
  1821. },
  1822. pop: function() {
  1823. var node = this.stack.pop();
  1824. if (node instanceof AST_Lambda) {
  1825. this.directives = Object.getPrototypeOf(this.directives);
  1826. }
  1827. },
  1828. self: function() {
  1829. return this.stack[this.stack.length - 1];
  1830. },
  1831. find_parent: function(type) {
  1832. var stack = this.stack;
  1833. for (var i = stack.length; --i >= 0;) {
  1834. var x = stack[i];
  1835. if (x instanceof type) return x;
  1836. }
  1837. },
  1838. has_directive: function(type) {
  1839. var dir = this.directives[type];
  1840. if (dir) return dir;
  1841. var node = this.stack[this.stack.length - 1];
  1842. if (node instanceof AST_Scope) {
  1843. for (var i = 0; i < node.body.length; ++i) {
  1844. var st = node.body[i];
  1845. if (!(st instanceof AST_Directive)) break;
  1846. if (st.value == type) return st;
  1847. }
  1848. }
  1849. },
  1850. loopcontrol_target: function(node) {
  1851. var stack = this.stack;
  1852. if (node.label) for (var i = stack.length; --i >= 0;) {
  1853. var x = stack[i];
  1854. if (x instanceof AST_LabeledStatement && x.label.name == node.label.name)
  1855. return x.body;
  1856. } else for (var i = stack.length; --i >= 0;) {
  1857. var x = stack[i];
  1858. if (x instanceof AST_IterationStatement
  1859. || node instanceof AST_Break && x instanceof AST_Switch)
  1860. return x;
  1861. }
  1862. },
  1863. in_boolean_context: function() {
  1864. var self = this.self();
  1865. for (var i = 0, p; p = this.parent(i); i++) {
  1866. if (p instanceof AST_Conditional && p.condition === self
  1867. || p instanceof AST_DWLoop && p.condition === self
  1868. || p instanceof AST_For && p.condition === self
  1869. || p instanceof AST_If && p.condition === self
  1870. || p instanceof AST_Return && p.in_bool
  1871. || p instanceof AST_Sequence && p.tail_node() !== self
  1872. || p instanceof AST_SimpleStatement
  1873. || p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self) {
  1874. return true;
  1875. }
  1876. if (p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")
  1877. || p instanceof AST_Conditional
  1878. || p.tail_node() === self) {
  1879. self = p;
  1880. } else if (p instanceof AST_Return) {
  1881. for (var call, fn = p; call = this.parent(++i); fn = call) {
  1882. if (call.TYPE == "Call") {
  1883. if (!(fn instanceof AST_Lambda) || fn.name) return false;
  1884. } else if (fn instanceof AST_Lambda) {
  1885. return false;
  1886. }
  1887. }
  1888. } else {
  1889. return false;
  1890. }
  1891. }
  1892. }
  1893. };