acorn.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. // Acorn is a tiny, fast JavaScript parser written in JavaScript.
  2. //
  3. // Acorn was written by Marijn Haverbeke and released under an MIT
  4. // license. The Unicode regexps (for identifiers and whitespace) were
  5. // taken from [Esprima](http://esprima.org) by Ariya Hidayat.
  6. //
  7. // Git repositories for Acorn are available at
  8. //
  9. // http://marijnhaverbeke.nl/git/acorn
  10. // https://github.com/marijnh/acorn.git
  11. //
  12. // Please use the [github bug tracker][ghbt] to report issues.
  13. //
  14. // [ghbt]: https://github.com/marijnh/acorn/issues
  15. //
  16. // This file defines the main parser interface. The library also comes
  17. // with a [error-tolerant parser][dammit] and an
  18. // [abstract syntax tree walker][walk], defined in other files.
  19. //
  20. // [dammit]: acorn_loose.js
  21. // [walk]: util/walk.js
  22. (function(root, mod) {
  23. if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS
  24. if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD
  25. mod(root.acorn || (root.acorn = {})); // Plain browser env
  26. })(this, function(exports) {
  27. "use strict";
  28. exports.version = "0.4.1";
  29. // The main exported interface (under `self.acorn` when in the
  30. // browser) is a `parse` function that takes a code string and
  31. // returns an abstract syntax tree as specified by [Mozilla parser
  32. // API][api], with the caveat that the SpiderMonkey-specific syntax
  33. // (`let`, `yield`, inline XML, etc) is not recognized.
  34. //
  35. // [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
  36. var options, input, inputLen, sourceFile;
  37. exports.parse = function(inpt, opts) {
  38. input = String(inpt); inputLen = input.length;
  39. setOptions(opts);
  40. initTokenState();
  41. return parseTopLevel(options.program);
  42. };
  43. // A second optional argument can be given to further configure
  44. // the parser process. These options are recognized:
  45. var defaultOptions = exports.defaultOptions = {
  46. // `ecmaVersion` indicates the ECMAScript version to parse. Must
  47. // be either 3 or 5. This
  48. // influences support for strict mode, the set of reserved words, and
  49. // support for getters and setter.
  50. ecmaVersion: 5,
  51. // Turn on `strictSemicolons` to prevent the parser from doing
  52. // automatic semicolon insertion.
  53. strictSemicolons: false,
  54. // When `allowTrailingCommas` is false, the parser will not allow
  55. // trailing commas in array and object literals.
  56. allowTrailingCommas: true,
  57. // By default, reserved words are not enforced. Enable
  58. // `forbidReserved` to enforce them. When this option has the
  59. // value "everywhere", reserved words and keywords can also not be
  60. // used as property names.
  61. forbidReserved: false,
  62. // When enabled, a return at the top level is not considered an
  63. // error.
  64. allowReturnOutsideFunction: false,
  65. // When `locations` is on, `loc` properties holding objects with
  66. // `start` and `end` properties in `{line, column}` form (with
  67. // line being 1-based and column 0-based) will be attached to the
  68. // nodes.
  69. locations: false,
  70. // A function can be passed as `onComment` option, which will
  71. // cause Acorn to call that function with `(block, text, start,
  72. // end)` parameters whenever a comment is skipped. `block` is a
  73. // boolean indicating whether this is a block (`/* */`) comment,
  74. // `text` is the content of the comment, and `start` and `end` are
  75. // character offsets that denote the start and end of the comment.
  76. // When the `locations` option is on, two more parameters are
  77. // passed, the full `{line, column}` locations of the start and
  78. // end of the comments. Note that you are not allowed to call the
  79. // parser from the callback—that will corrupt its internal state.
  80. onComment: null,
  81. // Nodes have their start and end characters offsets recorded in
  82. // `start` and `end` properties (directly on the node, rather than
  83. // the `loc` object, which holds line/column data. To also add a
  84. // [semi-standardized][range] `range` property holding a `[start,
  85. // end]` array with the same numbers, set the `ranges` option to
  86. // `true`.
  87. //
  88. // [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
  89. ranges: false,
  90. // It is possible to parse multiple files into a single AST by
  91. // passing the tree produced by parsing the first file as
  92. // `program` option in subsequent parses. This will add the
  93. // toplevel forms of the parsed file to the `Program` (top) node
  94. // of an existing parse tree.
  95. program: null,
  96. // When `locations` is on, you can pass this to record the source
  97. // file in every node's `loc` object.
  98. sourceFile: null,
  99. // This value, if given, is stored in every node, whether
  100. // `locations` is on or off.
  101. directSourceFile: null
  102. };
  103. function setOptions(opts) {
  104. options = opts || {};
  105. for (var opt in defaultOptions) if (!Object.prototype.hasOwnProperty.call(options, opt))
  106. options[opt] = defaultOptions[opt];
  107. sourceFile = options.sourceFile || null;
  108. }
  109. // The `getLineInfo` function is mostly useful when the
  110. // `locations` option is off (for performance reasons) and you
  111. // want to find the line/column position for a given character
  112. // offset. `input` should be the code string that the offset refers
  113. // into.
  114. var getLineInfo = exports.getLineInfo = function(input, offset) {
  115. for (var line = 1, cur = 0;;) {
  116. lineBreak.lastIndex = cur;
  117. var match = lineBreak.exec(input);
  118. if (match && match.index < offset) {
  119. ++line;
  120. cur = match.index + match[0].length;
  121. } else break;
  122. }
  123. return {line: line, column: offset - cur};
  124. };
  125. // Acorn is organized as a tokenizer and a recursive-descent parser.
  126. // The `tokenize` export provides an interface to the tokenizer.
  127. // Because the tokenizer is optimized for being efficiently used by
  128. // the Acorn parser itself, this interface is somewhat crude and not
  129. // very modular. Performing another parse or call to `tokenize` will
  130. // reset the internal state, and invalidate existing tokenizers.
  131. exports.tokenize = function(inpt, opts) {
  132. input = String(inpt); inputLen = input.length;
  133. setOptions(opts);
  134. initTokenState();
  135. var t = {};
  136. function getToken(forceRegexp) {
  137. lastEnd = tokEnd;
  138. readToken(forceRegexp);
  139. t.start = tokStart; t.end = tokEnd;
  140. t.startLoc = tokStartLoc; t.endLoc = tokEndLoc;
  141. t.type = tokType; t.value = tokVal;
  142. return t;
  143. }
  144. getToken.jumpTo = function(pos, reAllowed) {
  145. tokPos = pos;
  146. if (options.locations) {
  147. tokCurLine = 1;
  148. tokLineStart = lineBreak.lastIndex = 0;
  149. var match;
  150. while ((match = lineBreak.exec(input)) && match.index < pos) {
  151. ++tokCurLine;
  152. tokLineStart = match.index + match[0].length;
  153. }
  154. }
  155. tokRegexpAllowed = reAllowed;
  156. skipSpace();
  157. };
  158. return getToken;
  159. };
  160. // State is kept in (closure-)global variables. We already saw the
  161. // `options`, `input`, and `inputLen` variables above.
  162. // The current position of the tokenizer in the input.
  163. var tokPos;
  164. // The start and end offsets of the current token.
  165. var tokStart, tokEnd;
  166. // When `options.locations` is true, these hold objects
  167. // containing the tokens start and end line/column pairs.
  168. var tokStartLoc, tokEndLoc;
  169. // The type and value of the current token. Token types are objects,
  170. // named by variables against which they can be compared, and
  171. // holding properties that describe them (indicating, for example,
  172. // the precedence of an infix operator, and the original name of a
  173. // keyword token). The kind of value that's held in `tokVal` depends
  174. // on the type of the token. For literals, it is the literal value,
  175. // for operators, the operator name, and so on.
  176. var tokType, tokVal;
  177. // Interal state for the tokenizer. To distinguish between division
  178. // operators and regular expressions, it remembers whether the last
  179. // token was one that is allowed to be followed by an expression.
  180. // (If it is, a slash is probably a regexp, if it isn't it's a
  181. // division operator. See the `parseStatement` function for a
  182. // caveat.)
  183. var tokRegexpAllowed;
  184. // When `options.locations` is true, these are used to keep
  185. // track of the current line, and know when a new line has been
  186. // entered.
  187. var tokCurLine, tokLineStart;
  188. // These store the position of the previous token, which is useful
  189. // when finishing a node and assigning its `end` position.
  190. var lastStart, lastEnd, lastEndLoc;
  191. // This is the parser's state. `inFunction` is used to reject
  192. // `return` statements outside of functions, `labels` to verify that
  193. // `break` and `continue` have somewhere to jump to, and `strict`
  194. // indicates whether strict mode is on.
  195. var inFunction, labels, strict;
  196. // This function is used to raise exceptions on parse errors. It
  197. // takes an offset integer (into the current `input`) to indicate
  198. // the location of the error, attaches the position to the end
  199. // of the error message, and then raises a `SyntaxError` with that
  200. // message.
  201. function raise(pos, message) {
  202. var loc = getLineInfo(input, pos);
  203. message += " (" + loc.line + ":" + loc.column + ")";
  204. var err = new SyntaxError(message);
  205. err.pos = pos; err.loc = loc; err.raisedAt = tokPos;
  206. throw err;
  207. }
  208. // Reused empty array added for node fields that are always empty.
  209. var empty = [];
  210. // ## Token types
  211. // The assignment of fine-grained, information-carrying type objects
  212. // allows the tokenizer to store the information it has about a
  213. // token in a way that is very cheap for the parser to look up.
  214. // All token type variables start with an underscore, to make them
  215. // easy to recognize.
  216. // These are the general types. The `type` property is only used to
  217. // make them recognizeable when debugging.
  218. var _num = {type: "num"}, _regexp = {type: "regexp"}, _string = {type: "string"};
  219. var _name = {type: "name"}, _eof = {type: "eof"};
  220. // Keyword tokens. The `keyword` property (also used in keyword-like
  221. // operators) indicates that the token originated from an
  222. // identifier-like word, which is used when parsing property names.
  223. //
  224. // The `beforeExpr` property is used to disambiguate between regular
  225. // expressions and divisions. It is set on all token types that can
  226. // be followed by an expression (thus, a slash after them would be a
  227. // regular expression).
  228. //
  229. // `isLoop` marks a keyword as starting a loop, which is important
  230. // to know when parsing a label, in order to allow or disallow
  231. // continue jumps to that label.
  232. var _break = {keyword: "break"}, _case = {keyword: "case", beforeExpr: true}, _catch = {keyword: "catch"};
  233. var _continue = {keyword: "continue"}, _debugger = {keyword: "debugger"}, _default = {keyword: "default"};
  234. var _do = {keyword: "do", isLoop: true}, _else = {keyword: "else", beforeExpr: true};
  235. var _finally = {keyword: "finally"}, _for = {keyword: "for", isLoop: true}, _function = {keyword: "function"};
  236. var _if = {keyword: "if"}, _return = {keyword: "return", beforeExpr: true}, _switch = {keyword: "switch"};
  237. var _throw = {keyword: "throw", beforeExpr: true}, _try = {keyword: "try"}, _var = {keyword: "var"};
  238. var _while = {keyword: "while", isLoop: true}, _with = {keyword: "with"}, _new = {keyword: "new", beforeExpr: true};
  239. var _this = {keyword: "this"};
  240. // The keywords that denote values.
  241. var _null = {keyword: "null", atomValue: null}, _true = {keyword: "true", atomValue: true};
  242. var _false = {keyword: "false", atomValue: false};
  243. // Some keywords are treated as regular operators. `in` sometimes
  244. // (when parsing `for`) needs to be tested against specifically, so
  245. // we assign a variable name to it for quick comparing.
  246. var _in = {keyword: "in", binop: 7, beforeExpr: true};
  247. // Map keyword names to token types.
  248. var keywordTypes = {"break": _break, "case": _case, "catch": _catch,
  249. "continue": _continue, "debugger": _debugger, "default": _default,
  250. "do": _do, "else": _else, "finally": _finally, "for": _for,
  251. "function": _function, "if": _if, "return": _return, "switch": _switch,
  252. "throw": _throw, "try": _try, "var": _var, "while": _while, "with": _with,
  253. "null": _null, "true": _true, "false": _false, "new": _new, "in": _in,
  254. "instanceof": {keyword: "instanceof", binop: 7, beforeExpr: true}, "this": _this,
  255. "typeof": {keyword: "typeof", prefix: true, beforeExpr: true},
  256. "void": {keyword: "void", prefix: true, beforeExpr: true},
  257. "delete": {keyword: "delete", prefix: true, beforeExpr: true}};
  258. // Punctuation token types. Again, the `type` property is purely for debugging.
  259. var _bracketL = {type: "[", beforeExpr: true}, _bracketR = {type: "]"}, _braceL = {type: "{", beforeExpr: true};
  260. var _braceR = {type: "}"}, _parenL = {type: "(", beforeExpr: true}, _parenR = {type: ")"};
  261. var _comma = {type: ",", beforeExpr: true}, _semi = {type: ";", beforeExpr: true};
  262. var _colon = {type: ":", beforeExpr: true}, _dot = {type: "."}, _question = {type: "?", beforeExpr: true};
  263. // Operators. These carry several kinds of properties to help the
  264. // parser use them properly (the presence of these properties is
  265. // what categorizes them as operators).
  266. //
  267. // `binop`, when present, specifies that this operator is a binary
  268. // operator, and will refer to its precedence.
  269. //
  270. // `prefix` and `postfix` mark the operator as a prefix or postfix
  271. // unary operator. `isUpdate` specifies that the node produced by
  272. // the operator should be of type UpdateExpression rather than
  273. // simply UnaryExpression (`++` and `--`).
  274. //
  275. // `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
  276. // binary operators with a very low precedence, that should result
  277. // in AssignmentExpression nodes.
  278. var _slash = {binop: 10, beforeExpr: true}, _eq = {isAssign: true, beforeExpr: true};
  279. var _assign = {isAssign: true, beforeExpr: true};
  280. var _incDec = {postfix: true, prefix: true, isUpdate: true}, _prefix = {prefix: true, beforeExpr: true};
  281. var _logicalOR = {binop: 1, beforeExpr: true};
  282. var _logicalAND = {binop: 2, beforeExpr: true};
  283. var _bitwiseOR = {binop: 3, beforeExpr: true};
  284. var _bitwiseXOR = {binop: 4, beforeExpr: true};
  285. var _bitwiseAND = {binop: 5, beforeExpr: true};
  286. var _equality = {binop: 6, beforeExpr: true};
  287. var _relational = {binop: 7, beforeExpr: true};
  288. var _bitShift = {binop: 8, beforeExpr: true};
  289. var _plusMin = {binop: 9, prefix: true, beforeExpr: true};
  290. var _multiplyModulo = {binop: 10, beforeExpr: true};
  291. // Provide access to the token types for external users of the
  292. // tokenizer.
  293. exports.tokTypes = {bracketL: _bracketL, bracketR: _bracketR, braceL: _braceL, braceR: _braceR,
  294. parenL: _parenL, parenR: _parenR, comma: _comma, semi: _semi, colon: _colon,
  295. dot: _dot, question: _question, slash: _slash, eq: _eq, name: _name, eof: _eof,
  296. num: _num, regexp: _regexp, string: _string};
  297. for (var kw in keywordTypes) exports.tokTypes["_" + kw] = keywordTypes[kw];
  298. // This is a trick taken from Esprima. It turns out that, on
  299. // non-Chrome browsers, to check whether a string is in a set, a
  300. // predicate containing a big ugly `switch` statement is faster than
  301. // a regular expression, and on Chrome the two are about on par.
  302. // This function uses `eval` (non-lexical) to produce such a
  303. // predicate from a space-separated string of words.
  304. //
  305. // It starts by sorting the words by length.
  306. function makePredicate(words) {
  307. words = words.split(" ");
  308. var f = "", cats = [];
  309. out: for (var i = 0; i < words.length; ++i) {
  310. for (var j = 0; j < cats.length; ++j)
  311. if (cats[j][0].length == words[i].length) {
  312. cats[j].push(words[i]);
  313. continue out;
  314. }
  315. cats.push([words[i]]);
  316. }
  317. function compareTo(arr) {
  318. if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";";
  319. f += "switch(str){";
  320. for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":";
  321. f += "return true}return false;";
  322. }
  323. // When there are more than three length categories, an outer
  324. // switch first dispatches on the lengths, to save on comparisons.
  325. if (cats.length > 3) {
  326. cats.sort(function(a, b) {return b.length - a.length;});
  327. f += "switch(str.length){";
  328. for (var i = 0; i < cats.length; ++i) {
  329. var cat = cats[i];
  330. f += "case " + cat[0].length + ":";
  331. compareTo(cat);
  332. }
  333. f += "}";
  334. // Otherwise, simply generate a flat `switch` statement.
  335. } else {
  336. compareTo(words);
  337. }
  338. return new Function("str", f);
  339. }
  340. // The ECMAScript 3 reserved word list.
  341. var isReservedWord3 = makePredicate("abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile");
  342. // ECMAScript 5 reserved words.
  343. var isReservedWord5 = makePredicate("class enum extends super const export import");
  344. // The additional reserved words in strict mode.
  345. var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield");
  346. // The forbidden variable names in strict mode.
  347. var isStrictBadIdWord = makePredicate("eval arguments");
  348. // And the keywords.
  349. var isKeyword = makePredicate("break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this");
  350. // ## Character categories
  351. // Big ugly regular expressions that match characters in the
  352. // whitespace, identifier, and identifier-start categories. These
  353. // are only applied when a character is found to actually have a
  354. // code point above 128.
  355. var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/;
  356. var nonASCIIidentifierStartChars = "\xaa\xb5\xba\xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0-\u02e4\u02ec\u02ee\u0370-\u0374\u0376\u0377\u037a-\u037d\u0386\u0388-\u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7-\u0481\u048a-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a\u066e\u066f\u0671-\u06d3\u06d5\u06e5\u06e6\u06ee\u06ef\u06fa-\u06fc\u06ff\u0710\u0712-\u072f\u074d-\u07a5\u07b1\u07ca-\u07ea\u07f4\u07f5\u07fa\u0800-\u0815\u081a\u0824\u0828\u0840-\u0858\u08a0\u08a2-\u08ac\u0904-\u0939\u093d\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097f\u0985-\u098c\u098f\u0990\u0993-\u09a8\u09aa-\u09b0\u09b2\u09b6-\u09b9\u09bd\u09ce\u09dc\u09dd\u09df-\u09e1\u09f0\u09f1\u0a05-\u0a0a\u0a0f\u0a10\u0a13-\u0a28\u0a2a-\u0a30\u0a32\u0a33\u0a35\u0a36\u0a38\u0a39\u0a59-\u0a5c\u0a5e\u0a72-\u0a74\u0a85-\u0a8d\u0a8f-\u0a91\u0a93-\u0aa8\u0aaa-\u0ab0\u0ab2\u0ab3\u0ab5-\u0ab9\u0abd\u0ad0\u0ae0\u0ae1\u0b05-\u0b0c\u0b0f\u0b10\u0b13-\u0b28\u0b2a-\u0b30\u0b32\u0b33\u0b35-\u0b39\u0b3d\u0b5c\u0b5d\u0b5f-\u0b61\u0b71\u0b83\u0b85-\u0b8a\u0b8e-\u0b90\u0b92-\u0b95\u0b99\u0b9a\u0b9c\u0b9e\u0b9f\u0ba3\u0ba4\u0ba8-\u0baa\u0bae-\u0bb9\u0bd0\u0c05-\u0c0c\u0c0e-\u0c10\u0c12-\u0c28\u0c2a-\u0c33\u0c35-\u0c39\u0c3d\u0c58\u0c59\u0c60\u0c61\u0c85-\u0c8c\u0c8e-\u0c90\u0c92-\u0ca8\u0caa-\u0cb3\u0cb5-\u0cb9\u0cbd\u0cde\u0ce0\u0ce1\u0cf1\u0cf2\u0d05-\u0d0c\u0d0e-\u0d10\u0d12-\u0d3a\u0d3d\u0d4e\u0d60\u0d61\u0d7a-\u0d7f\u0d85-\u0d96\u0d9a-\u0db1\u0db3-\u0dbb\u0dbd\u0dc0-\u0dc6\u0e01-\u0e30\u0e32\u0e33\u0e40-\u0e46\u0e81\u0e82\u0e84\u0e87\u0e88\u0e8a\u0e8d\u0e94-\u0e97\u0e99-\u0e9f\u0ea1-\u0ea3\u0ea5\u0ea7\u0eaa\u0eab\u0ead-\u0eb0\u0eb2\u0eb3\u0ebd\u0ec0-\u0ec4\u0ec6\u0edc-\u0edf\u0f00\u0f40-\u0f47\u0f49-\u0f6c\u0f88-\u0f8c\u1000-\u102a\u103f\u1050-\u1055\u105a-\u105d\u1061\u1065\u1066\u106e-\u1070\u1075-\u1081\u108e\u10a0-\u10c5\u10c7\u10cd\u10d0-\u10fa\u10fc-\u1248\u124a-\u124d\u1250-\u1256\u1258\u125a-\u125d\u1260-\u1288\u128a-\u128d\u1290-\u12b0\u12b2-\u12b5\u12b8-\u12be\u12c0\u12c2-\u12c5\u12c8-\u12d6\u12d8-\u1310\u1312-\u1315\u1318-\u135a\u1380-\u138f\u13a0-\u13f4\u1401-\u166c\u166f-\u167f\u1681-\u169a\u16a0-\u16ea\u16ee-\u16f0\u1700-\u170c\u170e-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176c\u176e-\u1770\u1780-\u17b3\u17d7\u17dc\u1820-\u1877\u1880-\u18a8\u18aa\u18b0-\u18f5\u1900-\u191c\u1950-\u196d\u1970-\u1974\u1980-\u19ab\u19c1-\u19c7\u1a00-\u1a16\u1a20-\u1a54\u1aa7\u1b05-\u1b33\u1b45-\u1b4b\u1b83-\u1ba0\u1bae\u1baf\u1bba-\u1be5\u1c00-\u1c23\u1c4d-\u1c4f\u1c5a-\u1c7d\u1ce9-\u1cec\u1cee-\u1cf1\u1cf5\u1cf6\u1d00-\u1dbf\u1e00-\u1f15\u1f18-\u1f1d\u1f20-\u1f45\u1f48-\u1f4d\u1f50-\u1f57\u1f59\u1f5b\u1f5d\u1f5f-\u1f7d\u1f80-\u1fb4\u1fb6-\u1fbc\u1fbe\u1fc2-\u1fc4\u1fc6-\u1fcc\u1fd0-\u1fd3\u1fd6-\u1fdb\u1fe0-\u1fec\u1ff2-\u1ff4\u1ff6-\u1ffc\u2071\u207f\u2090-\u209c\u2102\u2107\u210a-\u2113\u2115\u2119-\u211d\u2124\u2126\u2128\u212a-\u212d\u212f-\u2139\u213c-\u213f\u2145-\u2149\u214e\u2160-\u2188\u2c00-\u2c2e\u2c30-\u2c5e\u2c60-\u2ce4\u2ceb-\u2cee\u2cf2\u2cf3\u2d00-\u2d25\u2d27\u2d2d\u2d30-\u2d67\u2d6f\u2d80-\u2d96\u2da0-\u2da6\u2da8-\u2dae\u2db0-\u2db6\u2db8-\u2dbe\u2dc0-\u2dc6\u2dc8-\u2dce\u2dd0-\u2dd6\u2dd8-\u2dde\u2e2f\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303c\u3041-\u3096\u309d-\u309f\u30a1-\u30fa\u30fc-\u30ff\u3105-\u312d\u3131-\u318e\u31a0-\u31ba\u31f0-\u31ff\u3400-\u4db5\u4e00-\u9fcc\ua000-\ua48c\ua4d0-\ua4fd\ua500-\ua60c\ua610-\ua61f\ua62a\ua62b\ua640-\ua66e\ua67f-\ua697\ua6a0-\ua6ef\ua717-\ua71f\ua722-\ua788\ua78b-\ua78e\ua790-\ua793\ua7a0-\ua7aa\ua7f8-\ua801\ua803-\ua805\ua807-\ua80a\ua80c-\ua822\ua840-\ua873\ua882-\ua8b3\ua8f2-\ua8f7\ua8fb\ua90a-\ua925\ua930-\ua946\ua960-\ua97c\ua984-\ua9b2\ua9cf\uaa00-\uaa28\uaa40-\uaa42\uaa44-\uaa4b\uaa60-\uaa76\uaa7a\uaa80-\uaaaf\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2\uaadb-\uaadd\uaae0-\uaaea\uaaf2-\uaaf4\uab01-\uab06\uab09-\uab0e\uab11-\uab16\uab20-\uab26\uab28-\uab2e\uabc0-\uabe2\uac00-\ud7a3\ud7b0-\ud7c6\ud7cb-\ud7fb\uf900-\ufa6d\ufa70-\ufad9\ufb00-\ufb06\ufb13-\ufb17\ufb1d\ufb1f-\ufb28\ufb2a-\ufb36\ufb38-\ufb3c\ufb3e\ufb40\ufb41\ufb43\ufb44\ufb46-\ufbb1\ufbd3-\ufd3d\ufd50-\ufd8f\ufd92-\ufdc7\ufdf0-\ufdfb\ufe70-\ufe74\ufe76-\ufefc\uff21-\uff3a\uff41-\uff5a\uff66-\uffbe\uffc2-\uffc7\uffca-\uffcf\uffd2-\uffd7\uffda-\uffdc";
  357. var nonASCIIidentifierChars = "\u0300-\u036f\u0483-\u0487\u0591-\u05bd\u05bf\u05c1\u05c2\u05c4\u05c5\u05c7\u0610-\u061a\u0620-\u0649\u0672-\u06d3\u06e7-\u06e8\u06fb-\u06fc\u0730-\u074a\u0800-\u0814\u081b-\u0823\u0825-\u0827\u0829-\u082d\u0840-\u0857\u08e4-\u08fe\u0900-\u0903\u093a-\u093c\u093e-\u094f\u0951-\u0957\u0962-\u0963\u0966-\u096f\u0981-\u0983\u09bc\u09be-\u09c4\u09c7\u09c8\u09d7\u09df-\u09e0\u0a01-\u0a03\u0a3c\u0a3e-\u0a42\u0a47\u0a48\u0a4b-\u0a4d\u0a51\u0a66-\u0a71\u0a75\u0a81-\u0a83\u0abc\u0abe-\u0ac5\u0ac7-\u0ac9\u0acb-\u0acd\u0ae2-\u0ae3\u0ae6-\u0aef\u0b01-\u0b03\u0b3c\u0b3e-\u0b44\u0b47\u0b48\u0b4b-\u0b4d\u0b56\u0b57\u0b5f-\u0b60\u0b66-\u0b6f\u0b82\u0bbe-\u0bc2\u0bc6-\u0bc8\u0bca-\u0bcd\u0bd7\u0be6-\u0bef\u0c01-\u0c03\u0c46-\u0c48\u0c4a-\u0c4d\u0c55\u0c56\u0c62-\u0c63\u0c66-\u0c6f\u0c82\u0c83\u0cbc\u0cbe-\u0cc4\u0cc6-\u0cc8\u0cca-\u0ccd\u0cd5\u0cd6\u0ce2-\u0ce3\u0ce6-\u0cef\u0d02\u0d03\u0d46-\u0d48\u0d57\u0d62-\u0d63\u0d66-\u0d6f\u0d82\u0d83\u0dca\u0dcf-\u0dd4\u0dd6\u0dd8-\u0ddf\u0df2\u0df3\u0e34-\u0e3a\u0e40-\u0e45\u0e50-\u0e59\u0eb4-\u0eb9\u0ec8-\u0ecd\u0ed0-\u0ed9\u0f18\u0f19\u0f20-\u0f29\u0f35\u0f37\u0f39\u0f41-\u0f47\u0f71-\u0f84\u0f86-\u0f87\u0f8d-\u0f97\u0f99-\u0fbc\u0fc6\u1000-\u1029\u1040-\u1049\u1067-\u106d\u1071-\u1074\u1082-\u108d\u108f-\u109d\u135d-\u135f\u170e-\u1710\u1720-\u1730\u1740-\u1750\u1772\u1773\u1780-\u17b2\u17dd\u17e0-\u17e9\u180b-\u180d\u1810-\u1819\u1920-\u192b\u1930-\u193b\u1951-\u196d\u19b0-\u19c0\u19c8-\u19c9\u19d0-\u19d9\u1a00-\u1a15\u1a20-\u1a53\u1a60-\u1a7c\u1a7f-\u1a89\u1a90-\u1a99\u1b46-\u1b4b\u1b50-\u1b59\u1b6b-\u1b73\u1bb0-\u1bb9\u1be6-\u1bf3\u1c00-\u1c22\u1c40-\u1c49\u1c5b-\u1c7d\u1cd0-\u1cd2\u1d00-\u1dbe\u1e01-\u1f15\u200c\u200d\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5-\u20f0\u2d81-\u2d96\u2de0-\u2dff\u3021-\u3028\u3099\u309a\ua640-\ua66d\ua674-\ua67d\ua69f\ua6f0-\ua6f1\ua7f8-\ua800\ua806\ua80b\ua823-\ua827\ua880-\ua881\ua8b4-\ua8c4\ua8d0-\ua8d9\ua8f3-\ua8f7\ua900-\ua909\ua926-\ua92d\ua930-\ua945\ua980-\ua983\ua9b3-\ua9c0\uaa00-\uaa27\uaa40-\uaa41\uaa4c-\uaa4d\uaa50-\uaa59\uaa7b\uaae0-\uaae9\uaaf2-\uaaf3\uabc0-\uabe1\uabec\uabed\uabf0-\uabf9\ufb20-\ufb28\ufe00-\ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f\uff10-\uff19\uff3f";
  358. var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  359. var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  360. // Whether a single character denotes a newline.
  361. var newline = /[\n\r\u2028\u2029]/;
  362. // Matches a whole line break (where CRLF is considered a single
  363. // line break). Used to count lines.
  364. var lineBreak = /\r\n|[\n\r\u2028\u2029]/g;
  365. // Test whether a given character code starts an identifier.
  366. var isIdentifierStart = exports.isIdentifierStart = function(code) {
  367. if (code < 65) return code === 36;
  368. if (code < 91) return true;
  369. if (code < 97) return code === 95;
  370. if (code < 123)return true;
  371. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  372. };
  373. // Test whether a given character is part of an identifier.
  374. var isIdentifierChar = exports.isIdentifierChar = function(code) {
  375. if (code < 48) return code === 36;
  376. if (code < 58) return true;
  377. if (code < 65) return false;
  378. if (code < 91) return true;
  379. if (code < 97) return code === 95;
  380. if (code < 123)return true;
  381. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  382. };
  383. // ## Tokenizer
  384. // These are used when `options.locations` is on, for the
  385. // `tokStartLoc` and `tokEndLoc` properties.
  386. function line_loc_t() {
  387. this.line = tokCurLine;
  388. this.column = tokPos - tokLineStart;
  389. }
  390. // Reset the token state. Used at the start of a parse.
  391. function initTokenState() {
  392. tokCurLine = 1;
  393. tokPos = tokLineStart = 0;
  394. tokRegexpAllowed = true;
  395. skipSpace();
  396. }
  397. // Called at the end of every token. Sets `tokEnd`, `tokVal`, and
  398. // `tokRegexpAllowed`, and skips the space after the token, so that
  399. // the next one's `tokStart` will point at the right position.
  400. function finishToken(type, val) {
  401. tokEnd = tokPos;
  402. if (options.locations) tokEndLoc = new line_loc_t;
  403. tokType = type;
  404. skipSpace();
  405. tokVal = val;
  406. tokRegexpAllowed = type.beforeExpr;
  407. }
  408. function skipBlockComment() {
  409. var startLoc = options.onComment && options.locations && new line_loc_t;
  410. var start = tokPos, end = input.indexOf("*/", tokPos += 2);
  411. if (end === -1) raise(tokPos - 2, "Unterminated comment");
  412. tokPos = end + 2;
  413. if (options.locations) {
  414. lineBreak.lastIndex = start;
  415. var match;
  416. while ((match = lineBreak.exec(input)) && match.index < tokPos) {
  417. ++tokCurLine;
  418. tokLineStart = match.index + match[0].length;
  419. }
  420. }
  421. if (options.onComment)
  422. options.onComment(true, input.slice(start + 2, end), start, tokPos,
  423. startLoc, options.locations && new line_loc_t);
  424. }
  425. function skipLineComment() {
  426. var start = tokPos;
  427. var startLoc = options.onComment && options.locations && new line_loc_t;
  428. var ch = input.charCodeAt(tokPos+=2);
  429. while (tokPos < inputLen && ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8233) {
  430. ++tokPos;
  431. ch = input.charCodeAt(tokPos);
  432. }
  433. if (options.onComment)
  434. options.onComment(false, input.slice(start + 2, tokPos), start, tokPos,
  435. startLoc, options.locations && new line_loc_t);
  436. }
  437. // Called at the start of the parse and after every token. Skips
  438. // whitespace and comments, and.
  439. function skipSpace() {
  440. while (tokPos < inputLen) {
  441. var ch = input.charCodeAt(tokPos);
  442. if (ch === 32) { // ' '
  443. ++tokPos;
  444. } else if (ch === 13) {
  445. ++tokPos;
  446. var next = input.charCodeAt(tokPos);
  447. if (next === 10) {
  448. ++tokPos;
  449. }
  450. if (options.locations) {
  451. ++tokCurLine;
  452. tokLineStart = tokPos;
  453. }
  454. } else if (ch === 10 || ch === 8232 || ch === 8233) {
  455. ++tokPos;
  456. if (options.locations) {
  457. ++tokCurLine;
  458. tokLineStart = tokPos;
  459. }
  460. } else if (ch > 8 && ch < 14) {
  461. ++tokPos;
  462. } else if (ch === 47) { // '/'
  463. var next = input.charCodeAt(tokPos + 1);
  464. if (next === 42) { // '*'
  465. skipBlockComment();
  466. } else if (next === 47) { // '/'
  467. skipLineComment();
  468. } else break;
  469. } else if (ch === 160) { // '\xa0'
  470. ++tokPos;
  471. } else if (ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) {
  472. ++tokPos;
  473. } else {
  474. break;
  475. }
  476. }
  477. }
  478. // ### Token reading
  479. // This is the function that is called to fetch the next token. It
  480. // is somewhat obscure, because it works in character codes rather
  481. // than characters, and because operator parsing has been inlined
  482. // into it.
  483. //
  484. // All in the name of speed.
  485. //
  486. // The `forceRegexp` parameter is used in the one case where the
  487. // `tokRegexpAllowed` trick does not work. See `parseStatement`.
  488. function readToken_dot() {
  489. var next = input.charCodeAt(tokPos + 1);
  490. if (next >= 48 && next <= 57) return readNumber(true);
  491. ++tokPos;
  492. return finishToken(_dot);
  493. }
  494. function readToken_slash() { // '/'
  495. var next = input.charCodeAt(tokPos + 1);
  496. if (tokRegexpAllowed) {++tokPos; return readRegexp();}
  497. if (next === 61) return finishOp(_assign, 2);
  498. return finishOp(_slash, 1);
  499. }
  500. function readToken_mult_modulo() { // '%*'
  501. var next = input.charCodeAt(tokPos + 1);
  502. if (next === 61) return finishOp(_assign, 2);
  503. return finishOp(_multiplyModulo, 1);
  504. }
  505. function readToken_pipe_amp(code) { // '|&'
  506. var next = input.charCodeAt(tokPos + 1);
  507. if (next === code) return finishOp(code === 124 ? _logicalOR : _logicalAND, 2);
  508. if (next === 61) return finishOp(_assign, 2);
  509. return finishOp(code === 124 ? _bitwiseOR : _bitwiseAND, 1);
  510. }
  511. function readToken_caret() { // '^'
  512. var next = input.charCodeAt(tokPos + 1);
  513. if (next === 61) return finishOp(_assign, 2);
  514. return finishOp(_bitwiseXOR, 1);
  515. }
  516. function readToken_plus_min(code) { // '+-'
  517. var next = input.charCodeAt(tokPos + 1);
  518. if (next === code) {
  519. if (next == 45 && input.charCodeAt(tokPos + 2) == 62 &&
  520. newline.test(input.slice(lastEnd, tokPos))) {
  521. // A `-->` line comment
  522. tokPos += 3;
  523. skipLineComment();
  524. skipSpace();
  525. return readToken();
  526. }
  527. return finishOp(_incDec, 2);
  528. }
  529. if (next === 61) return finishOp(_assign, 2);
  530. return finishOp(_plusMin, 1);
  531. }
  532. function readToken_lt_gt(code) { // '<>'
  533. var next = input.charCodeAt(tokPos + 1);
  534. var size = 1;
  535. if (next === code) {
  536. size = code === 62 && input.charCodeAt(tokPos + 2) === 62 ? 3 : 2;
  537. if (input.charCodeAt(tokPos + size) === 61) return finishOp(_assign, size + 1);
  538. return finishOp(_bitShift, size);
  539. }
  540. if (next == 33 && code == 60 && input.charCodeAt(tokPos + 2) == 45 &&
  541. input.charCodeAt(tokPos + 3) == 45) {
  542. // `<!--`, an XML-style comment that should be interpreted as a line comment
  543. tokPos += 4;
  544. skipLineComment();
  545. skipSpace();
  546. return readToken();
  547. }
  548. if (next === 61)
  549. size = input.charCodeAt(tokPos + 2) === 61 ? 3 : 2;
  550. return finishOp(_relational, size);
  551. }
  552. function readToken_eq_excl(code) { // '=!'
  553. var next = input.charCodeAt(tokPos + 1);
  554. if (next === 61) return finishOp(_equality, input.charCodeAt(tokPos + 2) === 61 ? 3 : 2);
  555. return finishOp(code === 61 ? _eq : _prefix, 1);
  556. }
  557. function getTokenFromCode(code) {
  558. switch(code) {
  559. // The interpretation of a dot depends on whether it is followed
  560. // by a digit.
  561. case 46: // '.'
  562. return readToken_dot();
  563. // Punctuation tokens.
  564. case 40: ++tokPos; return finishToken(_parenL);
  565. case 41: ++tokPos; return finishToken(_parenR);
  566. case 59: ++tokPos; return finishToken(_semi);
  567. case 44: ++tokPos; return finishToken(_comma);
  568. case 91: ++tokPos; return finishToken(_bracketL);
  569. case 93: ++tokPos; return finishToken(_bracketR);
  570. case 123: ++tokPos; return finishToken(_braceL);
  571. case 125: ++tokPos; return finishToken(_braceR);
  572. case 58: ++tokPos; return finishToken(_colon);
  573. case 63: ++tokPos; return finishToken(_question);
  574. // '0x' is a hexadecimal number.
  575. case 48: // '0'
  576. var next = input.charCodeAt(tokPos + 1);
  577. if (next === 120 || next === 88) return readHexNumber();
  578. // Anything else beginning with a digit is an integer, octal
  579. // number, or float.
  580. case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
  581. return readNumber(false);
  582. // Quotes produce strings.
  583. case 34: case 39: // '"', "'"
  584. return readString(code);
  585. // Operators are parsed inline in tiny state machines. '=' (61) is
  586. // often referred to. `finishOp` simply skips the amount of
  587. // characters it is given as second argument, and returns a token
  588. // of the type given by its first argument.
  589. case 47: // '/'
  590. return readToken_slash(code);
  591. case 37: case 42: // '%*'
  592. return readToken_mult_modulo();
  593. case 124: case 38: // '|&'
  594. return readToken_pipe_amp(code);
  595. case 94: // '^'
  596. return readToken_caret();
  597. case 43: case 45: // '+-'
  598. return readToken_plus_min(code);
  599. case 60: case 62: // '<>'
  600. return readToken_lt_gt(code);
  601. case 61: case 33: // '=!'
  602. return readToken_eq_excl(code);
  603. case 126: // '~'
  604. return finishOp(_prefix, 1);
  605. }
  606. return false;
  607. }
  608. function readToken(forceRegexp) {
  609. if (!forceRegexp) tokStart = tokPos;
  610. else tokPos = tokStart + 1;
  611. if (options.locations) tokStartLoc = new line_loc_t;
  612. if (forceRegexp) return readRegexp();
  613. if (tokPos >= inputLen) return finishToken(_eof);
  614. var code = input.charCodeAt(tokPos);
  615. // Identifier or keyword. '\uXXXX' sequences are allowed in
  616. // identifiers, so '\' also dispatches to that.
  617. if (isIdentifierStart(code) || code === 92 /* '\' */) return readWord();
  618. var tok = getTokenFromCode(code);
  619. if (tok === false) {
  620. // If we are here, we either found a non-ASCII identifier
  621. // character, or something that's entirely disallowed.
  622. var ch = String.fromCharCode(code);
  623. if (ch === "\\" || nonASCIIidentifierStart.test(ch)) return readWord();
  624. raise(tokPos, "Unexpected character '" + ch + "'");
  625. }
  626. return tok;
  627. }
  628. function finishOp(type, size) {
  629. var str = input.slice(tokPos, tokPos + size);
  630. tokPos += size;
  631. finishToken(type, str);
  632. }
  633. // Parse a regular expression. Some context-awareness is necessary,
  634. // since a '/' inside a '[]' set does not end the expression.
  635. function readRegexp() {
  636. var content = "", escaped, inClass, start = tokPos;
  637. for (;;) {
  638. if (tokPos >= inputLen) raise(start, "Unterminated regular expression");
  639. var ch = input.charAt(tokPos);
  640. if (newline.test(ch)) raise(start, "Unterminated regular expression");
  641. if (!escaped) {
  642. if (ch === "[") inClass = true;
  643. else if (ch === "]" && inClass) inClass = false;
  644. else if (ch === "/" && !inClass) break;
  645. escaped = ch === "\\";
  646. } else escaped = false;
  647. ++tokPos;
  648. }
  649. var content = input.slice(start, tokPos);
  650. ++tokPos;
  651. // Need to use `readWord1` because '\uXXXX' sequences are allowed
  652. // here (don't ask).
  653. var mods = readWord1();
  654. if (mods && !/^[gmsiy]*$/.test(mods)) raise(start, "Invalid regexp flag");
  655. try {
  656. var value = new RegExp(content, mods);
  657. } catch (e) {
  658. if (e instanceof SyntaxError) raise(start, e.message);
  659. raise(e);
  660. }
  661. return finishToken(_regexp, value);
  662. }
  663. // Read an integer in the given radix. Return null if zero digits
  664. // were read, the integer value otherwise. When `len` is given, this
  665. // will return `null` unless the integer has exactly `len` digits.
  666. function readInt(radix, len) {
  667. var start = tokPos, total = 0;
  668. for (var i = 0, e = len == null ? Infinity : len; i < e; ++i) {
  669. var code = input.charCodeAt(tokPos), val;
  670. if (code >= 97) val = code - 97 + 10; // a
  671. else if (code >= 65) val = code - 65 + 10; // A
  672. else if (code >= 48 && code <= 57) val = code - 48; // 0-9
  673. else val = Infinity;
  674. if (val >= radix) break;
  675. ++tokPos;
  676. total = total * radix + val;
  677. }
  678. if (tokPos === start || len != null && tokPos - start !== len) return null;
  679. return total;
  680. }
  681. function readHexNumber() {
  682. tokPos += 2; // 0x
  683. var val = readInt(16);
  684. if (val == null) raise(tokStart + 2, "Expected hexadecimal number");
  685. if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number");
  686. return finishToken(_num, val);
  687. }
  688. // Read an integer, octal integer, or floating-point number.
  689. function readNumber(startsWithDot) {
  690. var start = tokPos, isFloat = false, octal = input.charCodeAt(tokPos) === 48;
  691. if (!startsWithDot && readInt(10) === null) raise(start, "Invalid number");
  692. if (input.charCodeAt(tokPos) === 46) {
  693. ++tokPos;
  694. readInt(10);
  695. isFloat = true;
  696. }
  697. var next = input.charCodeAt(tokPos);
  698. if (next === 69 || next === 101) { // 'eE'
  699. next = input.charCodeAt(++tokPos);
  700. if (next === 43 || next === 45) ++tokPos; // '+-'
  701. if (readInt(10) === null) raise(start, "Invalid number");
  702. isFloat = true;
  703. }
  704. if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number");
  705. var str = input.slice(start, tokPos), val;
  706. if (isFloat) val = parseFloat(str);
  707. else if (!octal || str.length === 1) val = parseInt(str, 10);
  708. else if (/[89]/.test(str) || strict) raise(start, "Invalid number");
  709. else val = parseInt(str, 8);
  710. return finishToken(_num, val);
  711. }
  712. // Read a string value, interpreting backslash-escapes.
  713. function readString(quote) {
  714. tokPos++;
  715. var out = "";
  716. for (;;) {
  717. if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant");
  718. var ch = input.charCodeAt(tokPos);
  719. if (ch === quote) {
  720. ++tokPos;
  721. return finishToken(_string, out);
  722. }
  723. if (ch === 92) { // '\'
  724. ch = input.charCodeAt(++tokPos);
  725. var octal = /^[0-7]+/.exec(input.slice(tokPos, tokPos + 3));
  726. if (octal) octal = octal[0];
  727. while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, -1);
  728. if (octal === "0") octal = null;
  729. ++tokPos;
  730. if (octal) {
  731. if (strict) raise(tokPos - 2, "Octal literal in strict mode");
  732. out += String.fromCharCode(parseInt(octal, 8));
  733. tokPos += octal.length - 1;
  734. } else {
  735. switch (ch) {
  736. case 110: out += "\n"; break; // 'n' -> '\n'
  737. case 114: out += "\r"; break; // 'r' -> '\r'
  738. case 120: out += String.fromCharCode(readHexChar(2)); break; // 'x'
  739. case 117: out += String.fromCharCode(readHexChar(4)); break; // 'u'
  740. case 85: out += String.fromCharCode(readHexChar(8)); break; // 'U'
  741. case 116: out += "\t"; break; // 't' -> '\t'
  742. case 98: out += "\b"; break; // 'b' -> '\b'
  743. case 118: out += "\u000b"; break; // 'v' -> '\u000b'
  744. case 102: out += "\f"; break; // 'f' -> '\f'
  745. case 48: out += "\0"; break; // 0 -> '\0'
  746. case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n'
  747. case 10: // ' \n'
  748. if (options.locations) { tokLineStart = tokPos; ++tokCurLine; }
  749. break;
  750. default: out += String.fromCharCode(ch); break;
  751. }
  752. }
  753. } else {
  754. if (ch === 13 || ch === 10 || ch === 8232 || ch === 8233) raise(tokStart, "Unterminated string constant");
  755. out += String.fromCharCode(ch); // '\'
  756. ++tokPos;
  757. }
  758. }
  759. }
  760. // Used to read character escape sequences ('\x', '\u', '\U').
  761. function readHexChar(len) {
  762. var n = readInt(16, len);
  763. if (n === null) raise(tokStart, "Bad character escape sequence");
  764. return n;
  765. }
  766. // Used to signal to callers of `readWord1` whether the word
  767. // contained any escape sequences. This is needed because words with
  768. // escape sequences must not be interpreted as keywords.
  769. var containsEsc;
  770. // Read an identifier, and return it as a string. Sets `containsEsc`
  771. // to whether the word contained a '\u' escape.
  772. //
  773. // Only builds up the word character-by-character when it actually
  774. // containeds an escape, as a micro-optimization.
  775. function readWord1() {
  776. containsEsc = false;
  777. var word, first = true, start = tokPos;
  778. for (;;) {
  779. var ch = input.charCodeAt(tokPos);
  780. if (isIdentifierChar(ch)) {
  781. if (containsEsc) word += input.charAt(tokPos);
  782. ++tokPos;
  783. } else if (ch === 92) { // "\"
  784. if (!containsEsc) word = input.slice(start, tokPos);
  785. containsEsc = true;
  786. if (input.charCodeAt(++tokPos) != 117) // "u"
  787. raise(tokPos, "Expecting Unicode escape sequence \\uXXXX");
  788. ++tokPos;
  789. var esc = readHexChar(4);
  790. var escStr = String.fromCharCode(esc);
  791. if (!escStr) raise(tokPos - 1, "Invalid Unicode escape");
  792. if (!(first ? isIdentifierStart(esc) : isIdentifierChar(esc)))
  793. raise(tokPos - 4, "Invalid Unicode escape");
  794. word += escStr;
  795. } else {
  796. break;
  797. }
  798. first = false;
  799. }
  800. return containsEsc ? word : input.slice(start, tokPos);
  801. }
  802. // Read an identifier or keyword token. Will check for reserved
  803. // words when necessary.
  804. function readWord() {
  805. var word = readWord1();
  806. var type = _name;
  807. if (!containsEsc && isKeyword(word))
  808. type = keywordTypes[word];
  809. return finishToken(type, word);
  810. }
  811. // ## Parser
  812. // A recursive descent parser operates by defining functions for all
  813. // syntactic elements, and recursively calling those, each function
  814. // advancing the input stream and returning an AST node. Precedence
  815. // of constructs (for example, the fact that `!x[1]` means `!(x[1])`
  816. // instead of `(!x)[1]` is handled by the fact that the parser
  817. // function that parses unary prefix operators is called first, and
  818. // in turn calls the function that parses `[]` subscripts — that
  819. // way, it'll receive the node for `x[1]` already parsed, and wraps
  820. // *that* in the unary operator node.
  821. //
  822. // Acorn uses an [operator precedence parser][opp] to handle binary
  823. // operator precedence, because it is much more compact than using
  824. // the technique outlined above, which uses different, nesting
  825. // functions to specify precedence, for all of the ten binary
  826. // precedence levels that JavaScript defines.
  827. //
  828. // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
  829. // ### Parser utilities
  830. // Continue to the next token.
  831. function next() {
  832. lastStart = tokStart;
  833. lastEnd = tokEnd;
  834. lastEndLoc = tokEndLoc;
  835. readToken();
  836. }
  837. // Enter strict mode. Re-reads the next token to please pedantic
  838. // tests ("use strict"; 010; -- should fail).
  839. function setStrict(strct) {
  840. strict = strct;
  841. tokPos = tokStart;
  842. if (options.locations) {
  843. while (tokPos < tokLineStart) {
  844. tokLineStart = input.lastIndexOf("\n", tokLineStart - 2) + 1;
  845. --tokCurLine;
  846. }
  847. }
  848. skipSpace();
  849. readToken();
  850. }
  851. // Start an AST node, attaching a start offset.
  852. function node_t() {
  853. this.type = null;
  854. this.start = tokStart;
  855. this.end = null;
  856. }
  857. function node_loc_t() {
  858. this.start = tokStartLoc;
  859. this.end = null;
  860. if (sourceFile !== null) this.source = sourceFile;
  861. }
  862. function startNode() {
  863. var node = new node_t();
  864. if (options.locations)
  865. node.loc = new node_loc_t();
  866. if (options.directSourceFile)
  867. node.sourceFile = options.directSourceFile;
  868. if (options.ranges)
  869. node.range = [tokStart, 0];
  870. return node;
  871. }
  872. // Start a node whose start offset information should be based on
  873. // the start of another node. For example, a binary operator node is
  874. // only started after its left-hand side has already been parsed.
  875. function startNodeFrom(other) {
  876. var node = new node_t();
  877. node.start = other.start;
  878. if (options.locations) {
  879. node.loc = new node_loc_t();
  880. node.loc.start = other.loc.start;
  881. }
  882. if (options.ranges)
  883. node.range = [other.range[0], 0];
  884. return node;
  885. }
  886. // Finish an AST node, adding `type` and `end` properties.
  887. function finishNode(node, type) {
  888. node.type = type;
  889. node.end = lastEnd;
  890. if (options.locations)
  891. node.loc.end = lastEndLoc;
  892. if (options.ranges)
  893. node.range[1] = lastEnd;
  894. return node;
  895. }
  896. // Test whether a statement node is the string literal `"use strict"`.
  897. function isUseStrict(stmt) {
  898. return options.ecmaVersion >= 5 && stmt.type === "ExpressionStatement" &&
  899. stmt.expression.type === "Literal" && stmt.expression.value === "use strict";
  900. }
  901. // Predicate that tests whether the next token is of the given
  902. // type, and if yes, consumes it as a side effect.
  903. function eat(type) {
  904. if (tokType === type) {
  905. next();
  906. return true;
  907. }
  908. }
  909. // Test whether a semicolon can be inserted at the current position.
  910. function canInsertSemicolon() {
  911. return !options.strictSemicolons &&
  912. (tokType === _eof || tokType === _braceR || newline.test(input.slice(lastEnd, tokStart)));
  913. }
  914. // Consume a semicolon, or, failing that, see if we are allowed to
  915. // pretend that there is a semicolon at this position.
  916. function semicolon() {
  917. if (!eat(_semi) && !canInsertSemicolon()) unexpected();
  918. }
  919. // Expect a token of a given type. If found, consume it, otherwise,
  920. // raise an unexpected token error.
  921. function expect(type) {
  922. if (tokType === type) next();
  923. else unexpected();
  924. }
  925. // Raise an unexpected token error.
  926. function unexpected() {
  927. raise(tokStart, "Unexpected token");
  928. }
  929. // Verify that a node is an lval — something that can be assigned
  930. // to.
  931. function checkLVal(expr) {
  932. if (expr.type !== "Identifier" && expr.type !== "MemberExpression")
  933. raise(expr.start, "Assigning to rvalue");
  934. if (strict && expr.type === "Identifier" && isStrictBadIdWord(expr.name))
  935. raise(expr.start, "Assigning to " + expr.name + " in strict mode");
  936. }
  937. // ### Statement parsing
  938. // Parse a program. Initializes the parser, reads any number of
  939. // statements, and wraps them in a Program node. Optionally takes a
  940. // `program` argument. If present, the statements will be appended
  941. // to its body instead of creating a new node.
  942. function parseTopLevel(program) {
  943. lastStart = lastEnd = tokPos;
  944. if (options.locations) lastEndLoc = new line_loc_t;
  945. inFunction = strict = null;
  946. labels = [];
  947. readToken();
  948. var node = program || startNode(), first = true;
  949. if (!program) node.body = [];
  950. while (tokType !== _eof) {
  951. var stmt = parseStatement();
  952. node.body.push(stmt);
  953. if (first && isUseStrict(stmt)) setStrict(true);
  954. first = false;
  955. }
  956. return finishNode(node, "Program");
  957. }
  958. var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};
  959. // Parse a single statement.
  960. //
  961. // If expecting a statement and finding a slash operator, parse a
  962. // regular expression literal. This is to handle cases like
  963. // `if (foo) /blah/.exec(foo);`, where looking at the previous token
  964. // does not help.
  965. function parseStatement() {
  966. if (tokType === _slash || tokType === _assign && tokVal == "/=")
  967. readToken(true);
  968. var starttype = tokType, node = startNode();
  969. // Most types of statements are recognized by the keyword they
  970. // start with. Many are trivial to parse, some require a bit of
  971. // complexity.
  972. switch (starttype) {
  973. case _break: case _continue:
  974. next();
  975. var isBreak = starttype === _break;
  976. if (eat(_semi) || canInsertSemicolon()) node.label = null;
  977. else if (tokType !== _name) unexpected();
  978. else {
  979. node.label = parseIdent();
  980. semicolon();
  981. }
  982. // Verify that there is an actual destination to break or
  983. // continue to.
  984. for (var i = 0; i < labels.length; ++i) {
  985. var lab = labels[i];
  986. if (node.label == null || lab.name === node.label.name) {
  987. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  988. if (node.label && isBreak) break;
  989. }
  990. }
  991. if (i === labels.length) raise(node.start, "Unsyntactic " + starttype.keyword);
  992. return finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  993. case _debugger:
  994. next();
  995. semicolon();
  996. return finishNode(node, "DebuggerStatement");
  997. case _do:
  998. next();
  999. labels.push(loopLabel);
  1000. node.body = parseStatement();
  1001. labels.pop();
  1002. expect(_while);
  1003. node.test = parseParenExpression();
  1004. semicolon();
  1005. return finishNode(node, "DoWhileStatement");
  1006. // Disambiguating between a `for` and a `for`/`in` loop is
  1007. // non-trivial. Basically, we have to parse the init `var`
  1008. // statement or expression, disallowing the `in` operator (see
  1009. // the second parameter to `parseExpression`), and then check
  1010. // whether the next token is `in`. When there is no init part
  1011. // (semicolon immediately after the opening parenthesis), it is
  1012. // a regular `for` loop.
  1013. case _for:
  1014. next();
  1015. labels.push(loopLabel);
  1016. expect(_parenL);
  1017. if (tokType === _semi) return parseFor(node, null);
  1018. if (tokType === _var) {
  1019. var init = startNode();
  1020. next();
  1021. parseVar(init, true);
  1022. finishNode(init, "VariableDeclaration");
  1023. if (init.declarations.length === 1 && eat(_in))
  1024. return parseForIn(node, init);
  1025. return parseFor(node, init);
  1026. }
  1027. var init = parseExpression(false, true);
  1028. if (eat(_in)) {checkLVal(init); return parseForIn(node, init);}
  1029. return parseFor(node, init);
  1030. case _function:
  1031. next();
  1032. return parseFunction(node, true);
  1033. case _if:
  1034. next();
  1035. node.test = parseParenExpression();
  1036. node.consequent = parseStatement();
  1037. node.alternate = eat(_else) ? parseStatement() : null;
  1038. return finishNode(node, "IfStatement");
  1039. case _return:
  1040. if (!inFunction && !options.allowReturnOutsideFunction)
  1041. raise(tokStart, "'return' outside of function");
  1042. next();
  1043. // In `return` (and `break`/`continue`), the keywords with
  1044. // optional arguments, we eagerly look for a semicolon or the
  1045. // possibility to insert one.
  1046. if (eat(_semi) || canInsertSemicolon()) node.argument = null;
  1047. else { node.argument = parseExpression(); semicolon(); }
  1048. return finishNode(node, "ReturnStatement");
  1049. case _switch:
  1050. next();
  1051. node.discriminant = parseParenExpression();
  1052. node.cases = [];
  1053. expect(_braceL);
  1054. labels.push(switchLabel);
  1055. // Statements under must be grouped (by label) in SwitchCase
  1056. // nodes. `cur` is used to keep the node that we are currently
  1057. // adding statements to.
  1058. for (var cur, sawDefault; tokType != _braceR;) {
  1059. if (tokType === _case || tokType === _default) {
  1060. var isCase = tokType === _case;
  1061. if (cur) finishNode(cur, "SwitchCase");
  1062. node.cases.push(cur = startNode());
  1063. cur.consequent = [];
  1064. next();
  1065. if (isCase) cur.test = parseExpression();
  1066. else {
  1067. if (sawDefault) raise(lastStart, "Multiple default clauses"); sawDefault = true;
  1068. cur.test = null;
  1069. }
  1070. expect(_colon);
  1071. } else {
  1072. if (!cur) unexpected();
  1073. cur.consequent.push(parseStatement());
  1074. }
  1075. }
  1076. if (cur) finishNode(cur, "SwitchCase");
  1077. next(); // Closing brace
  1078. labels.pop();
  1079. return finishNode(node, "SwitchStatement");
  1080. case _throw:
  1081. next();
  1082. if (newline.test(input.slice(lastEnd, tokStart)))
  1083. raise(lastEnd, "Illegal newline after throw");
  1084. node.argument = parseExpression();
  1085. semicolon();
  1086. return finishNode(node, "ThrowStatement");
  1087. case _try:
  1088. next();
  1089. node.block = parseBlock();
  1090. node.handler = null;
  1091. if (tokType === _catch) {
  1092. var clause = startNode();
  1093. next();
  1094. expect(_parenL);
  1095. clause.param = parseIdent();
  1096. if (strict && isStrictBadIdWord(clause.param.name))
  1097. raise(clause.param.start, "Binding " + clause.param.name + " in strict mode");
  1098. expect(_parenR);
  1099. clause.guard = null;
  1100. clause.body = parseBlock();
  1101. node.handler = finishNode(clause, "CatchClause");
  1102. }
  1103. node.guardedHandlers = empty;
  1104. node.finalizer = eat(_finally) ? parseBlock() : null;
  1105. if (!node.handler && !node.finalizer)
  1106. raise(node.start, "Missing catch or finally clause");
  1107. return finishNode(node, "TryStatement");
  1108. case _var:
  1109. next();
  1110. parseVar(node);
  1111. semicolon();
  1112. return finishNode(node, "VariableDeclaration");
  1113. case _while:
  1114. next();
  1115. node.test = parseParenExpression();
  1116. labels.push(loopLabel);
  1117. node.body = parseStatement();
  1118. labels.pop();
  1119. return finishNode(node, "WhileStatement");
  1120. case _with:
  1121. if (strict) raise(tokStart, "'with' in strict mode");
  1122. next();
  1123. node.object = parseParenExpression();
  1124. node.body = parseStatement();
  1125. return finishNode(node, "WithStatement");
  1126. case _braceL:
  1127. return parseBlock();
  1128. case _semi:
  1129. next();
  1130. return finishNode(node, "EmptyStatement");
  1131. // If the statement does not start with a statement keyword or a
  1132. // brace, it's an ExpressionStatement or LabeledStatement. We
  1133. // simply start parsing an expression, and afterwards, if the
  1134. // next token is a colon and the expression was a simple
  1135. // Identifier node, we switch to interpreting it as a label.
  1136. default:
  1137. var maybeName = tokVal, expr = parseExpression();
  1138. if (starttype === _name && expr.type === "Identifier" && eat(_colon)) {
  1139. for (var i = 0; i < labels.length; ++i)
  1140. if (labels[i].name === maybeName) raise(expr.start, "Label '" + maybeName + "' is already declared");
  1141. var kind = tokType.isLoop ? "loop" : tokType === _switch ? "switch" : null;
  1142. labels.push({name: maybeName, kind: kind});
  1143. node.body = parseStatement();
  1144. labels.pop();
  1145. node.label = expr;
  1146. return finishNode(node, "LabeledStatement");
  1147. } else {
  1148. node.expression = expr;
  1149. semicolon();
  1150. return finishNode(node, "ExpressionStatement");
  1151. }
  1152. }
  1153. }
  1154. // Used for constructs like `switch` and `if` that insist on
  1155. // parentheses around their expression.
  1156. function parseParenExpression() {
  1157. expect(_parenL);
  1158. var val = parseExpression();
  1159. expect(_parenR);
  1160. return val;
  1161. }
  1162. // Parse a semicolon-enclosed block of statements, handling `"use
  1163. // strict"` declarations when `allowStrict` is true (used for
  1164. // function bodies).
  1165. function parseBlock(allowStrict) {
  1166. var node = startNode(), first = true, strict = false, oldStrict;
  1167. node.body = [];
  1168. expect(_braceL);
  1169. while (!eat(_braceR)) {
  1170. var stmt = parseStatement();
  1171. node.body.push(stmt);
  1172. if (first && allowStrict && isUseStrict(stmt)) {
  1173. oldStrict = strict;
  1174. setStrict(strict = true);
  1175. }
  1176. first = false;
  1177. }
  1178. if (strict && !oldStrict) setStrict(false);
  1179. return finishNode(node, "BlockStatement");
  1180. }
  1181. // Parse a regular `for` loop. The disambiguation code in
  1182. // `parseStatement` will already have parsed the init statement or
  1183. // expression.
  1184. function parseFor(node, init) {
  1185. node.init = init;
  1186. expect(_semi);
  1187. node.test = tokType === _semi ? null : parseExpression();
  1188. expect(_semi);
  1189. node.update = tokType === _parenR ? null : parseExpression();
  1190. expect(_parenR);
  1191. node.body = parseStatement();
  1192. labels.pop();
  1193. return finishNode(node, "ForStatement");
  1194. }
  1195. // Parse a `for`/`in` loop.
  1196. function parseForIn(node, init) {
  1197. node.left = init;
  1198. node.right = parseExpression();
  1199. expect(_parenR);
  1200. node.body = parseStatement();
  1201. labels.pop();
  1202. return finishNode(node, "ForInStatement");
  1203. }
  1204. // Parse a list of variable declarations.
  1205. function parseVar(node, noIn) {
  1206. node.declarations = [];
  1207. node.kind = "var";
  1208. for (;;) {
  1209. var decl = startNode();
  1210. decl.id = parseIdent();
  1211. if (strict && isStrictBadIdWord(decl.id.name))
  1212. raise(decl.id.start, "Binding " + decl.id.name + " in strict mode");
  1213. decl.init = eat(_eq) ? parseExpression(true, noIn) : null;
  1214. node.declarations.push(finishNode(decl, "VariableDeclarator"));
  1215. if (!eat(_comma)) break;
  1216. }
  1217. return node;
  1218. }
  1219. // ### Expression parsing
  1220. // These nest, from the most general expression type at the top to
  1221. // 'atomic', nondivisible expression types at the bottom. Most of
  1222. // the functions will simply let the function(s) below them parse,
  1223. // and, *if* the syntactic construct they handle is present, wrap
  1224. // the AST node that the inner parser gave them in another node.
  1225. // Parse a full expression. The arguments are used to forbid comma
  1226. // sequences (in argument lists, array literals, or object literals)
  1227. // or the `in` operator (in for loops initalization expressions).
  1228. function parseExpression(noComma, noIn) {
  1229. var expr = parseMaybeAssign(noIn);
  1230. if (!noComma && tokType === _comma) {
  1231. var node = startNodeFrom(expr);
  1232. node.expressions = [expr];
  1233. while (eat(_comma)) node.expressions.push(parseMaybeAssign(noIn));
  1234. return finishNode(node, "SequenceExpression");
  1235. }
  1236. return expr;
  1237. }
  1238. // Parse an assignment expression. This includes applications of
  1239. // operators like `+=`.
  1240. function parseMaybeAssign(noIn) {
  1241. var left = parseMaybeConditional(noIn);
  1242. if (tokType.isAssign) {
  1243. var node = startNodeFrom(left);
  1244. node.operator = tokVal;
  1245. node.left = left;
  1246. next();
  1247. node.right = parseMaybeAssign(noIn);
  1248. checkLVal(left);
  1249. return finishNode(node, "AssignmentExpression");
  1250. }
  1251. return left;
  1252. }
  1253. // Parse a ternary conditional (`?:`) operator.
  1254. function parseMaybeConditional(noIn) {
  1255. var expr = parseExprOps(noIn);
  1256. if (eat(_question)) {
  1257. var node = startNodeFrom(expr);
  1258. node.test = expr;
  1259. node.consequent = parseExpression(true);
  1260. expect(_colon);
  1261. node.alternate = parseExpression(true, noIn);
  1262. return finishNode(node, "ConditionalExpression");
  1263. }
  1264. return expr;
  1265. }
  1266. // Start the precedence parser.
  1267. function parseExprOps(noIn) {
  1268. return parseExprOp(parseMaybeUnary(), -1, noIn);
  1269. }
  1270. // Parse binary operators with the operator precedence parsing
  1271. // algorithm. `left` is the left-hand side of the operator.
  1272. // `minPrec` provides context that allows the function to stop and
  1273. // defer further parser to one of its callers when it encounters an
  1274. // operator that has a lower precedence than the set it is parsing.
  1275. function parseExprOp(left, minPrec, noIn) {
  1276. var prec = tokType.binop;
  1277. if (prec != null && (!noIn || tokType !== _in)) {
  1278. if (prec > minPrec) {
  1279. var node = startNodeFrom(left);
  1280. node.left = left;
  1281. node.operator = tokVal;
  1282. var op = tokType;
  1283. next();
  1284. node.right = parseExprOp(parseMaybeUnary(), prec, noIn);
  1285. var exprNode = finishNode(node, (op === _logicalOR || op === _logicalAND) ? "LogicalExpression" : "BinaryExpression");
  1286. return parseExprOp(exprNode, minPrec, noIn);
  1287. }
  1288. }
  1289. return left;
  1290. }
  1291. // Parse unary operators, both prefix and postfix.
  1292. function parseMaybeUnary() {
  1293. if (tokType.prefix) {
  1294. var node = startNode(), update = tokType.isUpdate;
  1295. node.operator = tokVal;
  1296. node.prefix = true;
  1297. tokRegexpAllowed = true;
  1298. next();
  1299. node.argument = parseMaybeUnary();
  1300. if (update) checkLVal(node.argument);
  1301. else if (strict && node.operator === "delete" &&
  1302. node.argument.type === "Identifier")
  1303. raise(node.start, "Deleting local variable in strict mode");
  1304. return finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  1305. }
  1306. var expr = parseExprSubscripts();
  1307. while (tokType.postfix && !canInsertSemicolon()) {
  1308. var node = startNodeFrom(expr);
  1309. node.operator = tokVal;
  1310. node.prefix = false;
  1311. node.argument = expr;
  1312. checkLVal(expr);
  1313. next();
  1314. expr = finishNode(node, "UpdateExpression");
  1315. }
  1316. return expr;
  1317. }
  1318. // Parse call, dot, and `[]`-subscript expressions.
  1319. function parseExprSubscripts() {
  1320. return parseSubscripts(parseExprAtom());
  1321. }
  1322. function parseSubscripts(base, noCalls) {
  1323. if (eat(_dot)) {
  1324. var node = startNodeFrom(base);
  1325. node.object = base;
  1326. node.property = parseIdent(true);
  1327. node.computed = false;
  1328. return parseSubscripts(finishNode(node, "MemberExpression"), noCalls);
  1329. } else if (eat(_bracketL)) {
  1330. var node = startNodeFrom(base);
  1331. node.object = base;
  1332. node.property = parseExpression();
  1333. node.computed = true;
  1334. expect(_bracketR);
  1335. return parseSubscripts(finishNode(node, "MemberExpression"), noCalls);
  1336. } else if (!noCalls && eat(_parenL)) {
  1337. var node = startNodeFrom(base);
  1338. node.callee = base;
  1339. node.arguments = parseExprList(_parenR, false);
  1340. return parseSubscripts(finishNode(node, "CallExpression"), noCalls);
  1341. } else return base;
  1342. }
  1343. // Parse an atomic expression — either a single token that is an
  1344. // expression, an expression started by a keyword like `function` or
  1345. // `new`, or an expression wrapped in punctuation like `()`, `[]`,
  1346. // or `{}`.
  1347. function parseExprAtom() {
  1348. switch (tokType) {
  1349. case _this:
  1350. var node = startNode();
  1351. next();
  1352. return finishNode(node, "ThisExpression");
  1353. case _name:
  1354. return parseIdent();
  1355. case _num: case _string: case _regexp:
  1356. var node = startNode();
  1357. node.value = tokVal;
  1358. node.raw = input.slice(tokStart, tokEnd);
  1359. next();
  1360. return finishNode(node, "Literal");
  1361. case _null: case _true: case _false:
  1362. var node = startNode();
  1363. node.value = tokType.atomValue;
  1364. node.raw = tokType.keyword;
  1365. next();
  1366. return finishNode(node, "Literal");
  1367. case _parenL:
  1368. var tokStartLoc1 = tokStartLoc, tokStart1 = tokStart;
  1369. next();
  1370. var val = parseExpression();
  1371. val.start = tokStart1;
  1372. val.end = tokEnd;
  1373. if (options.locations) {
  1374. val.loc.start = tokStartLoc1;
  1375. val.loc.end = tokEndLoc;
  1376. }
  1377. if (options.ranges)
  1378. val.range = [tokStart1, tokEnd];
  1379. expect(_parenR);
  1380. return val;
  1381. case _bracketL:
  1382. var node = startNode();
  1383. next();
  1384. node.elements = parseExprList(_bracketR, true, true);
  1385. return finishNode(node, "ArrayExpression");
  1386. case _braceL:
  1387. return parseObj();
  1388. case _function:
  1389. var node = startNode();
  1390. next();
  1391. return parseFunction(node, false);
  1392. case _new:
  1393. return parseNew();
  1394. default:
  1395. unexpected();
  1396. }
  1397. }
  1398. // New's precedence is slightly tricky. It must allow its argument
  1399. // to be a `[]` or dot subscript expression, but not a call — at
  1400. // least, not without wrapping it in parentheses. Thus, it uses the
  1401. function parseNew() {
  1402. var node = startNode();
  1403. next();
  1404. node.callee = parseSubscripts(parseExprAtom(), true);
  1405. if (eat(_parenL)) node.arguments = parseExprList(_parenR, false);
  1406. else node.arguments = empty;
  1407. return finishNode(node, "NewExpression");
  1408. }
  1409. // Parse an object literal.
  1410. function parseObj() {
  1411. var node = startNode(), first = true, sawGetSet = false;
  1412. node.properties = [];
  1413. next();
  1414. while (!eat(_braceR)) {
  1415. if (!first) {
  1416. expect(_comma);
  1417. if (options.allowTrailingCommas && eat(_braceR)) break;
  1418. } else first = false;
  1419. var prop = {key: parsePropertyName()}, isGetSet = false, kind;
  1420. if (eat(_colon)) {
  1421. prop.value = parseExpression(true);
  1422. kind = prop.kind = "init";
  1423. } else if (options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
  1424. (prop.key.name === "get" || prop.key.name === "set")) {
  1425. isGetSet = sawGetSet = true;
  1426. kind = prop.kind = prop.key.name;
  1427. prop.key = parsePropertyName();
  1428. if (tokType !== _parenL) unexpected();
  1429. prop.value = parseFunction(startNode(), false);
  1430. } else unexpected();
  1431. // getters and setters are not allowed to clash — either with
  1432. // each other or with an init property — and in strict mode,
  1433. // init properties are also not allowed to be repeated.
  1434. if (prop.key.type === "Identifier" && (strict || sawGetSet)) {
  1435. for (var i = 0; i < node.properties.length; ++i) {
  1436. var other = node.properties[i];
  1437. if (other.key.name === prop.key.name) {
  1438. var conflict = kind == other.kind || isGetSet && other.kind === "init" ||
  1439. kind === "init" && (other.kind === "get" || other.kind === "set");
  1440. if (conflict && !strict && kind === "init" && other.kind === "init") conflict = false;
  1441. if (conflict) raise(prop.key.start, "Redefinition of property");
  1442. }
  1443. }
  1444. }
  1445. node.properties.push(prop);
  1446. }
  1447. return finishNode(node, "ObjectExpression");
  1448. }
  1449. function parsePropertyName() {
  1450. if (tokType === _num || tokType === _string) return parseExprAtom();
  1451. return parseIdent(true);
  1452. }
  1453. // Parse a function declaration or literal (depending on the
  1454. // `isStatement` parameter).
  1455. function parseFunction(node, isStatement) {
  1456. if (tokType === _name) node.id = parseIdent();
  1457. else if (isStatement) unexpected();
  1458. else node.id = null;
  1459. node.params = [];
  1460. var first = true;
  1461. expect(_parenL);
  1462. while (!eat(_parenR)) {
  1463. if (!first) expect(_comma); else first = false;
  1464. node.params.push(parseIdent());
  1465. }
  1466. // Start a new scope with regard to labels and the `inFunction`
  1467. // flag (restore them to their old value afterwards).
  1468. var oldInFunc = inFunction, oldLabels = labels;
  1469. inFunction = true; labels = [];
  1470. node.body = parseBlock(true);
  1471. inFunction = oldInFunc; labels = oldLabels;
  1472. // If this is a strict mode function, verify that argument names
  1473. // are not repeated, and it does not try to bind the words `eval`
  1474. // or `arguments`.
  1475. if (strict || node.body.body.length && isUseStrict(node.body.body[0])) {
  1476. for (var i = node.id ? -1 : 0; i < node.params.length; ++i) {
  1477. var id = i < 0 ? node.id : node.params[i];
  1478. if (isStrictReservedWord(id.name) || isStrictBadIdWord(id.name))
  1479. raise(id.start, "Defining '" + id.name + "' in strict mode");
  1480. if (i >= 0) for (var j = 0; j < i; ++j) if (id.name === node.params[j].name)
  1481. raise(id.start, "Argument name clash in strict mode");
  1482. }
  1483. }
  1484. return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  1485. }
  1486. // Parses a comma-separated list of expressions, and returns them as
  1487. // an array. `close` is the token type that ends the list, and
  1488. // `allowEmpty` can be turned on to allow subsequent commas with
  1489. // nothing in between them to be parsed as `null` (which is needed
  1490. // for array literals).
  1491. function parseExprList(close, allowTrailingComma, allowEmpty) {
  1492. var elts = [], first = true;
  1493. while (!eat(close)) {
  1494. if (!first) {
  1495. expect(_comma);
  1496. if (allowTrailingComma && options.allowTrailingCommas && eat(close)) break;
  1497. } else first = false;
  1498. if (allowEmpty && tokType === _comma) elts.push(null);
  1499. else elts.push(parseExpression(true));
  1500. }
  1501. return elts;
  1502. }
  1503. // Parse the next token as an identifier. If `liberal` is true (used
  1504. // when parsing properties), it will also convert keywords into
  1505. // identifiers.
  1506. function parseIdent(liberal) {
  1507. var node = startNode();
  1508. if (liberal && options.forbidReserved == "everywhere") liberal = false;
  1509. if (tokType === _name) {
  1510. if (!liberal &&
  1511. (options.forbidReserved &&
  1512. (options.ecmaVersion === 3 ? isReservedWord3 : isReservedWord5)(tokVal) ||
  1513. strict && isStrictReservedWord(tokVal)) &&
  1514. input.slice(tokStart, tokEnd).indexOf("\\") == -1)
  1515. raise(tokStart, "The keyword '" + tokVal + "' is reserved");
  1516. node.name = tokVal;
  1517. } else if (liberal && tokType.keyword) {
  1518. node.name = tokType.keyword;
  1519. } else {
  1520. unexpected();
  1521. }
  1522. tokRegexpAllowed = false;
  1523. next();
  1524. return finishNode(node, "Identifier");
  1525. }
  1526. });