acorn_loose.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('./acorn')) :
  3. typeof define === 'function' && define.amd ? define(['exports', './acorn'], factory) :
  4. (factory((global.acorn = global.acorn || {}, global.acorn.loose = {}),global.acorn));
  5. }(this, (function (exports,__acorn) { 'use strict';
  6. function noop() {}
  7. // Registered plugins
  8. var pluginsLoose = {};
  9. var LooseParser = function LooseParser(input, options) {
  10. if ( options === void 0 ) options = {};
  11. this.toks = __acorn.tokenizer(input, options);
  12. this.options = this.toks.options;
  13. this.input = this.toks.input;
  14. this.tok = this.last = {type: __acorn.tokTypes.eof, start: 0, end: 0};
  15. this.tok.validateRegExpFlags = noop;
  16. this.tok.validateRegExpPattern = noop;
  17. if (this.options.locations) {
  18. var here = this.toks.curPosition();
  19. this.tok.loc = new __acorn.SourceLocation(this.toks, here, here);
  20. }
  21. this.ahead = []; // Tokens ahead
  22. this.context = []; // Indentation contexted
  23. this.curIndent = 0;
  24. this.curLineStart = 0;
  25. this.nextLineStart = this.lineEnd(this.curLineStart) + 1;
  26. this.inAsync = false;
  27. this.inFunction = false;
  28. // Load plugins
  29. this.options.pluginsLoose = options.pluginsLoose || {};
  30. this.loadPlugins(this.options.pluginsLoose);
  31. };
  32. LooseParser.prototype.startNode = function startNode () {
  33. return new __acorn.Node(this.toks, this.tok.start, this.options.locations ? this.tok.loc.start : null)
  34. };
  35. LooseParser.prototype.storeCurrentPos = function storeCurrentPos () {
  36. return this.options.locations ? [this.tok.start, this.tok.loc.start] : this.tok.start
  37. };
  38. LooseParser.prototype.startNodeAt = function startNodeAt (pos) {
  39. if (this.options.locations) {
  40. return new __acorn.Node(this.toks, pos[0], pos[1])
  41. } else {
  42. return new __acorn.Node(this.toks, pos)
  43. }
  44. };
  45. LooseParser.prototype.finishNode = function finishNode (node, type) {
  46. node.type = type;
  47. node.end = this.last.end;
  48. if (this.options.locations)
  49. { node.loc.end = this.last.loc.end; }
  50. if (this.options.ranges)
  51. { node.range[1] = this.last.end; }
  52. return node
  53. };
  54. LooseParser.prototype.dummyNode = function dummyNode (type) {
  55. var dummy = this.startNode();
  56. dummy.type = type;
  57. dummy.end = dummy.start;
  58. if (this.options.locations)
  59. { dummy.loc.end = dummy.loc.start; }
  60. if (this.options.ranges)
  61. { dummy.range[1] = dummy.start; }
  62. this.last = {type: __acorn.tokTypes.name, start: dummy.start, end: dummy.start, loc: dummy.loc};
  63. return dummy
  64. };
  65. LooseParser.prototype.dummyIdent = function dummyIdent () {
  66. var dummy = this.dummyNode("Identifier");
  67. dummy.name = "✖";
  68. return dummy
  69. };
  70. LooseParser.prototype.dummyString = function dummyString () {
  71. var dummy = this.dummyNode("Literal");
  72. dummy.value = dummy.raw = "✖";
  73. return dummy
  74. };
  75. LooseParser.prototype.eat = function eat (type) {
  76. if (this.tok.type === type) {
  77. this.next();
  78. return true
  79. } else {
  80. return false
  81. }
  82. };
  83. LooseParser.prototype.isContextual = function isContextual (name) {
  84. return this.tok.type === __acorn.tokTypes.name && this.tok.value === name
  85. };
  86. LooseParser.prototype.eatContextual = function eatContextual (name) {
  87. return this.tok.value === name && this.eat(__acorn.tokTypes.name)
  88. };
  89. LooseParser.prototype.canInsertSemicolon = function canInsertSemicolon () {
  90. return this.tok.type === __acorn.tokTypes.eof || this.tok.type === __acorn.tokTypes.braceR ||
  91. __acorn.lineBreak.test(this.input.slice(this.last.end, this.tok.start))
  92. };
  93. LooseParser.prototype.semicolon = function semicolon () {
  94. return this.eat(__acorn.tokTypes.semi)
  95. };
  96. LooseParser.prototype.expect = function expect (type) {
  97. var this$1 = this;
  98. if (this.eat(type)) { return true }
  99. for (var i = 1; i <= 2; i++) {
  100. if (this$1.lookAhead(i).type === type) {
  101. for (var j = 0; j < i; j++) { this$1.next(); }
  102. return true
  103. }
  104. }
  105. };
  106. LooseParser.prototype.pushCx = function pushCx () {
  107. this.context.push(this.curIndent);
  108. };
  109. LooseParser.prototype.popCx = function popCx () {
  110. this.curIndent = this.context.pop();
  111. };
  112. LooseParser.prototype.lineEnd = function lineEnd (pos) {
  113. while (pos < this.input.length && !__acorn.isNewLine(this.input.charCodeAt(pos))) { ++pos; }
  114. return pos
  115. };
  116. LooseParser.prototype.indentationAfter = function indentationAfter (pos) {
  117. var this$1 = this;
  118. for (var count = 0;; ++pos) {
  119. var ch = this$1.input.charCodeAt(pos);
  120. if (ch === 32) { ++count; }
  121. else if (ch === 9) { count += this$1.options.tabSize; }
  122. else { return count }
  123. }
  124. };
  125. LooseParser.prototype.closes = function closes (closeTok, indent, line, blockHeuristic) {
  126. if (this.tok.type === closeTok || this.tok.type === __acorn.tokTypes.eof) { return true }
  127. return line !== this.curLineStart && this.curIndent < indent && this.tokenStartsLine() &&
  128. (!blockHeuristic || this.nextLineStart >= this.input.length ||
  129. this.indentationAfter(this.nextLineStart) < indent)
  130. };
  131. LooseParser.prototype.tokenStartsLine = function tokenStartsLine () {
  132. var this$1 = this;
  133. for (var p = this.tok.start - 1; p >= this.curLineStart; --p) {
  134. var ch = this$1.input.charCodeAt(p);
  135. if (ch !== 9 && ch !== 32) { return false }
  136. }
  137. return true
  138. };
  139. LooseParser.prototype.extend = function extend (name, f) {
  140. this[name] = f(this[name]);
  141. };
  142. LooseParser.prototype.loadPlugins = function loadPlugins (pluginConfigs) {
  143. var this$1 = this;
  144. for (var name in pluginConfigs) {
  145. var plugin = pluginsLoose[name];
  146. if (!plugin) { throw new Error("Plugin '" + name + "' not found") }
  147. plugin(this$1, pluginConfigs[name]);
  148. }
  149. };
  150. LooseParser.prototype.parse = function parse () {
  151. this.next();
  152. return this.parseTopLevel()
  153. };
  154. var lp = LooseParser.prototype;
  155. function isSpace(ch) {
  156. return (ch < 14 && ch > 8) || ch === 32 || ch === 160 || __acorn.isNewLine(ch)
  157. }
  158. lp.next = function() {
  159. var this$1 = this;
  160. this.last = this.tok;
  161. if (this.ahead.length)
  162. { this.tok = this.ahead.shift(); }
  163. else
  164. { this.tok = this.readToken(); }
  165. if (this.tok.start >= this.nextLineStart) {
  166. while (this.tok.start >= this.nextLineStart) {
  167. this$1.curLineStart = this$1.nextLineStart;
  168. this$1.nextLineStart = this$1.lineEnd(this$1.curLineStart) + 1;
  169. }
  170. this.curIndent = this.indentationAfter(this.curLineStart);
  171. }
  172. };
  173. lp.readToken = function() {
  174. var this$1 = this;
  175. for (;;) {
  176. try {
  177. this$1.toks.next();
  178. if (this$1.toks.type === __acorn.tokTypes.dot &&
  179. this$1.input.substr(this$1.toks.end, 1) === "." &&
  180. this$1.options.ecmaVersion >= 6) {
  181. this$1.toks.end++;
  182. this$1.toks.type = __acorn.tokTypes.ellipsis;
  183. }
  184. return new __acorn.Token(this$1.toks)
  185. } catch (e) {
  186. if (!(e instanceof SyntaxError)) { throw e }
  187. // Try to skip some text, based on the error message, and then continue
  188. var msg = e.message, pos = e.raisedAt, replace = true;
  189. if (/unterminated/i.test(msg)) {
  190. pos = this$1.lineEnd(e.pos + 1);
  191. if (/string/.test(msg)) {
  192. replace = {start: e.pos, end: pos, type: __acorn.tokTypes.string, value: this$1.input.slice(e.pos + 1, pos)};
  193. } else if (/regular expr/i.test(msg)) {
  194. var re = this$1.input.slice(e.pos, pos);
  195. try { re = new RegExp(re); } catch (e) { /* ignore compilation error due to new syntax */ }
  196. replace = {start: e.pos, end: pos, type: __acorn.tokTypes.regexp, value: re};
  197. } else if (/template/.test(msg)) {
  198. replace = {
  199. start: e.pos,
  200. end: pos,
  201. type: __acorn.tokTypes.template,
  202. value: this$1.input.slice(e.pos, pos)
  203. };
  204. } else {
  205. replace = false;
  206. }
  207. } else if (/invalid (unicode|regexp|number)|expecting unicode|octal literal|is reserved|directly after number|expected number in radix/i.test(msg)) {
  208. while (pos < this.input.length && !isSpace(this.input.charCodeAt(pos))) { ++pos; }
  209. } else if (/character escape|expected hexadecimal/i.test(msg)) {
  210. while (pos < this.input.length) {
  211. var ch = this$1.input.charCodeAt(pos++);
  212. if (ch === 34 || ch === 39 || __acorn.isNewLine(ch)) { break }
  213. }
  214. } else if (/unexpected character/i.test(msg)) {
  215. pos++;
  216. replace = false;
  217. } else if (/regular expression/i.test(msg)) {
  218. replace = true;
  219. } else {
  220. throw e
  221. }
  222. this$1.resetTo(pos);
  223. if (replace === true) { replace = {start: pos, end: pos, type: __acorn.tokTypes.name, value: "✖"}; }
  224. if (replace) {
  225. if (this$1.options.locations)
  226. { replace.loc = new __acorn.SourceLocation(
  227. this$1.toks,
  228. __acorn.getLineInfo(this$1.input, replace.start),
  229. __acorn.getLineInfo(this$1.input, replace.end)); }
  230. return replace
  231. }
  232. }
  233. }
  234. };
  235. lp.resetTo = function(pos) {
  236. var this$1 = this;
  237. this.toks.pos = pos;
  238. var ch = this.input.charAt(pos - 1);
  239. this.toks.exprAllowed = !ch || /[[{(,;:?/*=+\-~!|&%^<>]/.test(ch) ||
  240. /[enwfd]/.test(ch) &&
  241. /\b(case|else|return|throw|new|in|(instance|type)?of|delete|void)$/.test(this.input.slice(pos - 10, pos));
  242. if (this.options.locations) {
  243. this.toks.curLine = 1;
  244. this.toks.lineStart = __acorn.lineBreakG.lastIndex = 0;
  245. var match;
  246. while ((match = __acorn.lineBreakG.exec(this.input)) && match.index < pos) {
  247. ++this$1.toks.curLine;
  248. this$1.toks.lineStart = match.index + match[0].length;
  249. }
  250. }
  251. };
  252. lp.lookAhead = function(n) {
  253. var this$1 = this;
  254. while (n > this.ahead.length)
  255. { this$1.ahead.push(this$1.readToken()); }
  256. return this.ahead[n - 1]
  257. };
  258. function isDummy(node) { return node.name === "✖" }
  259. var lp$1 = LooseParser.prototype;
  260. lp$1.parseTopLevel = function() {
  261. var this$1 = this;
  262. var node = this.startNodeAt(this.options.locations ? [0, __acorn.getLineInfo(this.input, 0)] : 0);
  263. node.body = [];
  264. while (this.tok.type !== __acorn.tokTypes.eof) { node.body.push(this$1.parseStatement()); }
  265. this.toks.adaptDirectivePrologue(node.body);
  266. this.last = this.tok;
  267. if (this.options.ecmaVersion >= 6) {
  268. node.sourceType = this.options.sourceType;
  269. }
  270. return this.finishNode(node, "Program")
  271. };
  272. lp$1.parseStatement = function() {
  273. var this$1 = this;
  274. var starttype = this.tok.type, node = this.startNode(), kind;
  275. if (this.toks.isLet()) {
  276. starttype = __acorn.tokTypes._var;
  277. kind = "let";
  278. }
  279. switch (starttype) {
  280. case __acorn.tokTypes._break: case __acorn.tokTypes._continue:
  281. this.next();
  282. var isBreak = starttype === __acorn.tokTypes._break;
  283. if (this.semicolon() || this.canInsertSemicolon()) {
  284. node.label = null;
  285. } else {
  286. node.label = this.tok.type === __acorn.tokTypes.name ? this.parseIdent() : null;
  287. this.semicolon();
  288. }
  289. return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement")
  290. case __acorn.tokTypes._debugger:
  291. this.next();
  292. this.semicolon();
  293. return this.finishNode(node, "DebuggerStatement")
  294. case __acorn.tokTypes._do:
  295. this.next();
  296. node.body = this.parseStatement();
  297. node.test = this.eat(__acorn.tokTypes._while) ? this.parseParenExpression() : this.dummyIdent();
  298. this.semicolon();
  299. return this.finishNode(node, "DoWhileStatement")
  300. case __acorn.tokTypes._for:
  301. this.next(); // `for` keyword
  302. var isAwait = this.options.ecmaVersion >= 9 && this.inAsync && this.eatContextual("await");
  303. this.pushCx();
  304. this.expect(__acorn.tokTypes.parenL);
  305. if (this.tok.type === __acorn.tokTypes.semi) { return this.parseFor(node, null) }
  306. var isLet = this.toks.isLet();
  307. if (isLet || this.tok.type === __acorn.tokTypes._var || this.tok.type === __acorn.tokTypes._const) {
  308. var init$1 = this.parseVar(true, isLet ? "let" : this.tok.value);
  309. if (init$1.declarations.length === 1 && (this.tok.type === __acorn.tokTypes._in || this.isContextual("of"))) {
  310. if (this.options.ecmaVersion >= 9 && this.tok.type !== __acorn.tokTypes._in) {
  311. node.await = isAwait;
  312. }
  313. return this.parseForIn(node, init$1)
  314. }
  315. return this.parseFor(node, init$1)
  316. }
  317. var init = this.parseExpression(true);
  318. if (this.tok.type === __acorn.tokTypes._in || this.isContextual("of")) {
  319. if (this.options.ecmaVersion >= 9 && this.tok.type !== __acorn.tokTypes._in) {
  320. node.await = isAwait;
  321. }
  322. return this.parseForIn(node, this.toAssignable(init))
  323. }
  324. return this.parseFor(node, init)
  325. case __acorn.tokTypes._function:
  326. this.next();
  327. return this.parseFunction(node, true)
  328. case __acorn.tokTypes._if:
  329. this.next();
  330. node.test = this.parseParenExpression();
  331. node.consequent = this.parseStatement();
  332. node.alternate = this.eat(__acorn.tokTypes._else) ? this.parseStatement() : null;
  333. return this.finishNode(node, "IfStatement")
  334. case __acorn.tokTypes._return:
  335. this.next();
  336. if (this.eat(__acorn.tokTypes.semi) || this.canInsertSemicolon()) { node.argument = null; }
  337. else { node.argument = this.parseExpression(); this.semicolon(); }
  338. return this.finishNode(node, "ReturnStatement")
  339. case __acorn.tokTypes._switch:
  340. var blockIndent = this.curIndent, line = this.curLineStart;
  341. this.next();
  342. node.discriminant = this.parseParenExpression();
  343. node.cases = [];
  344. this.pushCx();
  345. this.expect(__acorn.tokTypes.braceL);
  346. var cur;
  347. while (!this.closes(__acorn.tokTypes.braceR, blockIndent, line, true)) {
  348. if (this$1.tok.type === __acorn.tokTypes._case || this$1.tok.type === __acorn.tokTypes._default) {
  349. var isCase = this$1.tok.type === __acorn.tokTypes._case;
  350. if (cur) { this$1.finishNode(cur, "SwitchCase"); }
  351. node.cases.push(cur = this$1.startNode());
  352. cur.consequent = [];
  353. this$1.next();
  354. if (isCase) { cur.test = this$1.parseExpression(); }
  355. else { cur.test = null; }
  356. this$1.expect(__acorn.tokTypes.colon);
  357. } else {
  358. if (!cur) {
  359. node.cases.push(cur = this$1.startNode());
  360. cur.consequent = [];
  361. cur.test = null;
  362. }
  363. cur.consequent.push(this$1.parseStatement());
  364. }
  365. }
  366. if (cur) { this.finishNode(cur, "SwitchCase"); }
  367. this.popCx();
  368. this.eat(__acorn.tokTypes.braceR);
  369. return this.finishNode(node, "SwitchStatement")
  370. case __acorn.tokTypes._throw:
  371. this.next();
  372. node.argument = this.parseExpression();
  373. this.semicolon();
  374. return this.finishNode(node, "ThrowStatement")
  375. case __acorn.tokTypes._try:
  376. this.next();
  377. node.block = this.parseBlock();
  378. node.handler = null;
  379. if (this.tok.type === __acorn.tokTypes._catch) {
  380. var clause = this.startNode();
  381. this.next();
  382. if (this.eat(__acorn.tokTypes.parenL)) {
  383. clause.param = this.toAssignable(this.parseExprAtom(), true);
  384. this.expect(__acorn.tokTypes.parenR);
  385. } else {
  386. clause.param = null;
  387. }
  388. clause.body = this.parseBlock();
  389. node.handler = this.finishNode(clause, "CatchClause");
  390. }
  391. node.finalizer = this.eat(__acorn.tokTypes._finally) ? this.parseBlock() : null;
  392. if (!node.handler && !node.finalizer) { return node.block }
  393. return this.finishNode(node, "TryStatement")
  394. case __acorn.tokTypes._var:
  395. case __acorn.tokTypes._const:
  396. return this.parseVar(false, kind || this.tok.value)
  397. case __acorn.tokTypes._while:
  398. this.next();
  399. node.test = this.parseParenExpression();
  400. node.body = this.parseStatement();
  401. return this.finishNode(node, "WhileStatement")
  402. case __acorn.tokTypes._with:
  403. this.next();
  404. node.object = this.parseParenExpression();
  405. node.body = this.parseStatement();
  406. return this.finishNode(node, "WithStatement")
  407. case __acorn.tokTypes.braceL:
  408. return this.parseBlock()
  409. case __acorn.tokTypes.semi:
  410. this.next();
  411. return this.finishNode(node, "EmptyStatement")
  412. case __acorn.tokTypes._class:
  413. return this.parseClass(true)
  414. case __acorn.tokTypes._import:
  415. return this.parseImport()
  416. case __acorn.tokTypes._export:
  417. return this.parseExport()
  418. default:
  419. if (this.toks.isAsyncFunction()) {
  420. this.next();
  421. this.next();
  422. return this.parseFunction(node, true, true)
  423. }
  424. var expr = this.parseExpression();
  425. if (isDummy(expr)) {
  426. this.next();
  427. if (this.tok.type === __acorn.tokTypes.eof) { return this.finishNode(node, "EmptyStatement") }
  428. return this.parseStatement()
  429. } else if (starttype === __acorn.tokTypes.name && expr.type === "Identifier" && this.eat(__acorn.tokTypes.colon)) {
  430. node.body = this.parseStatement();
  431. node.label = expr;
  432. return this.finishNode(node, "LabeledStatement")
  433. } else {
  434. node.expression = expr;
  435. this.semicolon();
  436. return this.finishNode(node, "ExpressionStatement")
  437. }
  438. }
  439. };
  440. lp$1.parseBlock = function() {
  441. var this$1 = this;
  442. var node = this.startNode();
  443. this.pushCx();
  444. this.expect(__acorn.tokTypes.braceL);
  445. var blockIndent = this.curIndent, line = this.curLineStart;
  446. node.body = [];
  447. while (!this.closes(__acorn.tokTypes.braceR, blockIndent, line, true))
  448. { node.body.push(this$1.parseStatement()); }
  449. this.popCx();
  450. this.eat(__acorn.tokTypes.braceR);
  451. return this.finishNode(node, "BlockStatement")
  452. };
  453. lp$1.parseFor = function(node, init) {
  454. node.init = init;
  455. node.test = node.update = null;
  456. if (this.eat(__acorn.tokTypes.semi) && this.tok.type !== __acorn.tokTypes.semi) { node.test = this.parseExpression(); }
  457. if (this.eat(__acorn.tokTypes.semi) && this.tok.type !== __acorn.tokTypes.parenR) { node.update = this.parseExpression(); }
  458. this.popCx();
  459. this.expect(__acorn.tokTypes.parenR);
  460. node.body = this.parseStatement();
  461. return this.finishNode(node, "ForStatement")
  462. };
  463. lp$1.parseForIn = function(node, init) {
  464. var type = this.tok.type === __acorn.tokTypes._in ? "ForInStatement" : "ForOfStatement";
  465. this.next();
  466. node.left = init;
  467. node.right = this.parseExpression();
  468. this.popCx();
  469. this.expect(__acorn.tokTypes.parenR);
  470. node.body = this.parseStatement();
  471. return this.finishNode(node, type)
  472. };
  473. lp$1.parseVar = function(noIn, kind) {
  474. var this$1 = this;
  475. var node = this.startNode();
  476. node.kind = kind;
  477. this.next();
  478. node.declarations = [];
  479. do {
  480. var decl = this$1.startNode();
  481. decl.id = this$1.options.ecmaVersion >= 6 ? this$1.toAssignable(this$1.parseExprAtom(), true) : this$1.parseIdent();
  482. decl.init = this$1.eat(__acorn.tokTypes.eq) ? this$1.parseMaybeAssign(noIn) : null;
  483. node.declarations.push(this$1.finishNode(decl, "VariableDeclarator"));
  484. } while (this.eat(__acorn.tokTypes.comma))
  485. if (!node.declarations.length) {
  486. var decl$1 = this.startNode();
  487. decl$1.id = this.dummyIdent();
  488. node.declarations.push(this.finishNode(decl$1, "VariableDeclarator"));
  489. }
  490. if (!noIn) { this.semicolon(); }
  491. return this.finishNode(node, "VariableDeclaration")
  492. };
  493. lp$1.parseClass = function(isStatement) {
  494. var this$1 = this;
  495. var node = this.startNode();
  496. this.next();
  497. if (this.tok.type === __acorn.tokTypes.name) { node.id = this.parseIdent(); }
  498. else if (isStatement === true) { node.id = this.dummyIdent(); }
  499. else { node.id = null; }
  500. node.superClass = this.eat(__acorn.tokTypes._extends) ? this.parseExpression() : null;
  501. node.body = this.startNode();
  502. node.body.body = [];
  503. this.pushCx();
  504. var indent = this.curIndent + 1, line = this.curLineStart;
  505. this.eat(__acorn.tokTypes.braceL);
  506. if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart; }
  507. while (!this.closes(__acorn.tokTypes.braceR, indent, line)) {
  508. if (this$1.semicolon()) { continue }
  509. var method = this$1.startNode(), isGenerator = (void 0), isAsync = (void 0);
  510. if (this$1.options.ecmaVersion >= 6) {
  511. method.static = false;
  512. isGenerator = this$1.eat(__acorn.tokTypes.star);
  513. }
  514. this$1.parsePropertyName(method);
  515. if (isDummy(method.key)) { if (isDummy(this$1.parseMaybeAssign())) { this$1.next(); } this$1.eat(__acorn.tokTypes.comma); continue }
  516. if (method.key.type === "Identifier" && !method.computed && method.key.name === "static" &&
  517. (this$1.tok.type !== __acorn.tokTypes.parenL && this$1.tok.type !== __acorn.tokTypes.braceL)) {
  518. method.static = true;
  519. isGenerator = this$1.eat(__acorn.tokTypes.star);
  520. this$1.parsePropertyName(method);
  521. } else {
  522. method.static = false;
  523. }
  524. if (!method.computed &&
  525. method.key.type === "Identifier" && method.key.name === "async" && this$1.tok.type !== __acorn.tokTypes.parenL &&
  526. !this$1.canInsertSemicolon()) {
  527. isAsync = true;
  528. isGenerator = this$1.options.ecmaVersion >= 9 && this$1.eat(__acorn.tokTypes.star);
  529. this$1.parsePropertyName(method);
  530. } else {
  531. isAsync = false;
  532. }
  533. if (this$1.options.ecmaVersion >= 5 && method.key.type === "Identifier" &&
  534. !method.computed && (method.key.name === "get" || method.key.name === "set") &&
  535. this$1.tok.type !== __acorn.tokTypes.parenL && this$1.tok.type !== __acorn.tokTypes.braceL) {
  536. method.kind = method.key.name;
  537. this$1.parsePropertyName(method);
  538. method.value = this$1.parseMethod(false);
  539. } else {
  540. if (!method.computed && !method.static && !isGenerator && !isAsync && (
  541. method.key.type === "Identifier" && method.key.name === "constructor" ||
  542. method.key.type === "Literal" && method.key.value === "constructor")) {
  543. method.kind = "constructor";
  544. } else {
  545. method.kind = "method";
  546. }
  547. method.value = this$1.parseMethod(isGenerator, isAsync);
  548. }
  549. node.body.body.push(this$1.finishNode(method, "MethodDefinition"));
  550. }
  551. this.popCx();
  552. if (!this.eat(__acorn.tokTypes.braceR)) {
  553. // If there is no closing brace, make the node span to the start
  554. // of the next token (this is useful for Tern)
  555. this.last.end = this.tok.start;
  556. if (this.options.locations) { this.last.loc.end = this.tok.loc.start; }
  557. }
  558. this.semicolon();
  559. this.finishNode(node.body, "ClassBody");
  560. return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
  561. };
  562. lp$1.parseFunction = function(node, isStatement, isAsync) {
  563. var oldInAsync = this.inAsync, oldInFunction = this.inFunction;
  564. this.initFunction(node);
  565. if (this.options.ecmaVersion >= 6) {
  566. node.generator = this.eat(__acorn.tokTypes.star);
  567. }
  568. if (this.options.ecmaVersion >= 8) {
  569. node.async = !!isAsync;
  570. }
  571. if (this.tok.type === __acorn.tokTypes.name) { node.id = this.parseIdent(); }
  572. else if (isStatement === true) { node.id = this.dummyIdent(); }
  573. this.inAsync = node.async;
  574. this.inFunction = true;
  575. node.params = this.parseFunctionParams();
  576. node.body = this.parseBlock();
  577. this.toks.adaptDirectivePrologue(node.body.body);
  578. this.inAsync = oldInAsync;
  579. this.inFunction = oldInFunction;
  580. return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression")
  581. };
  582. lp$1.parseExport = function() {
  583. var node = this.startNode();
  584. this.next();
  585. if (this.eat(__acorn.tokTypes.star)) {
  586. node.source = this.eatContextual("from") ? this.parseExprAtom() : this.dummyString();
  587. return this.finishNode(node, "ExportAllDeclaration")
  588. }
  589. if (this.eat(__acorn.tokTypes._default)) {
  590. // export default (function foo() {}) // This is FunctionExpression.
  591. var isAsync;
  592. if (this.tok.type === __acorn.tokTypes._function || (isAsync = this.toks.isAsyncFunction())) {
  593. var fNode = this.startNode();
  594. this.next();
  595. if (isAsync) { this.next(); }
  596. node.declaration = this.parseFunction(fNode, "nullableID", isAsync);
  597. } else if (this.tok.type === __acorn.tokTypes._class) {
  598. node.declaration = this.parseClass("nullableID");
  599. } else {
  600. node.declaration = this.parseMaybeAssign();
  601. this.semicolon();
  602. }
  603. return this.finishNode(node, "ExportDefaultDeclaration")
  604. }
  605. if (this.tok.type.keyword || this.toks.isLet() || this.toks.isAsyncFunction()) {
  606. node.declaration = this.parseStatement();
  607. node.specifiers = [];
  608. node.source = null;
  609. } else {
  610. node.declaration = null;
  611. node.specifiers = this.parseExportSpecifierList();
  612. node.source = this.eatContextual("from") ? this.parseExprAtom() : null;
  613. this.semicolon();
  614. }
  615. return this.finishNode(node, "ExportNamedDeclaration")
  616. };
  617. lp$1.parseImport = function() {
  618. var node = this.startNode();
  619. this.next();
  620. if (this.tok.type === __acorn.tokTypes.string) {
  621. node.specifiers = [];
  622. node.source = this.parseExprAtom();
  623. } else {
  624. var elt;
  625. if (this.tok.type === __acorn.tokTypes.name && this.tok.value !== "from") {
  626. elt = this.startNode();
  627. elt.local = this.parseIdent();
  628. this.finishNode(elt, "ImportDefaultSpecifier");
  629. this.eat(__acorn.tokTypes.comma);
  630. }
  631. node.specifiers = this.parseImportSpecifierList();
  632. node.source = this.eatContextual("from") && this.tok.type === __acorn.tokTypes.string ? this.parseExprAtom() : this.dummyString();
  633. if (elt) { node.specifiers.unshift(elt); }
  634. }
  635. this.semicolon();
  636. return this.finishNode(node, "ImportDeclaration")
  637. };
  638. lp$1.parseImportSpecifierList = function() {
  639. var this$1 = this;
  640. var elts = [];
  641. if (this.tok.type === __acorn.tokTypes.star) {
  642. var elt = this.startNode();
  643. this.next();
  644. elt.local = this.eatContextual("as") ? this.parseIdent() : this.dummyIdent();
  645. elts.push(this.finishNode(elt, "ImportNamespaceSpecifier"));
  646. } else {
  647. var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart;
  648. this.pushCx();
  649. this.eat(__acorn.tokTypes.braceL);
  650. if (this.curLineStart > continuedLine) { continuedLine = this.curLineStart; }
  651. while (!this.closes(__acorn.tokTypes.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
  652. var elt$1 = this$1.startNode();
  653. if (this$1.eat(__acorn.tokTypes.star)) {
  654. elt$1.local = this$1.eatContextual("as") ? this$1.parseIdent() : this$1.dummyIdent();
  655. this$1.finishNode(elt$1, "ImportNamespaceSpecifier");
  656. } else {
  657. if (this$1.isContextual("from")) { break }
  658. elt$1.imported = this$1.parseIdent();
  659. if (isDummy(elt$1.imported)) { break }
  660. elt$1.local = this$1.eatContextual("as") ? this$1.parseIdent() : elt$1.imported;
  661. this$1.finishNode(elt$1, "ImportSpecifier");
  662. }
  663. elts.push(elt$1);
  664. this$1.eat(__acorn.tokTypes.comma);
  665. }
  666. this.eat(__acorn.tokTypes.braceR);
  667. this.popCx();
  668. }
  669. return elts
  670. };
  671. lp$1.parseExportSpecifierList = function() {
  672. var this$1 = this;
  673. var elts = [];
  674. var indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart;
  675. this.pushCx();
  676. this.eat(__acorn.tokTypes.braceL);
  677. if (this.curLineStart > continuedLine) { continuedLine = this.curLineStart; }
  678. while (!this.closes(__acorn.tokTypes.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
  679. if (this$1.isContextual("from")) { break }
  680. var elt = this$1.startNode();
  681. elt.local = this$1.parseIdent();
  682. if (isDummy(elt.local)) { break }
  683. elt.exported = this$1.eatContextual("as") ? this$1.parseIdent() : elt.local;
  684. this$1.finishNode(elt, "ExportSpecifier");
  685. elts.push(elt);
  686. this$1.eat(__acorn.tokTypes.comma);
  687. }
  688. this.eat(__acorn.tokTypes.braceR);
  689. this.popCx();
  690. return elts
  691. };
  692. var lp$2 = LooseParser.prototype;
  693. lp$2.checkLVal = function(expr) {
  694. if (!expr) { return expr }
  695. switch (expr.type) {
  696. case "Identifier":
  697. case "MemberExpression":
  698. return expr
  699. case "ParenthesizedExpression":
  700. expr.expression = this.checkLVal(expr.expression);
  701. return expr
  702. default:
  703. return this.dummyIdent()
  704. }
  705. };
  706. lp$2.parseExpression = function(noIn) {
  707. var this$1 = this;
  708. var start = this.storeCurrentPos();
  709. var expr = this.parseMaybeAssign(noIn);
  710. if (this.tok.type === __acorn.tokTypes.comma) {
  711. var node = this.startNodeAt(start);
  712. node.expressions = [expr];
  713. while (this.eat(__acorn.tokTypes.comma)) { node.expressions.push(this$1.parseMaybeAssign(noIn)); }
  714. return this.finishNode(node, "SequenceExpression")
  715. }
  716. return expr
  717. };
  718. lp$2.parseParenExpression = function() {
  719. this.pushCx();
  720. this.expect(__acorn.tokTypes.parenL);
  721. var val = this.parseExpression();
  722. this.popCx();
  723. this.expect(__acorn.tokTypes.parenR);
  724. return val
  725. };
  726. lp$2.parseMaybeAssign = function(noIn) {
  727. if (this.toks.isContextual("yield")) {
  728. var node = this.startNode();
  729. this.next();
  730. if (this.semicolon() || this.canInsertSemicolon() || (this.tok.type !== __acorn.tokTypes.star && !this.tok.type.startsExpr)) {
  731. node.delegate = false;
  732. node.argument = null;
  733. } else {
  734. node.delegate = this.eat(__acorn.tokTypes.star);
  735. node.argument = this.parseMaybeAssign();
  736. }
  737. return this.finishNode(node, "YieldExpression")
  738. }
  739. var start = this.storeCurrentPos();
  740. var left = this.parseMaybeConditional(noIn);
  741. if (this.tok.type.isAssign) {
  742. var node$1 = this.startNodeAt(start);
  743. node$1.operator = this.tok.value;
  744. node$1.left = this.tok.type === __acorn.tokTypes.eq ? this.toAssignable(left) : this.checkLVal(left);
  745. this.next();
  746. node$1.right = this.parseMaybeAssign(noIn);
  747. return this.finishNode(node$1, "AssignmentExpression")
  748. }
  749. return left
  750. };
  751. lp$2.parseMaybeConditional = function(noIn) {
  752. var start = this.storeCurrentPos();
  753. var expr = this.parseExprOps(noIn);
  754. if (this.eat(__acorn.tokTypes.question)) {
  755. var node = this.startNodeAt(start);
  756. node.test = expr;
  757. node.consequent = this.parseMaybeAssign();
  758. node.alternate = this.expect(__acorn.tokTypes.colon) ? this.parseMaybeAssign(noIn) : this.dummyIdent();
  759. return this.finishNode(node, "ConditionalExpression")
  760. }
  761. return expr
  762. };
  763. lp$2.parseExprOps = function(noIn) {
  764. var start = this.storeCurrentPos();
  765. var indent = this.curIndent, line = this.curLineStart;
  766. return this.parseExprOp(this.parseMaybeUnary(false), start, -1, noIn, indent, line)
  767. };
  768. lp$2.parseExprOp = function(left, start, minPrec, noIn, indent, line) {
  769. if (this.curLineStart !== line && this.curIndent < indent && this.tokenStartsLine()) { return left }
  770. var prec = this.tok.type.binop;
  771. if (prec != null && (!noIn || this.tok.type !== __acorn.tokTypes._in)) {
  772. if (prec > minPrec) {
  773. var node = this.startNodeAt(start);
  774. node.left = left;
  775. node.operator = this.tok.value;
  776. this.next();
  777. if (this.curLineStart !== line && this.curIndent < indent && this.tokenStartsLine()) {
  778. node.right = this.dummyIdent();
  779. } else {
  780. var rightStart = this.storeCurrentPos();
  781. node.right = this.parseExprOp(this.parseMaybeUnary(false), rightStart, prec, noIn, indent, line);
  782. }
  783. this.finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression");
  784. return this.parseExprOp(node, start, minPrec, noIn, indent, line)
  785. }
  786. }
  787. return left
  788. };
  789. lp$2.parseMaybeUnary = function(sawUnary) {
  790. var this$1 = this;
  791. var start = this.storeCurrentPos(), expr;
  792. if (this.options.ecmaVersion >= 8 && this.toks.isContextual("await") &&
  793. (this.inAsync || (!this.inFunction && this.options.allowAwaitOutsideFunction))
  794. ) {
  795. expr = this.parseAwait();
  796. sawUnary = true;
  797. } else if (this.tok.type.prefix) {
  798. var node = this.startNode(), update = this.tok.type === __acorn.tokTypes.incDec;
  799. if (!update) { sawUnary = true; }
  800. node.operator = this.tok.value;
  801. node.prefix = true;
  802. this.next();
  803. node.argument = this.parseMaybeUnary(true);
  804. if (update) { node.argument = this.checkLVal(node.argument); }
  805. expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  806. } else if (this.tok.type === __acorn.tokTypes.ellipsis) {
  807. var node$1 = this.startNode();
  808. this.next();
  809. node$1.argument = this.parseMaybeUnary(sawUnary);
  810. expr = this.finishNode(node$1, "SpreadElement");
  811. } else {
  812. expr = this.parseExprSubscripts();
  813. while (this.tok.type.postfix && !this.canInsertSemicolon()) {
  814. var node$2 = this$1.startNodeAt(start);
  815. node$2.operator = this$1.tok.value;
  816. node$2.prefix = false;
  817. node$2.argument = this$1.checkLVal(expr);
  818. this$1.next();
  819. expr = this$1.finishNode(node$2, "UpdateExpression");
  820. }
  821. }
  822. if (!sawUnary && this.eat(__acorn.tokTypes.starstar)) {
  823. var node$3 = this.startNodeAt(start);
  824. node$3.operator = "**";
  825. node$3.left = expr;
  826. node$3.right = this.parseMaybeUnary(false);
  827. return this.finishNode(node$3, "BinaryExpression")
  828. }
  829. return expr
  830. };
  831. lp$2.parseExprSubscripts = function() {
  832. var start = this.storeCurrentPos();
  833. return this.parseSubscripts(this.parseExprAtom(), start, false, this.curIndent, this.curLineStart)
  834. };
  835. lp$2.parseSubscripts = function(base, start, noCalls, startIndent, line) {
  836. var this$1 = this;
  837. for (;;) {
  838. if (this$1.curLineStart !== line && this$1.curIndent <= startIndent && this$1.tokenStartsLine()) {
  839. if (this$1.tok.type === __acorn.tokTypes.dot && this$1.curIndent === startIndent)
  840. { --startIndent; }
  841. else
  842. { return base }
  843. }
  844. var maybeAsyncArrow = base.type === "Identifier" && base.name === "async" && !this$1.canInsertSemicolon();
  845. if (this$1.eat(__acorn.tokTypes.dot)) {
  846. var node = this$1.startNodeAt(start);
  847. node.object = base;
  848. if (this$1.curLineStart !== line && this$1.curIndent <= startIndent && this$1.tokenStartsLine())
  849. { node.property = this$1.dummyIdent(); }
  850. else
  851. { node.property = this$1.parsePropertyAccessor() || this$1.dummyIdent(); }
  852. node.computed = false;
  853. base = this$1.finishNode(node, "MemberExpression");
  854. } else if (this$1.tok.type === __acorn.tokTypes.bracketL) {
  855. this$1.pushCx();
  856. this$1.next();
  857. var node$1 = this$1.startNodeAt(start);
  858. node$1.object = base;
  859. node$1.property = this$1.parseExpression();
  860. node$1.computed = true;
  861. this$1.popCx();
  862. this$1.expect(__acorn.tokTypes.bracketR);
  863. base = this$1.finishNode(node$1, "MemberExpression");
  864. } else if (!noCalls && this$1.tok.type === __acorn.tokTypes.parenL) {
  865. var exprList = this$1.parseExprList(__acorn.tokTypes.parenR);
  866. if (maybeAsyncArrow && this$1.eat(__acorn.tokTypes.arrow))
  867. { return this$1.parseArrowExpression(this$1.startNodeAt(start), exprList, true) }
  868. var node$2 = this$1.startNodeAt(start);
  869. node$2.callee = base;
  870. node$2.arguments = exprList;
  871. base = this$1.finishNode(node$2, "CallExpression");
  872. } else if (this$1.tok.type === __acorn.tokTypes.backQuote) {
  873. var node$3 = this$1.startNodeAt(start);
  874. node$3.tag = base;
  875. node$3.quasi = this$1.parseTemplate();
  876. base = this$1.finishNode(node$3, "TaggedTemplateExpression");
  877. } else {
  878. return base
  879. }
  880. }
  881. };
  882. lp$2.parseExprAtom = function() {
  883. var node;
  884. switch (this.tok.type) {
  885. case __acorn.tokTypes._this:
  886. case __acorn.tokTypes._super:
  887. var type = this.tok.type === __acorn.tokTypes._this ? "ThisExpression" : "Super";
  888. node = this.startNode();
  889. this.next();
  890. return this.finishNode(node, type)
  891. case __acorn.tokTypes.name:
  892. var start = this.storeCurrentPos();
  893. var id = this.parseIdent();
  894. var isAsync = false;
  895. if (id.name === "async" && !this.canInsertSemicolon()) {
  896. if (this.eat(__acorn.tokTypes._function))
  897. { return this.parseFunction(this.startNodeAt(start), false, true) }
  898. if (this.tok.type === __acorn.tokTypes.name) {
  899. id = this.parseIdent();
  900. isAsync = true;
  901. }
  902. }
  903. return this.eat(__acorn.tokTypes.arrow) ? this.parseArrowExpression(this.startNodeAt(start), [id], isAsync) : id
  904. case __acorn.tokTypes.regexp:
  905. node = this.startNode();
  906. var val = this.tok.value;
  907. node.regex = {pattern: val.pattern, flags: val.flags};
  908. node.value = val.value;
  909. node.raw = this.input.slice(this.tok.start, this.tok.end);
  910. this.next();
  911. return this.finishNode(node, "Literal")
  912. case __acorn.tokTypes.num: case __acorn.tokTypes.string:
  913. node = this.startNode();
  914. node.value = this.tok.value;
  915. node.raw = this.input.slice(this.tok.start, this.tok.end);
  916. this.next();
  917. return this.finishNode(node, "Literal")
  918. case __acorn.tokTypes._null: case __acorn.tokTypes._true: case __acorn.tokTypes._false:
  919. node = this.startNode();
  920. node.value = this.tok.type === __acorn.tokTypes._null ? null : this.tok.type === __acorn.tokTypes._true;
  921. node.raw = this.tok.type.keyword;
  922. this.next();
  923. return this.finishNode(node, "Literal")
  924. case __acorn.tokTypes.parenL:
  925. var parenStart = this.storeCurrentPos();
  926. this.next();
  927. var inner = this.parseExpression();
  928. this.expect(__acorn.tokTypes.parenR);
  929. if (this.eat(__acorn.tokTypes.arrow)) {
  930. // (a,)=>a // SequenceExpression makes dummy in the last hole. Drop the dummy.
  931. var params = inner.expressions || [inner];
  932. if (params.length && isDummy(params[params.length - 1]))
  933. { params.pop(); }
  934. return this.parseArrowExpression(this.startNodeAt(parenStart), params)
  935. }
  936. if (this.options.preserveParens) {
  937. var par = this.startNodeAt(parenStart);
  938. par.expression = inner;
  939. inner = this.finishNode(par, "ParenthesizedExpression");
  940. }
  941. return inner
  942. case __acorn.tokTypes.bracketL:
  943. node = this.startNode();
  944. node.elements = this.parseExprList(__acorn.tokTypes.bracketR, true);
  945. return this.finishNode(node, "ArrayExpression")
  946. case __acorn.tokTypes.braceL:
  947. return this.parseObj()
  948. case __acorn.tokTypes._class:
  949. return this.parseClass(false)
  950. case __acorn.tokTypes._function:
  951. node = this.startNode();
  952. this.next();
  953. return this.parseFunction(node, false)
  954. case __acorn.tokTypes._new:
  955. return this.parseNew()
  956. case __acorn.tokTypes.backQuote:
  957. return this.parseTemplate()
  958. default:
  959. return this.dummyIdent()
  960. }
  961. };
  962. lp$2.parseNew = function() {
  963. var node = this.startNode(), startIndent = this.curIndent, line = this.curLineStart;
  964. var meta = this.parseIdent(true);
  965. if (this.options.ecmaVersion >= 6 && this.eat(__acorn.tokTypes.dot)) {
  966. node.meta = meta;
  967. node.property = this.parseIdent(true);
  968. return this.finishNode(node, "MetaProperty")
  969. }
  970. var start = this.storeCurrentPos();
  971. node.callee = this.parseSubscripts(this.parseExprAtom(), start, true, startIndent, line);
  972. if (this.tok.type === __acorn.tokTypes.parenL) {
  973. node.arguments = this.parseExprList(__acorn.tokTypes.parenR);
  974. } else {
  975. node.arguments = [];
  976. }
  977. return this.finishNode(node, "NewExpression")
  978. };
  979. lp$2.parseTemplateElement = function() {
  980. var elem = this.startNode();
  981. // The loose parser accepts invalid unicode escapes even in untagged templates.
  982. if (this.tok.type === __acorn.tokTypes.invalidTemplate) {
  983. elem.value = {
  984. raw: this.tok.value,
  985. cooked: null
  986. };
  987. } else {
  988. elem.value = {
  989. raw: this.input.slice(this.tok.start, this.tok.end).replace(/\r\n?/g, "\n"),
  990. cooked: this.tok.value
  991. };
  992. }
  993. this.next();
  994. elem.tail = this.tok.type === __acorn.tokTypes.backQuote;
  995. return this.finishNode(elem, "TemplateElement")
  996. };
  997. lp$2.parseTemplate = function() {
  998. var this$1 = this;
  999. var node = this.startNode();
  1000. this.next();
  1001. node.expressions = [];
  1002. var curElt = this.parseTemplateElement();
  1003. node.quasis = [curElt];
  1004. while (!curElt.tail) {
  1005. this$1.next();
  1006. node.expressions.push(this$1.parseExpression());
  1007. if (this$1.expect(__acorn.tokTypes.braceR)) {
  1008. curElt = this$1.parseTemplateElement();
  1009. } else {
  1010. curElt = this$1.startNode();
  1011. curElt.value = {cooked: "", raw: ""};
  1012. curElt.tail = true;
  1013. this$1.finishNode(curElt, "TemplateElement");
  1014. }
  1015. node.quasis.push(curElt);
  1016. }
  1017. this.expect(__acorn.tokTypes.backQuote);
  1018. return this.finishNode(node, "TemplateLiteral")
  1019. };
  1020. lp$2.parseObj = function() {
  1021. var this$1 = this;
  1022. var node = this.startNode();
  1023. node.properties = [];
  1024. this.pushCx();
  1025. var indent = this.curIndent + 1, line = this.curLineStart;
  1026. this.eat(__acorn.tokTypes.braceL);
  1027. if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart; }
  1028. while (!this.closes(__acorn.tokTypes.braceR, indent, line)) {
  1029. var prop = this$1.startNode(), isGenerator = (void 0), isAsync = (void 0), start = (void 0);
  1030. if (this$1.options.ecmaVersion >= 9 && this$1.eat(__acorn.tokTypes.ellipsis)) {
  1031. prop.argument = this$1.parseMaybeAssign();
  1032. node.properties.push(this$1.finishNode(prop, "SpreadElement"));
  1033. this$1.eat(__acorn.tokTypes.comma);
  1034. continue
  1035. }
  1036. if (this$1.options.ecmaVersion >= 6) {
  1037. start = this$1.storeCurrentPos();
  1038. prop.method = false;
  1039. prop.shorthand = false;
  1040. isGenerator = this$1.eat(__acorn.tokTypes.star);
  1041. }
  1042. this$1.parsePropertyName(prop);
  1043. if (this$1.toks.isAsyncProp(prop)) {
  1044. isAsync = true;
  1045. isGenerator = this$1.options.ecmaVersion >= 9 && this$1.eat(__acorn.tokTypes.star);
  1046. this$1.parsePropertyName(prop);
  1047. } else {
  1048. isAsync = false;
  1049. }
  1050. if (isDummy(prop.key)) { if (isDummy(this$1.parseMaybeAssign())) { this$1.next(); } this$1.eat(__acorn.tokTypes.comma); continue }
  1051. if (this$1.eat(__acorn.tokTypes.colon)) {
  1052. prop.kind = "init";
  1053. prop.value = this$1.parseMaybeAssign();
  1054. } else if (this$1.options.ecmaVersion >= 6 && (this$1.tok.type === __acorn.tokTypes.parenL || this$1.tok.type === __acorn.tokTypes.braceL)) {
  1055. prop.kind = "init";
  1056. prop.method = true;
  1057. prop.value = this$1.parseMethod(isGenerator, isAsync);
  1058. } else if (this$1.options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
  1059. !prop.computed && (prop.key.name === "get" || prop.key.name === "set") &&
  1060. (this$1.tok.type !== __acorn.tokTypes.comma && this$1.tok.type !== __acorn.tokTypes.braceR && this$1.tok.type !== __acorn.tokTypes.eq)) {
  1061. prop.kind = prop.key.name;
  1062. this$1.parsePropertyName(prop);
  1063. prop.value = this$1.parseMethod(false);
  1064. } else {
  1065. prop.kind = "init";
  1066. if (this$1.options.ecmaVersion >= 6) {
  1067. if (this$1.eat(__acorn.tokTypes.eq)) {
  1068. var assign = this$1.startNodeAt(start);
  1069. assign.operator = "=";
  1070. assign.left = prop.key;
  1071. assign.right = this$1.parseMaybeAssign();
  1072. prop.value = this$1.finishNode(assign, "AssignmentExpression");
  1073. } else {
  1074. prop.value = prop.key;
  1075. }
  1076. } else {
  1077. prop.value = this$1.dummyIdent();
  1078. }
  1079. prop.shorthand = true;
  1080. }
  1081. node.properties.push(this$1.finishNode(prop, "Property"));
  1082. this$1.eat(__acorn.tokTypes.comma);
  1083. }
  1084. this.popCx();
  1085. if (!this.eat(__acorn.tokTypes.braceR)) {
  1086. // If there is no closing brace, make the node span to the start
  1087. // of the next token (this is useful for Tern)
  1088. this.last.end = this.tok.start;
  1089. if (this.options.locations) { this.last.loc.end = this.tok.loc.start; }
  1090. }
  1091. return this.finishNode(node, "ObjectExpression")
  1092. };
  1093. lp$2.parsePropertyName = function(prop) {
  1094. if (this.options.ecmaVersion >= 6) {
  1095. if (this.eat(__acorn.tokTypes.bracketL)) {
  1096. prop.computed = true;
  1097. prop.key = this.parseExpression();
  1098. this.expect(__acorn.tokTypes.bracketR);
  1099. return
  1100. } else {
  1101. prop.computed = false;
  1102. }
  1103. }
  1104. var key = (this.tok.type === __acorn.tokTypes.num || this.tok.type === __acorn.tokTypes.string) ? this.parseExprAtom() : this.parseIdent();
  1105. prop.key = key || this.dummyIdent();
  1106. };
  1107. lp$2.parsePropertyAccessor = function() {
  1108. if (this.tok.type === __acorn.tokTypes.name || this.tok.type.keyword) { return this.parseIdent() }
  1109. };
  1110. lp$2.parseIdent = function() {
  1111. var name = this.tok.type === __acorn.tokTypes.name ? this.tok.value : this.tok.type.keyword;
  1112. if (!name) { return this.dummyIdent() }
  1113. var node = this.startNode();
  1114. this.next();
  1115. node.name = name;
  1116. return this.finishNode(node, "Identifier")
  1117. };
  1118. lp$2.initFunction = function(node) {
  1119. node.id = null;
  1120. node.params = [];
  1121. if (this.options.ecmaVersion >= 6) {
  1122. node.generator = false;
  1123. node.expression = false;
  1124. }
  1125. if (this.options.ecmaVersion >= 8)
  1126. { node.async = false; }
  1127. };
  1128. // Convert existing expression atom to assignable pattern
  1129. // if possible.
  1130. lp$2.toAssignable = function(node, binding) {
  1131. var this$1 = this;
  1132. if (!node || node.type === "Identifier" || (node.type === "MemberExpression" && !binding)) {
  1133. // Okay
  1134. } else if (node.type === "ParenthesizedExpression") {
  1135. this.toAssignable(node.expression, binding);
  1136. } else if (this.options.ecmaVersion < 6) {
  1137. return this.dummyIdent()
  1138. } else if (node.type === "ObjectExpression") {
  1139. node.type = "ObjectPattern";
  1140. for (var i = 0, list = node.properties; i < list.length; i += 1)
  1141. {
  1142. var prop = list[i];
  1143. this$1.toAssignable(prop, binding);
  1144. }
  1145. } else if (node.type === "ArrayExpression") {
  1146. node.type = "ArrayPattern";
  1147. this.toAssignableList(node.elements, binding);
  1148. } else if (node.type === "Property") {
  1149. this.toAssignable(node.value, binding);
  1150. } else if (node.type === "SpreadElement") {
  1151. node.type = "RestElement";
  1152. this.toAssignable(node.argument, binding);
  1153. } else if (node.type === "AssignmentExpression") {
  1154. node.type = "AssignmentPattern";
  1155. delete node.operator;
  1156. } else {
  1157. return this.dummyIdent()
  1158. }
  1159. return node
  1160. };
  1161. lp$2.toAssignableList = function(exprList, binding) {
  1162. var this$1 = this;
  1163. for (var i = 0, list = exprList; i < list.length; i += 1)
  1164. {
  1165. var expr = list[i];
  1166. this$1.toAssignable(expr, binding);
  1167. }
  1168. return exprList
  1169. };
  1170. lp$2.parseFunctionParams = function(params) {
  1171. params = this.parseExprList(__acorn.tokTypes.parenR);
  1172. return this.toAssignableList(params, true)
  1173. };
  1174. lp$2.parseMethod = function(isGenerator, isAsync) {
  1175. var node = this.startNode(), oldInAsync = this.inAsync, oldInFunction = this.inFunction;
  1176. this.initFunction(node);
  1177. if (this.options.ecmaVersion >= 6)
  1178. { node.generator = !!isGenerator; }
  1179. if (this.options.ecmaVersion >= 8)
  1180. { node.async = !!isAsync; }
  1181. this.inAsync = node.async;
  1182. this.inFunction = true;
  1183. node.params = this.parseFunctionParams();
  1184. node.body = this.parseBlock();
  1185. this.toks.adaptDirectivePrologue(node.body.body);
  1186. this.inAsync = oldInAsync;
  1187. this.inFunction = oldInFunction;
  1188. return this.finishNode(node, "FunctionExpression")
  1189. };
  1190. lp$2.parseArrowExpression = function(node, params, isAsync) {
  1191. var oldInAsync = this.inAsync, oldInFunction = this.inFunction;
  1192. this.initFunction(node);
  1193. if (this.options.ecmaVersion >= 8)
  1194. { node.async = !!isAsync; }
  1195. this.inAsync = node.async;
  1196. this.inFunction = true;
  1197. node.params = this.toAssignableList(params, true);
  1198. node.expression = this.tok.type !== __acorn.tokTypes.braceL;
  1199. if (node.expression) {
  1200. node.body = this.parseMaybeAssign();
  1201. } else {
  1202. node.body = this.parseBlock();
  1203. this.toks.adaptDirectivePrologue(node.body.body);
  1204. }
  1205. this.inAsync = oldInAsync;
  1206. this.inFunction = oldInFunction;
  1207. return this.finishNode(node, "ArrowFunctionExpression")
  1208. };
  1209. lp$2.parseExprList = function(close, allowEmpty) {
  1210. var this$1 = this;
  1211. this.pushCx();
  1212. var indent = this.curIndent, line = this.curLineStart, elts = [];
  1213. this.next(); // Opening bracket
  1214. while (!this.closes(close, indent + 1, line)) {
  1215. if (this$1.eat(__acorn.tokTypes.comma)) {
  1216. elts.push(allowEmpty ? null : this$1.dummyIdent());
  1217. continue
  1218. }
  1219. var elt = this$1.parseMaybeAssign();
  1220. if (isDummy(elt)) {
  1221. if (this$1.closes(close, indent, line)) { break }
  1222. this$1.next();
  1223. } else {
  1224. elts.push(elt);
  1225. }
  1226. this$1.eat(__acorn.tokTypes.comma);
  1227. }
  1228. this.popCx();
  1229. if (!this.eat(close)) {
  1230. // If there is no closing brace, make the node span to the start
  1231. // of the next token (this is useful for Tern)
  1232. this.last.end = this.tok.start;
  1233. if (this.options.locations) { this.last.loc.end = this.tok.loc.start; }
  1234. }
  1235. return elts
  1236. };
  1237. lp$2.parseAwait = function() {
  1238. var node = this.startNode();
  1239. this.next();
  1240. node.argument = this.parseMaybeUnary();
  1241. return this.finishNode(node, "AwaitExpression")
  1242. };
  1243. // Acorn: Loose parser
  1244. //
  1245. // This module provides an alternative parser (`parse_dammit`) that
  1246. // exposes that same interface as `parse`, but will try to parse
  1247. // anything as JavaScript, repairing syntax error the best it can.
  1248. // There are circumstances in which it will raise an error and give
  1249. // up, but they are very rare. The resulting AST will be a mostly
  1250. // valid JavaScript AST (as per the [Mozilla parser API][api], except
  1251. // that:
  1252. //
  1253. // - Return outside functions is allowed
  1254. //
  1255. // - Label consistency (no conflicts, break only to existing labels)
  1256. // is not enforced.
  1257. //
  1258. // - Bogus Identifier nodes with a name of `"✖"` are inserted whenever
  1259. // the parser got too confused to return anything meaningful.
  1260. //
  1261. // [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
  1262. //
  1263. // The expected use for this is to *first* try `acorn.parse`, and only
  1264. // if that fails switch to `parse_dammit`. The loose parser might
  1265. // parse badly indented code incorrectly, so **don't** use it as
  1266. // your default parser.
  1267. //
  1268. // Quite a lot of acorn.js is duplicated here. The alternative was to
  1269. // add a *lot* of extra cruft to that file, making it less readable
  1270. // and slower. Copying and editing the code allowed me to make
  1271. // invasive changes and simplifications without creating a complicated
  1272. // tangle.
  1273. __acorn.defaultOptions.tabSize = 4;
  1274. // eslint-disable-next-line camelcase
  1275. function parse_dammit(input, options) {
  1276. return new LooseParser(input, options).parse()
  1277. }
  1278. __acorn.addLooseExports(parse_dammit, LooseParser, pluginsLoose);
  1279. exports.parse_dammit = parse_dammit;
  1280. exports.LooseParser = LooseParser;
  1281. exports.pluginsLoose = pluginsLoose;
  1282. Object.defineProperty(exports, '__esModule', { value: true });
  1283. })));