acorn_loose.js 47 KB

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