acorn.js 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  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. (function(exports) {
  16. "use strict";
  17. exports.version = "0.0.1";
  18. // The main exported interface (under `window.acorn` when in the
  19. // browser) is a `parse` function that takes a code string and
  20. // returns an abstract syntax tree as specified by [Mozilla parser
  21. // API][api], with the caveat that the SpiderMonkey-specific syntax
  22. // (`let`, `yield`, inline XML, etc) is not recognized.
  23. //
  24. // [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
  25. var options, input, inputLen, sourceFile;
  26. exports.parse = function(inpt, opts) {
  27. input = String(inpt); inputLen = input.length;
  28. options = opts || {};
  29. for (var opt in defaultOptions) if (!options.hasOwnProperty(opt))
  30. options[opt] = defaultOptions[opt];
  31. sourceFile = options.sourceFile || null;
  32. return parseTopLevel(options.program);
  33. };
  34. // A second optional argument can be given to further configure
  35. // the parser process. These options are recognized:
  36. var defaultOptions = exports.defaultOptions = {
  37. // `ecmaVersion` indicates the ECMAScript version to parse. Must
  38. // be either 3 or 5. This
  39. // influences support for strict mode, the set of reserved words, and
  40. // support for getters and setter.
  41. ecmaVersion: 5,
  42. // Turn on `strictSemicolons` to prevent the parser from doing
  43. // automatic semicolon insertion.
  44. strictSemicolons: false,
  45. // When `allowTrailingCommas` is false, the parser will not allow
  46. // trailing commas in array and object literals.
  47. allowTrailingCommas: true,
  48. // By default, reserved words are not enforced. Enable
  49. // `forbidReserved` to enforce them.
  50. forbidReserved: false,
  51. // When `trackComments` is turned on, the parser will attach
  52. // `commentsBefore` and `commentsAfter` properties to AST nodes
  53. // holding arrays of strings. A single comment may appear in both
  54. // a `commentsBefore` and `commentsAfter` array (of the nodes
  55. // after and before it), but never twice in the before (or after)
  56. // array of different nodes.
  57. trackComments: false,
  58. // When `locations` is on, `loc` properties holding objects with
  59. // `start` and `end` properties in `{line, column}` form (with
  60. // line being 1-based and column 0-based) will be attached to the
  61. // nodes.
  62. locations: false,
  63. // Nodes have their start and end characters offsets recorded in
  64. // `start` and `end` properties (directly on the node, rather than
  65. // the `loc` object, which holds line/column data. To also add a
  66. // [semi-standardized][range] `range` property holding a `[start,
  67. // end]` array with the same numbers, set the `ranges` option to
  68. // `true`.
  69. //
  70. // [range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678
  71. ranges: false,
  72. // It is possible to parse multiple files into a single AST by
  73. // passing the tree produced by parsing the first file as
  74. // `program` option in subsequent parses. This will add the
  75. // toplevel forms of the parsed file to the `Program` (top) node
  76. // of an existing parse tree.
  77. program: null,
  78. // When `location` is on, you can pass this to record the source
  79. // file in every node's `loc` object.
  80. sourceFile: null
  81. };
  82. // The `getLineInfo` function is mostly useful when the
  83. // `locations` option is off (for performance reasons) and you
  84. // want to find the line/column position for a given character
  85. // offset. `input` should be the code string that the offset refers
  86. // into.
  87. var getLineInfo = exports.getLineInfo = function(input, offset) {
  88. for (var line = 1, cur = 0;;) {
  89. lineBreak.lastIndex = cur;
  90. var match = lineBreak.exec(input);
  91. if (match && match.index < offset) {
  92. ++line;
  93. cur = match.index + match[0].length;
  94. } else break;
  95. }
  96. return {line: line, column: offset - cur};
  97. };
  98. // Acorn is organized as a tokenizer and a recursive-descent parser.
  99. // Both use (closure-)global variables to keep their state and
  100. // communicate. We already saw the `options`, `input`, and
  101. // `inputLen` variables above (set in `parse`).
  102. // The current position of the tokenizer in the input.
  103. var tokPos;
  104. // The start and end offsets of the current token.
  105. var tokStart, tokEnd;
  106. // When `options.locations` is true, these hold objects
  107. // containing the tokens start and end line/column pairs.
  108. var tokStartLoc, tokEndLoc;
  109. // The type and value of the current token. Token types are objects,
  110. // named by variables against which they can be compared, and
  111. // holding properties that describe them (indicating, for example,
  112. // the precedence of an infix operator, and the original name of a
  113. // keyword token). The kind of value that's held in `tokVal` depends
  114. // on the type of the token. For literals, it is the literal value,
  115. // for operators, the operator name, and so on.
  116. var tokType, tokVal;
  117. // These are used to hold arrays of comments when
  118. // `options.trackComments` is true.
  119. var tokCommentsBefore, tokCommentsAfter;
  120. // Interal state for the tokenizer. To distinguish between division
  121. // operators and regular expressions, it remembers whether the last
  122. // token was one that is allowed to be followed by an expression.
  123. // (If it is, a slash is probably a regexp, if it isn't it's a
  124. // division operator. See the `parseStatement` function for a
  125. // caveat.)
  126. var tokRegexpAllowed, tokComments;
  127. // When `options.locations` is true, these are used to keep
  128. // track of the current line, and know when a new line has been
  129. // entered. See the `curLineLoc` function.
  130. var tokCurLine, tokLineStart, tokLineStartNext;
  131. // These store the position of the previous token, which is useful
  132. // when finishing a node and assigning its `end` position.
  133. var lastStart, lastEnd, lastEndLoc;
  134. // This is the parser's state. `inFunction` is used to reject
  135. // `return` statements outside of functions, `labels` to verify that
  136. // `break` and `continue` have somewhere to jump to, and `strict`
  137. // indicates whether strict mode is on.
  138. var inFunction, labels, strict;
  139. // This function is used to raise exceptions on parse errors. It
  140. // takes either a `{line, column}` object or an offset integer (into
  141. // the current `input`) as `pos` argument. It attaches the position
  142. // to the end of the error message, and then raises a `SyntaxError`
  143. // with that message.
  144. function raise(pos, message) {
  145. if (typeof pos == "number") pos = getLineInfo(input, pos);
  146. message += " (" + pos.line + ":" + pos.column + ")";
  147. throw new SyntaxError(message);
  148. }
  149. // ## Token types
  150. // The assignment of fine-grained, information-carrying type objects
  151. // allows the tokenizer to store the information it has about a
  152. // token in a way that is very cheap for the parser to look up.
  153. // All token type variables start with an underscore, to make them
  154. // easy to recognize.
  155. // These are the general types. The `type` property is only used to
  156. // make them recognizeable when debugging.
  157. var _num = {type: "num"}, _regexp = {type: "regexp"}, _string = {type: "string"};
  158. var _name = {type: "name"}, _eof = {type: "eof"};
  159. // Keyword tokens. The `keyword` property (also used in keyword-like
  160. // operators) indicates that the token originated from an
  161. // identifier-like word, which is used when parsing property names.
  162. //
  163. // The `beforeExpr` property is used to disambiguate between regular
  164. // expressions and divisions. It is set on all token types that can
  165. // be followed by an expression (thus, a slash after them would be a
  166. // regular expression).
  167. //
  168. // `isLoop` marks a keyword as starting a loop, which is important
  169. // to know when parsing a label, in order to allow or disallow
  170. // continue jumps to that label.
  171. var _break = {keyword: "break"}, _case = {keyword: "case", beforeExpr: true}, _catch = {keyword: "catch"};
  172. var _continue = {keyword: "continue"}, _debugger = {keyword: "debugger"}, _default = {keyword: "default"};
  173. var _do = {keyword: "do", isLoop: true}, _else = {keyword: "else", beforeExpr: true};
  174. var _finally = {keyword: "finally"}, _for = {keyword: "for", isLoop: true}, _function = {keyword: "function"};
  175. var _if = {keyword: "if"}, _return = {keyword: "return", beforeExpr: true}, _switch = {keyword: "switch"};
  176. var _throw = {keyword: "throw", beforeExpr: true}, _try = {keyword: "try"}, _var = {keyword: "var"};
  177. var _while = {keyword: "while", isLoop: true}, _with = {keyword: "with"}, _new = {keyword: "new", beforeExpr: true};
  178. var _this = {keyword: "this"};
  179. // The keywords that denote values.
  180. var _null = {keyword: "null", atomValue: null}, _true = {keyword: "true", atomValue: true};
  181. var _false = {keyword: "false", atomValue: false};
  182. // Some keywords are treated as regular operators. `in` sometimes
  183. // (when parsing `for`) needs to be tested against specifically, so
  184. // we assign a variable name to it for quick comparing.
  185. var _in = {keyword: "in", binop: 7, beforeExpr: true};
  186. // Map keyword names to token types.
  187. var keywordTypes = {"break": _break, "case": _case, "catch": _catch,
  188. "continue": _continue, "debugger": _debugger, "default": _default,
  189. "do": _do, "else": _else, "finally": _finally, "for": _for,
  190. "function": _function, "if": _if, "return": _return, "switch": _switch,
  191. "throw": _throw, "try": _try, "var": _var, "while": _while, "with": _with,
  192. "null": _null, "true": _true, "false": _false, "new": _new, "in": _in,
  193. "instanceof": {keyword: "instanceof", binop: 7}, "this": _this,
  194. "typeof": {keyword: "typeof", prefix: true},
  195. "void": {keyword: "void", prefix: true},
  196. "delete": {keyword: "delete", prefix: true}};
  197. // Punctuation token types. Again, the `type` property is purely for debugging.
  198. var _bracketL = {type: "[", beforeExpr: true}, _bracketR = {type: "]"}, _braceL = {type: "{", beforeExpr: true};
  199. var _braceR = {type: "}"}, _parenL = {type: "(", beforeExpr: true}, _parenR = {type: ")"};
  200. var _comma = {type: ",", beforeExpr: true}, _semi = {type: ";", beforeExpr: true};
  201. var _colon = {type: ":", beforeExpr: true}, _dot = {type: "."}, _question = {type: "?", beforeExpr: true};
  202. // Operators. These carry several kinds of properties to help the
  203. // parser use them properly (the presence of these properties is
  204. // what categorizes them as operators).
  205. //
  206. // `binop`, when present, specifies that this operator is a binary
  207. // operator, and will refer to its precedence.
  208. //
  209. // `prefix` and `postfix` mark the operator as a prefix or postfix
  210. // unary operator. `isUpdate` specifies that the node produced by
  211. // the operator should be of type UpdateExpression rather than
  212. // simply UnaryExpression (`++` and `--`).
  213. //
  214. // `isAssign` marks all of `=`, `+=`, `-=` etcetera, which act as
  215. // binary operators with a very low precedence, that should result
  216. // in AssignmentExpression nodes.
  217. var _slash = {binop: 10, beforeExpr: true}, _eq = {isAssign: true, beforeExpr: true};
  218. var _assign = {isAssign: true, beforeExpr: true}, _plusmin = {binop: 9, prefix: true, beforeExpr: true};
  219. var _incdec = {postfix: true, prefix: true, isUpdate: true}, _prefix = {prefix: true, beforeExpr: true};
  220. var _bin1 = {binop: 1, beforeExpr: true}, _bin2 = {binop: 2, beforeExpr: true};
  221. var _bin3 = {binop: 3, beforeExpr: true}, _bin4 = {binop: 4, beforeExpr: true};
  222. var _bin5 = {binop: 5, beforeExpr: true}, _bin6 = {binop: 6, beforeExpr: true};
  223. var _bin7 = {binop: 7, beforeExpr: true}, _bin8 = {binop: 8, beforeExpr: true};
  224. var _bin10 = {binop: 10, beforeExpr: true};
  225. // This is a trick taken from Esprima. It turns out that, on
  226. // non-Chrome browsers, to check whether a string is in a set, a
  227. // predicate containing a big ugly `switch` statement is faster than
  228. // a regular expression, and on Chrome the two are about on par.
  229. // This function uses `eval` (non-lexical) to produce such a
  230. // predicate from a space-separated string of words.
  231. //
  232. // It starts by sorting the words by length.
  233. function makePredicate(words) {
  234. words = words.split(" ");
  235. var f = "", cats = [];
  236. out: for (var i = 0; i < words.length; ++i) {
  237. for (var j = 0; j < cats.length; ++j)
  238. if (cats[j][0].length == words[i].length) {
  239. cats[j].push(words[i]);
  240. continue out;
  241. }
  242. cats.push([words[i]]);
  243. }
  244. function compareTo(arr) {
  245. if (arr.length == 1) return f += "return str === " + JSON.stringify(arr[0]) + ";";
  246. f += "switch(str){";
  247. for (var i = 0; i < arr.length; ++i) f += "case " + JSON.stringify(arr[i]) + ":";
  248. f += "return true}return false;";
  249. }
  250. // When there are more than three length categories, an outer
  251. // switch first dispatches on the lengths, to save on comparisons.
  252. if (cats.length > 3) {
  253. cats.sort(function(a, b) {return b.length - a.length;});
  254. f += "switch(str.length){";
  255. for (var i = 0; i < cats.length; ++i) {
  256. var cat = cats[i];
  257. f += "case " + cat[0].length + ":";
  258. compareTo(cat);
  259. }
  260. f += "}";
  261. // Otherwise, simply generate a flat `switch` statement.
  262. } else {
  263. compareTo(words);
  264. }
  265. return new Function("str", f);
  266. }
  267. // The ECMAScript 3 reserved word list.
  268. 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");
  269. // ECMAScript 5 reserved words.
  270. var isReservedWord5 = makePredicate("class enum extends super const export import");
  271. // The additional reserved words in strict mode.
  272. var isStrictReservedWord = makePredicate("implements interface let package private protected public static yield");
  273. // The forbidden variable names in strict mode.
  274. var isStrictBadIdWord = makePredicate("eval arguments");
  275. // And the keywords.
  276. 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");
  277. // ## Character categories
  278. // Big ugly regular expressions that match characters in the
  279. // whitespace, identifier, and identifier-start categories. These
  280. // are only applied when a character is found to actually have a
  281. // code point above 128.
  282. var nonASCIIwhitespace = /[\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/;
  283. 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";
  284. var nonASCIIidentifierChars = "\u0371-\u0374\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";
  285. var nonASCIIidentifierStart = new RegExp("[" + nonASCIIidentifierStartChars + "]");
  286. var nonASCIIidentifier = new RegExp("[" + nonASCIIidentifierStartChars + nonASCIIidentifierChars + "]");
  287. // Whether a single character denotes a newline.
  288. var newline = /[\n\r\u2028\u2029]/;
  289. // Matches a whole line break (where CRLF is considered a single
  290. // line break). Used to count lines.
  291. var lineBreak = /\r\n|[\n\r\u2028\u2029]/g;
  292. // Test whether a given character code starts an identifier.
  293. function isIdentifierStart(code) {
  294. if (code < 65) return code === 36;
  295. if (code < 91) return true;
  296. if (code < 97) return code === 95;
  297. if (code < 123)return true;
  298. return code >= 0xaa && nonASCIIidentifierStart.test(String.fromCharCode(code));
  299. }
  300. // Test whether a given character is part of an identifier.
  301. function isIdentifierChar(code) {
  302. if (code < 48) return code === 36;
  303. if (code < 58) return true;
  304. if (code < 65) return false;
  305. if (code < 91) return true;
  306. if (code < 97) return code === 95;
  307. if (code < 123)return true;
  308. return code >= 0xaa && nonASCIIidentifier.test(String.fromCharCode(code));
  309. }
  310. // ## Tokenizer
  311. // These are used when `options.locations` is on, in order to track
  312. // the current line number and start of line offset, in order to set
  313. // `tokStartLoc` and `tokEndLoc`.
  314. function nextLineStart() {
  315. lineBreak.lastIndex = tokLineStart;
  316. var match = lineBreak.exec(input);
  317. return match ? match.index + match[0].length : input.length + 1;
  318. }
  319. function curLineLoc() {
  320. while (tokLineStartNext <= tokPos) {
  321. ++tokCurLine;
  322. tokLineStart = tokLineStartNext;
  323. tokLineStartNext = nextLineStart();
  324. }
  325. return {line: tokCurLine, column: tokPos - tokLineStart};
  326. }
  327. // Reset the token state. Used at the start of a parse.
  328. function initTokenState() {
  329. tokCurLine = 1;
  330. tokPos = tokLineStart = 0;
  331. tokLineStartNext = nextLineStart();
  332. tokRegexpAllowed = true;
  333. tokComments = null;
  334. skipSpace();
  335. }
  336. // Called at the end of every token. Sets `tokEnd`, `tokVal`,
  337. // `tokCommentsAfter`, and `tokRegexpAllowed`, and skips the space
  338. // after the token, so that the next one's `tokStart` will point at
  339. // the right position.
  340. function finishToken(type, val) {
  341. tokEnd = tokPos;
  342. if (options.locations) tokEndLoc = curLineLoc();
  343. tokType = type;
  344. skipSpace();
  345. tokVal = val;
  346. tokCommentsAfter = tokComments;
  347. tokRegexpAllowed = type.beforeExpr;
  348. }
  349. function skipBlockComment() {
  350. var end = input.indexOf("*/", tokPos += 2);
  351. if (end === -1) raise(tokPos - 2, "Unterminated comment");
  352. if (options.trackComments)
  353. (tokComments || (tokComments = [])).push(input.slice(tokPos, end));
  354. tokPos = end + 2;
  355. }
  356. function skipLineComment() {
  357. var start = tokPos;
  358. var ch = input.charCodeAt(tokPos+=2);
  359. while (tokPos < inputLen && ch !== 10 && ch !== 13 && ch !== 8232 && ch !== 8329) {
  360. ++tokPos;
  361. ch = input.charCodeAt(tokPos);
  362. }
  363. (tokComments || (tokComments = [])).push(input.slice(start, tokPos));
  364. }
  365. // Called at the start of the parse and after every token. Skips
  366. // whitespace and comments, and, if `options.trackComments` is on,
  367. // will store all skipped comments in `tokComments`.
  368. function skipSpace() {
  369. tokComments = null;
  370. while (tokPos < inputLen) {
  371. var ch = input.charCodeAt(tokPos);
  372. if (ch === 47) { // '/'
  373. var next = input.charCodeAt(tokPos+1);
  374. if (next === 42) { // '*'
  375. skipBlockComment();
  376. } else if (next === 47) { // '/'
  377. skipLineComment();
  378. } else break;
  379. } else if (ch < 14 && ch > 8) {
  380. ++tokPos;
  381. } else if (ch === 32 || ch === 160) { // ' ', '\xa0'
  382. ++tokPos;
  383. } else if (ch >= 5760 && nonASCIIwhitespace.test(String.fromCharCode(ch))) {
  384. ++tokPos;
  385. } else {
  386. break;
  387. }
  388. }
  389. }
  390. // ### Token reading
  391. // This is the function that is called to fetch the next token. It
  392. // is somewhat obscure, because it works in character codes rather
  393. // than characters, and because operator parsing has been inlined
  394. // into it.
  395. //
  396. // All in the name of speed.
  397. //
  398. // The `forceRegexp` parameter is used in the one case where the
  399. // `tokRegexpAllowed` trick does not work. See `parseStatement`.
  400. function readToken(forceRegexp) {
  401. tokStart = tokPos;
  402. if (options.locations) tokStartLoc = curLineLoc();
  403. tokCommentsBefore = tokComments;
  404. if (forceRegexp) return readRegexp();
  405. if (tokPos >= inputLen) return finishToken(_eof);
  406. var code = input.charCodeAt(tokPos);
  407. // Identifier or keyword. '\uXXXX' sequences are allowed in
  408. // identifiers, so '\' also dispatches to that.
  409. if (isIdentifierStart(code) || code === 92 /* '\' */) return readWord();
  410. var next = input.charCodeAt(tokPos+1);
  411. switch(code) {
  412. // The interpretation of a dot depends on whether it is followed
  413. // by a digit.
  414. case 46: // '.'
  415. if (next >= 48 && next <= 57) return readNumber(String.fromCharCode(code));
  416. ++tokPos;
  417. return finishToken(_dot);
  418. // Punctuation tokens.
  419. case 40: ++tokPos; return finishToken(_parenL);
  420. case 41: ++tokPos; return finishToken(_parenR);
  421. case 59: ++tokPos; return finishToken(_semi);
  422. case 44: ++tokPos; return finishToken(_comma);
  423. case 91: ++tokPos; return finishToken(_bracketL);
  424. case 93: ++tokPos; return finishToken(_bracketR);
  425. case 123: ++tokPos; return finishToken(_braceL);
  426. case 125: ++tokPos; return finishToken(_braceR);
  427. case 58: ++tokPos; return finishToken(_colon);
  428. case 63: ++tokPos; return finishToken(_question);
  429. // '0x' is a hexadecimal number.
  430. case 48: // '0'
  431. if (next === 120 || next === 88) return readHexNumber();
  432. // Anything else beginning with a digit is an integer, octal
  433. // number, or float.
  434. case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: // 1-9
  435. return readNumber(String.fromCharCode(code));
  436. // Quotes produce strings.
  437. case 34: case 39: // '"', "'"
  438. return readString(code);
  439. // Operators are parsed inline in tiny state machines. '=' (61) is
  440. // often referred to. `finishOp` simply skips the amount of
  441. // characters it is given as second argument, and returns a token
  442. // of the type given by its first argument.
  443. case 47: // '/'
  444. if (tokRegexpAllowed) {++tokPos; return readRegexp();}
  445. if (next === 61) return finishOp(_assign, 2);
  446. return finishOp(_slash, 1);
  447. case 37: case 42: // '%*'
  448. if (next === 61) return finishOp(_assign, 2);
  449. return finishOp(_bin10, 1);
  450. case 124: case 38: // '|&'
  451. if (next === code) return finishOp(code === 124 ? _bin1 : _bin2, 2);
  452. if (next === 61) return finishOp(_assign, 2);
  453. return finishOp(code === 124 ? _bin3 : _bin5, 1);
  454. case 94: // '^'
  455. if (next === 61) return finishOp(_assign, 2);
  456. return finishOp(_bin4, 1);
  457. case 43: case 45: // '+-'
  458. if (next === code) return finishOp(_incdec, 2);
  459. if (next === 61) return finishOp(_assign, 2);
  460. return finishOp(_plusmin, 1);
  461. case 60: case 62: // '<>'
  462. var size = 1;
  463. if (next === code) {
  464. size = code === 62 && input.charCodeAt(tokPos+2) === 62 ? 3 : 2;
  465. if (input.charCodeAt(tokPos + size) === 61) return finishOp(_assign, size + 1);
  466. return finishOp(_bin8, size);
  467. }
  468. if (next === 61)
  469. size = input.charCodeAt(tokPos+2) === 61 ? 3 : 2;
  470. return finishOp(_bin7, size);
  471. case 61: case 33: // '=!'
  472. if (next === 61) return finishOp(_bin6, input.charCodeAt(tokPos+2) === 61 ? 3 : 2);
  473. return finishOp(code === 61 ? _eq : _prefix, 1);
  474. case 126: // '~'
  475. return finishOp(_prefix, 1);
  476. }
  477. // If we are here, we either found a non-ASCII identifier
  478. // character, or something that's entirely disallowed.
  479. var ch = String.fromCharCode(code);
  480. if (ch === "\\" || nonASCIIidentifierStart.test(ch)) return readWord();
  481. raise(tokPos, "Unexpected character '" + ch + "'");
  482. }
  483. function finishOp(type, size) {
  484. var str = input.slice(tokPos, tokPos + size);
  485. tokPos += size;
  486. finishToken(type, str);
  487. }
  488. // Parse a regular expression. Some context-awareness is necessary,
  489. // since a '/' inside a '[]' set does not end the expression.
  490. function readRegexp() {
  491. var content = "", escaped, inClass, start = tokPos;
  492. for (;;) {
  493. if (tokPos >= inputLen) raise(start, "Unterminated regular expression");
  494. var ch = input.charAt(tokPos);
  495. if (newline.test(ch)) raise(start, "Unterminated regular expression");
  496. if (!escaped) {
  497. if (ch === "[") inClass = true;
  498. else if (ch === "]" && inClass) inClass = false;
  499. else if (ch === "/" && !inClass) break;
  500. escaped = ch === "\\";
  501. } else escaped = false;
  502. ++tokPos;
  503. }
  504. var content = input.slice(start, tokPos);
  505. ++tokPos;
  506. // Need to use `readWord1` because '\uXXXX' sequences are allowed
  507. // here (don't ask).
  508. var mods = readWord1();
  509. if (mods && !/^[gmsiy]*$/.test(mods)) raise(start, "Invalid regexp flag");
  510. return finishToken(_regexp, new RegExp(content, mods));
  511. }
  512. // Read an integer in the given radix. Return null if zero digits
  513. // were read, the integer value otherwise. When `len` is given, this
  514. // will return `null` unless the integer has exactly `len` digits.
  515. function readInt(radix, len) {
  516. var start = tokPos, total = 0;
  517. for (;;) {
  518. var code = input.charCodeAt(tokPos), val;
  519. if (code >= 97) val = code - 97 + 10; // a
  520. else if (code >= 65) val = code - 65 + 10; // A
  521. else if (code >= 48 && code <= 57) val = code - 48; // 0-9
  522. else val = Infinity;
  523. if (val >= radix) break;
  524. ++tokPos;
  525. total = total * radix + val;
  526. }
  527. if (tokPos === start || len != null && tokPos - start !== len) return null;
  528. return total;
  529. }
  530. function readHexNumber() {
  531. tokPos += 2; // 0x
  532. var val = readInt(16);
  533. if (val == null) raise(tokStart + 2, "Expected hexadecimal number");
  534. if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number");
  535. return finishToken(_num, val);
  536. }
  537. // Read an integer, octal integer, or floating-point number.
  538. function readNumber(ch) {
  539. var start = tokPos, isFloat = ch === ".";
  540. if (!isFloat && readInt(10) == null) raise(start, "Invalid number");
  541. if (isFloat || input.charAt(tokPos) === ".") {
  542. var next = input.charAt(++tokPos);
  543. if (next === "-" || next === "+") ++tokPos;
  544. if (readInt(10) === null && ch === ".") raise(start, "Invalid number");
  545. isFloat = true;
  546. }
  547. if (/e/i.test(input.charAt(tokPos))) {
  548. var next = input.charAt(++tokPos);
  549. if (next === "-" || next === "+") ++tokPos;
  550. if (readInt(10) === null) raise(start, "Invalid number")
  551. isFloat = true;
  552. }
  553. if (isIdentifierStart(input.charCodeAt(tokPos))) raise(tokPos, "Identifier directly after number");
  554. var str = input.slice(start, tokPos), val;
  555. if (isFloat) val = parseFloat(str);
  556. else if (ch !== "0" || str.length === 1) val = parseInt(str, 10);
  557. else if (/[89]/.test(str) || strict) raise(start, "Invalid number");
  558. else val = parseInt(str, 8);
  559. return finishToken(_num, val);
  560. }
  561. // Read a string value, interpreting backslash-escapes.
  562. function readString(quote) {
  563. tokPos++;
  564. var str = [];
  565. for (;;) {
  566. if (tokPos >= inputLen) raise(tokStart, "Unterminated string constant");
  567. var ch = input.charCodeAt(tokPos);
  568. if (ch === quote) {
  569. ++tokPos;
  570. return finishToken(_string, String.fromCharCode.apply(null, str));
  571. }
  572. if (ch === 92) { // '\'
  573. ch = input.charCodeAt(++tokPos);
  574. var octal = /^[0-7]+/.exec(input.slice(tokPos, tokPos + 3));
  575. if (octal) octal = octal[0];
  576. while (octal && parseInt(octal, 8) > 255) octal = octal.slice(0, octal.length - 1);
  577. if (octal === "0") octal = null;
  578. ++tokPos;
  579. if (octal) {
  580. if (strict) raise(tokPos - 2, "Octal literal in strict mode");
  581. str.push(parseInt(octal, 8));
  582. tokPos += octal.length - 1;
  583. } else {
  584. switch (ch) {
  585. case 110: str.push(10); break; // 'n' -> '\n'
  586. case 114: str.push(13); break; // 'r' -> '\r'
  587. case 120: str.push(readHexChar(2)); break; // 'x'
  588. case 117: str.push(readHexChar(4)); break; // 'u'
  589. case 85: str.push(readHexChar(8)); break; // 'U'
  590. case 116: str.push(9); break; // 't' -> '\t'
  591. case 98: str.push(8); break; // 'b' -> '\b'
  592. case 118: str.push(11); break; // 'v' -> '\u000b'
  593. case 102: str.push(12); break; // 'f' -> '\f'
  594. case 48: str.push(0); break; // 0 -> '\0'
  595. case 13: if (input.charCodeAt(tokPos) === 10) ++tokPos; // '\r\n'
  596. case 10: break; // ' \n'
  597. default: str.push(ch); break;
  598. }
  599. }
  600. } else {
  601. if (ch === 13 || ch === 10 || ch === 8232 || ch === 8329) raise(tokStart, "Unterminated string constant");
  602. if (ch !== 92) str.push(ch); // '\'
  603. ++tokPos;
  604. }
  605. }
  606. }
  607. // Used to read character escape sequences ('\x', '\u', '\U').
  608. function readHexChar(len) {
  609. var n = readInt(16, len);
  610. if (n === null) raise(tokStart, "Bad character escape sequence");
  611. return n;
  612. }
  613. // Used to signal to callers of `readWord1` whether the word
  614. // contained any escape sequences. This is needed because words with
  615. // escape sequences must not be interpreted as keywords.
  616. var containsEsc;
  617. // Read an identifier, and return it as a string. Sets `containsEsc`
  618. // to whether the word contained a '\u' escape.
  619. //
  620. // Only builds up the word character-by-character when it actually
  621. // containeds an escape, as a micro-optimization.
  622. function readWord1() {
  623. containsEsc = false;
  624. var word, first = true, start = tokPos;
  625. for (;;) {
  626. var ch = input.charCodeAt(tokPos);
  627. if (isIdentifierChar(ch)) {
  628. if (containsEsc) word += input.charAt(tokPos);
  629. ++tokPos;
  630. } else if (ch === 92) { // "\"
  631. if (!containsEsc) word = input.slice(start, tokPos);
  632. containsEsc = true;
  633. if (input.charCodeAt(++tokPos) != 117) // "u"
  634. raise(tokPos, "Expecting Unicode escape sequence \\uXXXX");
  635. ++tokPos;
  636. var esc = readHexChar(4);
  637. var escStr = String.fromCharCode(esc);
  638. if (!escStr) raise(tokPos - 1, "Invalid Unicode escape");
  639. if (!(first ? isIdentifierStart(esc) : isIdentifierChar(esc)))
  640. raise(tokPos - 4, "Invalid Unicode escape");
  641. word += escStr;
  642. } else {
  643. break;
  644. }
  645. first = false;
  646. }
  647. return containsEsc ? word : input.slice(start, tokPos);
  648. }
  649. // Read an identifier or keyword token. Will check for reserved
  650. // words when necessary.
  651. function readWord() {
  652. var word = readWord1();
  653. var type = _name;
  654. if (!containsEsc) {
  655. if (isKeyword(word)) type = keywordTypes[word];
  656. else if (options.forbidReserved &&
  657. (options.ecmaVersion === 3 ? isReservedWord3 : isReservedWord5)(word) ||
  658. strict && isStrictReservedWord(word))
  659. raise(tokStart, "The keyword '" + word + "' is reserved");
  660. }
  661. return finishToken(type, word);
  662. }
  663. // ## Parser
  664. // A recursive descent parser operates by defining functions for all
  665. // syntactic elements, and recursively calling those, each function
  666. // advancing the input stream and returning an AST node. Precedence
  667. // of constructs (for example, the fact that `!x[1]` means `!(x[1])`
  668. // instead of `(!x)[1]` is handled by the fact that the parser
  669. // function that parses unary prefix operators is called first, and
  670. // in turn calls the function that parses `[]` subscripts — that
  671. // way, it'll receive the node for `x[1]` already parsed, and wraps
  672. // *that* in the unary operator node.
  673. //
  674. // Acorn uses an [operator precedence parser][opp] to handle binary
  675. // operator precedence, because it is much more compact than using
  676. // the technique outlined above, which uses different, nesting
  677. // functions to specify precedence, for all of the ten binary
  678. // precedence levels that JavaScript defines.
  679. //
  680. // [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
  681. // ### Parser utilities
  682. // Continue to the next token.
  683. function next() {
  684. lastStart = tokStart;
  685. lastEnd = tokEnd;
  686. lastEndLoc = tokEndLoc;
  687. readToken();
  688. }
  689. // Enter strict mode. Re-reads the next token to please pedantic
  690. // tests ("use strict"; 010; -- should fail).
  691. function setStrict(strct) {
  692. strict = strct;
  693. tokPos = lastEnd;
  694. skipSpace();
  695. readToken();
  696. }
  697. // Start an AST node, attaching a start offset and optionally a
  698. // `commentsBefore` property to it.
  699. function startNode() {
  700. var node = {type: null, start: tokStart, end: null};
  701. if (options.trackComments && tokCommentsBefore) {
  702. node.commentsBefore = tokCommentsBefore;
  703. tokCommentsBefore = null;
  704. }
  705. if (options.locations)
  706. node.loc = {start: tokStartLoc, end: null, source: sourceFile};
  707. if (options.ranges)
  708. node.range = [tokStart, 0];
  709. return node;
  710. }
  711. // Start a node whose start offset/comments information should be
  712. // based on the start of another node. For example, a binary
  713. // operator node is only started after its left-hand side has
  714. // already been parsed.
  715. function startNodeFrom(other) {
  716. var node = {type: null, start: other.start};
  717. if (other.commentsBefore) {
  718. node.commentsBefore = other.commentsBefore;
  719. other.commentsBefore = null;
  720. }
  721. if (options.locations)
  722. node.loc = {start: other.loc.start, end: null, source: other.loc.source};
  723. if (options.ranges)
  724. node.range = [other.range[0], 0];
  725. return node;
  726. }
  727. // Finish an AST node, adding `type`, `end`, and `commentsAfter`
  728. // properties.
  729. //
  730. // We keep track of the last node that we finished, in order
  731. // 'bubble' `commentsAfter` properties up to the biggest node. I.e.
  732. // in '`1 + 1 // foo', the comment should be attached to the binary
  733. // operator node, not the second literal node.
  734. var lastFinishedNode;
  735. function finishNode(node, type) {
  736. node.type = type;
  737. node.end = lastEnd;
  738. if (options.trackComments) {
  739. if (tokCommentsAfter) {
  740. node.commentsAfter = tokCommentsAfter;
  741. tokCommentsAfter = null;
  742. } else if (lastFinishedNode && lastFinishedNode.end === lastEnd) {
  743. node.commentsAfter = lastFinishedNode.commentsAfter;
  744. lastFinishedNode.commentsAfter = null;
  745. }
  746. lastFinishedNode = node;
  747. }
  748. if (options.locations)
  749. node.loc.end = lastEndLoc;
  750. if (options.ranges)
  751. node.range[1] = lastEnd;
  752. return node;
  753. }
  754. // Test whether a statement node is the string literal `"use strict"`.
  755. function isUseStrict(stmt) {
  756. return options.ecmaVersion >= 5 && stmt.type === "ExpressionStatement" &&
  757. stmt.expression.type === "Literal" && stmt.expression.value === "use strict";
  758. }
  759. // Predicate that tests whether the next token is of the given
  760. // type, and if yes, consumes it as a side effect.
  761. function eat(type) {
  762. if (tokType === type) {
  763. next();
  764. return true;
  765. }
  766. }
  767. // Test whether a semicolon can be inserted at the current position.
  768. function canInsertSemicolon() {
  769. return !options.strictSemicolons &&
  770. (tokType === _eof || tokType === _braceR || newline.test(input.slice(lastEnd, tokStart)));
  771. }
  772. // Consume a semicolon, or, failing that, see if we are allowed to
  773. // pretend that there is a semicolon at this position.
  774. function semicolon() {
  775. if (!eat(_semi) && !canInsertSemicolon()) unexpected();
  776. }
  777. // Expect a token of a given type. If found, consume it, otherwise,
  778. // raise an unexpected token error.
  779. function expect(type) {
  780. if (tokType === type) next();
  781. else unexpected();
  782. }
  783. // Raise an unexpected token error.
  784. function unexpected() {
  785. raise(tokStart, "Unexpected token");
  786. }
  787. // Verify that a node is an lval — something that can be assigned
  788. // to.
  789. function checkLVal(expr) {
  790. if (expr.type !== "Identifier" && expr.type !== "MemberExpression")
  791. raise(expr.start, "Assigning to rvalue");
  792. if (strict && expr.type === "Identifier" && isStrictBadIdWord(expr.name))
  793. raise(expr.start, "Assigning to " + expr.name + " in strict mode");
  794. }
  795. // ### Statement parsing
  796. // Parse a program. Initializes the parser, reads any number of
  797. // statements, and wraps them in a Program node. Optionally takes a
  798. // `program` argument. If present, the statements will be appended
  799. // to its body instead of creating a new node.
  800. function parseTopLevel(program) {
  801. initTokenState();
  802. lastStart = lastEnd = tokPos;
  803. if (options.locations) lastEndLoc = curLineLoc();
  804. inFunction = strict = null;
  805. labels = [];
  806. readToken();
  807. var node = program || startNode(), first = true;
  808. if (!program) node.body = [];
  809. while (tokType !== _eof) {
  810. var stmt = parseStatement();
  811. node.body.push(stmt);
  812. if (first && isUseStrict(stmt)) setStrict(true);
  813. first = false;
  814. }
  815. return finishNode(node, "Program");
  816. };
  817. var loopLabel = {kind: "loop"}, switchLabel = {kind: "switch"};
  818. // Parse a single statement.
  819. //
  820. // If expecting a statement and finding a slash operator, parse a
  821. // regular expression literal. This is to handle cases like
  822. // `if (foo) /blah/.exec(foo);`, where looking at the previous token
  823. // does not help.
  824. function parseStatement() {
  825. if (tokType === _slash)
  826. readToken(true);
  827. var starttype = tokType, node = startNode();
  828. // Most types of statements are recognized by the keyword they
  829. // start with. Many are trivial to parse, some require a bit of
  830. // complexity.
  831. switch (starttype) {
  832. case _break: case _continue:
  833. next();
  834. var isBreak = starttype === _break;
  835. if (eat(_semi) || canInsertSemicolon()) node.label = null;
  836. else if (tokType !== _name) unexpected();
  837. else {
  838. node.label = parseIdent();
  839. semicolon();
  840. }
  841. // Verify that there is an actual destination to break or
  842. // continue to.
  843. for (var i = 0; i < labels.length; ++i) {
  844. var lab = labels[i];
  845. if (node.label == null || lab.name === node.label.name) {
  846. if (lab.kind != null && (isBreak || lab.kind === "loop")) break;
  847. if (node.label && isBreak) break;
  848. }
  849. }
  850. if (i === labels.length) raise(node.start, "Unsyntactic " + starttype.keyword);
  851. return finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement");
  852. case _debugger:
  853. next();
  854. return finishNode(node, "DebuggerStatement");
  855. case _do:
  856. next();
  857. labels.push(loopLabel);
  858. node.body = parseStatement();
  859. labels.pop();
  860. expect(_while);
  861. node.test = parseParenExpression();
  862. semicolon();
  863. return finishNode(node, "DoWhileStatement");
  864. // Disambiguating between a `for` and a `for`/`in` loop is
  865. // non-trivial. Basically, we have to parse the init `var`
  866. // statement or expression, disallowing the `in` operator (see
  867. // the second parameter to `parseExpression`), and then check
  868. // whether the next token is `in`. When there is no init part
  869. // (semicolon immediately after the opening parenthesis), it is
  870. // a regular `for` loop.
  871. case _for:
  872. next();
  873. labels.push(loopLabel);
  874. expect(_parenL);
  875. if (tokType === _semi) return parseFor(node, null);
  876. if (tokType === _var) {
  877. var init = startNode();
  878. next();
  879. parseVar(init, true);
  880. if (init.declarations.length === 1 && eat(_in))
  881. return parseForIn(node, init);
  882. return parseFor(node, init);
  883. }
  884. var init = parseExpression(false, true);
  885. if (eat(_in)) {checkLVal(init); return parseForIn(node, init);}
  886. return parseFor(node, init);
  887. case _function:
  888. next();
  889. return parseFunction(node, true);
  890. case _if:
  891. next();
  892. node.test = parseParenExpression();
  893. node.consequent = parseStatement();
  894. node.alternate = eat(_else) ? parseStatement() : null;
  895. return finishNode(node, "IfStatement");
  896. case _return:
  897. if (!inFunction) raise(tokStart, "'return' outside of function");
  898. next();
  899. // In `return` (and `break`/`continue`), the keywords with
  900. // optional arguments, we eagerly look for a semicolon or the
  901. // possibility to insert one.
  902. if (eat(_semi) || canInsertSemicolon()) node.argument = null;
  903. else { node.argument = parseExpression(); semicolon(); }
  904. return finishNode(node, "ReturnStatement");
  905. case _switch:
  906. next();
  907. node.discriminant = parseParenExpression();
  908. node.cases = [];
  909. expect(_braceL);
  910. labels.push(switchLabel);
  911. // Statements under must be grouped (by label) in SwitchCase
  912. // nodes. `cur` is used to keep the node that we are currently
  913. // adding statements to.
  914. for (var cur, sawDefault; tokType != _braceR;) {
  915. if (tokType === _case || tokType === _default) {
  916. var isCase = tokType === _case;
  917. if (cur) finishNode(cur, "SwitchCase");
  918. node.cases.push(cur = startNode());
  919. cur.consequent = [];
  920. next();
  921. if (isCase) cur.test = parseExpression();
  922. else {
  923. if (sawDefault) raise(lastStart, "Multiple default clauses"); sawDefault = true;
  924. cur.test = null;
  925. }
  926. expect(_colon);
  927. } else {
  928. if (!cur) unexpected();
  929. cur.consequent.push(parseStatement());
  930. }
  931. }
  932. if (cur) finishNode(cur, "SwitchCase");
  933. next(); // Closing brace
  934. labels.pop();
  935. return finishNode(node, "SwitchStatement");
  936. case _throw:
  937. next();
  938. if (newline.test(input.slice(lastEnd, tokStart)))
  939. raise(lastEnd, "Illegal newline after throw");
  940. node.argument = parseExpression();
  941. return finishNode(node, "ThrowStatement");
  942. case _try:
  943. next();
  944. node.block = parseBlock();
  945. node.handlers = [];
  946. while (tokType === _catch) {
  947. var clause = startNode();
  948. next();
  949. expect(_parenL);
  950. clause.param = parseIdent();
  951. if (strict && isStrictBadIdWord(clause.param.name))
  952. raise(clause.param.start, "Binding " + clause.param.name + " in strict mode");
  953. expect(_parenR);
  954. clause.guard = null;
  955. clause.body = parseBlock();
  956. node.handlers.push(finishNode(clause, "CatchClause"));
  957. }
  958. node.finalizer = eat(_finally) ? parseBlock() : null;
  959. if (!node.handlers.length && !node.finalizer)
  960. raise(node.start, "Missing catch or finally clause");
  961. return finishNode(node, "TryStatement");
  962. case _var:
  963. next();
  964. node = parseVar(node);
  965. semicolon();
  966. return node;
  967. case _while:
  968. next();
  969. node.test = parseParenExpression();
  970. labels.push(loopLabel);
  971. node.body = parseStatement();
  972. labels.pop();
  973. return finishNode(node, "WhileStatement");
  974. case _with:
  975. if (strict) raise(tokStart, "'with' in strict mode");
  976. next();
  977. node.object = parseParenExpression();
  978. node.body = parseStatement();
  979. return finishNode(node, "WithStatement");
  980. case _braceL:
  981. return parseBlock();
  982. case _semi:
  983. next();
  984. return finishNode(node, "EmptyStatement");
  985. // If the statement does not start with a statement keyword or a
  986. // brace, it's an ExpressionStatement or LabeledStatement. We
  987. // simply start parsing an expression, and afterwards, if the
  988. // next token is a colon and the expression was a simple
  989. // Identifier node, we switch to interpreting it as a label.
  990. default:
  991. var maybeName = tokVal, expr = parseExpression();
  992. if (starttype === _name && expr.type === "Identifier" && eat(_colon)) {
  993. for (var i = 0; i < labels.length; ++i)
  994. if (labels[i].name === maybeName) raise(expr.start, "Label '" + maybeName + "' is already declared");
  995. var kind = tokType.isLoop ? "loop" : tokType === _switch ? "switch" : null;
  996. labels.push({name: maybeName, kind: kind});
  997. node.body = parseStatement();
  998. node.label = expr;
  999. return finishNode(node, "LabeledStatement");
  1000. } else {
  1001. node.expression = expr;
  1002. semicolon();
  1003. return finishNode(node, "ExpressionStatement");
  1004. }
  1005. }
  1006. }
  1007. // Used for constructs like `switch` and `if` that insist on
  1008. // parentheses around their expression.
  1009. function parseParenExpression() {
  1010. expect(_parenL);
  1011. var val = parseExpression();
  1012. expect(_parenR);
  1013. return val;
  1014. }
  1015. // Parse a semicolon-enclosed block of statements, handling `"use
  1016. // strict"` declarations when `allowStrict` is true (used for
  1017. // function bodies).
  1018. function parseBlock(allowStrict) {
  1019. var node = startNode(), first = true, strict = false, oldStrict;
  1020. node.body = [];
  1021. expect(_braceL);
  1022. while (!eat(_braceR)) {
  1023. var stmt = parseStatement();
  1024. node.body.push(stmt);
  1025. if (first && isUseStrict(stmt)) {
  1026. oldStrict = strict;
  1027. setStrict(strict = true);
  1028. }
  1029. first = false
  1030. }
  1031. if (strict && !oldStrict) setStrict(false);
  1032. return finishNode(node, "BlockStatement");
  1033. }
  1034. // Parse a regular `for` loop. The disambiguation code in
  1035. // `parseStatement` will already have parsed the init statement or
  1036. // expression.
  1037. function parseFor(node, init) {
  1038. node.init = init;
  1039. expect(_semi);
  1040. node.test = tokType === _semi ? null : parseExpression();
  1041. expect(_semi);
  1042. node.update = tokType === _parenR ? null : parseExpression();
  1043. expect(_parenR);
  1044. node.body = parseStatement();
  1045. labels.pop();
  1046. return finishNode(node, "ForStatement");
  1047. }
  1048. // Parse a `for`/`in` loop.
  1049. function parseForIn(node, init) {
  1050. node.left = init;
  1051. node.right = parseExpression();
  1052. expect(_parenR);
  1053. node.body = parseStatement();
  1054. labels.pop();
  1055. return finishNode(node, "ForInStatement");
  1056. }
  1057. // Parse a list of variable declarations.
  1058. function parseVar(node, noIn) {
  1059. node.declarations = [];
  1060. node.kind = "var";
  1061. for (;;) {
  1062. var decl = startNode();
  1063. decl.id = parseIdent();
  1064. if (strict && isStrictBadIdWord(decl.id.name))
  1065. raise(decl.id.start, "Binding " + decl.id.name + " in strict mode");
  1066. decl.init = eat(_eq) ? parseExpression(true, noIn) : null;
  1067. node.declarations.push(finishNode(decl, "VariableDeclarator"));
  1068. if (!eat(_comma)) break;
  1069. }
  1070. return finishNode(node, "VariableDeclaration");
  1071. }
  1072. // ### Expression parsing
  1073. // These nest, from the most general expression type at the top to
  1074. // 'atomic', nondivisible expression types at the bottom. Most of
  1075. // the functions will simply let the function(s) below them parse,
  1076. // and, *if* the syntactic construct they handle is present, wrap
  1077. // the AST node that the inner parser gave them in another node.
  1078. // Parse a full expression. The arguments are used to forbid comma
  1079. // sequences (in argument lists, array literals, or object literals)
  1080. // or the `in` operator (in for loops initalization expressions).
  1081. function parseExpression(noComma, noIn) {
  1082. var expr = parseMaybeAssign(noIn);
  1083. if (!noComma && tokType === _comma) {
  1084. var node = startNodeFrom(expr);
  1085. node.expressions = [expr];
  1086. while (eat(_comma)) node.expressions.push(parseMaybeAssign(noIn));
  1087. return finishNode(node, "SequenceExpression");
  1088. }
  1089. return expr;
  1090. }
  1091. // Parse an assignment expression. This includes applications of
  1092. // operators like `+=`.
  1093. function parseMaybeAssign(noIn) {
  1094. var left = parseMaybeConditional(noIn);
  1095. if (tokType.isAssign) {
  1096. var node = startNodeFrom(left);
  1097. node.operator = tokVal;
  1098. node.left = left;
  1099. next();
  1100. node.right = parseMaybeAssign(noIn);
  1101. checkLVal(left);
  1102. return finishNode(node, "AssignmentExpression");
  1103. }
  1104. return left;
  1105. }
  1106. // Parse a ternary conditional (`?:`) operator.
  1107. function parseMaybeConditional(noIn) {
  1108. var expr = parseExprOps(noIn);
  1109. if (eat(_question)) {
  1110. var node = startNodeFrom(expr);
  1111. node.test = expr;
  1112. node.consequent = parseExpression(true);
  1113. expect(_colon);
  1114. node.alternate = parseExpression(true, noIn);
  1115. return finishNode(node, "ConditionalExpression");
  1116. }
  1117. return expr;
  1118. }
  1119. // Start the precedence parser.
  1120. function parseExprOps(noIn) {
  1121. return parseExprOp(parseMaybeUnary(noIn), -1, noIn);
  1122. }
  1123. // Parse binary operators with the operator precedence parsing
  1124. // algorithm. `left` is the left-hand side of the operator.
  1125. // `minPrec` provides context that allows the function to stop and
  1126. // defer further parser to one of its callers when it encounters an
  1127. // operator that has a lower precedence than the set it is parsing.
  1128. function parseExprOp(left, minPrec, noIn) {
  1129. var prec = tokType.binop;
  1130. if (prec != null && (!noIn || tokType !== _in)) {
  1131. if (prec > minPrec) {
  1132. var node = startNodeFrom(left);
  1133. node.left = left;
  1134. node.operator = tokVal;
  1135. next();
  1136. node.right = parseExprOp(parseMaybeUnary(noIn), prec, noIn);
  1137. var node = finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression");
  1138. return parseExprOp(node, minPrec, noIn);
  1139. }
  1140. }
  1141. return left;
  1142. }
  1143. // Parse unary operators, both prefix and postfix.
  1144. function parseMaybeUnary(noIn) {
  1145. if (tokType.prefix) {
  1146. var node = startNode(), update = tokType.isUpdate;
  1147. node.operator = tokVal;
  1148. node.prefix = true;
  1149. next();
  1150. node.argument = parseMaybeUnary(noIn);
  1151. if (update) checkLVal(node.argument);
  1152. else if (strict && node.operator === "delete" &&
  1153. node.argument.type === "Identifier")
  1154. raise(node.start, "Deleting local variable in strict mode");
  1155. return finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
  1156. }
  1157. var expr = parseExprSubscripts();
  1158. while (tokType.postfix && !canInsertSemicolon()) {
  1159. var node = startNodeFrom(expr);
  1160. node.operator = tokVal;
  1161. node.prefix = false;
  1162. node.argument = expr;
  1163. checkLVal(expr);
  1164. next();
  1165. expr = finishNode(node, "UpdateExpression");
  1166. }
  1167. return expr;
  1168. }
  1169. // Parse call, dot, and `[]`-subscript expressions.
  1170. function parseExprSubscripts() {
  1171. return parseSubscripts(parseExprAtom());
  1172. }
  1173. function parseSubscripts(base, noCalls) {
  1174. if (eat(_dot)) {
  1175. var node = startNodeFrom(base);
  1176. node.object = base;
  1177. node.property = parseIdent(true);
  1178. node.computed = false;
  1179. return parseSubscripts(finishNode(node, "MemberExpression"), noCalls);
  1180. } else if (eat(_bracketL)) {
  1181. var node = startNodeFrom(base);
  1182. node.object = base;
  1183. node.property = parseExpression();
  1184. node.computed = true;
  1185. expect(_bracketR);
  1186. return parseSubscripts(finishNode(node, "MemberExpression"), noCalls);
  1187. } else if (!noCalls && eat(_parenL)) {
  1188. var node = startNodeFrom(base);
  1189. node.callee = base;
  1190. node.arguments = parseExprList(_parenR, false);
  1191. return parseSubscripts(finishNode(node, "CallExpression"), noCalls);
  1192. } else return base;
  1193. }
  1194. // Parse an atomic expression — either a single token that is an
  1195. // expression, an expression started by a keyword like `function` or
  1196. // `new`, or an expression wrapped in punctuation like `()`, `[]`,
  1197. // or `{}`.
  1198. function parseExprAtom() {
  1199. switch (tokType) {
  1200. case _this:
  1201. var node = startNode();
  1202. next();
  1203. return finishNode(node, "ThisExpression");
  1204. case _name:
  1205. return parseIdent();
  1206. case _num: case _string: case _regexp:
  1207. var node = startNode();
  1208. node.value = tokVal;
  1209. node.raw = input.slice(tokStart, tokEnd);
  1210. next();
  1211. return finishNode(node, "Literal");
  1212. case _null: case _true: case _false:
  1213. var node = startNode();
  1214. node.value = tokType.atomValue;
  1215. next();
  1216. return finishNode(node, "Literal");
  1217. case _parenL:
  1218. next();
  1219. var val = parseExpression();
  1220. expect(_parenR);
  1221. return val;
  1222. case _bracketL:
  1223. var node = startNode();
  1224. next();
  1225. node.elements = parseExprList(_bracketR, true, true);
  1226. return finishNode(node, "ArrayExpression");
  1227. case _braceL:
  1228. return parseObj();
  1229. case _function:
  1230. var node = startNode();
  1231. next();
  1232. return parseFunction(node, false);
  1233. case _new:
  1234. return parseNew();
  1235. default:
  1236. unexpected();
  1237. }
  1238. }
  1239. // New's precedence is slightly tricky. It must allow its argument
  1240. // to be a `[]` or dot subscript expression, but not a call — at
  1241. // least, not without wrapping it in parentheses. Thus, it uses the
  1242. function parseNew() {
  1243. var node = startNode();
  1244. next();
  1245. node.callee = parseSubscripts(parseExprAtom(false), true);
  1246. if (eat(_parenL)) node.arguments = parseExprList(_parenR, false);
  1247. else node.arguments = [];
  1248. return finishNode(node, "NewExpression");
  1249. }
  1250. // Parse an object literal.
  1251. function parseObj() {
  1252. var node = startNode(), first = true, sawGetSet = false;
  1253. node.properties = [];
  1254. next();
  1255. while (!eat(_braceR)) {
  1256. if (!first) {
  1257. expect(_comma);
  1258. if (options.allowTrailingCommas && eat(_braceR)) break;
  1259. } else first = false;
  1260. var prop = {key: parsePropertyName()}, isGetSet = false, kind;
  1261. if (eat(_colon)) {
  1262. prop.value = parseExpression(true);
  1263. kind = prop.kind = "init";
  1264. } else if (options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
  1265. (prop.key.name === "get" || prop.key.name === "set")) {
  1266. isGetSet = sawGetSet = true;
  1267. kind = prop.kind = prop.key.name;
  1268. prop.key = parsePropertyName();
  1269. if (!tokType === _parenL) unexpected();
  1270. prop.value = parseFunction(startNode(), false);
  1271. } else unexpected();
  1272. // getters and setters are not allowed to clash — either with
  1273. // each other or with an init property — and in strict mode,
  1274. // init properties are also not allowed to be repeated.
  1275. if (prop.key.type === "Identifier" && (strict || sawGetSet)) {
  1276. for (var i = 0; i < node.properties.length; ++i) {
  1277. var other = node.properties[i];
  1278. if (other.key.name === prop.key.name) {
  1279. var conflict = kind == other.kind || isGetSet && other.kind === "init" ||
  1280. kind === "init" && (other.kind === "get" || other.kind === "set");
  1281. if (conflict && !strict && kind === "init" && other.kind === "init") conflict = false;
  1282. if (conflict) raise(prop.key.start, "Redefinition of property");
  1283. }
  1284. }
  1285. }
  1286. node.properties.push(prop);
  1287. }
  1288. return finishNode(node, "ObjectExpression");
  1289. }
  1290. function parsePropertyName() {
  1291. if (tokType === _num || tokType === _string) return parseExprAtom();
  1292. return parseIdent(true);
  1293. }
  1294. // Parse a function declaration or literal (depending on the
  1295. // `isStatement` parameter).
  1296. function parseFunction(node, isStatement) {
  1297. if (tokType === _name) node.id = parseIdent();
  1298. else if (isStatement) unexpected();
  1299. else node.id = null;
  1300. node.params = [];
  1301. var first = true;
  1302. expect(_parenL);
  1303. while (!eat(_parenR)) {
  1304. if (!first) expect(_comma); else first = false;
  1305. node.params.push(parseIdent());
  1306. }
  1307. // Start a new scope with regard to labels and the `inFunction`
  1308. // flag (restore them to their old value afterwards).
  1309. var oldInFunc = inFunction, oldLabels = labels;
  1310. inFunction = true; labels = [];
  1311. node.body = parseBlock(true);
  1312. inFunction = oldInFunc; labels = oldLabels;
  1313. // If this is a strict mode function, verify that argument names
  1314. // are not repeated, and it does not try to bind the words `eval`
  1315. // or `arguments`.
  1316. if (strict || node.body.body.length && isUseStrict(node.body.body[0])) {
  1317. for (var i = node.id ? -1 : 0; i < node.params.length; ++i) {
  1318. var id = i < 0 ? node.id : node.params[i];
  1319. if (isStrictReservedWord(id.name) || isStrictBadIdWord(id.name))
  1320. raise(id.start, "Defining '" + id.name + "' in strict mode");
  1321. if (i >= 0) for (var j = 0; j < i; ++j) if (id.name === node.params[j].name)
  1322. raise(id.start, "Argument name clash in strict mode");
  1323. }
  1324. }
  1325. return finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression");
  1326. }
  1327. // Parses a comma-separated list of expressions, and returns them as
  1328. // an array. `close` is the token type that ends the list, and
  1329. // `allowEmpty` can be turned on to allow subsequent commas with
  1330. // nothing in between them to be parsed as `null` (which is needed
  1331. // for array literals).
  1332. function parseExprList(close, allowTrailingComma, allowEmpty) {
  1333. var elts = [], first = true;
  1334. while (!eat(close)) {
  1335. if (!first) {
  1336. expect(_comma);
  1337. if (allowTrailingComma && options.allowTrailingCommas && eat(close)) break;
  1338. } else first = false;
  1339. if (allowEmpty && tokType === _comma) elts.push(null);
  1340. else elts.push(parseExpression(true));
  1341. }
  1342. return elts;
  1343. }
  1344. // Parse the next token as an identifier. If `liberal` is true (used
  1345. // when parsing properties), it will also convert keywords into
  1346. // identifiers.
  1347. function parseIdent(liberal) {
  1348. var node = startNode();
  1349. node.name = tokType === _name ? tokVal : (liberal && !options.forbidReserved && tokType.keyword) || unexpected();
  1350. next();
  1351. return finishNode(node, "Identifier");
  1352. }
  1353. })(typeof exports === "undefined" ? (window.acorn = {}) : exports);