[6a3a178] | 1 | /***********************************************************************
|
---|
| 2 |
|
---|
| 3 | A JavaScript tokenizer / parser / beautifier / compressor.
|
---|
| 4 | https://github.com/mishoo/UglifyJS2
|
---|
| 5 |
|
---|
| 6 | -------------------------------- (C) ---------------------------------
|
---|
| 7 |
|
---|
| 8 | Author: Mihai Bazon
|
---|
| 9 | <mihai.bazon@gmail.com>
|
---|
| 10 | http://mihai.bazon.net/blog
|
---|
| 11 |
|
---|
| 12 | Distributed under the BSD license:
|
---|
| 13 |
|
---|
| 14 | Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
|
---|
| 15 | Parser based on parse-js (http://marijn.haverbeke.nl/parse-js/).
|
---|
| 16 |
|
---|
| 17 | Redistribution and use in source and binary forms, with or without
|
---|
| 18 | modification, are permitted provided that the following conditions
|
---|
| 19 | are met:
|
---|
| 20 |
|
---|
| 21 | * Redistributions of source code must retain the above
|
---|
| 22 | copyright notice, this list of conditions and the following
|
---|
| 23 | disclaimer.
|
---|
| 24 |
|
---|
| 25 | * Redistributions in binary form must reproduce the above
|
---|
| 26 | copyright notice, this list of conditions and the following
|
---|
| 27 | disclaimer in the documentation and/or other materials
|
---|
| 28 | provided with the distribution.
|
---|
| 29 |
|
---|
| 30 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
|
---|
| 31 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 32 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
| 33 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
|
---|
| 34 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
---|
| 35 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 36 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
| 37 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 38 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
---|
| 39 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
---|
| 40 | THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 41 | SUCH DAMAGE.
|
---|
| 42 |
|
---|
| 43 | ***********************************************************************/
|
---|
| 44 |
|
---|
| 45 | "use strict";
|
---|
| 46 |
|
---|
| 47 | import {
|
---|
| 48 | characters,
|
---|
| 49 | defaults,
|
---|
| 50 | makePredicate,
|
---|
| 51 | set_annotation,
|
---|
| 52 | } from "./utils/index.js";
|
---|
| 53 | import {
|
---|
| 54 | AST_Accessor,
|
---|
| 55 | AST_Array,
|
---|
| 56 | AST_Arrow,
|
---|
| 57 | AST_Assign,
|
---|
| 58 | AST_Await,
|
---|
| 59 | AST_BigInt,
|
---|
| 60 | AST_Binary,
|
---|
| 61 | AST_BlockStatement,
|
---|
| 62 | AST_Break,
|
---|
| 63 | AST_Call,
|
---|
| 64 | AST_Case,
|
---|
| 65 | AST_Catch,
|
---|
| 66 | AST_Chain,
|
---|
| 67 | AST_ClassExpression,
|
---|
| 68 | AST_ClassPrivateProperty,
|
---|
| 69 | AST_ClassProperty,
|
---|
| 70 | AST_ConciseMethod,
|
---|
| 71 | AST_PrivateGetter,
|
---|
| 72 | AST_PrivateMethod,
|
---|
| 73 | AST_PrivateSetter,
|
---|
| 74 | AST_Conditional,
|
---|
| 75 | AST_Const,
|
---|
| 76 | AST_Continue,
|
---|
| 77 | AST_Debugger,
|
---|
| 78 | AST_Default,
|
---|
| 79 | AST_DefaultAssign,
|
---|
| 80 | AST_DefClass,
|
---|
| 81 | AST_Definitions,
|
---|
| 82 | AST_Defun,
|
---|
| 83 | AST_Destructuring,
|
---|
| 84 | AST_Directive,
|
---|
| 85 | AST_Do,
|
---|
| 86 | AST_Dot,
|
---|
| 87 | AST_DotHash,
|
---|
| 88 | AST_EmptyStatement,
|
---|
| 89 | AST_Expansion,
|
---|
| 90 | AST_Export,
|
---|
| 91 | AST_False,
|
---|
| 92 | AST_Finally,
|
---|
| 93 | AST_For,
|
---|
| 94 | AST_ForIn,
|
---|
| 95 | AST_ForOf,
|
---|
| 96 | AST_Function,
|
---|
| 97 | AST_Hole,
|
---|
| 98 | AST_If,
|
---|
| 99 | AST_Import,
|
---|
| 100 | AST_ImportMeta,
|
---|
| 101 | AST_IterationStatement,
|
---|
| 102 | AST_Label,
|
---|
| 103 | AST_LabeledStatement,
|
---|
| 104 | AST_LabelRef,
|
---|
| 105 | AST_Let,
|
---|
| 106 | AST_NameMapping,
|
---|
| 107 | AST_New,
|
---|
| 108 | AST_NewTarget,
|
---|
| 109 | AST_Node,
|
---|
| 110 | AST_Null,
|
---|
| 111 | AST_Number,
|
---|
| 112 | AST_Object,
|
---|
| 113 | AST_ObjectGetter,
|
---|
| 114 | AST_ObjectKeyVal,
|
---|
| 115 | AST_ObjectProperty,
|
---|
| 116 | AST_ObjectSetter,
|
---|
| 117 | AST_PrefixedTemplateString,
|
---|
| 118 | AST_PropAccess,
|
---|
| 119 | AST_RegExp,
|
---|
| 120 | AST_Return,
|
---|
| 121 | AST_Sequence,
|
---|
| 122 | AST_SimpleStatement,
|
---|
| 123 | AST_String,
|
---|
| 124 | AST_Sub,
|
---|
| 125 | AST_Super,
|
---|
| 126 | AST_Switch,
|
---|
| 127 | AST_SymbolCatch,
|
---|
| 128 | AST_SymbolClass,
|
---|
| 129 | AST_SymbolClassProperty,
|
---|
| 130 | AST_SymbolConst,
|
---|
| 131 | AST_SymbolDeclaration,
|
---|
| 132 | AST_SymbolDefClass,
|
---|
| 133 | AST_SymbolDefun,
|
---|
| 134 | AST_SymbolExport,
|
---|
| 135 | AST_SymbolExportForeign,
|
---|
| 136 | AST_SymbolFunarg,
|
---|
| 137 | AST_SymbolImport,
|
---|
| 138 | AST_SymbolImportForeign,
|
---|
| 139 | AST_SymbolLambda,
|
---|
| 140 | AST_SymbolLet,
|
---|
| 141 | AST_SymbolMethod,
|
---|
| 142 | AST_SymbolRef,
|
---|
| 143 | AST_SymbolVar,
|
---|
| 144 | AST_TemplateSegment,
|
---|
| 145 | AST_TemplateString,
|
---|
| 146 | AST_This,
|
---|
| 147 | AST_Throw,
|
---|
| 148 | AST_Token,
|
---|
| 149 | AST_Toplevel,
|
---|
| 150 | AST_True,
|
---|
| 151 | AST_Try,
|
---|
| 152 | AST_UnaryPostfix,
|
---|
| 153 | AST_UnaryPrefix,
|
---|
| 154 | AST_Var,
|
---|
| 155 | AST_VarDef,
|
---|
| 156 | AST_While,
|
---|
| 157 | AST_With,
|
---|
| 158 | AST_Yield,
|
---|
| 159 | _INLINE,
|
---|
| 160 | _NOINLINE,
|
---|
| 161 | _PURE
|
---|
| 162 | } from "./ast.js";
|
---|
| 163 |
|
---|
| 164 | var LATEST_RAW = ""; // Only used for numbers and template strings
|
---|
| 165 | var LATEST_TEMPLATE_END = true;
|
---|
| 166 |
|
---|
| 167 | var KEYWORDS = "break case catch class const continue debugger default delete do else export extends finally for function if in instanceof let new return switch throw try typeof var void while with";
|
---|
| 168 | var KEYWORDS_ATOM = "false null true";
|
---|
| 169 | var RESERVED_WORDS = "enum implements import interface package private protected public static super this " + KEYWORDS_ATOM + " " + KEYWORDS;
|
---|
| 170 | var KEYWORDS_BEFORE_EXPRESSION = "return new delete throw else case yield await";
|
---|
| 171 |
|
---|
| 172 | KEYWORDS = makePredicate(KEYWORDS);
|
---|
| 173 | RESERVED_WORDS = makePredicate(RESERVED_WORDS);
|
---|
| 174 | KEYWORDS_BEFORE_EXPRESSION = makePredicate(KEYWORDS_BEFORE_EXPRESSION);
|
---|
| 175 | KEYWORDS_ATOM = makePredicate(KEYWORDS_ATOM);
|
---|
| 176 |
|
---|
| 177 | var OPERATOR_CHARS = makePredicate(characters("+-*&%=<>!?|~^"));
|
---|
| 178 |
|
---|
| 179 | var RE_NUM_LITERAL = /[0-9a-f]/i;
|
---|
| 180 | var RE_HEX_NUMBER = /^0x[0-9a-f]+$/i;
|
---|
| 181 | var RE_OCT_NUMBER = /^0[0-7]+$/;
|
---|
| 182 | var RE_ES6_OCT_NUMBER = /^0o[0-7]+$/i;
|
---|
| 183 | var RE_BIN_NUMBER = /^0b[01]+$/i;
|
---|
| 184 | var RE_DEC_NUMBER = /^\d*\.?\d*(?:e[+-]?\d*(?:\d\.?|\.?\d)\d*)?$/i;
|
---|
| 185 | var RE_BIG_INT = /^(0[xob])?[0-9a-f]+n$/i;
|
---|
| 186 |
|
---|
| 187 | var OPERATORS = makePredicate([
|
---|
| 188 | "in",
|
---|
| 189 | "instanceof",
|
---|
| 190 | "typeof",
|
---|
| 191 | "new",
|
---|
| 192 | "void",
|
---|
| 193 | "delete",
|
---|
| 194 | "++",
|
---|
| 195 | "--",
|
---|
| 196 | "+",
|
---|
| 197 | "-",
|
---|
| 198 | "!",
|
---|
| 199 | "~",
|
---|
| 200 | "&",
|
---|
| 201 | "|",
|
---|
| 202 | "^",
|
---|
| 203 | "*",
|
---|
| 204 | "**",
|
---|
| 205 | "/",
|
---|
| 206 | "%",
|
---|
| 207 | ">>",
|
---|
| 208 | "<<",
|
---|
| 209 | ">>>",
|
---|
| 210 | "<",
|
---|
| 211 | ">",
|
---|
| 212 | "<=",
|
---|
| 213 | ">=",
|
---|
| 214 | "==",
|
---|
| 215 | "===",
|
---|
| 216 | "!=",
|
---|
| 217 | "!==",
|
---|
| 218 | "?",
|
---|
| 219 | "=",
|
---|
| 220 | "+=",
|
---|
| 221 | "-=",
|
---|
| 222 | "||=",
|
---|
| 223 | "&&=",
|
---|
| 224 | "??=",
|
---|
| 225 | "/=",
|
---|
| 226 | "*=",
|
---|
| 227 | "**=",
|
---|
| 228 | "%=",
|
---|
| 229 | ">>=",
|
---|
| 230 | "<<=",
|
---|
| 231 | ">>>=",
|
---|
| 232 | "|=",
|
---|
| 233 | "^=",
|
---|
| 234 | "&=",
|
---|
| 235 | "&&",
|
---|
| 236 | "??",
|
---|
| 237 | "||",
|
---|
| 238 | ]);
|
---|
| 239 |
|
---|
| 240 | var WHITESPACE_CHARS = makePredicate(characters(" \u00a0\n\r\t\f\u000b\u200b\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\uFEFF"));
|
---|
| 241 |
|
---|
| 242 | var NEWLINE_CHARS = makePredicate(characters("\n\r\u2028\u2029"));
|
---|
| 243 |
|
---|
| 244 | var PUNC_AFTER_EXPRESSION = makePredicate(characters(";]),:"));
|
---|
| 245 |
|
---|
| 246 | var PUNC_BEFORE_EXPRESSION = makePredicate(characters("[{(,;:"));
|
---|
| 247 |
|
---|
| 248 | var PUNC_CHARS = makePredicate(characters("[]{}(),;:"));
|
---|
| 249 |
|
---|
| 250 | /* -----[ Tokenizer ]----- */
|
---|
| 251 |
|
---|
| 252 | // surrogate safe regexps adapted from https://github.com/mathiasbynens/unicode-8.0.0/tree/89b412d8a71ecca9ed593d9e9fa073ab64acfebe/Binary_Property
|
---|
| 253 | var UNICODE = {
|
---|
| 254 | ID_Start: /[$A-Z_a-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\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-\u08B4\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\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\u0AF9\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-\u0C39\u0C3D\u0C58-\u0C5A\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\u0D5F-\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-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\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\u2118-\u211D\u2124\u2126\u2128\u212A-\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\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309B-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FD5\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA7AD\uA7B0-\uA7B7\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB65\uAB70-\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]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF30-\uDF4A\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2]|\uD804[\uDC03-\uDC37\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61]|\uD805[\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDF00-\uDF19]|\uD806[\uDCA0-\uDCDF\uDCFF\uDEC0-\uDEF8]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50\uDF93-\uDF9F]|\uD82C[\uDC00\uDC01]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD83A[\uDC00-\uDCC4]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1]|\uD87E[\uDC00-\uDE1D]/,
|
---|
| 255 | ID_Continue: /(?:[$0-9A-Z_a-z\xAA\xB5\xB7\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u0483-\u0487\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u05D0-\u05EA\u05F0-\u05F2\u0610-\u061A\u0620-\u0669\u066E-\u06D3\u06D5-\u06DC\u06DF-\u06E8\u06EA-\u06FC\u06FF\u0710-\u074A\u074D-\u07B1\u07C0-\u07F5\u07FA\u0800-\u082D\u0840-\u085B\u08A0-\u08B4\u08E3-\u0963\u0966-\u096F\u0971-\u0983\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BC-\u09C4\u09C7\u09C8\u09CB-\u09CE\u09D7\u09DC\u09DD\u09DF-\u09E3\u09E6-\u09F1\u0A01-\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A59-\u0A5C\u0A5E\u0A66-\u0A75\u0A81-\u0A83\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABC-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AD0\u0AE0-\u0AE3\u0AE6-\u0AEF\u0AF9\u0B01-\u0B03\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3C-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B5C\u0B5D\u0B5F-\u0B63\u0B66-\u0B6F\u0B71\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD0\u0BD7\u0BE6-\u0BEF\u0C00-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C58-\u0C5A\u0C60-\u0C63\u0C66-\u0C6F\u0C81-\u0C83\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBC-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CDE\u0CE0-\u0CE3\u0CE6-\u0CEF\u0CF1\u0CF2\u0D01-\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D-\u0D44\u0D46-\u0D48\u0D4A-\u0D4E\u0D57\u0D5F-\u0D63\u0D66-\u0D6F\u0D7A-\u0D7F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E01-\u0E3A\u0E40-\u0E4E\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB9\u0EBB-\u0EBD\u0EC0-\u0EC4\u0EC6\u0EC8-\u0ECD\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E-\u0F47\u0F49-\u0F6C\u0F71-\u0F84\u0F86-\u0F97\u0F99-\u0FBC\u0FC6\u1000-\u1049\u1050-\u109D\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\u135D-\u135F\u1369-\u1371\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176C\u176E-\u1770\u1772\u1773\u1780-\u17D3\u17D7\u17DC\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u1820-\u1877\u1880-\u18AA\u18B0-\u18F5\u1900-\u191E\u1920-\u192B\u1930-\u193B\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19DA\u1A00-\u1A1B\u1A20-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AA7\u1AB0-\u1ABD\u1B00-\u1B4B\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1BF3\u1C00-\u1C37\u1C40-\u1C49\u1C4D-\u1C7D\u1CD0-\u1CD2\u1CD4-\u1CF6\u1CF8\u1CF9\u1D00-\u1DF5\u1DFC-\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\u200C\u200D\u203F\u2040\u2054\u2071\u207F\u2090-\u209C\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2102\u2107\u210A-\u2113\u2115\u2118-\u211D\u2124\u2126\u2128\u212A-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D7F-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2DE0-\u2DFF\u3005-\u3007\u3021-\u302F\u3031-\u3035\u3038-\u303C\u3041-\u3096\u3099-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FD5\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66F\uA674-\uA67D\uA67F-\uA6F1\uA717-\uA71F\uA722-\uA788\uA78B-\uA7AD\uA7B0-\uA7B7\uA7F7-\uA827\uA840-\uA873\uA880-\uA8C4\uA8D0-\uA8D9\uA8E0-\uA8F7\uA8FB\uA8FD\uA900-\uA92D\uA930-\uA953\uA960-\uA97C\uA980-\uA9C0\uA9CF-\uA9D9\uA9E0-\uA9FE\uAA00-\uAA36\uAA40-\uAA4D\uAA50-\uAA59\uAA60-\uAA76\uAA7A-\uAAC2\uAADB-\uAADD\uAAE0-\uAAEF\uAAF2-\uAAF6\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB65\uAB70-\uABEA\uABEC\uABED\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE00-\uFE0F\uFE20-\uFE2F\uFE33\uFE34\uFE4D-\uFE4F\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF3F\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDDFD\uDE80-\uDE9C\uDEA0-\uDED0\uDEE0\uDF00-\uDF1F\uDF30-\uDF4A\uDF50-\uDF7A\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDCA0-\uDCA9\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00-\uDE03\uDE05\uDE06\uDE0C-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE38-\uDE3A\uDE3F\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE6\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2]|\uD804[\uDC00-\uDC46\uDC66-\uDC6F\uDC7F-\uDCBA\uDCD0-\uDCE8\uDCF0-\uDCF9\uDD00-\uDD34\uDD36-\uDD3F\uDD50-\uDD73\uDD76\uDD80-\uDDC4\uDDCA-\uDDCC\uDDD0-\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE37\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEEA\uDEF0-\uDEF9\uDF00-\uDF03\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3C-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF50\uDF57\uDF5D-\uDF63\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDC80-\uDCC5\uDCC7\uDCD0-\uDCD9\uDD80-\uDDB5\uDDB8-\uDDC0\uDDD8-\uDDDD\uDE00-\uDE40\uDE44\uDE50-\uDE59\uDE80-\uDEB7\uDEC0-\uDEC9\uDF00-\uDF19\uDF1D-\uDF2B\uDF30-\uDF39]|\uD806[\uDCA0-\uDCE9\uDCFF\uDEC0-\uDEF8]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE60-\uDE69\uDED0-\uDEED\uDEF0-\uDEF4\uDF00-\uDF36\uDF40-\uDF43\uDF50-\uDF59\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50-\uDF7E\uDF8F-\uDF9F]|\uD82C[\uDC00\uDC01]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99\uDC9D\uDC9E]|\uD834[\uDD65-\uDD69\uDD6D-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB\uDFCE-\uDFFF]|\uD836[\uDE00-\uDE36\uDE3B-\uDE6C\uDE75\uDE84\uDE9B-\uDE9F\uDEA1-\uDEAF]|\uD83A[\uDC00-\uDCC4\uDCD0-\uDCD6]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1]|\uD87E[\uDC00-\uDE1D]|\uDB40[\uDD00-\uDDEF])+/,
|
---|
| 256 | };
|
---|
| 257 |
|
---|
| 258 | function get_full_char(str, pos) {
|
---|
| 259 | if (is_surrogate_pair_head(str.charCodeAt(pos))) {
|
---|
| 260 | if (is_surrogate_pair_tail(str.charCodeAt(pos + 1))) {
|
---|
| 261 | return str.charAt(pos) + str.charAt(pos + 1);
|
---|
| 262 | }
|
---|
| 263 | } else if (is_surrogate_pair_tail(str.charCodeAt(pos))) {
|
---|
| 264 | if (is_surrogate_pair_head(str.charCodeAt(pos - 1))) {
|
---|
| 265 | return str.charAt(pos - 1) + str.charAt(pos);
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 | return str.charAt(pos);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | function get_full_char_code(str, pos) {
|
---|
| 272 | // https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Surrogates
|
---|
| 273 | if (is_surrogate_pair_head(str.charCodeAt(pos))) {
|
---|
| 274 | return 0x10000 + (str.charCodeAt(pos) - 0xd800 << 10) + str.charCodeAt(pos + 1) - 0xdc00;
|
---|
| 275 | }
|
---|
| 276 | return str.charCodeAt(pos);
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | function get_full_char_length(str) {
|
---|
| 280 | var surrogates = 0;
|
---|
| 281 |
|
---|
| 282 | for (var i = 0; i < str.length; i++) {
|
---|
| 283 | if (is_surrogate_pair_head(str.charCodeAt(i)) && is_surrogate_pair_tail(str.charCodeAt(i + 1))) {
|
---|
| 284 | surrogates++;
|
---|
| 285 | i++;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | return str.length - surrogates;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | function from_char_code(code) {
|
---|
| 293 | // Based on https://github.com/mathiasbynens/String.fromCodePoint/blob/master/fromcodepoint.js
|
---|
| 294 | if (code > 0xFFFF) {
|
---|
| 295 | code -= 0x10000;
|
---|
| 296 | return (String.fromCharCode((code >> 10) + 0xD800) +
|
---|
| 297 | String.fromCharCode((code % 0x400) + 0xDC00));
|
---|
| 298 | }
|
---|
| 299 | return String.fromCharCode(code);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | function is_surrogate_pair_head(code) {
|
---|
| 303 | return code >= 0xd800 && code <= 0xdbff;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | function is_surrogate_pair_tail(code) {
|
---|
| 307 | return code >= 0xdc00 && code <= 0xdfff;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | function is_digit(code) {
|
---|
| 311 | return code >= 48 && code <= 57;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | function is_identifier_start(ch) {
|
---|
| 315 | return UNICODE.ID_Start.test(ch);
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | function is_identifier_char(ch) {
|
---|
| 319 | return UNICODE.ID_Continue.test(ch);
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | const BASIC_IDENT = /^[a-z_$][a-z0-9_$]*$/i;
|
---|
| 323 |
|
---|
| 324 | function is_basic_identifier_string(str) {
|
---|
| 325 | return BASIC_IDENT.test(str);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | function is_identifier_string(str, allow_surrogates) {
|
---|
| 329 | if (BASIC_IDENT.test(str)) {
|
---|
| 330 | return true;
|
---|
| 331 | }
|
---|
| 332 | if (!allow_surrogates && /[\ud800-\udfff]/.test(str)) {
|
---|
| 333 | return false;
|
---|
| 334 | }
|
---|
| 335 | var match = UNICODE.ID_Start.exec(str);
|
---|
| 336 | if (!match || match.index !== 0) {
|
---|
| 337 | return false;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | str = str.slice(match[0].length);
|
---|
| 341 | if (!str) {
|
---|
| 342 | return true;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | match = UNICODE.ID_Continue.exec(str);
|
---|
| 346 | return !!match && match[0].length === str.length;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | function parse_js_number(num, allow_e = true) {
|
---|
| 350 | if (!allow_e && num.includes("e")) {
|
---|
| 351 | return NaN;
|
---|
| 352 | }
|
---|
| 353 | if (RE_HEX_NUMBER.test(num)) {
|
---|
| 354 | return parseInt(num.substr(2), 16);
|
---|
| 355 | } else if (RE_OCT_NUMBER.test(num)) {
|
---|
| 356 | return parseInt(num.substr(1), 8);
|
---|
| 357 | } else if (RE_ES6_OCT_NUMBER.test(num)) {
|
---|
| 358 | return parseInt(num.substr(2), 8);
|
---|
| 359 | } else if (RE_BIN_NUMBER.test(num)) {
|
---|
| 360 | return parseInt(num.substr(2), 2);
|
---|
| 361 | } else if (RE_DEC_NUMBER.test(num)) {
|
---|
| 362 | return parseFloat(num);
|
---|
| 363 | } else {
|
---|
| 364 | var val = parseFloat(num);
|
---|
| 365 | if (val == num) return val;
|
---|
| 366 | }
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | class JS_Parse_Error extends Error {
|
---|
| 370 | constructor(message, filename, line, col, pos) {
|
---|
| 371 | super();
|
---|
| 372 |
|
---|
| 373 | this.name = "SyntaxError";
|
---|
| 374 | this.message = message;
|
---|
| 375 | this.filename = filename;
|
---|
| 376 | this.line = line;
|
---|
| 377 | this.col = col;
|
---|
| 378 | this.pos = pos;
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | function js_error(message, filename, line, col, pos) {
|
---|
| 383 | throw new JS_Parse_Error(message, filename, line, col, pos);
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | function is_token(token, type, val) {
|
---|
| 387 | return token.type == type && (val == null || token.value == val);
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | var EX_EOF = {};
|
---|
| 391 |
|
---|
| 392 | function tokenizer($TEXT, filename, html5_comments, shebang) {
|
---|
| 393 | var S = {
|
---|
| 394 | text : $TEXT,
|
---|
| 395 | filename : filename,
|
---|
| 396 | pos : 0,
|
---|
| 397 | tokpos : 0,
|
---|
| 398 | line : 1,
|
---|
| 399 | tokline : 0,
|
---|
| 400 | col : 0,
|
---|
| 401 | tokcol : 0,
|
---|
| 402 | newline_before : false,
|
---|
| 403 | regex_allowed : false,
|
---|
| 404 | brace_counter : 0,
|
---|
| 405 | template_braces : [],
|
---|
| 406 | comments_before : [],
|
---|
| 407 | directives : {},
|
---|
| 408 | directive_stack : []
|
---|
| 409 | };
|
---|
| 410 |
|
---|
| 411 | function peek() { return get_full_char(S.text, S.pos); }
|
---|
| 412 |
|
---|
| 413 | // Used because parsing ?. involves a lookahead for a digit
|
---|
| 414 | function is_option_chain_op() {
|
---|
| 415 | const must_be_dot = S.text.charCodeAt(S.pos + 1) === 46;
|
---|
| 416 | if (!must_be_dot) return false;
|
---|
| 417 |
|
---|
| 418 | const cannot_be_digit = S.text.charCodeAt(S.pos + 2);
|
---|
| 419 | return cannot_be_digit < 48 || cannot_be_digit > 57;
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | function next(signal_eof, in_string) {
|
---|
| 423 | var ch = get_full_char(S.text, S.pos++);
|
---|
| 424 | if (signal_eof && !ch)
|
---|
| 425 | throw EX_EOF;
|
---|
| 426 | if (NEWLINE_CHARS.has(ch)) {
|
---|
| 427 | S.newline_before = S.newline_before || !in_string;
|
---|
| 428 | ++S.line;
|
---|
| 429 | S.col = 0;
|
---|
| 430 | if (ch == "\r" && peek() == "\n") {
|
---|
| 431 | // treat a \r\n sequence as a single \n
|
---|
| 432 | ++S.pos;
|
---|
| 433 | ch = "\n";
|
---|
| 434 | }
|
---|
| 435 | } else {
|
---|
| 436 | if (ch.length > 1) {
|
---|
| 437 | ++S.pos;
|
---|
| 438 | ++S.col;
|
---|
| 439 | }
|
---|
| 440 | ++S.col;
|
---|
| 441 | }
|
---|
| 442 | return ch;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | function forward(i) {
|
---|
| 446 | while (i--) next();
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | function looking_at(str) {
|
---|
| 450 | return S.text.substr(S.pos, str.length) == str;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | function find_eol() {
|
---|
| 454 | var text = S.text;
|
---|
| 455 | for (var i = S.pos, n = S.text.length; i < n; ++i) {
|
---|
| 456 | var ch = text[i];
|
---|
| 457 | if (NEWLINE_CHARS.has(ch))
|
---|
| 458 | return i;
|
---|
| 459 | }
|
---|
| 460 | return -1;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | function find(what, signal_eof) {
|
---|
| 464 | var pos = S.text.indexOf(what, S.pos);
|
---|
| 465 | if (signal_eof && pos == -1) throw EX_EOF;
|
---|
| 466 | return pos;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | function start_token() {
|
---|
| 470 | S.tokline = S.line;
|
---|
| 471 | S.tokcol = S.col;
|
---|
| 472 | S.tokpos = S.pos;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | var prev_was_dot = false;
|
---|
| 476 | var previous_token = null;
|
---|
| 477 | function token(type, value, is_comment) {
|
---|
| 478 | S.regex_allowed = ((type == "operator" && !UNARY_POSTFIX.has(value)) ||
|
---|
| 479 | (type == "keyword" && KEYWORDS_BEFORE_EXPRESSION.has(value)) ||
|
---|
| 480 | (type == "punc" && PUNC_BEFORE_EXPRESSION.has(value))) ||
|
---|
| 481 | (type == "arrow");
|
---|
| 482 | if (type == "punc" && (value == "." || value == "?.")) {
|
---|
| 483 | prev_was_dot = true;
|
---|
| 484 | } else if (!is_comment) {
|
---|
| 485 | prev_was_dot = false;
|
---|
| 486 | }
|
---|
| 487 | const line = S.tokline;
|
---|
| 488 | const col = S.tokcol;
|
---|
| 489 | const pos = S.tokpos;
|
---|
| 490 | const nlb = S.newline_before;
|
---|
| 491 | const file = filename;
|
---|
| 492 | let comments_before = [];
|
---|
| 493 | let comments_after = [];
|
---|
| 494 |
|
---|
| 495 | if (!is_comment) {
|
---|
| 496 | comments_before = S.comments_before;
|
---|
| 497 | comments_after = S.comments_before = [];
|
---|
| 498 | }
|
---|
| 499 | S.newline_before = false;
|
---|
| 500 | const tok = new AST_Token(type, value, line, col, pos, nlb, comments_before, comments_after, file);
|
---|
| 501 |
|
---|
| 502 | if (!is_comment) previous_token = tok;
|
---|
| 503 | return tok;
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | function skip_whitespace() {
|
---|
| 507 | while (WHITESPACE_CHARS.has(peek()))
|
---|
| 508 | next();
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 | function read_while(pred) {
|
---|
| 512 | var ret = "", ch, i = 0;
|
---|
| 513 | while ((ch = peek()) && pred(ch, i++))
|
---|
| 514 | ret += next();
|
---|
| 515 | return ret;
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | function parse_error(err) {
|
---|
| 519 | js_error(err, filename, S.tokline, S.tokcol, S.tokpos);
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | function read_num(prefix) {
|
---|
| 523 | var has_e = false, after_e = false, has_x = false, has_dot = prefix == ".", is_big_int = false, numeric_separator = false;
|
---|
| 524 | var num = read_while(function(ch, i) {
|
---|
| 525 | if (is_big_int) return false;
|
---|
| 526 |
|
---|
| 527 | var code = ch.charCodeAt(0);
|
---|
| 528 | switch (code) {
|
---|
| 529 | case 95: // _
|
---|
| 530 | return (numeric_separator = true);
|
---|
| 531 | case 98: case 66: // bB
|
---|
| 532 | return (has_x = true); // Can occur in hex sequence, don't return false yet
|
---|
| 533 | case 111: case 79: // oO
|
---|
| 534 | case 120: case 88: // xX
|
---|
| 535 | return has_x ? false : (has_x = true);
|
---|
| 536 | case 101: case 69: // eE
|
---|
| 537 | return has_x ? true : has_e ? false : (has_e = after_e = true);
|
---|
| 538 | case 45: // -
|
---|
| 539 | return after_e || (i == 0 && !prefix);
|
---|
| 540 | case 43: // +
|
---|
| 541 | return after_e;
|
---|
| 542 | case (after_e = false, 46): // .
|
---|
| 543 | return (!has_dot && !has_x && !has_e) ? (has_dot = true) : false;
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | if (ch === "n") {
|
---|
| 547 | is_big_int = true;
|
---|
| 548 |
|
---|
| 549 | return true;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | return RE_NUM_LITERAL.test(ch);
|
---|
| 553 | });
|
---|
| 554 | if (prefix) num = prefix + num;
|
---|
| 555 |
|
---|
| 556 | LATEST_RAW = num;
|
---|
| 557 |
|
---|
| 558 | if (RE_OCT_NUMBER.test(num) && next_token.has_directive("use strict")) {
|
---|
| 559 | parse_error("Legacy octal literals are not allowed in strict mode");
|
---|
| 560 | }
|
---|
| 561 | if (numeric_separator) {
|
---|
| 562 | if (num.endsWith("_")) {
|
---|
| 563 | parse_error("Numeric separators are not allowed at the end of numeric literals");
|
---|
| 564 | } else if (num.includes("__")) {
|
---|
| 565 | parse_error("Only one underscore is allowed as numeric separator");
|
---|
| 566 | }
|
---|
| 567 | num = num.replace(/_/g, "");
|
---|
| 568 | }
|
---|
| 569 | if (num.endsWith("n")) {
|
---|
| 570 | const without_n = num.slice(0, -1);
|
---|
| 571 | const allow_e = RE_HEX_NUMBER.test(without_n);
|
---|
| 572 | const valid = parse_js_number(without_n, allow_e);
|
---|
| 573 | if (!has_dot && RE_BIG_INT.test(num) && !isNaN(valid))
|
---|
| 574 | return token("big_int", without_n);
|
---|
| 575 | parse_error("Invalid or unexpected token");
|
---|
| 576 | }
|
---|
| 577 | var valid = parse_js_number(num);
|
---|
| 578 | if (!isNaN(valid)) {
|
---|
| 579 | return token("num", valid);
|
---|
| 580 | } else {
|
---|
| 581 | parse_error("Invalid syntax: " + num);
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | function is_octal(ch) {
|
---|
| 586 | return ch >= "0" && ch <= "7";
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | function read_escaped_char(in_string, strict_hex, template_string) {
|
---|
| 590 | var ch = next(true, in_string);
|
---|
| 591 | switch (ch.charCodeAt(0)) {
|
---|
| 592 | case 110 : return "\n";
|
---|
| 593 | case 114 : return "\r";
|
---|
| 594 | case 116 : return "\t";
|
---|
| 595 | case 98 : return "\b";
|
---|
| 596 | case 118 : return "\u000b"; // \v
|
---|
| 597 | case 102 : return "\f";
|
---|
| 598 | case 120 : return String.fromCharCode(hex_bytes(2, strict_hex)); // \x
|
---|
| 599 | case 117 : // \u
|
---|
| 600 | if (peek() == "{") {
|
---|
| 601 | next(true);
|
---|
| 602 | if (peek() === "}")
|
---|
| 603 | parse_error("Expecting hex-character between {}");
|
---|
| 604 | while (peek() == "0") next(true); // No significance
|
---|
| 605 | var result, length = find("}", true) - S.pos;
|
---|
| 606 | // Avoid 32 bit integer overflow (1 << 32 === 1)
|
---|
| 607 | // We know first character isn't 0 and thus out of range anyway
|
---|
| 608 | if (length > 6 || (result = hex_bytes(length, strict_hex)) > 0x10FFFF) {
|
---|
| 609 | parse_error("Unicode reference out of bounds");
|
---|
| 610 | }
|
---|
| 611 | next(true);
|
---|
| 612 | return from_char_code(result);
|
---|
| 613 | }
|
---|
| 614 | return String.fromCharCode(hex_bytes(4, strict_hex));
|
---|
| 615 | case 10 : return ""; // newline
|
---|
| 616 | case 13 : // \r
|
---|
| 617 | if (peek() == "\n") { // DOS newline
|
---|
| 618 | next(true, in_string);
|
---|
| 619 | return "";
|
---|
| 620 | }
|
---|
| 621 | }
|
---|
| 622 | if (is_octal(ch)) {
|
---|
| 623 | if (template_string && strict_hex) {
|
---|
| 624 | const represents_null_character = ch === "0" && !is_octal(peek());
|
---|
| 625 | if (!represents_null_character) {
|
---|
| 626 | parse_error("Octal escape sequences are not allowed in template strings");
|
---|
| 627 | }
|
---|
| 628 | }
|
---|
| 629 | return read_octal_escape_sequence(ch, strict_hex);
|
---|
| 630 | }
|
---|
| 631 | return ch;
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | function read_octal_escape_sequence(ch, strict_octal) {
|
---|
| 635 | // Read
|
---|
| 636 | var p = peek();
|
---|
| 637 | if (p >= "0" && p <= "7") {
|
---|
| 638 | ch += next(true);
|
---|
| 639 | if (ch[0] <= "3" && (p = peek()) >= "0" && p <= "7")
|
---|
| 640 | ch += next(true);
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | // Parse
|
---|
| 644 | if (ch === "0") return "\0";
|
---|
| 645 | if (ch.length > 0 && next_token.has_directive("use strict") && strict_octal)
|
---|
| 646 | parse_error("Legacy octal escape sequences are not allowed in strict mode");
|
---|
| 647 | return String.fromCharCode(parseInt(ch, 8));
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | function hex_bytes(n, strict_hex) {
|
---|
| 651 | var num = 0;
|
---|
| 652 | for (; n > 0; --n) {
|
---|
| 653 | if (!strict_hex && isNaN(parseInt(peek(), 16))) {
|
---|
| 654 | return parseInt(num, 16) || "";
|
---|
| 655 | }
|
---|
| 656 | var digit = next(true);
|
---|
| 657 | if (isNaN(parseInt(digit, 16)))
|
---|
| 658 | parse_error("Invalid hex-character pattern in string");
|
---|
| 659 | num += digit;
|
---|
| 660 | }
|
---|
| 661 | return parseInt(num, 16);
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 | var read_string = with_eof_error("Unterminated string constant", function() {
|
---|
| 665 | const start_pos = S.pos;
|
---|
| 666 | var quote = next(), ret = [];
|
---|
| 667 | for (;;) {
|
---|
| 668 | var ch = next(true, true);
|
---|
| 669 | if (ch == "\\") ch = read_escaped_char(true, true);
|
---|
| 670 | else if (ch == "\r" || ch == "\n") parse_error("Unterminated string constant");
|
---|
| 671 | else if (ch == quote) break;
|
---|
| 672 | ret.push(ch);
|
---|
| 673 | }
|
---|
| 674 | var tok = token("string", ret.join(""));
|
---|
| 675 | LATEST_RAW = S.text.slice(start_pos, S.pos);
|
---|
| 676 | tok.quote = quote;
|
---|
| 677 | return tok;
|
---|
| 678 | });
|
---|
| 679 |
|
---|
| 680 | var read_template_characters = with_eof_error("Unterminated template", function(begin) {
|
---|
| 681 | if (begin) {
|
---|
| 682 | S.template_braces.push(S.brace_counter);
|
---|
| 683 | }
|
---|
| 684 | var content = "", raw = "", ch, tok;
|
---|
| 685 | next(true, true);
|
---|
| 686 | while ((ch = next(true, true)) != "`") {
|
---|
| 687 | if (ch == "\r") {
|
---|
| 688 | if (peek() == "\n") ++S.pos;
|
---|
| 689 | ch = "\n";
|
---|
| 690 | } else if (ch == "$" && peek() == "{") {
|
---|
| 691 | next(true, true);
|
---|
| 692 | S.brace_counter++;
|
---|
| 693 | tok = token(begin ? "template_head" : "template_substitution", content);
|
---|
| 694 | LATEST_RAW = raw;
|
---|
| 695 | LATEST_TEMPLATE_END = false;
|
---|
| 696 | return tok;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | raw += ch;
|
---|
| 700 | if (ch == "\\") {
|
---|
| 701 | var tmp = S.pos;
|
---|
| 702 | var prev_is_tag = previous_token && (previous_token.type === "name" || previous_token.type === "punc" && (previous_token.value === ")" || previous_token.value === "]"));
|
---|
| 703 | ch = read_escaped_char(true, !prev_is_tag, true);
|
---|
| 704 | raw += S.text.substr(tmp, S.pos - tmp);
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | content += ch;
|
---|
| 708 | }
|
---|
| 709 | S.template_braces.pop();
|
---|
| 710 | tok = token(begin ? "template_head" : "template_substitution", content);
|
---|
| 711 | LATEST_RAW = raw;
|
---|
| 712 | LATEST_TEMPLATE_END = true;
|
---|
| 713 | return tok;
|
---|
| 714 | });
|
---|
| 715 |
|
---|
| 716 | function skip_line_comment(type) {
|
---|
| 717 | var regex_allowed = S.regex_allowed;
|
---|
| 718 | var i = find_eol(), ret;
|
---|
| 719 | if (i == -1) {
|
---|
| 720 | ret = S.text.substr(S.pos);
|
---|
| 721 | S.pos = S.text.length;
|
---|
| 722 | } else {
|
---|
| 723 | ret = S.text.substring(S.pos, i);
|
---|
| 724 | S.pos = i;
|
---|
| 725 | }
|
---|
| 726 | S.col = S.tokcol + (S.pos - S.tokpos);
|
---|
| 727 | S.comments_before.push(token(type, ret, true));
|
---|
| 728 | S.regex_allowed = regex_allowed;
|
---|
| 729 | return next_token;
|
---|
| 730 | }
|
---|
| 731 |
|
---|
| 732 | var skip_multiline_comment = with_eof_error("Unterminated multiline comment", function() {
|
---|
| 733 | var regex_allowed = S.regex_allowed;
|
---|
| 734 | var i = find("*/", true);
|
---|
| 735 | var text = S.text.substring(S.pos, i).replace(/\r\n|\r|\u2028|\u2029/g, "\n");
|
---|
| 736 | // update stream position
|
---|
| 737 | forward(get_full_char_length(text) /* text length doesn't count \r\n as 2 char while S.pos - i does */ + 2);
|
---|
| 738 | S.comments_before.push(token("comment2", text, true));
|
---|
| 739 | S.newline_before = S.newline_before || text.includes("\n");
|
---|
| 740 | S.regex_allowed = regex_allowed;
|
---|
| 741 | return next_token;
|
---|
| 742 | });
|
---|
| 743 |
|
---|
| 744 | var read_name = with_eof_error("Unterminated identifier name", function() {
|
---|
| 745 | var name = [], ch, escaped = false;
|
---|
| 746 | var read_escaped_identifier_char = function() {
|
---|
| 747 | escaped = true;
|
---|
| 748 | next();
|
---|
| 749 | if (peek() !== "u") {
|
---|
| 750 | parse_error("Expecting UnicodeEscapeSequence -- uXXXX or u{XXXX}");
|
---|
| 751 | }
|
---|
| 752 | return read_escaped_char(false, true);
|
---|
| 753 | };
|
---|
| 754 |
|
---|
| 755 | // Read first character (ID_Start)
|
---|
| 756 | if ((ch = peek()) === "\\") {
|
---|
| 757 | ch = read_escaped_identifier_char();
|
---|
| 758 | if (!is_identifier_start(ch)) {
|
---|
| 759 | parse_error("First identifier char is an invalid identifier char");
|
---|
| 760 | }
|
---|
| 761 | } else if (is_identifier_start(ch)) {
|
---|
| 762 | next();
|
---|
| 763 | } else {
|
---|
| 764 | return "";
|
---|
| 765 | }
|
---|
| 766 |
|
---|
| 767 | name.push(ch);
|
---|
| 768 |
|
---|
| 769 | // Read ID_Continue
|
---|
| 770 | while ((ch = peek()) != null) {
|
---|
| 771 | if ((ch = peek()) === "\\") {
|
---|
| 772 | ch = read_escaped_identifier_char();
|
---|
| 773 | if (!is_identifier_char(ch)) {
|
---|
| 774 | parse_error("Invalid escaped identifier char");
|
---|
| 775 | }
|
---|
| 776 | } else {
|
---|
| 777 | if (!is_identifier_char(ch)) {
|
---|
| 778 | break;
|
---|
| 779 | }
|
---|
| 780 | next();
|
---|
| 781 | }
|
---|
| 782 | name.push(ch);
|
---|
| 783 | }
|
---|
| 784 | const name_str = name.join("");
|
---|
| 785 | if (RESERVED_WORDS.has(name_str) && escaped) {
|
---|
| 786 | parse_error("Escaped characters are not allowed in keywords");
|
---|
| 787 | }
|
---|
| 788 | return name_str;
|
---|
| 789 | });
|
---|
| 790 |
|
---|
| 791 | var read_regexp = with_eof_error("Unterminated regular expression", function(source) {
|
---|
| 792 | var prev_backslash = false, ch, in_class = false;
|
---|
| 793 | while ((ch = next(true))) if (NEWLINE_CHARS.has(ch)) {
|
---|
| 794 | parse_error("Unexpected line terminator");
|
---|
| 795 | } else if (prev_backslash) {
|
---|
| 796 | source += "\\" + ch;
|
---|
| 797 | prev_backslash = false;
|
---|
| 798 | } else if (ch == "[") {
|
---|
| 799 | in_class = true;
|
---|
| 800 | source += ch;
|
---|
| 801 | } else if (ch == "]" && in_class) {
|
---|
| 802 | in_class = false;
|
---|
| 803 | source += ch;
|
---|
| 804 | } else if (ch == "/" && !in_class) {
|
---|
| 805 | break;
|
---|
| 806 | } else if (ch == "\\") {
|
---|
| 807 | prev_backslash = true;
|
---|
| 808 | } else {
|
---|
| 809 | source += ch;
|
---|
| 810 | }
|
---|
| 811 | const flags = read_name();
|
---|
| 812 | return token("regexp", "/" + source + "/" + flags);
|
---|
| 813 | });
|
---|
| 814 |
|
---|
| 815 | function read_operator(prefix) {
|
---|
| 816 | function grow(op) {
|
---|
| 817 | if (!peek()) return op;
|
---|
| 818 | var bigger = op + peek();
|
---|
| 819 | if (OPERATORS.has(bigger)) {
|
---|
| 820 | next();
|
---|
| 821 | return grow(bigger);
|
---|
| 822 | } else {
|
---|
| 823 | return op;
|
---|
| 824 | }
|
---|
| 825 | }
|
---|
| 826 | return token("operator", grow(prefix || next()));
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 | function handle_slash() {
|
---|
| 830 | next();
|
---|
| 831 | switch (peek()) {
|
---|
| 832 | case "/":
|
---|
| 833 | next();
|
---|
| 834 | return skip_line_comment("comment1");
|
---|
| 835 | case "*":
|
---|
| 836 | next();
|
---|
| 837 | return skip_multiline_comment();
|
---|
| 838 | }
|
---|
| 839 | return S.regex_allowed ? read_regexp("") : read_operator("/");
|
---|
| 840 | }
|
---|
| 841 |
|
---|
| 842 | function handle_eq_sign() {
|
---|
| 843 | next();
|
---|
| 844 | if (peek() === ">") {
|
---|
| 845 | next();
|
---|
| 846 | return token("arrow", "=>");
|
---|
| 847 | } else {
|
---|
| 848 | return read_operator("=");
|
---|
| 849 | }
|
---|
| 850 | }
|
---|
| 851 |
|
---|
| 852 | function handle_dot() {
|
---|
| 853 | next();
|
---|
| 854 | if (is_digit(peek().charCodeAt(0))) {
|
---|
| 855 | return read_num(".");
|
---|
| 856 | }
|
---|
| 857 | if (peek() === ".") {
|
---|
| 858 | next(); // Consume second dot
|
---|
| 859 | next(); // Consume third dot
|
---|
| 860 | return token("expand", "...");
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | return token("punc", ".");
|
---|
| 864 | }
|
---|
| 865 |
|
---|
| 866 | function read_word() {
|
---|
| 867 | var word = read_name();
|
---|
| 868 | if (prev_was_dot) return token("name", word);
|
---|
| 869 | return KEYWORDS_ATOM.has(word) ? token("atom", word)
|
---|
| 870 | : !KEYWORDS.has(word) ? token("name", word)
|
---|
| 871 | : OPERATORS.has(word) ? token("operator", word)
|
---|
| 872 | : token("keyword", word);
|
---|
| 873 | }
|
---|
| 874 |
|
---|
| 875 | function read_private_word() {
|
---|
| 876 | next();
|
---|
| 877 | return token("privatename", read_name());
|
---|
| 878 | }
|
---|
| 879 |
|
---|
| 880 | function with_eof_error(eof_error, cont) {
|
---|
| 881 | return function(x) {
|
---|
| 882 | try {
|
---|
| 883 | return cont(x);
|
---|
| 884 | } catch(ex) {
|
---|
| 885 | if (ex === EX_EOF) parse_error(eof_error);
|
---|
| 886 | else throw ex;
|
---|
| 887 | }
|
---|
| 888 | };
|
---|
| 889 | }
|
---|
| 890 |
|
---|
| 891 | function next_token(force_regexp) {
|
---|
| 892 | if (force_regexp != null)
|
---|
| 893 | return read_regexp(force_regexp);
|
---|
| 894 | if (shebang && S.pos == 0 && looking_at("#!")) {
|
---|
| 895 | start_token();
|
---|
| 896 | forward(2);
|
---|
| 897 | skip_line_comment("comment5");
|
---|
| 898 | }
|
---|
| 899 | for (;;) {
|
---|
| 900 | skip_whitespace();
|
---|
| 901 | start_token();
|
---|
| 902 | if (html5_comments) {
|
---|
| 903 | if (looking_at("<!--")) {
|
---|
| 904 | forward(4);
|
---|
| 905 | skip_line_comment("comment3");
|
---|
| 906 | continue;
|
---|
| 907 | }
|
---|
| 908 | if (looking_at("-->") && S.newline_before) {
|
---|
| 909 | forward(3);
|
---|
| 910 | skip_line_comment("comment4");
|
---|
| 911 | continue;
|
---|
| 912 | }
|
---|
| 913 | }
|
---|
| 914 | var ch = peek();
|
---|
| 915 | if (!ch) return token("eof");
|
---|
| 916 | var code = ch.charCodeAt(0);
|
---|
| 917 | switch (code) {
|
---|
| 918 | case 34: case 39: return read_string();
|
---|
| 919 | case 46: return handle_dot();
|
---|
| 920 | case 47: {
|
---|
| 921 | var tok = handle_slash();
|
---|
| 922 | if (tok === next_token) continue;
|
---|
| 923 | return tok;
|
---|
| 924 | }
|
---|
| 925 | case 61: return handle_eq_sign();
|
---|
| 926 | case 63: {
|
---|
| 927 | if (!is_option_chain_op()) break; // Handled below
|
---|
| 928 |
|
---|
| 929 | next(); // ?
|
---|
| 930 | next(); // .
|
---|
| 931 |
|
---|
| 932 | return token("punc", "?.");
|
---|
| 933 | }
|
---|
| 934 | case 96: return read_template_characters(true);
|
---|
| 935 | case 123:
|
---|
| 936 | S.brace_counter++;
|
---|
| 937 | break;
|
---|
| 938 | case 125:
|
---|
| 939 | S.brace_counter--;
|
---|
| 940 | if (S.template_braces.length > 0
|
---|
| 941 | && S.template_braces[S.template_braces.length - 1] === S.brace_counter)
|
---|
| 942 | return read_template_characters(false);
|
---|
| 943 | break;
|
---|
| 944 | }
|
---|
| 945 | if (is_digit(code)) return read_num();
|
---|
| 946 | if (PUNC_CHARS.has(ch)) return token("punc", next());
|
---|
| 947 | if (OPERATOR_CHARS.has(ch)) return read_operator();
|
---|
| 948 | if (code == 92 || is_identifier_start(ch)) return read_word();
|
---|
| 949 | if (code == 35) return read_private_word();
|
---|
| 950 | break;
|
---|
| 951 | }
|
---|
| 952 | parse_error("Unexpected character '" + ch + "'");
|
---|
| 953 | }
|
---|
| 954 |
|
---|
| 955 | next_token.next = next;
|
---|
| 956 | next_token.peek = peek;
|
---|
| 957 |
|
---|
| 958 | next_token.context = function(nc) {
|
---|
| 959 | if (nc) S = nc;
|
---|
| 960 | return S;
|
---|
| 961 | };
|
---|
| 962 |
|
---|
| 963 | next_token.add_directive = function(directive) {
|
---|
| 964 | S.directive_stack[S.directive_stack.length - 1].push(directive);
|
---|
| 965 |
|
---|
| 966 | if (S.directives[directive] === undefined) {
|
---|
| 967 | S.directives[directive] = 1;
|
---|
| 968 | } else {
|
---|
| 969 | S.directives[directive]++;
|
---|
| 970 | }
|
---|
| 971 | };
|
---|
| 972 |
|
---|
| 973 | next_token.push_directives_stack = function() {
|
---|
| 974 | S.directive_stack.push([]);
|
---|
| 975 | };
|
---|
| 976 |
|
---|
| 977 | next_token.pop_directives_stack = function() {
|
---|
| 978 | var directives = S.directive_stack[S.directive_stack.length - 1];
|
---|
| 979 |
|
---|
| 980 | for (var i = 0; i < directives.length; i++) {
|
---|
| 981 | S.directives[directives[i]]--;
|
---|
| 982 | }
|
---|
| 983 |
|
---|
| 984 | S.directive_stack.pop();
|
---|
| 985 | };
|
---|
| 986 |
|
---|
| 987 | next_token.has_directive = function(directive) {
|
---|
| 988 | return S.directives[directive] > 0;
|
---|
| 989 | };
|
---|
| 990 |
|
---|
| 991 | return next_token;
|
---|
| 992 |
|
---|
| 993 | }
|
---|
| 994 |
|
---|
| 995 | /* -----[ Parser (constants) ]----- */
|
---|
| 996 |
|
---|
| 997 | var UNARY_PREFIX = makePredicate([
|
---|
| 998 | "typeof",
|
---|
| 999 | "void",
|
---|
| 1000 | "delete",
|
---|
| 1001 | "--",
|
---|
| 1002 | "++",
|
---|
| 1003 | "!",
|
---|
| 1004 | "~",
|
---|
| 1005 | "-",
|
---|
| 1006 | "+"
|
---|
| 1007 | ]);
|
---|
| 1008 |
|
---|
| 1009 | var UNARY_POSTFIX = makePredicate([ "--", "++" ]);
|
---|
| 1010 |
|
---|
| 1011 | var ASSIGNMENT = makePredicate([ "=", "+=", "-=", "??=", "&&=", "||=", "/=", "*=", "**=", "%=", ">>=", "<<=", ">>>=", "|=", "^=", "&=" ]);
|
---|
| 1012 |
|
---|
| 1013 | var LOGICAL_ASSIGNMENT = makePredicate([ "??=", "&&=", "||=" ]);
|
---|
| 1014 |
|
---|
| 1015 | var PRECEDENCE = (function(a, ret) {
|
---|
| 1016 | for (var i = 0; i < a.length; ++i) {
|
---|
| 1017 | var b = a[i];
|
---|
| 1018 | for (var j = 0; j < b.length; ++j) {
|
---|
| 1019 | ret[b[j]] = i + 1;
|
---|
| 1020 | }
|
---|
| 1021 | }
|
---|
| 1022 | return ret;
|
---|
| 1023 | })(
|
---|
| 1024 | [
|
---|
| 1025 | ["||"],
|
---|
| 1026 | ["??"],
|
---|
| 1027 | ["&&"],
|
---|
| 1028 | ["|"],
|
---|
| 1029 | ["^"],
|
---|
| 1030 | ["&"],
|
---|
| 1031 | ["==", "===", "!=", "!=="],
|
---|
| 1032 | ["<", ">", "<=", ">=", "in", "instanceof"],
|
---|
| 1033 | [">>", "<<", ">>>"],
|
---|
| 1034 | ["+", "-"],
|
---|
| 1035 | ["*", "/", "%"],
|
---|
| 1036 | ["**"]
|
---|
| 1037 | ],
|
---|
| 1038 | {}
|
---|
| 1039 | );
|
---|
| 1040 |
|
---|
| 1041 | var ATOMIC_START_TOKEN = makePredicate([ "atom", "num", "big_int", "string", "regexp", "name" ]);
|
---|
| 1042 |
|
---|
| 1043 | /* -----[ Parser ]----- */
|
---|
| 1044 |
|
---|
| 1045 | function parse($TEXT, options) {
|
---|
| 1046 | // maps start tokens to count of comments found outside of their parens
|
---|
| 1047 | // Example: /* I count */ ( /* I don't */ foo() )
|
---|
| 1048 | // Useful because comments_before property of call with parens outside
|
---|
| 1049 | // contains both comments inside and outside these parens. Used to find the
|
---|
| 1050 | // right #__PURE__ comments for an expression
|
---|
| 1051 | const outer_comments_before_counts = new WeakMap();
|
---|
| 1052 |
|
---|
| 1053 | options = defaults(options, {
|
---|
| 1054 | bare_returns : false,
|
---|
| 1055 | ecma : null, // Legacy
|
---|
| 1056 | expression : false,
|
---|
| 1057 | filename : null,
|
---|
| 1058 | html5_comments : true,
|
---|
| 1059 | module : false,
|
---|
| 1060 | shebang : true,
|
---|
| 1061 | strict : false,
|
---|
| 1062 | toplevel : null,
|
---|
| 1063 | }, true);
|
---|
| 1064 |
|
---|
| 1065 | var S = {
|
---|
| 1066 | input : (typeof $TEXT == "string"
|
---|
| 1067 | ? tokenizer($TEXT, options.filename,
|
---|
| 1068 | options.html5_comments, options.shebang)
|
---|
| 1069 | : $TEXT),
|
---|
| 1070 | token : null,
|
---|
| 1071 | prev : null,
|
---|
| 1072 | peeked : null,
|
---|
| 1073 | in_function : 0,
|
---|
| 1074 | in_async : -1,
|
---|
| 1075 | in_generator : -1,
|
---|
| 1076 | in_directives : true,
|
---|
| 1077 | in_loop : 0,
|
---|
| 1078 | labels : []
|
---|
| 1079 | };
|
---|
| 1080 |
|
---|
| 1081 | S.token = next();
|
---|
| 1082 |
|
---|
| 1083 | function is(type, value) {
|
---|
| 1084 | return is_token(S.token, type, value);
|
---|
| 1085 | }
|
---|
| 1086 |
|
---|
| 1087 | function peek() { return S.peeked || (S.peeked = S.input()); }
|
---|
| 1088 |
|
---|
| 1089 | function next() {
|
---|
| 1090 | S.prev = S.token;
|
---|
| 1091 |
|
---|
| 1092 | if (!S.peeked) peek();
|
---|
| 1093 | S.token = S.peeked;
|
---|
| 1094 | S.peeked = null;
|
---|
| 1095 | S.in_directives = S.in_directives && (
|
---|
| 1096 | S.token.type == "string" || is("punc", ";")
|
---|
| 1097 | );
|
---|
| 1098 | return S.token;
|
---|
| 1099 | }
|
---|
| 1100 |
|
---|
| 1101 | function prev() {
|
---|
| 1102 | return S.prev;
|
---|
| 1103 | }
|
---|
| 1104 |
|
---|
| 1105 | function croak(msg, line, col, pos) {
|
---|
| 1106 | var ctx = S.input.context();
|
---|
| 1107 | js_error(msg,
|
---|
| 1108 | ctx.filename,
|
---|
| 1109 | line != null ? line : ctx.tokline,
|
---|
| 1110 | col != null ? col : ctx.tokcol,
|
---|
| 1111 | pos != null ? pos : ctx.tokpos);
|
---|
| 1112 | }
|
---|
| 1113 |
|
---|
| 1114 | function token_error(token, msg) {
|
---|
| 1115 | croak(msg, token.line, token.col);
|
---|
| 1116 | }
|
---|
| 1117 |
|
---|
| 1118 | function unexpected(token) {
|
---|
| 1119 | if (token == null)
|
---|
| 1120 | token = S.token;
|
---|
| 1121 | token_error(token, "Unexpected token: " + token.type + " (" + token.value + ")");
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | function expect_token(type, val) {
|
---|
| 1125 | if (is(type, val)) {
|
---|
| 1126 | return next();
|
---|
| 1127 | }
|
---|
| 1128 | token_error(S.token, "Unexpected token " + S.token.type + " «" + S.token.value + "»" + ", expected " + type + " «" + val + "»");
|
---|
| 1129 | }
|
---|
| 1130 |
|
---|
| 1131 | function expect(punc) { return expect_token("punc", punc); }
|
---|
| 1132 |
|
---|
| 1133 | function has_newline_before(token) {
|
---|
| 1134 | return token.nlb || !token.comments_before.every((comment) => !comment.nlb);
|
---|
| 1135 | }
|
---|
| 1136 |
|
---|
| 1137 | function can_insert_semicolon() {
|
---|
| 1138 | return !options.strict
|
---|
| 1139 | && (is("eof") || is("punc", "}") || has_newline_before(S.token));
|
---|
| 1140 | }
|
---|
| 1141 |
|
---|
| 1142 | function is_in_generator() {
|
---|
| 1143 | return S.in_generator === S.in_function;
|
---|
| 1144 | }
|
---|
| 1145 |
|
---|
| 1146 | function is_in_async() {
|
---|
| 1147 | return S.in_async === S.in_function;
|
---|
| 1148 | }
|
---|
| 1149 |
|
---|
| 1150 | function can_await() {
|
---|
| 1151 | return (
|
---|
| 1152 | S.in_async === S.in_function
|
---|
| 1153 | || S.in_function === 0 && S.input.has_directive("use strict")
|
---|
| 1154 | );
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | function semicolon(optional) {
|
---|
| 1158 | if (is("punc", ";")) next();
|
---|
| 1159 | else if (!optional && !can_insert_semicolon()) unexpected();
|
---|
| 1160 | }
|
---|
| 1161 |
|
---|
| 1162 | function parenthesised() {
|
---|
| 1163 | expect("(");
|
---|
| 1164 | var exp = expression(true);
|
---|
| 1165 | expect(")");
|
---|
| 1166 | return exp;
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | function embed_tokens(parser) {
|
---|
| 1170 | return function _embed_tokens_wrapper(...args) {
|
---|
| 1171 | const start = S.token;
|
---|
| 1172 | const expr = parser(...args);
|
---|
| 1173 | expr.start = start;
|
---|
| 1174 | expr.end = prev();
|
---|
| 1175 | return expr;
|
---|
| 1176 | };
|
---|
| 1177 | }
|
---|
| 1178 |
|
---|
| 1179 | function handle_regexp() {
|
---|
| 1180 | if (is("operator", "/") || is("operator", "/=")) {
|
---|
| 1181 | S.peeked = null;
|
---|
| 1182 | S.token = S.input(S.token.value.substr(1)); // force regexp
|
---|
| 1183 | }
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
| 1186 | var statement = embed_tokens(function statement(is_export_default, is_for_body, is_if_body) {
|
---|
| 1187 | handle_regexp();
|
---|
| 1188 | switch (S.token.type) {
|
---|
| 1189 | case "string":
|
---|
| 1190 | if (S.in_directives) {
|
---|
| 1191 | var token = peek();
|
---|
| 1192 | if (!LATEST_RAW.includes("\\")
|
---|
| 1193 | && (is_token(token, "punc", ";")
|
---|
| 1194 | || is_token(token, "punc", "}")
|
---|
| 1195 | || has_newline_before(token)
|
---|
| 1196 | || is_token(token, "eof"))) {
|
---|
| 1197 | S.input.add_directive(S.token.value);
|
---|
| 1198 | } else {
|
---|
| 1199 | S.in_directives = false;
|
---|
| 1200 | }
|
---|
| 1201 | }
|
---|
| 1202 | var dir = S.in_directives, stat = simple_statement();
|
---|
| 1203 | return dir && stat.body instanceof AST_String ? new AST_Directive(stat.body) : stat;
|
---|
| 1204 | case "template_head":
|
---|
| 1205 | case "num":
|
---|
| 1206 | case "big_int":
|
---|
| 1207 | case "regexp":
|
---|
| 1208 | case "operator":
|
---|
| 1209 | case "atom":
|
---|
| 1210 | return simple_statement();
|
---|
| 1211 |
|
---|
| 1212 | case "name":
|
---|
| 1213 | if (S.token.value == "async" && is_token(peek(), "keyword", "function")) {
|
---|
| 1214 | next();
|
---|
| 1215 | next();
|
---|
| 1216 | if (is_for_body) {
|
---|
| 1217 | croak("functions are not allowed as the body of a loop");
|
---|
| 1218 | }
|
---|
| 1219 | return function_(AST_Defun, false, true, is_export_default);
|
---|
| 1220 | }
|
---|
| 1221 | if (S.token.value == "import" && !is_token(peek(), "punc", "(") && !is_token(peek(), "punc", ".")) {
|
---|
| 1222 | next();
|
---|
| 1223 | var node = import_();
|
---|
| 1224 | semicolon();
|
---|
| 1225 | return node;
|
---|
| 1226 | }
|
---|
| 1227 | return is_token(peek(), "punc", ":")
|
---|
| 1228 | ? labeled_statement()
|
---|
| 1229 | : simple_statement();
|
---|
| 1230 |
|
---|
| 1231 | case "punc":
|
---|
| 1232 | switch (S.token.value) {
|
---|
| 1233 | case "{":
|
---|
| 1234 | return new AST_BlockStatement({
|
---|
| 1235 | start : S.token,
|
---|
| 1236 | body : block_(),
|
---|
| 1237 | end : prev()
|
---|
| 1238 | });
|
---|
| 1239 | case "[":
|
---|
| 1240 | case "(":
|
---|
| 1241 | return simple_statement();
|
---|
| 1242 | case ";":
|
---|
| 1243 | S.in_directives = false;
|
---|
| 1244 | next();
|
---|
| 1245 | return new AST_EmptyStatement();
|
---|
| 1246 | default:
|
---|
| 1247 | unexpected();
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
| 1250 | case "keyword":
|
---|
| 1251 | switch (S.token.value) {
|
---|
| 1252 | case "break":
|
---|
| 1253 | next();
|
---|
| 1254 | return break_cont(AST_Break);
|
---|
| 1255 |
|
---|
| 1256 | case "continue":
|
---|
| 1257 | next();
|
---|
| 1258 | return break_cont(AST_Continue);
|
---|
| 1259 |
|
---|
| 1260 | case "debugger":
|
---|
| 1261 | next();
|
---|
| 1262 | semicolon();
|
---|
| 1263 | return new AST_Debugger();
|
---|
| 1264 |
|
---|
| 1265 | case "do":
|
---|
| 1266 | next();
|
---|
| 1267 | var body = in_loop(statement);
|
---|
| 1268 | expect_token("keyword", "while");
|
---|
| 1269 | var condition = parenthesised();
|
---|
| 1270 | semicolon(true);
|
---|
| 1271 | return new AST_Do({
|
---|
| 1272 | body : body,
|
---|
| 1273 | condition : condition
|
---|
| 1274 | });
|
---|
| 1275 |
|
---|
| 1276 | case "while":
|
---|
| 1277 | next();
|
---|
| 1278 | return new AST_While({
|
---|
| 1279 | condition : parenthesised(),
|
---|
| 1280 | body : in_loop(function() { return statement(false, true); })
|
---|
| 1281 | });
|
---|
| 1282 |
|
---|
| 1283 | case "for":
|
---|
| 1284 | next();
|
---|
| 1285 | return for_();
|
---|
| 1286 |
|
---|
| 1287 | case "class":
|
---|
| 1288 | next();
|
---|
| 1289 | if (is_for_body) {
|
---|
| 1290 | croak("classes are not allowed as the body of a loop");
|
---|
| 1291 | }
|
---|
| 1292 | if (is_if_body) {
|
---|
| 1293 | croak("classes are not allowed as the body of an if");
|
---|
| 1294 | }
|
---|
| 1295 | return class_(AST_DefClass, is_export_default);
|
---|
| 1296 |
|
---|
| 1297 | case "function":
|
---|
| 1298 | next();
|
---|
| 1299 | if (is_for_body) {
|
---|
| 1300 | croak("functions are not allowed as the body of a loop");
|
---|
| 1301 | }
|
---|
| 1302 | return function_(AST_Defun, false, false, is_export_default);
|
---|
| 1303 |
|
---|
| 1304 | case "if":
|
---|
| 1305 | next();
|
---|
| 1306 | return if_();
|
---|
| 1307 |
|
---|
| 1308 | case "return":
|
---|
| 1309 | if (S.in_function == 0 && !options.bare_returns)
|
---|
| 1310 | croak("'return' outside of function");
|
---|
| 1311 | next();
|
---|
| 1312 | var value = null;
|
---|
| 1313 | if (is("punc", ";")) {
|
---|
| 1314 | next();
|
---|
| 1315 | } else if (!can_insert_semicolon()) {
|
---|
| 1316 | value = expression(true);
|
---|
| 1317 | semicolon();
|
---|
| 1318 | }
|
---|
| 1319 | return new AST_Return({
|
---|
| 1320 | value: value
|
---|
| 1321 | });
|
---|
| 1322 |
|
---|
| 1323 | case "switch":
|
---|
| 1324 | next();
|
---|
| 1325 | return new AST_Switch({
|
---|
| 1326 | expression : parenthesised(),
|
---|
| 1327 | body : in_loop(switch_body_)
|
---|
| 1328 | });
|
---|
| 1329 |
|
---|
| 1330 | case "throw":
|
---|
| 1331 | next();
|
---|
| 1332 | if (has_newline_before(S.token))
|
---|
| 1333 | croak("Illegal newline after 'throw'");
|
---|
| 1334 | var value = expression(true);
|
---|
| 1335 | semicolon();
|
---|
| 1336 | return new AST_Throw({
|
---|
| 1337 | value: value
|
---|
| 1338 | });
|
---|
| 1339 |
|
---|
| 1340 | case "try":
|
---|
| 1341 | next();
|
---|
| 1342 | return try_();
|
---|
| 1343 |
|
---|
| 1344 | case "var":
|
---|
| 1345 | next();
|
---|
| 1346 | var node = var_();
|
---|
| 1347 | semicolon();
|
---|
| 1348 | return node;
|
---|
| 1349 |
|
---|
| 1350 | case "let":
|
---|
| 1351 | next();
|
---|
| 1352 | var node = let_();
|
---|
| 1353 | semicolon();
|
---|
| 1354 | return node;
|
---|
| 1355 |
|
---|
| 1356 | case "const":
|
---|
| 1357 | next();
|
---|
| 1358 | var node = const_();
|
---|
| 1359 | semicolon();
|
---|
| 1360 | return node;
|
---|
| 1361 |
|
---|
| 1362 | case "with":
|
---|
| 1363 | if (S.input.has_directive("use strict")) {
|
---|
| 1364 | croak("Strict mode may not include a with statement");
|
---|
| 1365 | }
|
---|
| 1366 | next();
|
---|
| 1367 | return new AST_With({
|
---|
| 1368 | expression : parenthesised(),
|
---|
| 1369 | body : statement()
|
---|
| 1370 | });
|
---|
| 1371 |
|
---|
| 1372 | case "export":
|
---|
| 1373 | if (!is_token(peek(), "punc", "(")) {
|
---|
| 1374 | next();
|
---|
| 1375 | var node = export_();
|
---|
| 1376 | if (is("punc", ";")) semicolon();
|
---|
| 1377 | return node;
|
---|
| 1378 | }
|
---|
| 1379 | }
|
---|
| 1380 | }
|
---|
| 1381 | unexpected();
|
---|
| 1382 | });
|
---|
| 1383 |
|
---|
| 1384 | function labeled_statement() {
|
---|
| 1385 | var label = as_symbol(AST_Label);
|
---|
| 1386 | if (label.name === "await" && is_in_async()) {
|
---|
| 1387 | token_error(S.prev, "await cannot be used as label inside async function");
|
---|
| 1388 | }
|
---|
| 1389 | if (S.labels.some((l) => l.name === label.name)) {
|
---|
| 1390 | // ECMA-262, 12.12: An ECMAScript program is considered
|
---|
| 1391 | // syntactically incorrect if it contains a
|
---|
| 1392 | // LabelledStatement that is enclosed by a
|
---|
| 1393 | // LabelledStatement with the same Identifier as label.
|
---|
| 1394 | croak("Label " + label.name + " defined twice");
|
---|
| 1395 | }
|
---|
| 1396 | expect(":");
|
---|
| 1397 | S.labels.push(label);
|
---|
| 1398 | var stat = statement();
|
---|
| 1399 | S.labels.pop();
|
---|
| 1400 | if (!(stat instanceof AST_IterationStatement)) {
|
---|
| 1401 | // check for `continue` that refers to this label.
|
---|
| 1402 | // those should be reported as syntax errors.
|
---|
| 1403 | // https://github.com/mishoo/UglifyJS2/issues/287
|
---|
| 1404 | label.references.forEach(function(ref) {
|
---|
| 1405 | if (ref instanceof AST_Continue) {
|
---|
| 1406 | ref = ref.label.start;
|
---|
| 1407 | croak("Continue label `" + label.name + "` refers to non-IterationStatement.",
|
---|
| 1408 | ref.line, ref.col, ref.pos);
|
---|
| 1409 | }
|
---|
| 1410 | });
|
---|
| 1411 | }
|
---|
| 1412 | return new AST_LabeledStatement({ body: stat, label: label });
|
---|
| 1413 | }
|
---|
| 1414 |
|
---|
| 1415 | function simple_statement(tmp) {
|
---|
| 1416 | return new AST_SimpleStatement({ body: (tmp = expression(true), semicolon(), tmp) });
|
---|
| 1417 | }
|
---|
| 1418 |
|
---|
| 1419 | function break_cont(type) {
|
---|
| 1420 | var label = null, ldef;
|
---|
| 1421 | if (!can_insert_semicolon()) {
|
---|
| 1422 | label = as_symbol(AST_LabelRef, true);
|
---|
| 1423 | }
|
---|
| 1424 | if (label != null) {
|
---|
| 1425 | ldef = S.labels.find((l) => l.name === label.name);
|
---|
| 1426 | if (!ldef)
|
---|
| 1427 | croak("Undefined label " + label.name);
|
---|
| 1428 | label.thedef = ldef;
|
---|
| 1429 | } else if (S.in_loop == 0)
|
---|
| 1430 | croak(type.TYPE + " not inside a loop or switch");
|
---|
| 1431 | semicolon();
|
---|
| 1432 | var stat = new type({ label: label });
|
---|
| 1433 | if (ldef) ldef.references.push(stat);
|
---|
| 1434 | return stat;
|
---|
| 1435 | }
|
---|
| 1436 |
|
---|
| 1437 | function for_() {
|
---|
| 1438 | var for_await_error = "`for await` invalid in this context";
|
---|
| 1439 | var await_tok = S.token;
|
---|
| 1440 | if (await_tok.type == "name" && await_tok.value == "await") {
|
---|
| 1441 | if (!can_await()) {
|
---|
| 1442 | token_error(await_tok, for_await_error);
|
---|
| 1443 | }
|
---|
| 1444 | next();
|
---|
| 1445 | } else {
|
---|
| 1446 | await_tok = false;
|
---|
| 1447 | }
|
---|
| 1448 | expect("(");
|
---|
| 1449 | var init = null;
|
---|
| 1450 | if (!is("punc", ";")) {
|
---|
| 1451 | init =
|
---|
| 1452 | is("keyword", "var") ? (next(), var_(true)) :
|
---|
| 1453 | is("keyword", "let") ? (next(), let_(true)) :
|
---|
| 1454 | is("keyword", "const") ? (next(), const_(true)) :
|
---|
| 1455 | expression(true, true);
|
---|
| 1456 | var is_in = is("operator", "in");
|
---|
| 1457 | var is_of = is("name", "of");
|
---|
| 1458 | if (await_tok && !is_of) {
|
---|
| 1459 | token_error(await_tok, for_await_error);
|
---|
| 1460 | }
|
---|
| 1461 | if (is_in || is_of) {
|
---|
| 1462 | if (init instanceof AST_Definitions) {
|
---|
| 1463 | if (init.definitions.length > 1)
|
---|
| 1464 | token_error(init.start, "Only one variable declaration allowed in for..in loop");
|
---|
| 1465 | } else if (!(is_assignable(init) || (init = to_destructuring(init)) instanceof AST_Destructuring)) {
|
---|
| 1466 | token_error(init.start, "Invalid left-hand side in for..in loop");
|
---|
| 1467 | }
|
---|
| 1468 | next();
|
---|
| 1469 | if (is_in) {
|
---|
| 1470 | return for_in(init);
|
---|
| 1471 | } else {
|
---|
| 1472 | return for_of(init, !!await_tok);
|
---|
| 1473 | }
|
---|
| 1474 | }
|
---|
| 1475 | } else if (await_tok) {
|
---|
| 1476 | token_error(await_tok, for_await_error);
|
---|
| 1477 | }
|
---|
| 1478 | return regular_for(init);
|
---|
| 1479 | }
|
---|
| 1480 |
|
---|
| 1481 | function regular_for(init) {
|
---|
| 1482 | expect(";");
|
---|
| 1483 | var test = is("punc", ";") ? null : expression(true);
|
---|
| 1484 | expect(";");
|
---|
| 1485 | var step = is("punc", ")") ? null : expression(true);
|
---|
| 1486 | expect(")");
|
---|
| 1487 | return new AST_For({
|
---|
| 1488 | init : init,
|
---|
| 1489 | condition : test,
|
---|
| 1490 | step : step,
|
---|
| 1491 | body : in_loop(function() { return statement(false, true); })
|
---|
| 1492 | });
|
---|
| 1493 | }
|
---|
| 1494 |
|
---|
| 1495 | function for_of(init, is_await) {
|
---|
| 1496 | var lhs = init instanceof AST_Definitions ? init.definitions[0].name : null;
|
---|
| 1497 | var obj = expression(true);
|
---|
| 1498 | expect(")");
|
---|
| 1499 | return new AST_ForOf({
|
---|
| 1500 | await : is_await,
|
---|
| 1501 | init : init,
|
---|
| 1502 | name : lhs,
|
---|
| 1503 | object : obj,
|
---|
| 1504 | body : in_loop(function() { return statement(false, true); })
|
---|
| 1505 | });
|
---|
| 1506 | }
|
---|
| 1507 |
|
---|
| 1508 | function for_in(init) {
|
---|
| 1509 | var obj = expression(true);
|
---|
| 1510 | expect(")");
|
---|
| 1511 | return new AST_ForIn({
|
---|
| 1512 | init : init,
|
---|
| 1513 | object : obj,
|
---|
| 1514 | body : in_loop(function() { return statement(false, true); })
|
---|
| 1515 | });
|
---|
| 1516 | }
|
---|
| 1517 |
|
---|
| 1518 | var arrow_function = function(start, argnames, is_async) {
|
---|
| 1519 | if (has_newline_before(S.token)) {
|
---|
| 1520 | croak("Unexpected newline before arrow (=>)");
|
---|
| 1521 | }
|
---|
| 1522 |
|
---|
| 1523 | expect_token("arrow", "=>");
|
---|
| 1524 |
|
---|
| 1525 | var body = _function_body(is("punc", "{"), false, is_async);
|
---|
| 1526 |
|
---|
| 1527 | var end =
|
---|
| 1528 | body instanceof Array && body.length ? body[body.length - 1].end :
|
---|
| 1529 | body instanceof Array ? start :
|
---|
| 1530 | body.end;
|
---|
| 1531 |
|
---|
| 1532 | return new AST_Arrow({
|
---|
| 1533 | start : start,
|
---|
| 1534 | end : end,
|
---|
| 1535 | async : is_async,
|
---|
| 1536 | argnames : argnames,
|
---|
| 1537 | body : body
|
---|
| 1538 | });
|
---|
| 1539 | };
|
---|
| 1540 |
|
---|
| 1541 | var function_ = function(ctor, is_generator_property, is_async, is_export_default) {
|
---|
| 1542 | var in_statement = ctor === AST_Defun;
|
---|
| 1543 | var is_generator = is("operator", "*");
|
---|
| 1544 | if (is_generator) {
|
---|
| 1545 | next();
|
---|
| 1546 | }
|
---|
| 1547 |
|
---|
| 1548 | var name = is("name") ? as_symbol(in_statement ? AST_SymbolDefun : AST_SymbolLambda) : null;
|
---|
| 1549 | if (in_statement && !name) {
|
---|
| 1550 | if (is_export_default) {
|
---|
| 1551 | ctor = AST_Function;
|
---|
| 1552 | } else {
|
---|
| 1553 | unexpected();
|
---|
| 1554 | }
|
---|
| 1555 | }
|
---|
| 1556 |
|
---|
| 1557 | if (name && ctor !== AST_Accessor && !(name instanceof AST_SymbolDeclaration))
|
---|
| 1558 | unexpected(prev());
|
---|
| 1559 |
|
---|
| 1560 | var args = [];
|
---|
| 1561 | var body = _function_body(true, is_generator || is_generator_property, is_async, name, args);
|
---|
| 1562 | return new ctor({
|
---|
| 1563 | start : args.start,
|
---|
| 1564 | end : body.end,
|
---|
| 1565 | is_generator: is_generator,
|
---|
| 1566 | async : is_async,
|
---|
| 1567 | name : name,
|
---|
| 1568 | argnames: args,
|
---|
| 1569 | body : body
|
---|
| 1570 | });
|
---|
| 1571 | };
|
---|
| 1572 |
|
---|
| 1573 | function track_used_binding_identifiers(is_parameter, strict) {
|
---|
| 1574 | var parameters = new Set();
|
---|
| 1575 | var duplicate = false;
|
---|
| 1576 | var default_assignment = false;
|
---|
| 1577 | var spread = false;
|
---|
| 1578 | var strict_mode = !!strict;
|
---|
| 1579 | var tracker = {
|
---|
| 1580 | add_parameter: function(token) {
|
---|
| 1581 | if (parameters.has(token.value)) {
|
---|
| 1582 | if (duplicate === false) {
|
---|
| 1583 | duplicate = token;
|
---|
| 1584 | }
|
---|
| 1585 | tracker.check_strict();
|
---|
| 1586 | } else {
|
---|
| 1587 | parameters.add(token.value);
|
---|
| 1588 | if (is_parameter) {
|
---|
| 1589 | switch (token.value) {
|
---|
| 1590 | case "arguments":
|
---|
| 1591 | case "eval":
|
---|
| 1592 | case "yield":
|
---|
| 1593 | if (strict_mode) {
|
---|
| 1594 | token_error(token, "Unexpected " + token.value + " identifier as parameter inside strict mode");
|
---|
| 1595 | }
|
---|
| 1596 | break;
|
---|
| 1597 | default:
|
---|
| 1598 | if (RESERVED_WORDS.has(token.value)) {
|
---|
| 1599 | unexpected();
|
---|
| 1600 | }
|
---|
| 1601 | }
|
---|
| 1602 | }
|
---|
| 1603 | }
|
---|
| 1604 | },
|
---|
| 1605 | mark_default_assignment: function(token) {
|
---|
| 1606 | if (default_assignment === false) {
|
---|
| 1607 | default_assignment = token;
|
---|
| 1608 | }
|
---|
| 1609 | },
|
---|
| 1610 | mark_spread: function(token) {
|
---|
| 1611 | if (spread === false) {
|
---|
| 1612 | spread = token;
|
---|
| 1613 | }
|
---|
| 1614 | },
|
---|
| 1615 | mark_strict_mode: function() {
|
---|
| 1616 | strict_mode = true;
|
---|
| 1617 | },
|
---|
| 1618 | is_strict: function() {
|
---|
| 1619 | return default_assignment !== false || spread !== false || strict_mode;
|
---|
| 1620 | },
|
---|
| 1621 | check_strict: function() {
|
---|
| 1622 | if (tracker.is_strict() && duplicate !== false) {
|
---|
| 1623 | token_error(duplicate, "Parameter " + duplicate.value + " was used already");
|
---|
| 1624 | }
|
---|
| 1625 | }
|
---|
| 1626 | };
|
---|
| 1627 |
|
---|
| 1628 | return tracker;
|
---|
| 1629 | }
|
---|
| 1630 |
|
---|
| 1631 | function parameters(params) {
|
---|
| 1632 | var used_parameters = track_used_binding_identifiers(true, S.input.has_directive("use strict"));
|
---|
| 1633 |
|
---|
| 1634 | expect("(");
|
---|
| 1635 |
|
---|
| 1636 | while (!is("punc", ")")) {
|
---|
| 1637 | var param = parameter(used_parameters);
|
---|
| 1638 | params.push(param);
|
---|
| 1639 |
|
---|
| 1640 | if (!is("punc", ")")) {
|
---|
| 1641 | expect(",");
|
---|
| 1642 | }
|
---|
| 1643 |
|
---|
| 1644 | if (param instanceof AST_Expansion) {
|
---|
| 1645 | break;
|
---|
| 1646 | }
|
---|
| 1647 | }
|
---|
| 1648 |
|
---|
| 1649 | next();
|
---|
| 1650 | }
|
---|
| 1651 |
|
---|
| 1652 | function parameter(used_parameters, symbol_type) {
|
---|
| 1653 | var param;
|
---|
| 1654 | var expand = false;
|
---|
| 1655 | if (used_parameters === undefined) {
|
---|
| 1656 | used_parameters = track_used_binding_identifiers(true, S.input.has_directive("use strict"));
|
---|
| 1657 | }
|
---|
| 1658 | if (is("expand", "...")) {
|
---|
| 1659 | expand = S.token;
|
---|
| 1660 | used_parameters.mark_spread(S.token);
|
---|
| 1661 | next();
|
---|
| 1662 | }
|
---|
| 1663 | param = binding_element(used_parameters, symbol_type);
|
---|
| 1664 |
|
---|
| 1665 | if (is("operator", "=") && expand === false) {
|
---|
| 1666 | used_parameters.mark_default_assignment(S.token);
|
---|
| 1667 | next();
|
---|
| 1668 | param = new AST_DefaultAssign({
|
---|
| 1669 | start: param.start,
|
---|
| 1670 | left: param,
|
---|
| 1671 | operator: "=",
|
---|
| 1672 | right: expression(false),
|
---|
| 1673 | end: S.token
|
---|
| 1674 | });
|
---|
| 1675 | }
|
---|
| 1676 |
|
---|
| 1677 | if (expand !== false) {
|
---|
| 1678 | if (!is("punc", ")")) {
|
---|
| 1679 | unexpected();
|
---|
| 1680 | }
|
---|
| 1681 | param = new AST_Expansion({
|
---|
| 1682 | start: expand,
|
---|
| 1683 | expression: param,
|
---|
| 1684 | end: expand
|
---|
| 1685 | });
|
---|
| 1686 | }
|
---|
| 1687 | used_parameters.check_strict();
|
---|
| 1688 |
|
---|
| 1689 | return param;
|
---|
| 1690 | }
|
---|
| 1691 |
|
---|
| 1692 | function binding_element(used_parameters, symbol_type) {
|
---|
| 1693 | var elements = [];
|
---|
| 1694 | var first = true;
|
---|
| 1695 | var is_expand = false;
|
---|
| 1696 | var expand_token;
|
---|
| 1697 | var first_token = S.token;
|
---|
| 1698 | if (used_parameters === undefined) {
|
---|
| 1699 | used_parameters = track_used_binding_identifiers(false, S.input.has_directive("use strict"));
|
---|
| 1700 | }
|
---|
| 1701 | symbol_type = symbol_type === undefined ? AST_SymbolFunarg : symbol_type;
|
---|
| 1702 | if (is("punc", "[")) {
|
---|
| 1703 | next();
|
---|
| 1704 | while (!is("punc", "]")) {
|
---|
| 1705 | if (first) {
|
---|
| 1706 | first = false;
|
---|
| 1707 | } else {
|
---|
| 1708 | expect(",");
|
---|
| 1709 | }
|
---|
| 1710 |
|
---|
| 1711 | if (is("expand", "...")) {
|
---|
| 1712 | is_expand = true;
|
---|
| 1713 | expand_token = S.token;
|
---|
| 1714 | used_parameters.mark_spread(S.token);
|
---|
| 1715 | next();
|
---|
| 1716 | }
|
---|
| 1717 | if (is("punc")) {
|
---|
| 1718 | switch (S.token.value) {
|
---|
| 1719 | case ",":
|
---|
| 1720 | elements.push(new AST_Hole({
|
---|
| 1721 | start: S.token,
|
---|
| 1722 | end: S.token
|
---|
| 1723 | }));
|
---|
| 1724 | continue;
|
---|
| 1725 | case "]": // Trailing comma after last element
|
---|
| 1726 | break;
|
---|
| 1727 | case "[":
|
---|
| 1728 | case "{":
|
---|
| 1729 | elements.push(binding_element(used_parameters, symbol_type));
|
---|
| 1730 | break;
|
---|
| 1731 | default:
|
---|
| 1732 | unexpected();
|
---|
| 1733 | }
|
---|
| 1734 | } else if (is("name")) {
|
---|
| 1735 | used_parameters.add_parameter(S.token);
|
---|
| 1736 | elements.push(as_symbol(symbol_type));
|
---|
| 1737 | } else {
|
---|
| 1738 | croak("Invalid function parameter");
|
---|
| 1739 | }
|
---|
| 1740 | if (is("operator", "=") && is_expand === false) {
|
---|
| 1741 | used_parameters.mark_default_assignment(S.token);
|
---|
| 1742 | next();
|
---|
| 1743 | elements[elements.length - 1] = new AST_DefaultAssign({
|
---|
| 1744 | start: elements[elements.length - 1].start,
|
---|
| 1745 | left: elements[elements.length - 1],
|
---|
| 1746 | operator: "=",
|
---|
| 1747 | right: expression(false),
|
---|
| 1748 | end: S.token
|
---|
| 1749 | });
|
---|
| 1750 | }
|
---|
| 1751 | if (is_expand) {
|
---|
| 1752 | if (!is("punc", "]")) {
|
---|
| 1753 | croak("Rest element must be last element");
|
---|
| 1754 | }
|
---|
| 1755 | elements[elements.length - 1] = new AST_Expansion({
|
---|
| 1756 | start: expand_token,
|
---|
| 1757 | expression: elements[elements.length - 1],
|
---|
| 1758 | end: expand_token
|
---|
| 1759 | });
|
---|
| 1760 | }
|
---|
| 1761 | }
|
---|
| 1762 | expect("]");
|
---|
| 1763 | used_parameters.check_strict();
|
---|
| 1764 | return new AST_Destructuring({
|
---|
| 1765 | start: first_token,
|
---|
| 1766 | names: elements,
|
---|
| 1767 | is_array: true,
|
---|
| 1768 | end: prev()
|
---|
| 1769 | });
|
---|
| 1770 | } else if (is("punc", "{")) {
|
---|
| 1771 | next();
|
---|
| 1772 | while (!is("punc", "}")) {
|
---|
| 1773 | if (first) {
|
---|
| 1774 | first = false;
|
---|
| 1775 | } else {
|
---|
| 1776 | expect(",");
|
---|
| 1777 | }
|
---|
| 1778 | if (is("expand", "...")) {
|
---|
| 1779 | is_expand = true;
|
---|
| 1780 | expand_token = S.token;
|
---|
| 1781 | used_parameters.mark_spread(S.token);
|
---|
| 1782 | next();
|
---|
| 1783 | }
|
---|
| 1784 | if (is("name") && (is_token(peek(), "punc") || is_token(peek(), "operator")) && [",", "}", "="].includes(peek().value)) {
|
---|
| 1785 | used_parameters.add_parameter(S.token);
|
---|
| 1786 | var start = prev();
|
---|
| 1787 | var value = as_symbol(symbol_type);
|
---|
| 1788 | if (is_expand) {
|
---|
| 1789 | elements.push(new AST_Expansion({
|
---|
| 1790 | start: expand_token,
|
---|
| 1791 | expression: value,
|
---|
| 1792 | end: value.end,
|
---|
| 1793 | }));
|
---|
| 1794 | } else {
|
---|
| 1795 | elements.push(new AST_ObjectKeyVal({
|
---|
| 1796 | start: start,
|
---|
| 1797 | key: value.name,
|
---|
| 1798 | value: value,
|
---|
| 1799 | end: value.end,
|
---|
| 1800 | }));
|
---|
| 1801 | }
|
---|
| 1802 | } else if (is("punc", "}")) {
|
---|
| 1803 | continue; // Allow trailing hole
|
---|
| 1804 | } else {
|
---|
| 1805 | var property_token = S.token;
|
---|
| 1806 | var property = as_property_name();
|
---|
| 1807 | if (property === null) {
|
---|
| 1808 | unexpected(prev());
|
---|
| 1809 | } else if (prev().type === "name" && !is("punc", ":")) {
|
---|
| 1810 | elements.push(new AST_ObjectKeyVal({
|
---|
| 1811 | start: prev(),
|
---|
| 1812 | key: property,
|
---|
| 1813 | value: new symbol_type({
|
---|
| 1814 | start: prev(),
|
---|
| 1815 | name: property,
|
---|
| 1816 | end: prev()
|
---|
| 1817 | }),
|
---|
| 1818 | end: prev()
|
---|
| 1819 | }));
|
---|
| 1820 | } else {
|
---|
| 1821 | expect(":");
|
---|
| 1822 | elements.push(new AST_ObjectKeyVal({
|
---|
| 1823 | start: property_token,
|
---|
| 1824 | quote: property_token.quote,
|
---|
| 1825 | key: property,
|
---|
| 1826 | value: binding_element(used_parameters, symbol_type),
|
---|
| 1827 | end: prev()
|
---|
| 1828 | }));
|
---|
| 1829 | }
|
---|
| 1830 | }
|
---|
| 1831 | if (is_expand) {
|
---|
| 1832 | if (!is("punc", "}")) {
|
---|
| 1833 | croak("Rest element must be last element");
|
---|
| 1834 | }
|
---|
| 1835 | } else if (is("operator", "=")) {
|
---|
| 1836 | used_parameters.mark_default_assignment(S.token);
|
---|
| 1837 | next();
|
---|
| 1838 | elements[elements.length - 1].value = new AST_DefaultAssign({
|
---|
| 1839 | start: elements[elements.length - 1].value.start,
|
---|
| 1840 | left: elements[elements.length - 1].value,
|
---|
| 1841 | operator: "=",
|
---|
| 1842 | right: expression(false),
|
---|
| 1843 | end: S.token
|
---|
| 1844 | });
|
---|
| 1845 | }
|
---|
| 1846 | }
|
---|
| 1847 | expect("}");
|
---|
| 1848 | used_parameters.check_strict();
|
---|
| 1849 | return new AST_Destructuring({
|
---|
| 1850 | start: first_token,
|
---|
| 1851 | names: elements,
|
---|
| 1852 | is_array: false,
|
---|
| 1853 | end: prev()
|
---|
| 1854 | });
|
---|
| 1855 | } else if (is("name")) {
|
---|
| 1856 | used_parameters.add_parameter(S.token);
|
---|
| 1857 | return as_symbol(symbol_type);
|
---|
| 1858 | } else {
|
---|
| 1859 | croak("Invalid function parameter");
|
---|
| 1860 | }
|
---|
| 1861 | }
|
---|
| 1862 |
|
---|
| 1863 | function params_or_seq_(allow_arrows, maybe_sequence) {
|
---|
| 1864 | var spread_token;
|
---|
| 1865 | var invalid_sequence;
|
---|
| 1866 | var trailing_comma;
|
---|
| 1867 | var a = [];
|
---|
| 1868 | expect("(");
|
---|
| 1869 | while (!is("punc", ")")) {
|
---|
| 1870 | if (spread_token) unexpected(spread_token);
|
---|
| 1871 | if (is("expand", "...")) {
|
---|
| 1872 | spread_token = S.token;
|
---|
| 1873 | if (maybe_sequence) invalid_sequence = S.token;
|
---|
| 1874 | next();
|
---|
| 1875 | a.push(new AST_Expansion({
|
---|
| 1876 | start: prev(),
|
---|
| 1877 | expression: expression(),
|
---|
| 1878 | end: S.token,
|
---|
| 1879 | }));
|
---|
| 1880 | } else {
|
---|
| 1881 | a.push(expression());
|
---|
| 1882 | }
|
---|
| 1883 | if (!is("punc", ")")) {
|
---|
| 1884 | expect(",");
|
---|
| 1885 | if (is("punc", ")")) {
|
---|
| 1886 | trailing_comma = prev();
|
---|
| 1887 | if (maybe_sequence) invalid_sequence = trailing_comma;
|
---|
| 1888 | }
|
---|
| 1889 | }
|
---|
| 1890 | }
|
---|
| 1891 | expect(")");
|
---|
| 1892 | if (allow_arrows && is("arrow", "=>")) {
|
---|
| 1893 | if (spread_token && trailing_comma) unexpected(trailing_comma);
|
---|
| 1894 | } else if (invalid_sequence) {
|
---|
| 1895 | unexpected(invalid_sequence);
|
---|
| 1896 | }
|
---|
| 1897 | return a;
|
---|
| 1898 | }
|
---|
| 1899 |
|
---|
| 1900 | function _function_body(block, generator, is_async, name, args) {
|
---|
| 1901 | var loop = S.in_loop;
|
---|
| 1902 | var labels = S.labels;
|
---|
| 1903 | var current_generator = S.in_generator;
|
---|
| 1904 | var current_async = S.in_async;
|
---|
| 1905 | ++S.in_function;
|
---|
| 1906 | if (generator)
|
---|
| 1907 | S.in_generator = S.in_function;
|
---|
| 1908 | if (is_async)
|
---|
| 1909 | S.in_async = S.in_function;
|
---|
| 1910 | if (args) parameters(args);
|
---|
| 1911 | if (block)
|
---|
| 1912 | S.in_directives = true;
|
---|
| 1913 | S.in_loop = 0;
|
---|
| 1914 | S.labels = [];
|
---|
| 1915 | if (block) {
|
---|
| 1916 | S.input.push_directives_stack();
|
---|
| 1917 | var a = block_();
|
---|
| 1918 | if (name) _verify_symbol(name);
|
---|
| 1919 | if (args) args.forEach(_verify_symbol);
|
---|
| 1920 | S.input.pop_directives_stack();
|
---|
| 1921 | } else {
|
---|
| 1922 | var a = [new AST_Return({
|
---|
| 1923 | start: S.token,
|
---|
| 1924 | value: expression(false),
|
---|
| 1925 | end: S.token
|
---|
| 1926 | })];
|
---|
| 1927 | }
|
---|
| 1928 | --S.in_function;
|
---|
| 1929 | S.in_loop = loop;
|
---|
| 1930 | S.labels = labels;
|
---|
| 1931 | S.in_generator = current_generator;
|
---|
| 1932 | S.in_async = current_async;
|
---|
| 1933 | return a;
|
---|
| 1934 | }
|
---|
| 1935 |
|
---|
| 1936 | function _await_expression() {
|
---|
| 1937 | // Previous token must be "await" and not be interpreted as an identifier
|
---|
| 1938 | if (!can_await()) {
|
---|
| 1939 | croak("Unexpected await expression outside async function",
|
---|
| 1940 | S.prev.line, S.prev.col, S.prev.pos);
|
---|
| 1941 | }
|
---|
| 1942 | // the await expression is parsed as a unary expression in Babel
|
---|
| 1943 | return new AST_Await({
|
---|
| 1944 | start: prev(),
|
---|
| 1945 | end: S.token,
|
---|
| 1946 | expression : maybe_unary(true),
|
---|
| 1947 | });
|
---|
| 1948 | }
|
---|
| 1949 |
|
---|
| 1950 | function _yield_expression() {
|
---|
| 1951 | // Previous token must be keyword yield and not be interpret as an identifier
|
---|
| 1952 | if (!is_in_generator()) {
|
---|
| 1953 | croak("Unexpected yield expression outside generator function",
|
---|
| 1954 | S.prev.line, S.prev.col, S.prev.pos);
|
---|
| 1955 | }
|
---|
| 1956 | var start = S.token;
|
---|
| 1957 | var star = false;
|
---|
| 1958 | var has_expression = true;
|
---|
| 1959 |
|
---|
| 1960 | // Attempt to get expression or star (and then the mandatory expression)
|
---|
| 1961 | // behind yield on the same line.
|
---|
| 1962 | //
|
---|
| 1963 | // If nothing follows on the same line of the yieldExpression,
|
---|
| 1964 | // it should default to the value `undefined` for yield to return.
|
---|
| 1965 | // In that case, the `undefined` stored as `null` in ast.
|
---|
| 1966 | //
|
---|
| 1967 | // Note 1: It isn't allowed for yield* to close without an expression
|
---|
| 1968 | // Note 2: If there is a nlb between yield and star, it is interpret as
|
---|
| 1969 | // yield <explicit undefined> <inserted automatic semicolon> *
|
---|
| 1970 | if (can_insert_semicolon() ||
|
---|
| 1971 | (is("punc") && PUNC_AFTER_EXPRESSION.has(S.token.value))) {
|
---|
| 1972 | has_expression = false;
|
---|
| 1973 |
|
---|
| 1974 | } else if (is("operator", "*")) {
|
---|
| 1975 | star = true;
|
---|
| 1976 | next();
|
---|
| 1977 | }
|
---|
| 1978 |
|
---|
| 1979 | return new AST_Yield({
|
---|
| 1980 | start : start,
|
---|
| 1981 | is_star : star,
|
---|
| 1982 | expression : has_expression ? expression() : null,
|
---|
| 1983 | end : prev()
|
---|
| 1984 | });
|
---|
| 1985 | }
|
---|
| 1986 |
|
---|
| 1987 | function if_() {
|
---|
| 1988 | var cond = parenthesised(), body = statement(false, false, true), belse = null;
|
---|
| 1989 | if (is("keyword", "else")) {
|
---|
| 1990 | next();
|
---|
| 1991 | belse = statement(false, false, true);
|
---|
| 1992 | }
|
---|
| 1993 | return new AST_If({
|
---|
| 1994 | condition : cond,
|
---|
| 1995 | body : body,
|
---|
| 1996 | alternative : belse
|
---|
| 1997 | });
|
---|
| 1998 | }
|
---|
| 1999 |
|
---|
| 2000 | function block_() {
|
---|
| 2001 | expect("{");
|
---|
| 2002 | var a = [];
|
---|
| 2003 | while (!is("punc", "}")) {
|
---|
| 2004 | if (is("eof")) unexpected();
|
---|
| 2005 | a.push(statement());
|
---|
| 2006 | }
|
---|
| 2007 | next();
|
---|
| 2008 | return a;
|
---|
| 2009 | }
|
---|
| 2010 |
|
---|
| 2011 | function switch_body_() {
|
---|
| 2012 | expect("{");
|
---|
| 2013 | var a = [], cur = null, branch = null, tmp;
|
---|
| 2014 | while (!is("punc", "}")) {
|
---|
| 2015 | if (is("eof")) unexpected();
|
---|
| 2016 | if (is("keyword", "case")) {
|
---|
| 2017 | if (branch) branch.end = prev();
|
---|
| 2018 | cur = [];
|
---|
| 2019 | branch = new AST_Case({
|
---|
| 2020 | start : (tmp = S.token, next(), tmp),
|
---|
| 2021 | expression : expression(true),
|
---|
| 2022 | body : cur
|
---|
| 2023 | });
|
---|
| 2024 | a.push(branch);
|
---|
| 2025 | expect(":");
|
---|
| 2026 | } else if (is("keyword", "default")) {
|
---|
| 2027 | if (branch) branch.end = prev();
|
---|
| 2028 | cur = [];
|
---|
| 2029 | branch = new AST_Default({
|
---|
| 2030 | start : (tmp = S.token, next(), expect(":"), tmp),
|
---|
| 2031 | body : cur
|
---|
| 2032 | });
|
---|
| 2033 | a.push(branch);
|
---|
| 2034 | } else {
|
---|
| 2035 | if (!cur) unexpected();
|
---|
| 2036 | cur.push(statement());
|
---|
| 2037 | }
|
---|
| 2038 | }
|
---|
| 2039 | if (branch) branch.end = prev();
|
---|
| 2040 | next();
|
---|
| 2041 | return a;
|
---|
| 2042 | }
|
---|
| 2043 |
|
---|
| 2044 | function try_() {
|
---|
| 2045 | var body = block_(), bcatch = null, bfinally = null;
|
---|
| 2046 | if (is("keyword", "catch")) {
|
---|
| 2047 | var start = S.token;
|
---|
| 2048 | next();
|
---|
| 2049 | if (is("punc", "{")) {
|
---|
| 2050 | var name = null;
|
---|
| 2051 | } else {
|
---|
| 2052 | expect("(");
|
---|
| 2053 | var name = parameter(undefined, AST_SymbolCatch);
|
---|
| 2054 | expect(")");
|
---|
| 2055 | }
|
---|
| 2056 | bcatch = new AST_Catch({
|
---|
| 2057 | start : start,
|
---|
| 2058 | argname : name,
|
---|
| 2059 | body : block_(),
|
---|
| 2060 | end : prev()
|
---|
| 2061 | });
|
---|
| 2062 | }
|
---|
| 2063 | if (is("keyword", "finally")) {
|
---|
| 2064 | var start = S.token;
|
---|
| 2065 | next();
|
---|
| 2066 | bfinally = new AST_Finally({
|
---|
| 2067 | start : start,
|
---|
| 2068 | body : block_(),
|
---|
| 2069 | end : prev()
|
---|
| 2070 | });
|
---|
| 2071 | }
|
---|
| 2072 | if (!bcatch && !bfinally)
|
---|
| 2073 | croak("Missing catch/finally blocks");
|
---|
| 2074 | return new AST_Try({
|
---|
| 2075 | body : body,
|
---|
| 2076 | bcatch : bcatch,
|
---|
| 2077 | bfinally : bfinally
|
---|
| 2078 | });
|
---|
| 2079 | }
|
---|
| 2080 |
|
---|
| 2081 | function vardefs(no_in, kind) {
|
---|
| 2082 | var a = [];
|
---|
| 2083 | var def;
|
---|
| 2084 | for (;;) {
|
---|
| 2085 | var sym_type =
|
---|
| 2086 | kind === "var" ? AST_SymbolVar :
|
---|
| 2087 | kind === "const" ? AST_SymbolConst :
|
---|
| 2088 | kind === "let" ? AST_SymbolLet : null;
|
---|
| 2089 | if (is("punc", "{") || is("punc", "[")) {
|
---|
| 2090 | def = new AST_VarDef({
|
---|
| 2091 | start: S.token,
|
---|
| 2092 | name: binding_element(undefined ,sym_type),
|
---|
| 2093 | value: is("operator", "=") ? (expect_token("operator", "="), expression(false, no_in)) : null,
|
---|
| 2094 | end: prev()
|
---|
| 2095 | });
|
---|
| 2096 | } else {
|
---|
| 2097 | def = new AST_VarDef({
|
---|
| 2098 | start : S.token,
|
---|
| 2099 | name : as_symbol(sym_type),
|
---|
| 2100 | value : is("operator", "=")
|
---|
| 2101 | ? (next(), expression(false, no_in))
|
---|
| 2102 | : !no_in && kind === "const"
|
---|
| 2103 | ? croak("Missing initializer in const declaration") : null,
|
---|
| 2104 | end : prev()
|
---|
| 2105 | });
|
---|
| 2106 | if (def.name.name == "import") croak("Unexpected token: import");
|
---|
| 2107 | }
|
---|
| 2108 | a.push(def);
|
---|
| 2109 | if (!is("punc", ","))
|
---|
| 2110 | break;
|
---|
| 2111 | next();
|
---|
| 2112 | }
|
---|
| 2113 | return a;
|
---|
| 2114 | }
|
---|
| 2115 |
|
---|
| 2116 | var var_ = function(no_in) {
|
---|
| 2117 | return new AST_Var({
|
---|
| 2118 | start : prev(),
|
---|
| 2119 | definitions : vardefs(no_in, "var"),
|
---|
| 2120 | end : prev()
|
---|
| 2121 | });
|
---|
| 2122 | };
|
---|
| 2123 |
|
---|
| 2124 | var let_ = function(no_in) {
|
---|
| 2125 | return new AST_Let({
|
---|
| 2126 | start : prev(),
|
---|
| 2127 | definitions : vardefs(no_in, "let"),
|
---|
| 2128 | end : prev()
|
---|
| 2129 | });
|
---|
| 2130 | };
|
---|
| 2131 |
|
---|
| 2132 | var const_ = function(no_in) {
|
---|
| 2133 | return new AST_Const({
|
---|
| 2134 | start : prev(),
|
---|
| 2135 | definitions : vardefs(no_in, "const"),
|
---|
| 2136 | end : prev()
|
---|
| 2137 | });
|
---|
| 2138 | };
|
---|
| 2139 |
|
---|
| 2140 | var new_ = function(allow_calls) {
|
---|
| 2141 | var start = S.token;
|
---|
| 2142 | expect_token("operator", "new");
|
---|
| 2143 | if (is("punc", ".")) {
|
---|
| 2144 | next();
|
---|
| 2145 | expect_token("name", "target");
|
---|
| 2146 | return subscripts(new AST_NewTarget({
|
---|
| 2147 | start : start,
|
---|
| 2148 | end : prev()
|
---|
| 2149 | }), allow_calls);
|
---|
| 2150 | }
|
---|
| 2151 | var newexp = expr_atom(false), args;
|
---|
| 2152 | if (is("punc", "(")) {
|
---|
| 2153 | next();
|
---|
| 2154 | args = expr_list(")", true);
|
---|
| 2155 | } else {
|
---|
| 2156 | args = [];
|
---|
| 2157 | }
|
---|
| 2158 | var call = new AST_New({
|
---|
| 2159 | start : start,
|
---|
| 2160 | expression : newexp,
|
---|
| 2161 | args : args,
|
---|
| 2162 | end : prev()
|
---|
| 2163 | });
|
---|
| 2164 | annotate(call);
|
---|
| 2165 | return subscripts(call, allow_calls);
|
---|
| 2166 | };
|
---|
| 2167 |
|
---|
| 2168 | function as_atom_node() {
|
---|
| 2169 | var tok = S.token, ret;
|
---|
| 2170 | switch (tok.type) {
|
---|
| 2171 | case "name":
|
---|
| 2172 | ret = _make_symbol(AST_SymbolRef);
|
---|
| 2173 | break;
|
---|
| 2174 | case "num":
|
---|
| 2175 | ret = new AST_Number({
|
---|
| 2176 | start: tok,
|
---|
| 2177 | end: tok,
|
---|
| 2178 | value: tok.value,
|
---|
| 2179 | raw: LATEST_RAW
|
---|
| 2180 | });
|
---|
| 2181 | break;
|
---|
| 2182 | case "big_int":
|
---|
| 2183 | ret = new AST_BigInt({ start: tok, end: tok, value: tok.value });
|
---|
| 2184 | break;
|
---|
| 2185 | case "string":
|
---|
| 2186 | ret = new AST_String({
|
---|
| 2187 | start : tok,
|
---|
| 2188 | end : tok,
|
---|
| 2189 | value : tok.value,
|
---|
| 2190 | quote : tok.quote
|
---|
| 2191 | });
|
---|
| 2192 | break;
|
---|
| 2193 | case "regexp":
|
---|
| 2194 | const [_, source, flags] = tok.value.match(/^\/(.*)\/(\w*)$/);
|
---|
| 2195 |
|
---|
| 2196 | ret = new AST_RegExp({ start: tok, end: tok, value: { source, flags } });
|
---|
| 2197 | break;
|
---|
| 2198 | case "atom":
|
---|
| 2199 | switch (tok.value) {
|
---|
| 2200 | case "false":
|
---|
| 2201 | ret = new AST_False({ start: tok, end: tok });
|
---|
| 2202 | break;
|
---|
| 2203 | case "true":
|
---|
| 2204 | ret = new AST_True({ start: tok, end: tok });
|
---|
| 2205 | break;
|
---|
| 2206 | case "null":
|
---|
| 2207 | ret = new AST_Null({ start: tok, end: tok });
|
---|
| 2208 | break;
|
---|
| 2209 | }
|
---|
| 2210 | break;
|
---|
| 2211 | }
|
---|
| 2212 | next();
|
---|
| 2213 | return ret;
|
---|
| 2214 | }
|
---|
| 2215 |
|
---|
| 2216 | function to_fun_args(ex, default_seen_above) {
|
---|
| 2217 | var insert_default = function(ex, default_value) {
|
---|
| 2218 | if (default_value) {
|
---|
| 2219 | return new AST_DefaultAssign({
|
---|
| 2220 | start: ex.start,
|
---|
| 2221 | left: ex,
|
---|
| 2222 | operator: "=",
|
---|
| 2223 | right: default_value,
|
---|
| 2224 | end: default_value.end
|
---|
| 2225 | });
|
---|
| 2226 | }
|
---|
| 2227 | return ex;
|
---|
| 2228 | };
|
---|
| 2229 | if (ex instanceof AST_Object) {
|
---|
| 2230 | return insert_default(new AST_Destructuring({
|
---|
| 2231 | start: ex.start,
|
---|
| 2232 | end: ex.end,
|
---|
| 2233 | is_array: false,
|
---|
| 2234 | names: ex.properties.map(prop => to_fun_args(prop))
|
---|
| 2235 | }), default_seen_above);
|
---|
| 2236 | } else if (ex instanceof AST_ObjectKeyVal) {
|
---|
| 2237 | ex.value = to_fun_args(ex.value);
|
---|
| 2238 | return insert_default(ex, default_seen_above);
|
---|
| 2239 | } else if (ex instanceof AST_Hole) {
|
---|
| 2240 | return ex;
|
---|
| 2241 | } else if (ex instanceof AST_Destructuring) {
|
---|
| 2242 | ex.names = ex.names.map(name => to_fun_args(name));
|
---|
| 2243 | return insert_default(ex, default_seen_above);
|
---|
| 2244 | } else if (ex instanceof AST_SymbolRef) {
|
---|
| 2245 | return insert_default(new AST_SymbolFunarg({
|
---|
| 2246 | name: ex.name,
|
---|
| 2247 | start: ex.start,
|
---|
| 2248 | end: ex.end
|
---|
| 2249 | }), default_seen_above);
|
---|
| 2250 | } else if (ex instanceof AST_Expansion) {
|
---|
| 2251 | ex.expression = to_fun_args(ex.expression);
|
---|
| 2252 | return insert_default(ex, default_seen_above);
|
---|
| 2253 | } else if (ex instanceof AST_Array) {
|
---|
| 2254 | return insert_default(new AST_Destructuring({
|
---|
| 2255 | start: ex.start,
|
---|
| 2256 | end: ex.end,
|
---|
| 2257 | is_array: true,
|
---|
| 2258 | names: ex.elements.map(elm => to_fun_args(elm))
|
---|
| 2259 | }), default_seen_above);
|
---|
| 2260 | } else if (ex instanceof AST_Assign) {
|
---|
| 2261 | return insert_default(to_fun_args(ex.left, ex.right), default_seen_above);
|
---|
| 2262 | } else if (ex instanceof AST_DefaultAssign) {
|
---|
| 2263 | ex.left = to_fun_args(ex.left);
|
---|
| 2264 | return ex;
|
---|
| 2265 | } else {
|
---|
| 2266 | croak("Invalid function parameter", ex.start.line, ex.start.col);
|
---|
| 2267 | }
|
---|
| 2268 | }
|
---|
| 2269 |
|
---|
| 2270 | var expr_atom = function(allow_calls, allow_arrows) {
|
---|
| 2271 | if (is("operator", "new")) {
|
---|
| 2272 | return new_(allow_calls);
|
---|
| 2273 | }
|
---|
| 2274 | if (is("operator", "import")) {
|
---|
| 2275 | return import_meta();
|
---|
| 2276 | }
|
---|
| 2277 | var start = S.token;
|
---|
| 2278 | var peeked;
|
---|
| 2279 | var async = is("name", "async")
|
---|
| 2280 | && (peeked = peek()).value != "["
|
---|
| 2281 | && peeked.type != "arrow"
|
---|
| 2282 | && as_atom_node();
|
---|
| 2283 | if (is("punc")) {
|
---|
| 2284 | switch (S.token.value) {
|
---|
| 2285 | case "(":
|
---|
| 2286 | if (async && !allow_calls) break;
|
---|
| 2287 | var exprs = params_or_seq_(allow_arrows, !async);
|
---|
| 2288 | if (allow_arrows && is("arrow", "=>")) {
|
---|
| 2289 | return arrow_function(start, exprs.map(e => to_fun_args(e)), !!async);
|
---|
| 2290 | }
|
---|
| 2291 | var ex = async ? new AST_Call({
|
---|
| 2292 | expression: async,
|
---|
| 2293 | args: exprs
|
---|
| 2294 | }) : exprs.length == 1 ? exprs[0] : new AST_Sequence({
|
---|
| 2295 | expressions: exprs
|
---|
| 2296 | });
|
---|
| 2297 | if (ex.start) {
|
---|
| 2298 | const outer_comments_before = start.comments_before.length;
|
---|
| 2299 | outer_comments_before_counts.set(start, outer_comments_before);
|
---|
| 2300 | ex.start.comments_before.unshift(...start.comments_before);
|
---|
| 2301 | start.comments_before = ex.start.comments_before;
|
---|
| 2302 | if (outer_comments_before == 0 && start.comments_before.length > 0) {
|
---|
| 2303 | var comment = start.comments_before[0];
|
---|
| 2304 | if (!comment.nlb) {
|
---|
| 2305 | comment.nlb = start.nlb;
|
---|
| 2306 | start.nlb = false;
|
---|
| 2307 | }
|
---|
| 2308 | }
|
---|
| 2309 | start.comments_after = ex.start.comments_after;
|
---|
| 2310 | }
|
---|
| 2311 | ex.start = start;
|
---|
| 2312 | var end = prev();
|
---|
| 2313 | if (ex.end) {
|
---|
| 2314 | end.comments_before = ex.end.comments_before;
|
---|
| 2315 | ex.end.comments_after.push(...end.comments_after);
|
---|
| 2316 | end.comments_after = ex.end.comments_after;
|
---|
| 2317 | }
|
---|
| 2318 | ex.end = end;
|
---|
| 2319 | if (ex instanceof AST_Call) annotate(ex);
|
---|
| 2320 | return subscripts(ex, allow_calls);
|
---|
| 2321 | case "[":
|
---|
| 2322 | return subscripts(array_(), allow_calls);
|
---|
| 2323 | case "{":
|
---|
| 2324 | return subscripts(object_or_destructuring_(), allow_calls);
|
---|
| 2325 | }
|
---|
| 2326 | if (!async) unexpected();
|
---|
| 2327 | }
|
---|
| 2328 | if (allow_arrows && is("name") && is_token(peek(), "arrow")) {
|
---|
| 2329 | var param = new AST_SymbolFunarg({
|
---|
| 2330 | name: S.token.value,
|
---|
| 2331 | start: start,
|
---|
| 2332 | end: start,
|
---|
| 2333 | });
|
---|
| 2334 | next();
|
---|
| 2335 | return arrow_function(start, [param], !!async);
|
---|
| 2336 | }
|
---|
| 2337 | if (is("keyword", "function")) {
|
---|
| 2338 | next();
|
---|
| 2339 | var func = function_(AST_Function, false, !!async);
|
---|
| 2340 | func.start = start;
|
---|
| 2341 | func.end = prev();
|
---|
| 2342 | return subscripts(func, allow_calls);
|
---|
| 2343 | }
|
---|
| 2344 | if (async) return subscripts(async, allow_calls);
|
---|
| 2345 | if (is("keyword", "class")) {
|
---|
| 2346 | next();
|
---|
| 2347 | var cls = class_(AST_ClassExpression);
|
---|
| 2348 | cls.start = start;
|
---|
| 2349 | cls.end = prev();
|
---|
| 2350 | return subscripts(cls, allow_calls);
|
---|
| 2351 | }
|
---|
| 2352 | if (is("template_head")) {
|
---|
| 2353 | return subscripts(template_string(), allow_calls);
|
---|
| 2354 | }
|
---|
| 2355 | if (ATOMIC_START_TOKEN.has(S.token.type)) {
|
---|
| 2356 | return subscripts(as_atom_node(), allow_calls);
|
---|
| 2357 | }
|
---|
| 2358 | unexpected();
|
---|
| 2359 | };
|
---|
| 2360 |
|
---|
| 2361 | function template_string() {
|
---|
| 2362 | var segments = [], start = S.token;
|
---|
| 2363 |
|
---|
| 2364 | segments.push(new AST_TemplateSegment({
|
---|
| 2365 | start: S.token,
|
---|
| 2366 | raw: LATEST_RAW,
|
---|
| 2367 | value: S.token.value,
|
---|
| 2368 | end: S.token
|
---|
| 2369 | }));
|
---|
| 2370 |
|
---|
| 2371 | while (!LATEST_TEMPLATE_END) {
|
---|
| 2372 | next();
|
---|
| 2373 | handle_regexp();
|
---|
| 2374 | segments.push(expression(true));
|
---|
| 2375 |
|
---|
| 2376 | segments.push(new AST_TemplateSegment({
|
---|
| 2377 | start: S.token,
|
---|
| 2378 | raw: LATEST_RAW,
|
---|
| 2379 | value: S.token.value,
|
---|
| 2380 | end: S.token
|
---|
| 2381 | }));
|
---|
| 2382 | }
|
---|
| 2383 | next();
|
---|
| 2384 |
|
---|
| 2385 | return new AST_TemplateString({
|
---|
| 2386 | start: start,
|
---|
| 2387 | segments: segments,
|
---|
| 2388 | end: S.token
|
---|
| 2389 | });
|
---|
| 2390 | }
|
---|
| 2391 |
|
---|
| 2392 | function expr_list(closing, allow_trailing_comma, allow_empty) {
|
---|
| 2393 | var first = true, a = [];
|
---|
| 2394 | while (!is("punc", closing)) {
|
---|
| 2395 | if (first) first = false; else expect(",");
|
---|
| 2396 | if (allow_trailing_comma && is("punc", closing)) break;
|
---|
| 2397 | if (is("punc", ",") && allow_empty) {
|
---|
| 2398 | a.push(new AST_Hole({ start: S.token, end: S.token }));
|
---|
| 2399 | } else if (is("expand", "...")) {
|
---|
| 2400 | next();
|
---|
| 2401 | a.push(new AST_Expansion({start: prev(), expression: expression(),end: S.token}));
|
---|
| 2402 | } else {
|
---|
| 2403 | a.push(expression(false));
|
---|
| 2404 | }
|
---|
| 2405 | }
|
---|
| 2406 | next();
|
---|
| 2407 | return a;
|
---|
| 2408 | }
|
---|
| 2409 |
|
---|
| 2410 | var array_ = embed_tokens(function() {
|
---|
| 2411 | expect("[");
|
---|
| 2412 | return new AST_Array({
|
---|
| 2413 | elements: expr_list("]", !options.strict, true)
|
---|
| 2414 | });
|
---|
| 2415 | });
|
---|
| 2416 |
|
---|
| 2417 | var create_accessor = embed_tokens((is_generator, is_async) => {
|
---|
| 2418 | return function_(AST_Accessor, is_generator, is_async);
|
---|
| 2419 | });
|
---|
| 2420 |
|
---|
| 2421 | var object_or_destructuring_ = embed_tokens(function object_or_destructuring_() {
|
---|
| 2422 | var start = S.token, first = true, a = [];
|
---|
| 2423 | expect("{");
|
---|
| 2424 | while (!is("punc", "}")) {
|
---|
| 2425 | if (first) first = false; else expect(",");
|
---|
| 2426 | if (!options.strict && is("punc", "}"))
|
---|
| 2427 | // allow trailing comma
|
---|
| 2428 | break;
|
---|
| 2429 |
|
---|
| 2430 | start = S.token;
|
---|
| 2431 | if (start.type == "expand") {
|
---|
| 2432 | next();
|
---|
| 2433 | a.push(new AST_Expansion({
|
---|
| 2434 | start: start,
|
---|
| 2435 | expression: expression(false),
|
---|
| 2436 | end: prev(),
|
---|
| 2437 | }));
|
---|
| 2438 | continue;
|
---|
| 2439 | }
|
---|
| 2440 |
|
---|
| 2441 | var name = as_property_name();
|
---|
| 2442 | var value;
|
---|
| 2443 |
|
---|
| 2444 | // Check property and fetch value
|
---|
| 2445 | if (!is("punc", ":")) {
|
---|
| 2446 | var concise = concise_method_or_getset(name, start);
|
---|
| 2447 | if (concise) {
|
---|
| 2448 | a.push(concise);
|
---|
| 2449 | continue;
|
---|
| 2450 | }
|
---|
| 2451 |
|
---|
| 2452 | value = new AST_SymbolRef({
|
---|
| 2453 | start: prev(),
|
---|
| 2454 | name: name,
|
---|
| 2455 | end: prev()
|
---|
| 2456 | });
|
---|
| 2457 | } else if (name === null) {
|
---|
| 2458 | unexpected(prev());
|
---|
| 2459 | } else {
|
---|
| 2460 | next(); // `:` - see first condition
|
---|
| 2461 | value = expression(false);
|
---|
| 2462 | }
|
---|
| 2463 |
|
---|
| 2464 | // Check for default value and alter value accordingly if necessary
|
---|
| 2465 | if (is("operator", "=")) {
|
---|
| 2466 | next();
|
---|
| 2467 | value = new AST_Assign({
|
---|
| 2468 | start: start,
|
---|
| 2469 | left: value,
|
---|
| 2470 | operator: "=",
|
---|
| 2471 | right: expression(false),
|
---|
| 2472 | logical: false,
|
---|
| 2473 | end: prev()
|
---|
| 2474 | });
|
---|
| 2475 | }
|
---|
| 2476 |
|
---|
| 2477 | // Create property
|
---|
| 2478 | a.push(new AST_ObjectKeyVal({
|
---|
| 2479 | start: start,
|
---|
| 2480 | quote: start.quote,
|
---|
| 2481 | key: name instanceof AST_Node ? name : "" + name,
|
---|
| 2482 | value: value,
|
---|
| 2483 | end: prev()
|
---|
| 2484 | }));
|
---|
| 2485 | }
|
---|
| 2486 | next();
|
---|
| 2487 | return new AST_Object({ properties: a });
|
---|
| 2488 | });
|
---|
| 2489 |
|
---|
| 2490 | function class_(KindOfClass, is_export_default) {
|
---|
| 2491 | var start, method, class_name, extends_, a = [];
|
---|
| 2492 |
|
---|
| 2493 | S.input.push_directives_stack(); // Push directive stack, but not scope stack
|
---|
| 2494 | S.input.add_directive("use strict");
|
---|
| 2495 |
|
---|
| 2496 | if (S.token.type == "name" && S.token.value != "extends") {
|
---|
| 2497 | class_name = as_symbol(KindOfClass === AST_DefClass ? AST_SymbolDefClass : AST_SymbolClass);
|
---|
| 2498 | }
|
---|
| 2499 |
|
---|
| 2500 | if (KindOfClass === AST_DefClass && !class_name) {
|
---|
| 2501 | if (is_export_default) {
|
---|
| 2502 | KindOfClass = AST_ClassExpression;
|
---|
| 2503 | } else {
|
---|
| 2504 | unexpected();
|
---|
| 2505 | }
|
---|
| 2506 | }
|
---|
| 2507 |
|
---|
| 2508 | if (S.token.value == "extends") {
|
---|
| 2509 | next();
|
---|
| 2510 | extends_ = expression(true);
|
---|
| 2511 | }
|
---|
| 2512 |
|
---|
| 2513 | expect("{");
|
---|
| 2514 |
|
---|
| 2515 | while (is("punc", ";")) { next(); } // Leading semicolons are okay in class bodies.
|
---|
| 2516 | while (!is("punc", "}")) {
|
---|
| 2517 | start = S.token;
|
---|
| 2518 | method = concise_method_or_getset(as_property_name(), start, true);
|
---|
| 2519 | if (!method) { unexpected(); }
|
---|
| 2520 | a.push(method);
|
---|
| 2521 | while (is("punc", ";")) { next(); }
|
---|
| 2522 | }
|
---|
| 2523 |
|
---|
| 2524 | S.input.pop_directives_stack();
|
---|
| 2525 |
|
---|
| 2526 | next();
|
---|
| 2527 |
|
---|
| 2528 | return new KindOfClass({
|
---|
| 2529 | start: start,
|
---|
| 2530 | name: class_name,
|
---|
| 2531 | extends: extends_,
|
---|
| 2532 | properties: a,
|
---|
| 2533 | end: prev(),
|
---|
| 2534 | });
|
---|
| 2535 | }
|
---|
| 2536 |
|
---|
| 2537 | function concise_method_or_getset(name, start, is_class) {
|
---|
| 2538 | const get_symbol_ast = (name, SymbolClass = AST_SymbolMethod) => {
|
---|
| 2539 | if (typeof name === "string" || typeof name === "number") {
|
---|
| 2540 | return new SymbolClass({
|
---|
| 2541 | start,
|
---|
| 2542 | name: "" + name,
|
---|
| 2543 | end: prev()
|
---|
| 2544 | });
|
---|
| 2545 | } else if (name === null) {
|
---|
| 2546 | unexpected();
|
---|
| 2547 | }
|
---|
| 2548 | return name;
|
---|
| 2549 | };
|
---|
| 2550 |
|
---|
| 2551 | const is_not_method_start = () =>
|
---|
| 2552 | !is("punc", "(") && !is("punc", ",") && !is("punc", "}") && !is("operator", "=");
|
---|
| 2553 |
|
---|
| 2554 | var is_async = false;
|
---|
| 2555 | var is_static = false;
|
---|
| 2556 | var is_generator = false;
|
---|
| 2557 | var is_private = false;
|
---|
| 2558 | var accessor_type = null;
|
---|
| 2559 |
|
---|
| 2560 | if (is_class && name === "static" && is_not_method_start()) {
|
---|
| 2561 | is_static = true;
|
---|
| 2562 | name = as_property_name();
|
---|
| 2563 | }
|
---|
| 2564 | if (name === "async" && is_not_method_start()) {
|
---|
| 2565 | is_async = true;
|
---|
| 2566 | name = as_property_name();
|
---|
| 2567 | }
|
---|
| 2568 | if (prev().type === "operator" && prev().value === "*") {
|
---|
| 2569 | is_generator = true;
|
---|
| 2570 | name = as_property_name();
|
---|
| 2571 | }
|
---|
| 2572 | if ((name === "get" || name === "set") && is_not_method_start()) {
|
---|
| 2573 | accessor_type = name;
|
---|
| 2574 | name = as_property_name();
|
---|
| 2575 | }
|
---|
| 2576 | if (prev().type === "privatename") {
|
---|
| 2577 | is_private = true;
|
---|
| 2578 | }
|
---|
| 2579 |
|
---|
| 2580 | const property_token = prev();
|
---|
| 2581 |
|
---|
| 2582 | if (accessor_type != null) {
|
---|
| 2583 | if (!is_private) {
|
---|
| 2584 | const AccessorClass = accessor_type === "get"
|
---|
| 2585 | ? AST_ObjectGetter
|
---|
| 2586 | : AST_ObjectSetter;
|
---|
| 2587 |
|
---|
| 2588 | name = get_symbol_ast(name);
|
---|
| 2589 | return new AccessorClass({
|
---|
| 2590 | start,
|
---|
| 2591 | static: is_static,
|
---|
| 2592 | key: name,
|
---|
| 2593 | quote: name instanceof AST_SymbolMethod ? property_token.quote : undefined,
|
---|
| 2594 | value: create_accessor(),
|
---|
| 2595 | end: prev()
|
---|
| 2596 | });
|
---|
| 2597 | } else {
|
---|
| 2598 | const AccessorClass = accessor_type === "get"
|
---|
| 2599 | ? AST_PrivateGetter
|
---|
| 2600 | : AST_PrivateSetter;
|
---|
| 2601 |
|
---|
| 2602 | return new AccessorClass({
|
---|
| 2603 | start,
|
---|
| 2604 | static: is_static,
|
---|
| 2605 | key: get_symbol_ast(name),
|
---|
| 2606 | value: create_accessor(),
|
---|
| 2607 | end: prev(),
|
---|
| 2608 | });
|
---|
| 2609 | }
|
---|
| 2610 | }
|
---|
| 2611 |
|
---|
| 2612 | if (is("punc", "(")) {
|
---|
| 2613 | name = get_symbol_ast(name);
|
---|
| 2614 | const AST_MethodVariant = is_private
|
---|
| 2615 | ? AST_PrivateMethod
|
---|
| 2616 | : AST_ConciseMethod;
|
---|
| 2617 | var node = new AST_MethodVariant({
|
---|
| 2618 | start : start,
|
---|
| 2619 | static : is_static,
|
---|
| 2620 | is_generator: is_generator,
|
---|
| 2621 | async : is_async,
|
---|
| 2622 | key : name,
|
---|
| 2623 | quote : name instanceof AST_SymbolMethod ?
|
---|
| 2624 | property_token.quote : undefined,
|
---|
| 2625 | value : create_accessor(is_generator, is_async),
|
---|
| 2626 | end : prev()
|
---|
| 2627 | });
|
---|
| 2628 | return node;
|
---|
| 2629 | }
|
---|
| 2630 |
|
---|
| 2631 | if (is_class) {
|
---|
| 2632 | const key = get_symbol_ast(name, AST_SymbolClassProperty);
|
---|
| 2633 | const quote = key instanceof AST_SymbolClassProperty
|
---|
| 2634 | ? property_token.quote
|
---|
| 2635 | : undefined;
|
---|
| 2636 | const AST_ClassPropertyVariant = is_private
|
---|
| 2637 | ? AST_ClassPrivateProperty
|
---|
| 2638 | : AST_ClassProperty;
|
---|
| 2639 | if (is("operator", "=")) {
|
---|
| 2640 | next();
|
---|
| 2641 | return new AST_ClassPropertyVariant({
|
---|
| 2642 | start,
|
---|
| 2643 | static: is_static,
|
---|
| 2644 | quote,
|
---|
| 2645 | key,
|
---|
| 2646 | value: expression(false),
|
---|
| 2647 | end: prev()
|
---|
| 2648 | });
|
---|
| 2649 | } else if (
|
---|
| 2650 | is("name")
|
---|
| 2651 | || is("privatename")
|
---|
| 2652 | || is("operator", "*")
|
---|
| 2653 | || is("punc", ";")
|
---|
| 2654 | || is("punc", "}")
|
---|
| 2655 | ) {
|
---|
| 2656 | return new AST_ClassPropertyVariant({
|
---|
| 2657 | start,
|
---|
| 2658 | static: is_static,
|
---|
| 2659 | quote,
|
---|
| 2660 | key,
|
---|
| 2661 | end: prev()
|
---|
| 2662 | });
|
---|
| 2663 | }
|
---|
| 2664 | }
|
---|
| 2665 | }
|
---|
| 2666 |
|
---|
| 2667 | function import_() {
|
---|
| 2668 | var start = prev();
|
---|
| 2669 |
|
---|
| 2670 | var imported_name;
|
---|
| 2671 | var imported_names;
|
---|
| 2672 | if (is("name")) {
|
---|
| 2673 | imported_name = as_symbol(AST_SymbolImport);
|
---|
| 2674 | }
|
---|
| 2675 |
|
---|
| 2676 | if (is("punc", ",")) {
|
---|
| 2677 | next();
|
---|
| 2678 | }
|
---|
| 2679 |
|
---|
| 2680 | imported_names = map_names(true);
|
---|
| 2681 |
|
---|
| 2682 | if (imported_names || imported_name) {
|
---|
| 2683 | expect_token("name", "from");
|
---|
| 2684 | }
|
---|
| 2685 | var mod_str = S.token;
|
---|
| 2686 | if (mod_str.type !== "string") {
|
---|
| 2687 | unexpected();
|
---|
| 2688 | }
|
---|
| 2689 | next();
|
---|
| 2690 | return new AST_Import({
|
---|
| 2691 | start: start,
|
---|
| 2692 | imported_name: imported_name,
|
---|
| 2693 | imported_names: imported_names,
|
---|
| 2694 | module_name: new AST_String({
|
---|
| 2695 | start: mod_str,
|
---|
| 2696 | value: mod_str.value,
|
---|
| 2697 | quote: mod_str.quote,
|
---|
| 2698 | end: mod_str,
|
---|
| 2699 | }),
|
---|
| 2700 | end: S.token,
|
---|
| 2701 | });
|
---|
| 2702 | }
|
---|
| 2703 |
|
---|
| 2704 | function import_meta() {
|
---|
| 2705 | var start = S.token;
|
---|
| 2706 | expect_token("operator", "import");
|
---|
| 2707 | expect_token("punc", ".");
|
---|
| 2708 | expect_token("name", "meta");
|
---|
| 2709 | return subscripts(new AST_ImportMeta({
|
---|
| 2710 | start: start,
|
---|
| 2711 | end: prev()
|
---|
| 2712 | }), false);
|
---|
| 2713 | }
|
---|
| 2714 |
|
---|
| 2715 | function map_name(is_import) {
|
---|
| 2716 | function make_symbol(type) {
|
---|
| 2717 | return new type({
|
---|
| 2718 | name: as_property_name(),
|
---|
| 2719 | start: prev(),
|
---|
| 2720 | end: prev()
|
---|
| 2721 | });
|
---|
| 2722 | }
|
---|
| 2723 |
|
---|
| 2724 | var foreign_type = is_import ? AST_SymbolImportForeign : AST_SymbolExportForeign;
|
---|
| 2725 | var type = is_import ? AST_SymbolImport : AST_SymbolExport;
|
---|
| 2726 | var start = S.token;
|
---|
| 2727 | var foreign_name;
|
---|
| 2728 | var name;
|
---|
| 2729 |
|
---|
| 2730 | if (is_import) {
|
---|
| 2731 | foreign_name = make_symbol(foreign_type);
|
---|
| 2732 | } else {
|
---|
| 2733 | name = make_symbol(type);
|
---|
| 2734 | }
|
---|
| 2735 | if (is("name", "as")) {
|
---|
| 2736 | next(); // The "as" word
|
---|
| 2737 | if (is_import) {
|
---|
| 2738 | name = make_symbol(type);
|
---|
| 2739 | } else {
|
---|
| 2740 | foreign_name = make_symbol(foreign_type);
|
---|
| 2741 | }
|
---|
| 2742 | } else if (is_import) {
|
---|
| 2743 | name = new type(foreign_name);
|
---|
| 2744 | } else {
|
---|
| 2745 | foreign_name = new foreign_type(name);
|
---|
| 2746 | }
|
---|
| 2747 |
|
---|
| 2748 | return new AST_NameMapping({
|
---|
| 2749 | start: start,
|
---|
| 2750 | foreign_name: foreign_name,
|
---|
| 2751 | name: name,
|
---|
| 2752 | end: prev(),
|
---|
| 2753 | });
|
---|
| 2754 | }
|
---|
| 2755 |
|
---|
| 2756 | function map_nameAsterisk(is_import, name) {
|
---|
| 2757 | var foreign_type = is_import ? AST_SymbolImportForeign : AST_SymbolExportForeign;
|
---|
| 2758 | var type = is_import ? AST_SymbolImport : AST_SymbolExport;
|
---|
| 2759 | var start = S.token;
|
---|
| 2760 | var foreign_name;
|
---|
| 2761 | var end = prev();
|
---|
| 2762 |
|
---|
| 2763 | name = name || new type({
|
---|
| 2764 | name: "*",
|
---|
| 2765 | start: start,
|
---|
| 2766 | end: end,
|
---|
| 2767 | });
|
---|
| 2768 |
|
---|
| 2769 | foreign_name = new foreign_type({
|
---|
| 2770 | name: "*",
|
---|
| 2771 | start: start,
|
---|
| 2772 | end: end,
|
---|
| 2773 | });
|
---|
| 2774 |
|
---|
| 2775 | return new AST_NameMapping({
|
---|
| 2776 | start: start,
|
---|
| 2777 | foreign_name: foreign_name,
|
---|
| 2778 | name: name,
|
---|
| 2779 | end: end,
|
---|
| 2780 | });
|
---|
| 2781 | }
|
---|
| 2782 |
|
---|
| 2783 | function map_names(is_import) {
|
---|
| 2784 | var names;
|
---|
| 2785 | if (is("punc", "{")) {
|
---|
| 2786 | next();
|
---|
| 2787 | names = [];
|
---|
| 2788 | while (!is("punc", "}")) {
|
---|
| 2789 | names.push(map_name(is_import));
|
---|
| 2790 | if (is("punc", ",")) {
|
---|
| 2791 | next();
|
---|
| 2792 | }
|
---|
| 2793 | }
|
---|
| 2794 | next();
|
---|
| 2795 | } else if (is("operator", "*")) {
|
---|
| 2796 | var name;
|
---|
| 2797 | next();
|
---|
| 2798 | if (is_import && is("name", "as")) {
|
---|
| 2799 | next(); // The "as" word
|
---|
| 2800 | name = as_symbol(is_import ? AST_SymbolImport : AST_SymbolExportForeign);
|
---|
| 2801 | }
|
---|
| 2802 | names = [map_nameAsterisk(is_import, name)];
|
---|
| 2803 | }
|
---|
| 2804 | return names;
|
---|
| 2805 | }
|
---|
| 2806 |
|
---|
| 2807 | function export_() {
|
---|
| 2808 | var start = S.token;
|
---|
| 2809 | var is_default;
|
---|
| 2810 | var exported_names;
|
---|
| 2811 |
|
---|
| 2812 | if (is("keyword", "default")) {
|
---|
| 2813 | is_default = true;
|
---|
| 2814 | next();
|
---|
| 2815 | } else if (exported_names = map_names(false)) {
|
---|
| 2816 | if (is("name", "from")) {
|
---|
| 2817 | next();
|
---|
| 2818 |
|
---|
| 2819 | var mod_str = S.token;
|
---|
| 2820 | if (mod_str.type !== "string") {
|
---|
| 2821 | unexpected();
|
---|
| 2822 | }
|
---|
| 2823 | next();
|
---|
| 2824 |
|
---|
| 2825 | return new AST_Export({
|
---|
| 2826 | start: start,
|
---|
| 2827 | is_default: is_default,
|
---|
| 2828 | exported_names: exported_names,
|
---|
| 2829 | module_name: new AST_String({
|
---|
| 2830 | start: mod_str,
|
---|
| 2831 | value: mod_str.value,
|
---|
| 2832 | quote: mod_str.quote,
|
---|
| 2833 | end: mod_str,
|
---|
| 2834 | }),
|
---|
| 2835 | end: prev(),
|
---|
| 2836 | });
|
---|
| 2837 | } else {
|
---|
| 2838 | return new AST_Export({
|
---|
| 2839 | start: start,
|
---|
| 2840 | is_default: is_default,
|
---|
| 2841 | exported_names: exported_names,
|
---|
| 2842 | end: prev(),
|
---|
| 2843 | });
|
---|
| 2844 | }
|
---|
| 2845 | }
|
---|
| 2846 |
|
---|
| 2847 | var node;
|
---|
| 2848 | var exported_value;
|
---|
| 2849 | var exported_definition;
|
---|
| 2850 | if (is("punc", "{")
|
---|
| 2851 | || is_default
|
---|
| 2852 | && (is("keyword", "class") || is("keyword", "function"))
|
---|
| 2853 | && is_token(peek(), "punc")) {
|
---|
| 2854 | exported_value = expression(false);
|
---|
| 2855 | semicolon();
|
---|
| 2856 | } else if ((node = statement(is_default)) instanceof AST_Definitions && is_default) {
|
---|
| 2857 | unexpected(node.start);
|
---|
| 2858 | } else if (
|
---|
| 2859 | node instanceof AST_Definitions
|
---|
| 2860 | || node instanceof AST_Defun
|
---|
| 2861 | || node instanceof AST_DefClass
|
---|
| 2862 | ) {
|
---|
| 2863 | exported_definition = node;
|
---|
| 2864 | } else if (
|
---|
| 2865 | node instanceof AST_ClassExpression
|
---|
| 2866 | || node instanceof AST_Function
|
---|
| 2867 | ) {
|
---|
| 2868 | exported_value = node;
|
---|
| 2869 | } else if (node instanceof AST_SimpleStatement) {
|
---|
| 2870 | exported_value = node.body;
|
---|
| 2871 | } else {
|
---|
| 2872 | unexpected(node.start);
|
---|
| 2873 | }
|
---|
| 2874 |
|
---|
| 2875 | return new AST_Export({
|
---|
| 2876 | start: start,
|
---|
| 2877 | is_default: is_default,
|
---|
| 2878 | exported_value: exported_value,
|
---|
| 2879 | exported_definition: exported_definition,
|
---|
| 2880 | end: prev(),
|
---|
| 2881 | });
|
---|
| 2882 | }
|
---|
| 2883 |
|
---|
| 2884 | function as_property_name() {
|
---|
| 2885 | var tmp = S.token;
|
---|
| 2886 | switch (tmp.type) {
|
---|
| 2887 | case "punc":
|
---|
| 2888 | if (tmp.value === "[") {
|
---|
| 2889 | next();
|
---|
| 2890 | var ex = expression(false);
|
---|
| 2891 | expect("]");
|
---|
| 2892 | return ex;
|
---|
| 2893 | } else unexpected(tmp);
|
---|
| 2894 | case "operator":
|
---|
| 2895 | if (tmp.value === "*") {
|
---|
| 2896 | next();
|
---|
| 2897 | return null;
|
---|
| 2898 | }
|
---|
| 2899 | if (!["delete", "in", "instanceof", "new", "typeof", "void"].includes(tmp.value)) {
|
---|
| 2900 | unexpected(tmp);
|
---|
| 2901 | }
|
---|
| 2902 | /* falls through */
|
---|
| 2903 | case "name":
|
---|
| 2904 | case "privatename":
|
---|
| 2905 | case "string":
|
---|
| 2906 | case "num":
|
---|
| 2907 | case "big_int":
|
---|
| 2908 | case "keyword":
|
---|
| 2909 | case "atom":
|
---|
| 2910 | next();
|
---|
| 2911 | return tmp.value;
|
---|
| 2912 | default:
|
---|
| 2913 | unexpected(tmp);
|
---|
| 2914 | }
|
---|
| 2915 | }
|
---|
| 2916 |
|
---|
| 2917 | function as_name() {
|
---|
| 2918 | var tmp = S.token;
|
---|
| 2919 | if (tmp.type != "name" && tmp.type != "privatename") unexpected();
|
---|
| 2920 | next();
|
---|
| 2921 | return tmp.value;
|
---|
| 2922 | }
|
---|
| 2923 |
|
---|
| 2924 | function _make_symbol(type) {
|
---|
| 2925 | var name = S.token.value;
|
---|
| 2926 | return new (name == "this" ? AST_This :
|
---|
| 2927 | name == "super" ? AST_Super :
|
---|
| 2928 | type)({
|
---|
| 2929 | name : String(name),
|
---|
| 2930 | start : S.token,
|
---|
| 2931 | end : S.token
|
---|
| 2932 | });
|
---|
| 2933 | }
|
---|
| 2934 |
|
---|
| 2935 | function _verify_symbol(sym) {
|
---|
| 2936 | var name = sym.name;
|
---|
| 2937 | if (is_in_generator() && name == "yield") {
|
---|
| 2938 | token_error(sym.start, "Yield cannot be used as identifier inside generators");
|
---|
| 2939 | }
|
---|
| 2940 | if (S.input.has_directive("use strict")) {
|
---|
| 2941 | if (name == "yield") {
|
---|
| 2942 | token_error(sym.start, "Unexpected yield identifier inside strict mode");
|
---|
| 2943 | }
|
---|
| 2944 | if (sym instanceof AST_SymbolDeclaration && (name == "arguments" || name == "eval")) {
|
---|
| 2945 | token_error(sym.start, "Unexpected " + name + " in strict mode");
|
---|
| 2946 | }
|
---|
| 2947 | }
|
---|
| 2948 | }
|
---|
| 2949 |
|
---|
| 2950 | function as_symbol(type, noerror) {
|
---|
| 2951 | if (!is("name")) {
|
---|
| 2952 | if (!noerror) croak("Name expected");
|
---|
| 2953 | return null;
|
---|
| 2954 | }
|
---|
| 2955 | var sym = _make_symbol(type);
|
---|
| 2956 | _verify_symbol(sym);
|
---|
| 2957 | next();
|
---|
| 2958 | return sym;
|
---|
| 2959 | }
|
---|
| 2960 |
|
---|
| 2961 | // Annotate AST_Call, AST_Lambda or AST_New with the special comments
|
---|
| 2962 | function annotate(node) {
|
---|
| 2963 | var start = node.start;
|
---|
| 2964 | var comments = start.comments_before;
|
---|
| 2965 | const comments_outside_parens = outer_comments_before_counts.get(start);
|
---|
| 2966 | var i = comments_outside_parens != null ? comments_outside_parens : comments.length;
|
---|
| 2967 | while (--i >= 0) {
|
---|
| 2968 | var comment = comments[i];
|
---|
| 2969 | if (/[@#]__/.test(comment.value)) {
|
---|
| 2970 | if (/[@#]__PURE__/.test(comment.value)) {
|
---|
| 2971 | set_annotation(node, _PURE);
|
---|
| 2972 | break;
|
---|
| 2973 | }
|
---|
| 2974 | if (/[@#]__INLINE__/.test(comment.value)) {
|
---|
| 2975 | set_annotation(node, _INLINE);
|
---|
| 2976 | break;
|
---|
| 2977 | }
|
---|
| 2978 | if (/[@#]__NOINLINE__/.test(comment.value)) {
|
---|
| 2979 | set_annotation(node, _NOINLINE);
|
---|
| 2980 | break;
|
---|
| 2981 | }
|
---|
| 2982 | }
|
---|
| 2983 | }
|
---|
| 2984 | }
|
---|
| 2985 |
|
---|
| 2986 | var subscripts = function(expr, allow_calls, is_chain) {
|
---|
| 2987 | var start = expr.start;
|
---|
| 2988 | if (is("punc", ".")) {
|
---|
| 2989 | next();
|
---|
| 2990 | const AST_DotVariant = is("privatename") ? AST_DotHash : AST_Dot;
|
---|
| 2991 | return subscripts(new AST_DotVariant({
|
---|
| 2992 | start : start,
|
---|
| 2993 | expression : expr,
|
---|
| 2994 | optional : false,
|
---|
| 2995 | property : as_name(),
|
---|
| 2996 | end : prev()
|
---|
| 2997 | }), allow_calls, is_chain);
|
---|
| 2998 | }
|
---|
| 2999 | if (is("punc", "[")) {
|
---|
| 3000 | next();
|
---|
| 3001 | var prop = expression(true);
|
---|
| 3002 | expect("]");
|
---|
| 3003 | return subscripts(new AST_Sub({
|
---|
| 3004 | start : start,
|
---|
| 3005 | expression : expr,
|
---|
| 3006 | optional : false,
|
---|
| 3007 | property : prop,
|
---|
| 3008 | end : prev()
|
---|
| 3009 | }), allow_calls, is_chain);
|
---|
| 3010 | }
|
---|
| 3011 | if (allow_calls && is("punc", "(")) {
|
---|
| 3012 | next();
|
---|
| 3013 | var call = new AST_Call({
|
---|
| 3014 | start : start,
|
---|
| 3015 | expression : expr,
|
---|
| 3016 | optional : false,
|
---|
| 3017 | args : call_args(),
|
---|
| 3018 | end : prev()
|
---|
| 3019 | });
|
---|
| 3020 | annotate(call);
|
---|
| 3021 | return subscripts(call, true, is_chain);
|
---|
| 3022 | }
|
---|
| 3023 |
|
---|
| 3024 | if (is("punc", "?.")) {
|
---|
| 3025 | next();
|
---|
| 3026 |
|
---|
| 3027 | let chain_contents;
|
---|
| 3028 |
|
---|
| 3029 | if (allow_calls && is("punc", "(")) {
|
---|
| 3030 | next();
|
---|
| 3031 |
|
---|
| 3032 | const call = new AST_Call({
|
---|
| 3033 | start,
|
---|
| 3034 | optional: true,
|
---|
| 3035 | expression: expr,
|
---|
| 3036 | args: call_args(),
|
---|
| 3037 | end: prev()
|
---|
| 3038 | });
|
---|
| 3039 | annotate(call);
|
---|
| 3040 |
|
---|
| 3041 | chain_contents = subscripts(call, true, true);
|
---|
| 3042 | } else if (is("name") || is("privatename")) {
|
---|
| 3043 | const AST_DotVariant = is("privatename") ? AST_DotHash : AST_Dot;
|
---|
| 3044 | chain_contents = subscripts(new AST_DotVariant({
|
---|
| 3045 | start,
|
---|
| 3046 | expression: expr,
|
---|
| 3047 | optional: true,
|
---|
| 3048 | property: as_name(),
|
---|
| 3049 | end: prev()
|
---|
| 3050 | }), allow_calls, true);
|
---|
| 3051 | } else if (is("punc", "[")) {
|
---|
| 3052 | next();
|
---|
| 3053 | const property = expression(true);
|
---|
| 3054 | expect("]");
|
---|
| 3055 | chain_contents = subscripts(new AST_Sub({
|
---|
| 3056 | start,
|
---|
| 3057 | expression: expr,
|
---|
| 3058 | optional: true,
|
---|
| 3059 | property,
|
---|
| 3060 | end: prev()
|
---|
| 3061 | }), allow_calls, true);
|
---|
| 3062 | }
|
---|
| 3063 |
|
---|
| 3064 | if (!chain_contents) unexpected();
|
---|
| 3065 |
|
---|
| 3066 | if (chain_contents instanceof AST_Chain) return chain_contents;
|
---|
| 3067 |
|
---|
| 3068 | return new AST_Chain({
|
---|
| 3069 | start,
|
---|
| 3070 | expression: chain_contents,
|
---|
| 3071 | end: prev()
|
---|
| 3072 | });
|
---|
| 3073 | }
|
---|
| 3074 |
|
---|
| 3075 | if (is("template_head")) {
|
---|
| 3076 | if (is_chain) {
|
---|
| 3077 | // a?.b`c` is a syntax error
|
---|
| 3078 | unexpected();
|
---|
| 3079 | }
|
---|
| 3080 |
|
---|
| 3081 | return subscripts(new AST_PrefixedTemplateString({
|
---|
| 3082 | start: start,
|
---|
| 3083 | prefix: expr,
|
---|
| 3084 | template_string: template_string(),
|
---|
| 3085 | end: prev()
|
---|
| 3086 | }), allow_calls);
|
---|
| 3087 | }
|
---|
| 3088 |
|
---|
| 3089 | return expr;
|
---|
| 3090 | };
|
---|
| 3091 |
|
---|
| 3092 | function call_args() {
|
---|
| 3093 | var args = [];
|
---|
| 3094 | while (!is("punc", ")")) {
|
---|
| 3095 | if (is("expand", "...")) {
|
---|
| 3096 | next();
|
---|
| 3097 | args.push(new AST_Expansion({
|
---|
| 3098 | start: prev(),
|
---|
| 3099 | expression: expression(false),
|
---|
| 3100 | end: prev()
|
---|
| 3101 | }));
|
---|
| 3102 | } else {
|
---|
| 3103 | args.push(expression(false));
|
---|
| 3104 | }
|
---|
| 3105 | if (!is("punc", ")")) {
|
---|
| 3106 | expect(",");
|
---|
| 3107 | }
|
---|
| 3108 | }
|
---|
| 3109 | next();
|
---|
| 3110 | return args;
|
---|
| 3111 | }
|
---|
| 3112 |
|
---|
| 3113 | var maybe_unary = function(allow_calls, allow_arrows) {
|
---|
| 3114 | var start = S.token;
|
---|
| 3115 | if (start.type == "name" && start.value == "await" && can_await()) {
|
---|
| 3116 | next();
|
---|
| 3117 | return _await_expression();
|
---|
| 3118 | }
|
---|
| 3119 | if (is("operator") && UNARY_PREFIX.has(start.value)) {
|
---|
| 3120 | next();
|
---|
| 3121 | handle_regexp();
|
---|
| 3122 | var ex = make_unary(AST_UnaryPrefix, start, maybe_unary(allow_calls));
|
---|
| 3123 | ex.start = start;
|
---|
| 3124 | ex.end = prev();
|
---|
| 3125 | return ex;
|
---|
| 3126 | }
|
---|
| 3127 | var val = expr_atom(allow_calls, allow_arrows);
|
---|
| 3128 | while (is("operator") && UNARY_POSTFIX.has(S.token.value) && !has_newline_before(S.token)) {
|
---|
| 3129 | if (val instanceof AST_Arrow) unexpected();
|
---|
| 3130 | val = make_unary(AST_UnaryPostfix, S.token, val);
|
---|
| 3131 | val.start = start;
|
---|
| 3132 | val.end = S.token;
|
---|
| 3133 | next();
|
---|
| 3134 | }
|
---|
| 3135 | return val;
|
---|
| 3136 | };
|
---|
| 3137 |
|
---|
| 3138 | function make_unary(ctor, token, expr) {
|
---|
| 3139 | var op = token.value;
|
---|
| 3140 | switch (op) {
|
---|
| 3141 | case "++":
|
---|
| 3142 | case "--":
|
---|
| 3143 | if (!is_assignable(expr))
|
---|
| 3144 | croak("Invalid use of " + op + " operator", token.line, token.col, token.pos);
|
---|
| 3145 | break;
|
---|
| 3146 | case "delete":
|
---|
| 3147 | if (expr instanceof AST_SymbolRef && S.input.has_directive("use strict"))
|
---|
| 3148 | croak("Calling delete on expression not allowed in strict mode", expr.start.line, expr.start.col, expr.start.pos);
|
---|
| 3149 | break;
|
---|
| 3150 | }
|
---|
| 3151 | return new ctor({ operator: op, expression: expr });
|
---|
| 3152 | }
|
---|
| 3153 |
|
---|
| 3154 | var expr_op = function(left, min_prec, no_in) {
|
---|
| 3155 | var op = is("operator") ? S.token.value : null;
|
---|
| 3156 | if (op == "in" && no_in) op = null;
|
---|
| 3157 | if (op == "**" && left instanceof AST_UnaryPrefix
|
---|
| 3158 | /* unary token in front not allowed - parenthesis required */
|
---|
| 3159 | && !is_token(left.start, "punc", "(")
|
---|
| 3160 | && left.operator !== "--" && left.operator !== "++")
|
---|
| 3161 | unexpected(left.start);
|
---|
| 3162 | var prec = op != null ? PRECEDENCE[op] : null;
|
---|
| 3163 | if (prec != null && (prec > min_prec || (op === "**" && min_prec === prec))) {
|
---|
| 3164 | next();
|
---|
| 3165 | var right = expr_op(maybe_unary(true), prec, no_in);
|
---|
| 3166 | return expr_op(new AST_Binary({
|
---|
| 3167 | start : left.start,
|
---|
| 3168 | left : left,
|
---|
| 3169 | operator : op,
|
---|
| 3170 | right : right,
|
---|
| 3171 | end : right.end
|
---|
| 3172 | }), min_prec, no_in);
|
---|
| 3173 | }
|
---|
| 3174 | return left;
|
---|
| 3175 | };
|
---|
| 3176 |
|
---|
| 3177 | function expr_ops(no_in) {
|
---|
| 3178 | return expr_op(maybe_unary(true, true), 0, no_in);
|
---|
| 3179 | }
|
---|
| 3180 |
|
---|
| 3181 | var maybe_conditional = function(no_in) {
|
---|
| 3182 | var start = S.token;
|
---|
| 3183 | var expr = expr_ops(no_in);
|
---|
| 3184 | if (is("operator", "?")) {
|
---|
| 3185 | next();
|
---|
| 3186 | var yes = expression(false);
|
---|
| 3187 | expect(":");
|
---|
| 3188 | return new AST_Conditional({
|
---|
| 3189 | start : start,
|
---|
| 3190 | condition : expr,
|
---|
| 3191 | consequent : yes,
|
---|
| 3192 | alternative : expression(false, no_in),
|
---|
| 3193 | end : prev()
|
---|
| 3194 | });
|
---|
| 3195 | }
|
---|
| 3196 | return expr;
|
---|
| 3197 | };
|
---|
| 3198 |
|
---|
| 3199 | function is_assignable(expr) {
|
---|
| 3200 | return expr instanceof AST_PropAccess || expr instanceof AST_SymbolRef;
|
---|
| 3201 | }
|
---|
| 3202 |
|
---|
| 3203 | function to_destructuring(node) {
|
---|
| 3204 | if (node instanceof AST_Object) {
|
---|
| 3205 | node = new AST_Destructuring({
|
---|
| 3206 | start: node.start,
|
---|
| 3207 | names: node.properties.map(to_destructuring),
|
---|
| 3208 | is_array: false,
|
---|
| 3209 | end: node.end
|
---|
| 3210 | });
|
---|
| 3211 | } else if (node instanceof AST_Array) {
|
---|
| 3212 | var names = [];
|
---|
| 3213 |
|
---|
| 3214 | for (var i = 0; i < node.elements.length; i++) {
|
---|
| 3215 | // Only allow expansion as last element
|
---|
| 3216 | if (node.elements[i] instanceof AST_Expansion) {
|
---|
| 3217 | if (i + 1 !== node.elements.length) {
|
---|
| 3218 | token_error(node.elements[i].start, "Spread must the be last element in destructuring array");
|
---|
| 3219 | }
|
---|
| 3220 | node.elements[i].expression = to_destructuring(node.elements[i].expression);
|
---|
| 3221 | }
|
---|
| 3222 |
|
---|
| 3223 | names.push(to_destructuring(node.elements[i]));
|
---|
| 3224 | }
|
---|
| 3225 |
|
---|
| 3226 | node = new AST_Destructuring({
|
---|
| 3227 | start: node.start,
|
---|
| 3228 | names: names,
|
---|
| 3229 | is_array: true,
|
---|
| 3230 | end: node.end
|
---|
| 3231 | });
|
---|
| 3232 | } else if (node instanceof AST_ObjectProperty) {
|
---|
| 3233 | node.value = to_destructuring(node.value);
|
---|
| 3234 | } else if (node instanceof AST_Assign) {
|
---|
| 3235 | node = new AST_DefaultAssign({
|
---|
| 3236 | start: node.start,
|
---|
| 3237 | left: node.left,
|
---|
| 3238 | operator: "=",
|
---|
| 3239 | right: node.right,
|
---|
| 3240 | end: node.end
|
---|
| 3241 | });
|
---|
| 3242 | }
|
---|
| 3243 | return node;
|
---|
| 3244 | }
|
---|
| 3245 |
|
---|
| 3246 | // In ES6, AssignmentExpression can also be an ArrowFunction
|
---|
| 3247 | var maybe_assign = function(no_in) {
|
---|
| 3248 | handle_regexp();
|
---|
| 3249 | var start = S.token;
|
---|
| 3250 |
|
---|
| 3251 | if (start.type == "name" && start.value == "yield") {
|
---|
| 3252 | if (is_in_generator()) {
|
---|
| 3253 | next();
|
---|
| 3254 | return _yield_expression();
|
---|
| 3255 | } else if (S.input.has_directive("use strict")) {
|
---|
| 3256 | token_error(S.token, "Unexpected yield identifier inside strict mode");
|
---|
| 3257 | }
|
---|
| 3258 | }
|
---|
| 3259 |
|
---|
| 3260 | var left = maybe_conditional(no_in);
|
---|
| 3261 | var val = S.token.value;
|
---|
| 3262 |
|
---|
| 3263 | if (is("operator") && ASSIGNMENT.has(val)) {
|
---|
| 3264 | if (is_assignable(left) || (left = to_destructuring(left)) instanceof AST_Destructuring) {
|
---|
| 3265 | next();
|
---|
| 3266 |
|
---|
| 3267 | return new AST_Assign({
|
---|
| 3268 | start : start,
|
---|
| 3269 | left : left,
|
---|
| 3270 | operator : val,
|
---|
| 3271 | right : maybe_assign(no_in),
|
---|
| 3272 | logical : LOGICAL_ASSIGNMENT.has(val),
|
---|
| 3273 | end : prev()
|
---|
| 3274 | });
|
---|
| 3275 | }
|
---|
| 3276 | croak("Invalid assignment");
|
---|
| 3277 | }
|
---|
| 3278 | return left;
|
---|
| 3279 | };
|
---|
| 3280 |
|
---|
| 3281 | var expression = function(commas, no_in) {
|
---|
| 3282 | var start = S.token;
|
---|
| 3283 | var exprs = [];
|
---|
| 3284 | while (true) {
|
---|
| 3285 | exprs.push(maybe_assign(no_in));
|
---|
| 3286 | if (!commas || !is("punc", ",")) break;
|
---|
| 3287 | next();
|
---|
| 3288 | commas = true;
|
---|
| 3289 | }
|
---|
| 3290 | return exprs.length == 1 ? exprs[0] : new AST_Sequence({
|
---|
| 3291 | start : start,
|
---|
| 3292 | expressions : exprs,
|
---|
| 3293 | end : peek()
|
---|
| 3294 | });
|
---|
| 3295 | };
|
---|
| 3296 |
|
---|
| 3297 | function in_loop(cont) {
|
---|
| 3298 | ++S.in_loop;
|
---|
| 3299 | var ret = cont();
|
---|
| 3300 | --S.in_loop;
|
---|
| 3301 | return ret;
|
---|
| 3302 | }
|
---|
| 3303 |
|
---|
| 3304 | if (options.expression) {
|
---|
| 3305 | return expression(true);
|
---|
| 3306 | }
|
---|
| 3307 |
|
---|
| 3308 | return (function parse_toplevel() {
|
---|
| 3309 | var start = S.token;
|
---|
| 3310 | var body = [];
|
---|
| 3311 | S.input.push_directives_stack();
|
---|
| 3312 | if (options.module) S.input.add_directive("use strict");
|
---|
| 3313 | while (!is("eof")) {
|
---|
| 3314 | body.push(statement());
|
---|
| 3315 | }
|
---|
| 3316 | S.input.pop_directives_stack();
|
---|
| 3317 | var end = prev();
|
---|
| 3318 | var toplevel = options.toplevel;
|
---|
| 3319 | if (toplevel) {
|
---|
| 3320 | toplevel.body = toplevel.body.concat(body);
|
---|
| 3321 | toplevel.end = end;
|
---|
| 3322 | } else {
|
---|
| 3323 | toplevel = new AST_Toplevel({ start: start, body: body, end: end });
|
---|
| 3324 | }
|
---|
| 3325 | return toplevel;
|
---|
| 3326 | })();
|
---|
| 3327 |
|
---|
| 3328 | }
|
---|
| 3329 |
|
---|
| 3330 | export {
|
---|
| 3331 | get_full_char_code,
|
---|
| 3332 | get_full_char,
|
---|
| 3333 | is_identifier_char,
|
---|
| 3334 | is_basic_identifier_string,
|
---|
| 3335 | is_identifier_string,
|
---|
| 3336 | is_surrogate_pair_head,
|
---|
| 3337 | is_surrogate_pair_tail,
|
---|
| 3338 | js_error,
|
---|
| 3339 | JS_Parse_Error,
|
---|
| 3340 | parse,
|
---|
| 3341 | PRECEDENCE,
|
---|
| 3342 | RESERVED_WORDS,
|
---|
| 3343 | tokenizer,
|
---|
| 3344 | };
|
---|