Debian Codegraph @

vendor/wikimedia/minify/src/JavaScriptMinifier.php

raw
1 <?php
2 /**
3 * Copyright 2011 Paul Copperman <[email protected]>
4 * Copyright 2018 Timo Tijhof
5 * Copyright 2021 Roan Kattouw <[email protected]>
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @file
20 * @license Apache-2.0
21 * @license MIT
22 * @license GPL-2.0-or-later
23 * @license LGPL-2.1-or-later
24 */
25
26 namespace Wikimedia\Minify;
27
28 use ReflectionClass;
29
30 /**
31 * JavaScript Minifier
32 *
33 * This class is meant to safely minify JavaScript code, while leaving syntactically correct
34 * programs intact. Other libraries, such as JSMin require a certain coding style to work
35 * correctly. OTOH, libraries like jsminplus, that do parse the code correctly are rather
36 * slow, because they construct a complete parse tree before outputting the code minified.
37 * So this class is meant to allow arbitrary (but syntactically correct) input, while being
38 * fast enough to be used for on-the-fly minifying.
39 *
40 * This class was written with ECMA-262 8th Edition in mind ("ECMAScript 2017"). Parsing features
41 * new to later editions of ECMAScript might not be supported. It's assumed that the input is
42 * syntactically correct; if it's not, this class may not detect that, and may produce incorrect
43 * output.
44 *
45 * See also:
46 * - <https://262.ecma-international.org/8.0/>
47 * - <https://262.ecma-international.org/10.0/>
48 * - <https://262.ecma-international.org/11.0/>
49 */
50 class JavaScriptMinifier {
51
52 /* Parsing states.
53 * The state machine is necessary to decide whether to parse a slash as division
54 * operator or as regexp literal, and to know where semicolon insertion is possible.
55 * States are generally named after the next expected item. We only distinguish states when the
56 * distinction is relevant for our purpose. The meaning of these states is documented
57 * in $model below.
58 *
59 * Negative numbers are used to indicate that the state is inside a generator function,
60 * which changes the behavior of 'yield'
61 */
62 private const STATEMENT = 1;
63 private const CONDITION = 2;
64 private const FUNC = 3;
65 private const GENFUNC = 4;
66 private const PROPERTY_ASSIGNMENT = 5;
67 private const EXPRESSION = 6;
68 private const EXPRESSION_NO_NL = 7;
69 private const EXPRESSION_OP = 8;
70 private const EXPRESSION_DOT = 9;
71 private const EXPRESSION_END = 10;
72 private const EXPRESSION_ARROWFUNC = 11;
73 private const EXPRESSION_TERNARY = 12;
74 private const EXPRESSION_TERNARY_OP = 13;
75 private const EXPRESSION_TERNARY_DOT = 14;
76 private const EXPRESSION_TERNARY_ARROWFUNC = 15;
77 private const PAREN_EXPRESSION = 16;
78 private const PAREN_EXPRESSION_OP = 17;
79 private const PAREN_EXPRESSION_DOT = 18;
80 private const PAREN_EXPRESSION_ARROWFUNC = 19;
81 private const PROPERTY_EXPRESSION = 20;
82 private const PROPERTY_EXPRESSION_OP = 21;
83 private const PROPERTY_EXPRESSION_DOT = 22;
84 private const PROPERTY_EXPRESSION_ARROWFUNC = 23;
85 private const CLASS_DEF = 24;
86 private const IMPORT_EXPORT = 25;
87 private const TEMPLATE_STRING_HEAD = 26;
88 private const TEMPLATE_STRING_TAIL = 27;
89 private const PAREN_EXPRESSION_OP_NO_NL = 28;
90 private const EXPRESSION_TERNARY_NO_NL = 29;
91 private const PAREN_EXPRESSION_NO_NL = 30;
92 private const PROPERTY_EXPRESSION_NO_NL = 31;
93 private const PROPERTY_EXPRESSION_ASYNC = 32;
94
95 /* Token types */
96
97 /** @var int unary operators */
98 private const TYPE_UN_OP = 101;
99
100 /** @var int ++ and -- */
101 private const TYPE_INCR_OP = 102;
102
103 /** @var int binary operators (except .) */
104 private const TYPE_BIN_OP = 103;
105
106 /** @var int + and - which can be either unary or binary ops */
107 private const TYPE_ADD_OP = 104;
108
109 /** @var int . */
110 private const TYPE_DOT = 105;
111
112 /** @var int ? */
113 private const TYPE_HOOK = 106;
114
115 /** @var int : */
116 private const TYPE_COLON = 107;
117
118 /** @var int , */
119 private const TYPE_COMMA = 108;
120
121 /** @var int ; */
122 private const TYPE_SEMICOLON = 109;
123
124 /** @var int { */
125 private const TYPE_BRACE_OPEN = 110;
126
127 /** @var int } */
128 private const TYPE_BRACE_CLOSE = 111;
129
130 /** @var int ( and [ */
131 private const TYPE_PAREN_OPEN = 112;
132
133 /** @var int ) and ] */
134 private const TYPE_PAREN_CLOSE = 113;
135
136 /** @var int => */
137 private const TYPE_ARROW = 114;
138
139 /** @var int keywords: break, continue, return, throw (and yield, if we're in a generator) */
140 private const TYPE_RETURN = 115;
141
142 /** @var int keywords: catch, for, with, switch, while, if */
143 private const TYPE_IF = 116;
144
145 /** @var int keywords: case, finally, else, do, try */
146 private const TYPE_DO = 117;
147
148 /** @var int keywords: var, let, const */
149 private const TYPE_VAR = 118;
150
151 /** @var int keywords: yield */
152 private const TYPE_YIELD = 119;
153
154 /** @var int keywords: function */
155 private const TYPE_FUNC = 120;
156
157 /** @var int keywords: class */
158 private const TYPE_CLASS = 121;
159
160 /** @var int all literals, identifiers, unrecognised tokens, and other keywords */
161 private const TYPE_LITERAL = 122;
162
163 /** @var int For special treatment of tokens that usually mean something else */
164 private const TYPE_SPECIAL = 123;
165
166 /** @var int keywords: async */
167 private const TYPE_ASYNC = 124;
168
169 /** @var int keywords: await */
170 private const TYPE_AWAIT = 125;
171
172 /** @var int Go to another state */
173 private const ACTION_GOTO = 201;
174
175 /** @var int Push a state to the stack */
176 private const ACTION_PUSH = 202;
177
178 /** @var int Pop the state from the top of the stack, and go to that state */
179 private const ACTION_POP = 203;
180
181 /** @var int Limit to avoid excessive memory usage */
182 private const STACK_LIMIT = 1000;
183
184 /** Length of the longest token in $tokenTypes made of punctuation characters,
185 * as defined in $opChars. Update this if you add longer tokens to $tokenTypes.
186 *
187 * Currently, the longest punctuation token is `>>>=`, which is 4 characters.
188 */
189 private const LONGEST_PUNCTUATION_TOKEN = 4;
190
191 /**
192 * @var int $maxLineLength
193 *
194 * Maximum line length
195 *
196 * This is not a strict maximum, but a guideline. Longer lines will be
197 * produced when literals (e.g. quoted strings) longer than this are
198 * encountered, or when required to guard against semicolon insertion.
199 *
200 * This is a private member (instead of constant) to allow tests to
201 * set it to 1, to verify ASI and line-breaking behaviour.
202 */
203 private static $maxLineLength = 1000;
204
205 private static bool $expandedStates = false;
206
207 /**
208 * @var array $opChars
209 *
210 * Characters which can be combined without whitespace between them.
211 * Unlike the ECMAScript spec, we define these as individual symbols, not sequences.
212 */
213 private static $opChars = [
214 // ECMAScript 8.0 § 11.7 Punctuators
215 //
216 // Punctuator
217 // DivPunctuator
218 // RightBracePunctuator
219 //
220 '{' => true,
221 '(' => true,
222 ')' => true,
223 '[' => true,
224 ']' => true,
225 // Dots have a special case after $dotlessNum which require whitespace
226 '.' => true,
227 ';' => true,
228 ',' => true,
229 '<' => true,
230 '>' => true,
231 '=' => true,
232 '!' => true,
233 '+' => true,
234 '-' => true,
235 '*' => true,
236 '%' => true,
237 '&' => true,
238 '|' => true,
239 '^' => true,
240 '~' => true,
241 '?' => true,
242 ':' => true,
243 '/' => true,
244 '}' => true,
245
246 // ECMAScript 8.0 § 11.8.4 String Literals
247 '"' => true,
248 "'" => true,
249
250 // ECMAScript 8.0 § 11.8.6 Template Literal Lexical Components
251 '`' => true,
252 ];
253
254 /**
255 * @var array $tokenTypes
256 *
257 * Tokens and their types.
258 */
259 private static $tokenTypes = [
260 // ECMAScript 8.0 § 12.2 Primary Expression
261 //
262 // ...BindingIdentifier
263 //
264 '...' => self::TYPE_UN_OP,
265
266 // ECMAScript 8.0 § 12.3 Left-Hand-Side Expressions
267 //
268 // MemberExpression
269 //
270 // A dot can also be part of a DecimalLiteral, but in that case we handle the entire
271 // DecimalLiteral as one token. A separate '.' token is always part of a MemberExpression.
272 '.' => self::TYPE_DOT,
273
274 // ECMAScript 8.0 § 12.4 Update Expressions
275 //
276 // LeftHandSideExpression [no LineTerminator here] ++
277 // LeftHandSideExpression [no LineTerminator here] --
278 // ++ UnaryExpression
279 // -- UnaryExpression
280 //
281 // This is given a separate type from TYPE_UN_OP,
282 // because `++` and `--` require special handling
283 // around new lines and semicolon insertion.
284 //
285 '++' => self::TYPE_INCR_OP,
286 '--' => self::TYPE_INCR_OP,
287
288 // ECMAScript 8.0 § 12.5 Unary Operators
289 //
290 // UnaryExpression
291 // includes UpdateExpression
292 // includes NewExpression, which defines 'new'
293 //
294 'new' => self::TYPE_UN_OP,
295 'delete' => self::TYPE_UN_OP,
296 'void' => self::TYPE_UN_OP,
297 'typeof' => self::TYPE_UN_OP,
298 '~' => self::TYPE_UN_OP,
299 '!' => self::TYPE_UN_OP,
300
301 // These operators can be either binary or unary depending on context,
302 // and thus require separate type from TYPE_UN_OP and TYPE_BIN_OP.
303 //
304 // var z = +y; // unary (convert to number)
305 // var z = x + y; // binary (add operation)
306 //
307 // ECMAScript 8.0 § 12.5 Unary Operators
308 //
309 // + UnaryExpression
310 // - UnaryExpression
311 //
312 // ECMAScript 8.0 § 12.8 Additive Operators
313 //
314 // Expression + Expression
315 // Expression - Expression
316 //
317 '+' => self::TYPE_ADD_OP,
318 '-' => self::TYPE_ADD_OP,
319
320 // These operators can be treated the same as binary operators.
321 // They are all defined in one of these two forms, and do
322 // not require special handling for preserving whitespace or
323 // line breaks.
324 //
325 // Expression operator Expression
326 //
327 // Defined in:
328 // - ECMAScript 8.0 § 12.6 Exponentiation Operator
329 // ExponentiationExpression
330 // - ECMAScript 8.0 § 12.7 Multiplicative Operators
331 // MultiplicativeOperator
332 // - ECMAScript 8.0 § 12.9 Bitwise Shift Operators
333 // ShiftExpression
334 // - ECMAScript 8.0 § 12.10 Relational Operators
335 // RelationalExpression
336 // - ECMAScript 8.0 § 12.11 Equality Operators
337 // EqualityExpression
338 '**' => self::TYPE_BIN_OP,
339 '*' => self::TYPE_BIN_OP,
340 '/' => self::TYPE_BIN_OP,
341 '%' => self::TYPE_BIN_OP,
342 '<<' => self::TYPE_BIN_OP,
343 '>>' => self::TYPE_BIN_OP,
344 '>>>' => self::TYPE_BIN_OP,
345 '<' => self::TYPE_BIN_OP,
346 '>' => self::TYPE_BIN_OP,
347 '<=' => self::TYPE_BIN_OP,
348 '>=' => self::TYPE_BIN_OP,
349 'instanceof' => self::TYPE_BIN_OP,
350 'in' => self::TYPE_BIN_OP,
351 '==' => self::TYPE_BIN_OP,
352 '!=' => self::TYPE_BIN_OP,
353 '===' => self::TYPE_BIN_OP,
354 '!==' => self::TYPE_BIN_OP,
355
356 // ECMAScript 8.0 § 12.12 Binary Bitwise Operators
357 //
358 // BitwiseANDExpression
359 // BitwiseXORExpression
360 // BitwiseORExpression
361 //
362 '&' => self::TYPE_BIN_OP,
363 '^' => self::TYPE_BIN_OP,
364 '|' => self::TYPE_BIN_OP,
365
366 // ECMAScript 8.0 § 12.13 Binary Logical Operators
367 //
368 // LogicalANDExpression
369 // LogicalORExpression
370 //
371 '&&' => self::TYPE_BIN_OP,
372 '||' => self::TYPE_BIN_OP,
373
374 // ECMAScript 11.0 § 12.13 Binary Logical Operators
375 '??' => self::TYPE_BIN_OP,
376
377 // ECMAScript 8.0 § 12.14 Conditional Operator
378 //
379 // ConditionalExpression:
380 // LogicalORExpression ? AssignmentExpression : AssignmentExpression
381 //
382 // Also known as ternary.
383 '?' => self::TYPE_HOOK,
384 ':' => self::TYPE_COLON,
385
386 // ECMAScript 8.0 § 12.15 Assignment Operators
387 '=' => self::TYPE_BIN_OP,
388 '*=' => self::TYPE_BIN_OP,
389 '/=' => self::TYPE_BIN_OP,
390 '%=' => self::TYPE_BIN_OP,
391 '+=' => self::TYPE_BIN_OP,
392 '-=' => self::TYPE_BIN_OP,
393 '<<=' => self::TYPE_BIN_OP,
394 '>>=' => self::TYPE_BIN_OP,
395 '>>>=' => self::TYPE_BIN_OP,
396 '&=' => self::TYPE_BIN_OP,
397 '^=' => self::TYPE_BIN_OP,
398 '|=' => self::TYPE_BIN_OP,
399 '**=' => self::TYPE_BIN_OP,
400
401 // ECMAScript 8.0 § 12.16 Comma Operator
402 ',' => self::TYPE_COMMA,
403
404 // ECMAScript 8.0 § 11.9.1 Rules of Automatic Semicolon Insertion
405 //
406 // These keywords disallow LineTerminator before their (sometimes optional)
407 // Expression or Identifier. They are similar enough that we can treat
408 // them all the same way that we treat return, with regards to new line
409 // and semicolon insertion.
410 //
411 // keyword ;
412 // keyword [no LineTerminator here] Identifier ;
413 // keyword [no LineTerminator here] Expression ;
414 //
415 // See also ECMAScript 8.0:
416 // - § 13.8 The continue Statement
417 // - § 13.9 The break Statement
418 // - § 13.10 The return Statement
419 // - § 13.14 The throw Statement
420 // - § 14.4 Generator Function Definitions (yield)
421 'continue' => self::TYPE_RETURN,
422 'break' => self::TYPE_RETURN,
423 'return' => self::TYPE_RETURN,
424 'throw' => self::TYPE_RETURN,
425 // "yield" only counts as a keyword if when inside inside a generator functions,
426 // otherwise it is a regular identifier.
427 // This is handled with the negative states hack: if the state is negative, TYPE_YIELD
428 // is treated as TYPE_RETURN, if it's positive it's treated as TYPE_LITERAL
429 'yield' => self::TYPE_YIELD,
430
431 // These keywords require a parenthesised Expression or Identifier before the
432 // next Statement. They are similar enough to all treat like "if".
433 //
434 // keyword ( Expression ) Statement
435 // keyword ( Identifier ) Statement
436 //
437 // See also ECMAScript 8.0:
438 // - § 13.6 The if Statement
439 // - § 13.7 Iteration Statements (while, for)
440 // - § 13.11 The with Statement
441 // - § 13.12 The switch Statement
442 // - § 13.15 The try Statement (catch)
443 'if' => self::TYPE_IF,
444 'while' => self::TYPE_IF,
445 'for' => self::TYPE_IF,
446 'with' => self::TYPE_IF,
447 'switch' => self::TYPE_IF,
448 'catch' => self::TYPE_IF,
449
450 // The keywords followed by a Statement, Expression, or Block.
451 //
452 // keyword Statement
453 // keyword Expression
454 // keyword Block
455 //
456 // See also ECMAScript 8.0:
457 // - § 13.6 The if Statement (else)
458 // - § 13.7 Iteration Statements (do)
459 // - § 13.12 The switch Statement (case)
460 // - § 13.15 The try Statement (try, finally)
461 'else' => self::TYPE_DO,
462 'do' => self::TYPE_DO,
463 'case' => self::TYPE_DO,
464 'try' => self::TYPE_DO,
465 'finally' => self::TYPE_DO,
466
467 // ECMAScript 8.0 § 13.3 Declarations and the Variable Statement
468 //
469 // LetOrConst
470 // VariableStatement
471 //
472 // These keywords are followed by a variable declaration statement.
473 // This needs to be treated differently from the TYPE_DO group,
474 // because for TYPE_VAR, when we see an "{" open curly brace, it
475 // begins object destructuring (ObjectBindingPattern), not a block.
476 'var' => self::TYPE_VAR,
477 'let' => self::TYPE_VAR,
478 'const' => self::TYPE_VAR,
479
480 // ECMAScript 8.0 § 14.1 Function Definitions
481 'function' => self::TYPE_FUNC,
482
483 // ECMAScript 8.0 § 14.2 Arrow Function Definitions
484 '=>' => self::TYPE_ARROW,
485
486 // ECMAScript 8.0 § 14.5 Class Definitions
487 //
488 // class Identifier { ClassBody }
489 // class { ClassBody }
490 // class Identifier extends Expression { ClassBody }
491 // class extends Expression { ClassBody }
492 //
493 'class' => self::TYPE_CLASS,
494
495 // ECMAScript 8.0 § 14.6 AwaitExpression
496 //
497 // await UnaryExpression
498 //
499 'await' => self::TYPE_AWAIT,
500
501 // Can be one of:
502 // - Block (ECMAScript 8.0 § 13.2 Block)
503 // - ObjectLiteral (ECMAScript 8.0 § 12.2 Primary Expression)
504 '{' => self::TYPE_BRACE_OPEN,
505 '}' => self::TYPE_BRACE_CLOSE,
506
507 // Can be one of:
508 // - Parenthesised Identifier or Expression after a
509 // TYPE_IF or TYPE_FUNC keyword.
510 // - PrimaryExpression (ECMAScript 8.0 § 12.2 Primary Expression)
511 // - CallExpression (ECMAScript 8.0 § 12.3 Left-Hand-Side Expressions)
512 // - Beginning of an ArrowFunction (ECMAScript 8.0 § 14.2 Arrow Function Definitions)
513 '(' => self::TYPE_PAREN_OPEN,
514 ')' => self::TYPE_PAREN_CLOSE,
515
516 // Can be one of:
517 // - ArrayLiteral (ECMAScript 8.0 § 12.2 Primary Expressions)
518 // - ComputedPropertyName (ECMAScript 8.0 § 12.2.6 Object Initializer)
519 '[' => self::TYPE_PAREN_OPEN,
520 ']' => self::TYPE_PAREN_CLOSE,
521
522 // Can be one of:
523 // - End of any statement
524 // - EmptyStatement (ECMAScript 8.0 § 13.4 Empty Statement)
525 ';' => self::TYPE_SEMICOLON,
526
527 // ECMAScript 8.0 § 14.6 Async Function Definitions
528 // async [no LineTerminator here] function ...
529 // async [no LineTerminator here] propertyName() ...
530 'async' => self::TYPE_ASYNC,
531 ];
532
533 /**
534 * @var array $model
535 *
536 * The main table for the state machine. Defines the desired action for every state/token pair.
537 *
538 * The state pushed onto the stack by ACTION_PUSH will be returned to by ACTION_POP.
539 * A state/token pair may not specify both ACTION_POP and ACTION_GOTO. If that does happen,
540 * ACTION_POP takes precedence.
541 *
542 * This table is augmented by self::ensureExpandedStates().
543 */
544 private static $model = [
545 // Statement - This is the initial state.
546 self::STATEMENT => [
547 self::TYPE_UN_OP => [
548 self::ACTION_GOTO => self::EXPRESSION,
549 ],
550 self::TYPE_INCR_OP => [
551 self::ACTION_GOTO => self::EXPRESSION,
552 ],
553 self::TYPE_ADD_OP => [
554 self::ACTION_GOTO => self::EXPRESSION,
555 ],
556 self::TYPE_BRACE_OPEN => [
557 // Use of '{' in statement context, creates a Block.
558 self::ACTION_PUSH => self::STATEMENT,
559 ],
560 self::TYPE_BRACE_CLOSE => [
561 // Ends a Block
562 self::ACTION_POP => true,
563 ],
564 self::TYPE_PAREN_OPEN => [
565 self::ACTION_PUSH => self::EXPRESSION_OP,
566 self::ACTION_GOTO => self::PAREN_EXPRESSION,
567 ],
568 self::TYPE_RETURN => [
569 self::ACTION_GOTO => self::EXPRESSION_NO_NL,
570 ],
571 self::TYPE_IF => [
572 self::ACTION_GOTO => self::CONDITION,
573 ],
574 self::TYPE_VAR => [
575 self::ACTION_GOTO => self::EXPRESSION,
576 ],
577 self::TYPE_FUNC => [
578 self::ACTION_PUSH => self::STATEMENT,
579 self::ACTION_GOTO => self::FUNC,
580 ],
581 self::TYPE_CLASS => [
582 self::ACTION_PUSH => self::STATEMENT,
583 self::ACTION_GOTO => self::CLASS_DEF,
584 ],
585 self::TYPE_SPECIAL => [
586 'import' => [
587 self::ACTION_GOTO => self::IMPORT_EXPORT,
588 ],
589 'export' => [
590 self::ACTION_GOTO => self::IMPORT_EXPORT,
591 ],
592 ],
593 self::TYPE_LITERAL => [
594 self::ACTION_GOTO => self::EXPRESSION_OP,
595 ],
596 self::TYPE_ASYNC => [
597 self::ACTION_GOTO => self::EXPRESSION_OP,
598 ],
599 self::TYPE_AWAIT => [
600 self::ACTION_GOTO => self::EXPRESSION,
601 ],
602 ],
603 // The state after if/catch/while/for/switch/with
604 // Waits for an expression in parentheses, then goes to STATEMENT
605 self::CONDITION => [
606 self::TYPE_PAREN_OPEN => [
607 self::ACTION_PUSH => self::STATEMENT,
608 self::ACTION_GOTO => self::PAREN_EXPRESSION,
609 ],
610 self::TYPE_BRACE_OPEN => [
611 self::ACTION_PUSH => self::STATEMENT,
612 self::ACTION_GOTO => self::STATEMENT,
613 ]
614 ],
615 // The state after the function keyword. Waits for {, then goes to STATEMENT.
616 // The function body's closing } will pop the stack, so the state to return to
617 // after the function should be pushed to the stack first
618 self::FUNC => [
619 // Needed to prevent * in an expression in the argument list from improperly
620 // triggering GENFUNC
621 self::TYPE_PAREN_OPEN => [
622 self::ACTION_PUSH => self::FUNC,
623 self::ACTION_GOTO => self::PAREN_EXPRESSION,
624 ],
625 self::TYPE_BRACE_OPEN => [
626 self::ACTION_GOTO => self::STATEMENT,
627 ],
628 self::TYPE_SPECIAL => [
629 '*' => [
630 self::ACTION_GOTO => self::GENFUNC,
631 ],
632 ],
633 ],
634 // After function*. Waits for { , then goes to a generator function statement.
635 self::GENFUNC => [
636 self::TYPE_BRACE_OPEN => [
637 // Note negative value: generator function states are negative
638 self::ACTION_GOTO => -self::STATEMENT
639 ],
640 ],
641 // Property assignment - This is an object literal declaration.
642 // For example: `{ key: value, key2, [computedKey3]: value3, method4() { ... } }`
643 self::PROPERTY_ASSIGNMENT => [
644 // Note that keywords like "if", "class", "var", "delete", "async", etc, are
645 // valid key names, and should be treated as literals here. Like in EXPRESSION_DOT.
646 // For this state, this requires no special handling because TYPE_LITERAL
647 // has no action here, so we remain in this state.
648 //
649 // If this state ever gets a transition for TYPE_LITERAL, then that same transition
650 // must apply to TYPE_IF, TYPE_CLASS, TYPE_VAR, TYPE_ASYNC, etc as well.
651 self::TYPE_COLON => [
652 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
653 ],
654 // For {, which begins a method
655 self::TYPE_BRACE_OPEN => [
656 self::ACTION_PUSH => self::PROPERTY_ASSIGNMENT,
657 // This is not flipped, see "Special cases" below
658 self::ACTION_GOTO => self::STATEMENT,
659 ],
660 self::TYPE_BRACE_CLOSE => [
661 self::ACTION_POP => true,
662 ],
663 // For [, which begins a computed key
664 self::TYPE_PAREN_OPEN => [
665 self::ACTION_PUSH => self::PROPERTY_ASSIGNMENT,
666 self::ACTION_GOTO => self::PAREN_EXPRESSION,
667 ],
668 self::TYPE_SPECIAL => [
669 '*' => [
670 self::ACTION_PUSH => self::PROPERTY_ASSIGNMENT,
671 self::ACTION_GOTO => self::GENFUNC,
672 ],
673 ],
674 ],
675 // Place in an expression where we expect an operand or a unary operator: the start
676 // of an expression or after an operator. Note that unary operators (including INCR_OP
677 // and ADD_OP) cause us to stay in this state, while operands take us to EXPRESSION_OP
678 self::EXPRESSION => [
679 self::TYPE_SEMICOLON => [
680 self::ACTION_GOTO => self::STATEMENT,
681 ],
682 self::TYPE_BRACE_OPEN => [
683 self::ACTION_PUSH => self::EXPRESSION_OP,
684 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
685 ],
686 self::TYPE_BRACE_CLOSE => [
687 self::ACTION_POP => true,
688 ],
689 self::TYPE_PAREN_OPEN => [
690 self::ACTION_PUSH => self::EXPRESSION_OP,
691 self::ACTION_GOTO => self::PAREN_EXPRESSION,
692 ],
693 self::TYPE_FUNC => [
694 self::ACTION_PUSH => self::EXPRESSION_OP,
695 self::ACTION_GOTO => self::FUNC,
696 ],
697 self::TYPE_CLASS => [
698 self::ACTION_PUSH => self::EXPRESSION_OP,
699 self::ACTION_GOTO => self::CLASS_DEF,
700 ],
701 self::TYPE_LITERAL => [
702 self::ACTION_GOTO => self::EXPRESSION_OP,
703 ],
704 self::TYPE_ASYNC => [
705 self::ACTION_GOTO => self::EXPRESSION_OP,
706 ],
707 // 'return' can't appear here, but 'yield' can
708 self::TYPE_RETURN => [
709 self::ACTION_GOTO => self::EXPRESSION_NO_NL,
710 ],
711 ],
712 // An expression immediately after return/throw/break/continue/yield, where a newline
713 // is not allowed. This state is identical to EXPRESSION, except that semicolon
714 // insertion can happen here, and we (almost) never stay here: in cases where EXPRESSION
715 // would do nothing, we go to EXPRESSION. We only stay here if there's a double yield,
716 // because 'yield yield foo' is a valid expression.
717 self::EXPRESSION_NO_NL => [
718 self::TYPE_UN_OP => [
719 self::ACTION_GOTO => self::EXPRESSION,
720 ],
721 self::TYPE_INCR_OP => [
722 self::ACTION_GOTO => self::EXPRESSION,
723 ],
724 // BIN_OP seems impossible at the start of an expression, but it can happen in
725 // yield *foo
726 self::TYPE_BIN_OP => [
727 self::ACTION_GOTO => self::EXPRESSION,
728 ],
729 self::TYPE_ADD_OP => [
730 self::ACTION_GOTO => self::EXPRESSION,
731 ],
732 self::TYPE_SEMICOLON => [
733 self::ACTION_GOTO => self::STATEMENT,
734 ],
735 self::TYPE_BRACE_OPEN => [
736 self::ACTION_PUSH => self::EXPRESSION_OP,
737 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
738 ],
739 self::TYPE_BRACE_CLOSE => [
740 self::ACTION_POP => true,
741 ],
742 self::TYPE_PAREN_OPEN => [
743 self::ACTION_PUSH => self::EXPRESSION_OP,
744 self::ACTION_GOTO => self::PAREN_EXPRESSION,
745 ],
746 self::TYPE_FUNC => [
747 self::ACTION_PUSH => self::EXPRESSION_OP,
748 self::ACTION_GOTO => self::FUNC,
749 ],
750 self::TYPE_CLASS => [
751 self::ACTION_PUSH => self::EXPRESSION_OP,
752 self::ACTION_GOTO => self::CLASS_DEF,
753 ],
754 self::TYPE_LITERAL => [
755 self::ACTION_GOTO => self::EXPRESSION_OP,
756 ],
757 self::TYPE_ASYNC => [
758 self::ACTION_GOTO => self::EXPRESSION_OP,
759 ],
760 self::TYPE_AWAIT => [
761 self::ACTION_GOTO => self::EXPRESSION,
762 ],
763 // 'return' can't appear here, because 'return return' isn't allowed
764 // But 'yield' can appear here, because 'yield yield' is allowed
765 self::TYPE_RETURN => [
766 self::ACTION_GOTO => self::EXPRESSION_NO_NL,
767 ],
768 ],
769 // Place in an expression after an operand, where we expect an operator
770 self::EXPRESSION_OP => [
771 self::TYPE_BIN_OP => [
772 self::ACTION_GOTO => self::EXPRESSION,
773 ],
774 self::TYPE_ADD_OP => [
775 self::ACTION_GOTO => self::EXPRESSION,
776 ],
777 self::TYPE_DOT => [
778 self::ACTION_GOTO => self::EXPRESSION_DOT,
779 ],
780 self::TYPE_HOOK => [
781 self::ACTION_PUSH => self::EXPRESSION,
782 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
783 ],
784 self::TYPE_COLON => [
785 self::ACTION_GOTO => self::STATEMENT,
786 ],
787 self::TYPE_COMMA => [
788 self::ACTION_GOTO => self::EXPRESSION,
789 ],
790 self::TYPE_SEMICOLON => [
791 self::ACTION_GOTO => self::STATEMENT,
792 ],
793 self::TYPE_ARROW => [
794 self::ACTION_GOTO => self::EXPRESSION_ARROWFUNC,
795 ],
796 self::TYPE_PAREN_OPEN => [
797 self::ACTION_PUSH => self::EXPRESSION_OP,
798 self::ACTION_GOTO => self::PAREN_EXPRESSION,
799 ],
800 self::TYPE_BRACE_CLOSE => [
801 self::ACTION_POP => true,
802 ],
803 self::TYPE_FUNC => [
804 self::ACTION_PUSH => self::EXPRESSION_OP,
805 self::ACTION_GOTO => self::FUNC,
806 ],
807 ],
808 // State after a dot (.). Like EXPRESSION, except that many keywords behave like literals
809 // (e.g. class, if, else, var, function) because they're not valid as identifiers but are
810 // valid as property names.
811 self::EXPRESSION_DOT => [
812 self::TYPE_LITERAL => [
813 self::ACTION_GOTO => self::EXPRESSION_OP,
814 ],
815 // The following are keywords behaving as literals
816 self::TYPE_RETURN => [
817 self::ACTION_GOTO => self::EXPRESSION_OP,
818 ],
819 self::TYPE_IF => [
820 self::ACTION_GOTO => self::EXPRESSION_OP,
821 ],
822 self::TYPE_DO => [
823 self::ACTION_GOTO => self::EXPRESSION_OP,
824 ],
825 self::TYPE_VAR => [
826 self::ACTION_GOTO => self::EXPRESSION_OP,
827 ],
828 self::TYPE_FUNC => [
829 self::ACTION_GOTO => self::EXPRESSION_OP,
830 ],
831 self::TYPE_CLASS => [
832 self::ACTION_GOTO => self::EXPRESSION_OP,
833 ],
834 // We don't expect real unary/binary operators here, but some keywords
835 // (new, delete, void, typeof, instanceof, in) are classified as such, and they can be
836 // used as property names
837 self::TYPE_UN_OP => [
838 self::ACTION_GOTO => self::EXPRESSION_OP,
839 ],
840 self::TYPE_BIN_OP => [
841 self::ACTION_GOTO => self::EXPRESSION_OP,
842 ],
843 ],
844 // State after the } closing an arrow function body: like STATEMENT except
845 // that it has semicolon insertion, COMMA can continue the expression, and after
846 // a function we go to STATEMENT instead of EXPRESSION_OP
847 self::EXPRESSION_END => [
848 self::TYPE_UN_OP => [
849 self::ACTION_GOTO => self::EXPRESSION,
850 ],
851 self::TYPE_INCR_OP => [
852 self::ACTION_GOTO => self::EXPRESSION,
853 ],
854 self::TYPE_ADD_OP => [
855 self::ACTION_GOTO => self::EXPRESSION,
856 ],
857 self::TYPE_COMMA => [
858 self::ACTION_GOTO => self::EXPRESSION,
859 ],
860 self::TYPE_SEMICOLON => [
861 self::ACTION_GOTO => self::STATEMENT,
862 ],
863 self::TYPE_BRACE_OPEN => [
864 self::ACTION_PUSH => self::STATEMENT,
865 self::ACTION_GOTO => self::STATEMENT,
866 ],
867 self::TYPE_BRACE_CLOSE => [
868 self::ACTION_POP => true,
869 ],
870 self::TYPE_PAREN_OPEN => [
871 self::ACTION_PUSH => self::EXPRESSION_OP,
872 self::ACTION_GOTO => self::PAREN_EXPRESSION,
873 ],
874 self::TYPE_RETURN => [
875 self::ACTION_GOTO => self::EXPRESSION_NO_NL,
876 ],
877 self::TYPE_IF => [
878 self::ACTION_GOTO => self::CONDITION,
879 ],
880 self::TYPE_VAR => [
881 self::ACTION_GOTO => self::EXPRESSION,
882 ],
883 self::TYPE_FUNC => [
884 self::ACTION_PUSH => self::STATEMENT,
885 self::ACTION_GOTO => self::FUNC,
886 ],
887 self::TYPE_CLASS => [
888 self::ACTION_PUSH => self::STATEMENT,
889 self::ACTION_GOTO => self::CLASS_DEF,
890 ],
891 self::TYPE_LITERAL => [
892 self::ACTION_GOTO => self::EXPRESSION_OP,
893 ],
894 self::TYPE_ASYNC => [
895 self::ACTION_GOTO => self::EXPRESSION_OP,
896 ],
897 ],
898 // State after =>. Like EXPRESSION, except that { begins an arrow function body
899 // rather than an object literal.
900 self::EXPRESSION_ARROWFUNC => [
901 self::TYPE_UN_OP => [
902 self::ACTION_GOTO => self::EXPRESSION,
903 ],
904 self::TYPE_INCR_OP => [
905 self::ACTION_GOTO => self::EXPRESSION,
906 ],
907 self::TYPE_ADD_OP => [
908 self::ACTION_GOTO => self::EXPRESSION,
909 ],
910 self::TYPE_BRACE_OPEN => [
911 self::ACTION_PUSH => self::EXPRESSION_END,
912 self::ACTION_GOTO => self::STATEMENT,
913 ],
914 self::TYPE_PAREN_OPEN => [
915 self::ACTION_PUSH => self::EXPRESSION_OP,
916 self::ACTION_GOTO => self::PAREN_EXPRESSION,
917 ],
918 self::TYPE_FUNC => [
919 self::ACTION_PUSH => self::EXPRESSION_OP,
920 self::ACTION_GOTO => self::FUNC,
921 ],
922 self::TYPE_CLASS => [
923 self::ACTION_PUSH => self::EXPRESSION_OP,
924 self::ACTION_GOTO => self::CLASS_DEF,
925 ],
926 self::TYPE_LITERAL => [
927 self::ACTION_GOTO => self::EXPRESSION_OP,
928 ],
929 ],
930 // Expression after a ? . This differs from EXPRESSION because a : ends the ternary
931 // rather than starting STATEMENT (outside a ternary, : comes after a goto label)
932 // The actual rule for : ending the ternary is in EXPRESSION_TERNARY_OP.
933 self::EXPRESSION_TERNARY => [
934 self::TYPE_BRACE_OPEN => [
935 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
936 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
937 ],
938 self::TYPE_PAREN_OPEN => [
939 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
940 self::ACTION_GOTO => self::PAREN_EXPRESSION,
941 ],
942 self::TYPE_FUNC => [
943 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
944 self::ACTION_GOTO => self::FUNC,
945 ],
946 self::TYPE_CLASS => [
947 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
948 self::ACTION_GOTO => self::CLASS_DEF,
949 ],
950 self::TYPE_LITERAL => [
951 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
952 ],
953 // 'return' can't appear here, but 'yield' can
954 self::TYPE_RETURN => [
955 self::ACTION_GOTO => self::EXPRESSION_TERNARY_NO_NL,
956 ],
957 ],
958 // Like EXPRESSION_TERNARY, except that semicolon insertion can happen
959 // See also EXPRESSION_NO_NL
960 self::EXPRESSION_TERNARY_NO_NL => [
961 self::TYPE_BRACE_OPEN => [
962 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
963 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
964 ],
965 self::TYPE_PAREN_OPEN => [
966 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
967 self::ACTION_GOTO => self::PAREN_EXPRESSION,
968 ],
969 self::TYPE_FUNC => [
970 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
971 self::ACTION_GOTO => self::FUNC,
972 ],
973 self::TYPE_CLASS => [
974 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
975 self::ACTION_GOTO => self::CLASS_DEF,
976 ],
977 self::TYPE_LITERAL => [
978 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
979 ],
980 // 'yield' can appear here, because 'yield yield' is allowed
981 self::TYPE_RETURN => [
982 self::ACTION_GOTO => self::EXPRESSION_TERNARY_NO_NL,
983 ],
984 self::TYPE_UN_OP => [
985 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
986 ],
987 self::TYPE_INCR_OP => [
988 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
989 ],
990 // BIN_OP seems impossible at the start of an expression, but it can happen in
991 // yield *foo
992 self::TYPE_BIN_OP => [
993 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
994 ],
995 self::TYPE_ADD_OP => [
996 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
997 ],
998 ],
999 // Like EXPRESSION_OP, but for ternaries, see EXPRESSION_TERNARY
1000 self::EXPRESSION_TERNARY_OP => [
1001 self::TYPE_BIN_OP => [
1002 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
1003 ],
1004 self::TYPE_ADD_OP => [
1005 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
1006 ],
1007 self::TYPE_DOT => [
1008 self::ACTION_GOTO => self::EXPRESSION_TERNARY_DOT,
1009 ],
1010 self::TYPE_HOOK => [
1011 self::ACTION_PUSH => self::EXPRESSION_TERNARY,
1012 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
1013 ],
1014 self::TYPE_COMMA => [
1015 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
1016 ],
1017 self::TYPE_ARROW => [
1018 self::ACTION_GOTO => self::EXPRESSION_TERNARY_ARROWFUNC,
1019 ],
1020 self::TYPE_PAREN_OPEN => [
1021 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
1022 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1023 ],
1024 self::TYPE_COLON => [
1025 self::ACTION_POP => true,
1026 ],
1027 ],
1028 // Like EXPRESSION_DOT, but for ternaries, see EXPRESSION_TERNARY
1029 self::EXPRESSION_TERNARY_DOT => [
1030 self::TYPE_LITERAL => [
1031 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1032 ],
1033 // The following are keywords behaving as literals
1034 self::TYPE_RETURN => [
1035 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1036 ],
1037 self::TYPE_IF => [
1038 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1039 ],
1040 self::TYPE_DO => [
1041 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1042 ],
1043 self::TYPE_VAR => [
1044 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1045 ],
1046 self::TYPE_FUNC => [
1047 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1048 ],
1049 self::TYPE_CLASS => [
1050 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1051 ],
1052 // We don't expect real unary/binary operators here, but some keywords
1053 // (new, delete, void, typeof, instanceof, in) are classified as such, and they can be
1054 // used as property names
1055 self::TYPE_UN_OP => [
1056 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1057 ],
1058 self::TYPE_BIN_OP => [
1059 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1060 ],
1061 ],
1062 // Like EXPRESSION_ARROWFUNC, but for ternaries, see EXPRESSION_TERNARY
1063 self::EXPRESSION_TERNARY_ARROWFUNC => [
1064 self::TYPE_UN_OP => [
1065 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
1066 ],
1067 self::TYPE_INCR_OP => [
1068 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
1069 ],
1070 self::TYPE_ADD_OP => [
1071 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
1072 ],
1073 self::TYPE_BRACE_OPEN => [
1074 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
1075 self::ACTION_GOTO => self::STATEMENT,
1076 ],
1077 self::TYPE_PAREN_OPEN => [
1078 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
1079 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1080 ],
1081 self::TYPE_FUNC => [
1082 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
1083 self::ACTION_GOTO => self::FUNC,
1084 ],
1085 self::TYPE_CLASS => [
1086 self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
1087 self::ACTION_GOTO => self::CLASS_DEF,
1088 ],
1089 self::TYPE_LITERAL => [
1090 self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
1091 ],
1092 ],
1093 // Expression inside parentheses. Like EXPRESSION, except that ) ends this state
1094 // This differs from EXPRESSION because semicolon insertion can't happen here
1095 self::PAREN_EXPRESSION => [
1096 self::TYPE_BRACE_OPEN => [
1097 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1098 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
1099 ],
1100 self::TYPE_PAREN_OPEN => [
1101 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1102 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1103 ],
1104 self::TYPE_PAREN_CLOSE => [
1105 self::ACTION_POP => true,
1106 ],
1107 self::TYPE_FUNC => [
1108 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1109 self::ACTION_GOTO => self::FUNC,
1110 ],
1111 self::TYPE_CLASS => [
1112 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1113 self::ACTION_GOTO => self::CLASS_DEF,
1114 ],
1115 self::TYPE_LITERAL => [
1116 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1117 ],
1118 self::TYPE_ASYNC => [
1119 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP_NO_NL,
1120 ],
1121 // 'return' can't appear here, but 'yield' can
1122 self::TYPE_RETURN => [
1123 self::ACTION_GOTO => self::PAREN_EXPRESSION_NO_NL,
1124 ],
1125 ],
1126 // Like PAREN_EXPRESSION, except that semicolon insertion can happen
1127 // See also EXPRESSION_NO_NL
1128 self::PAREN_EXPRESSION_NO_NL => [
1129 self::TYPE_BRACE_OPEN => [
1130 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1131 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
1132 ],
1133 self::TYPE_PAREN_OPEN => [
1134 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1135 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1136 ],
1137 self::TYPE_PAREN_CLOSE => [
1138 self::ACTION_POP => true,
1139 ],
1140 self::TYPE_FUNC => [
1141 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1142 self::ACTION_GOTO => self::FUNC,
1143 ],
1144 self::TYPE_CLASS => [
1145 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1146 self::ACTION_GOTO => self::CLASS_DEF,
1147 ],
1148 self::TYPE_LITERAL => [
1149 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1150 ],
1151 self::TYPE_ASYNC => [
1152 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP_NO_NL,
1153 ],
1154 // 'yield' can appear here, because 'yield yield' is allowed
1155 self::TYPE_RETURN => [
1156 self::ACTION_GOTO => self::PAREN_EXPRESSION_NO_NL,
1157 ],
1158 self::TYPE_UN_OP => [
1159 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1160 ],
1161 self::TYPE_INCR_OP => [
1162 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1163 ],
1164 // BIN_OP seems impossible at the start of an expression, but it can happen in
1165 // yield *foo
1166 self::TYPE_BIN_OP => [
1167 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1168 ],
1169 self::TYPE_ADD_OP => [
1170 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1171 ],
1172 self::TYPE_AWAIT => [
1173 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1174 ],
1175 ],
1176 // Like EXPRESSION_OP, but in parentheses, see PAREN_EXPRESSION
1177 self::PAREN_EXPRESSION_OP => [
1178 self::TYPE_BIN_OP => [
1179 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1180 ],
1181 self::TYPE_ADD_OP => [
1182 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1183 ],
1184 self::TYPE_DOT => [
1185 self::ACTION_GOTO => self::PAREN_EXPRESSION_DOT,
1186 ],
1187 self::TYPE_HOOK => [
1188 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1189 ],
1190 self::TYPE_COLON => [
1191 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1192 ],
1193 self::TYPE_COMMA => [
1194 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1195 ],
1196 self::TYPE_SEMICOLON => [
1197 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1198 ],
1199 self::TYPE_ARROW => [
1200 self::ACTION_GOTO => self::PAREN_EXPRESSION_ARROWFUNC,
1201 ],
1202 self::TYPE_PAREN_OPEN => [
1203 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1204 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1205 ],
1206 self::TYPE_PAREN_CLOSE => [
1207 self::ACTION_POP => true,
1208 ],
1209 ],
1210 // Like EXPRESSION_DOT, but in parentheses, see PAREN_EXPRESSION
1211 self::PAREN_EXPRESSION_DOT => [
1212 self::TYPE_LITERAL => [
1213 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1214 ],
1215 // The following are keywords behaving as literals
1216 self::TYPE_RETURN => [
1217 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1218 ],
1219 self::TYPE_IF => [
1220 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1221 ],
1222 self::TYPE_DO => [
1223 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1224 ],
1225 self::TYPE_VAR => [
1226 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1227 ],
1228 self::TYPE_FUNC => [
1229 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1230 ],
1231 self::TYPE_CLASS => [
1232 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1233 ],
1234 // We don't expect real unary/binary operators here, but some keywords
1235 // (new, delete, void, typeof, instanceof, in) are classified as such, and they can be
1236 // used as property names
1237 self::TYPE_UN_OP => [
1238 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1239 ],
1240 self::TYPE_BIN_OP => [
1241 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1242 ],
1243 ],
1244 // Like EXPRESSION_ARROWFUNC, but in parentheses, see PAREN_EXPRESSION
1245 self::PAREN_EXPRESSION_ARROWFUNC => [
1246 self::TYPE_UN_OP => [
1247 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1248 ],
1249 self::TYPE_INCR_OP => [
1250 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1251 ],
1252 self::TYPE_ADD_OP => [
1253 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1254 ],
1255 self::TYPE_BRACE_OPEN => [
1256 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1257 self::ACTION_GOTO => self::STATEMENT,
1258 ],
1259 self::TYPE_PAREN_OPEN => [
1260 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1261 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1262 ],
1263 self::TYPE_FUNC => [
1264 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1265 self::ACTION_GOTO => self::FUNC,
1266 ],
1267 self::TYPE_CLASS => [
1268 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1269 self::ACTION_GOTO => self::CLASS_DEF,
1270 ],
1271 self::TYPE_LITERAL => [
1272 self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
1273 ],
1274 ],
1275
1276 // Like PAREN_EXPRESSION_OP, for the state after "async" in a PAREN_EXPRESSION,
1277 // for use by the $semicolon model.
1278 self::PAREN_EXPRESSION_OP_NO_NL => [
1279 self::TYPE_BIN_OP => [
1280 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1281 ],
1282 self::TYPE_ADD_OP => [
1283 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1284 ],
1285 self::TYPE_DOT => [
1286 self::ACTION_GOTO => self::PAREN_EXPRESSION_DOT,
1287 ],
1288 self::TYPE_HOOK => [
1289 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1290 ],
1291 self::TYPE_COLON => [
1292 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1293 ],
1294 self::TYPE_COMMA => [
1295 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1296 ],
1297 self::TYPE_SEMICOLON => [
1298 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1299 ],
1300 self::TYPE_ARROW => [
1301 self::ACTION_GOTO => self::PAREN_EXPRESSION_ARROWFUNC,
1302 ],
1303 self::TYPE_PAREN_OPEN => [
1304 self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
1305 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1306 ],
1307 self::TYPE_PAREN_CLOSE => [
1308 self::ACTION_POP => true,
1309 ],
1310 ],
1311 // Expression as the value of a key in an object literal.
1312 // This means we're at "{ foo:".
1313 // Like EXPRESSION, except that a comma (in PROPERTY_EXPRESSION_OP) goes to PROPERTY_ASSIGNMENT instead
1314 self::PROPERTY_EXPRESSION => [
1315 self::TYPE_BRACE_OPEN => [
1316 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1317 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
1318 ],
1319 self::TYPE_BRACE_CLOSE => [
1320 self::ACTION_POP => true,
1321 ],
1322 self::TYPE_PAREN_OPEN => [
1323 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1324 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1325 ],
1326 self::TYPE_FUNC => [
1327 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1328 self::ACTION_GOTO => self::FUNC,
1329 ],
1330 self::TYPE_CLASS => [
1331 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1332 self::ACTION_GOTO => self::CLASS_DEF,
1333 ],
1334 self::TYPE_LITERAL => [
1335 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1336 ],
1337 self::TYPE_ASYNC => [
1338 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_ASYNC,
1339 ],
1340 // 'return' can't appear here, but 'yield' can
1341 self::TYPE_RETURN => [
1342 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_NO_NL,
1343 ],
1344 ],
1345 // Like PROPERTY_EXPRESSION, except that semicolon insertion can happen
1346 // See also EXPRESSION_NO_NL
1347 self::PROPERTY_EXPRESSION_NO_NL => [
1348 self::TYPE_BRACE_OPEN => [
1349 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1350 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
1351 ],
1352 self::TYPE_BRACE_CLOSE => [
1353 self::ACTION_POP => true,
1354 ],
1355 self::TYPE_PAREN_OPEN => [
1356 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1357 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1358 ],
1359 self::TYPE_FUNC => [
1360 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1361 self::ACTION_GOTO => self::FUNC,
1362 ],
1363 self::TYPE_CLASS => [
1364 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1365 self::ACTION_GOTO => self::CLASS_DEF,
1366 ],
1367 self::TYPE_LITERAL => [
1368 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1369 ],
1370 // 'yield' can appear here, because 'yield yield' is allowed
1371 self::TYPE_RETURN => [
1372 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_NO_NL,
1373 ],
1374 self::TYPE_UN_OP => [
1375 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1376 ],
1377 self::TYPE_INCR_OP => [
1378 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1379 ],
1380 // BIN_OP seems impossible at the start of an expression, but it can happen in
1381 // yield *foo
1382 self::TYPE_BIN_OP => [
1383 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1384 ],
1385 self::TYPE_ADD_OP => [
1386 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1387 ],
1388 ],
1389 // Like EXPRESSION_OP, but in a property expression, see PROPERTY_EXPRESSION
1390 // This means we're at "{ foo: bar".
1391 self::PROPERTY_EXPRESSION_OP => [
1392 self::TYPE_BIN_OP => [
1393 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1394 ],
1395 self::TYPE_ADD_OP => [
1396 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1397 ],
1398 self::TYPE_DOT => [
1399 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_DOT,
1400 ],
1401 self::TYPE_HOOK => [
1402 self::ACTION_PUSH => self::PROPERTY_EXPRESSION,
1403 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
1404 ],
1405 self::TYPE_COMMA => [
1406 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
1407 ],
1408 self::TYPE_ARROW => [
1409 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_ARROWFUNC,
1410 ],
1411 self::TYPE_BRACE_OPEN => [
1412 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1413 ],
1414 self::TYPE_BRACE_CLOSE => [
1415 self::ACTION_POP => true,
1416 ],
1417 self::TYPE_PAREN_OPEN => [
1418 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1419 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1420 ],
1421 ],
1422 // Like PROPERTY_EXPRESSION_OP, but with an added TYPE_FUNC handler.
1423 // This means we're at "{ foo: async".
1424 //
1425 // This state exists to support "{ foo: async function() {",
1426 // which can't re-use PROPERTY_EXPRESSION_OP, because handling TYPE_FUNC there
1427 // would treat invalid "{ foo: bar function () {" as valid.
1428 //
1429 // For other cases we treat "async" like a literal key or key-less value.
1430 //
1431 // ```
1432 // var noAsyncHere = {
1433 // async,
1434 // async: 1,
1435 // async() { return 2; },
1436 // foo: async
1437 // foo: async + 2,
1438 // foo: async(),
1439 // }
1440 // ```
1441 self::PROPERTY_EXPRESSION_ASYNC => [
1442 self::TYPE_BIN_OP => [
1443 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1444 ],
1445 self::TYPE_ADD_OP => [
1446 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1447 ],
1448 self::TYPE_DOT => [
1449 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_DOT,
1450 ],
1451 self::TYPE_HOOK => [
1452 self::ACTION_PUSH => self::PROPERTY_EXPRESSION,
1453 self::ACTION_GOTO => self::EXPRESSION_TERNARY,
1454 ],
1455 self::TYPE_COMMA => [
1456 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
1457 ],
1458 self::TYPE_ARROW => [
1459 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_ARROWFUNC,
1460 ],
1461 self::TYPE_BRACE_OPEN => [
1462 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1463 ],
1464 self::TYPE_BRACE_CLOSE => [
1465 self::ACTION_POP => true,
1466 ],
1467 self::TYPE_PAREN_OPEN => [
1468 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1469 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1470 ],
1471 self::TYPE_FUNC => [
1472 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1473 self::ACTION_GOTO => self::FUNC,
1474 ],
1475 ],
1476 // Like EXPRESSION_DOT, but in a property expression, see PROPERTY_EXPRESSION
1477 self::PROPERTY_EXPRESSION_DOT => [
1478 self::TYPE_LITERAL => [
1479 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1480 ],
1481 // The following are keywords behaving as literals
1482 self::TYPE_RETURN => [
1483 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1484 ],
1485 self::TYPE_IF => [
1486 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1487 ],
1488 self::TYPE_DO => [
1489 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1490 ],
1491 self::TYPE_VAR => [
1492 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1493 ],
1494 self::TYPE_FUNC => [
1495 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1496 ],
1497 self::TYPE_CLASS => [
1498 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1499 ],
1500 // We don't expect real unary/binary operators here, but some keywords
1501 // (new, delete, void, typeof, instanceof, in) are classified as such, and they can be
1502 // used as property names
1503 self::TYPE_UN_OP => [
1504 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1505 ],
1506 self::TYPE_BIN_OP => [
1507 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1508 ],
1509 ],
1510 // Like EXPRESSION_ARROWFUNC, but in a property expression, see PROPERTY_EXPRESSION
1511 self::PROPERTY_EXPRESSION_ARROWFUNC => [
1512 self::TYPE_UN_OP => [
1513 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1514 ],
1515 self::TYPE_INCR_OP => [
1516 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1517 ],
1518 self::TYPE_ADD_OP => [
1519 self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
1520 ],
1521 self::TYPE_BRACE_OPEN => [
1522 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1523 self::ACTION_GOTO => self::STATEMENT,
1524 ],
1525 self::TYPE_PAREN_OPEN => [
1526 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1527 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1528 ],
1529 self::TYPE_FUNC => [
1530 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1531 self::ACTION_GOTO => self::FUNC,
1532 ],
1533 self::TYPE_CLASS => [
1534 self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
1535 self::ACTION_GOTO => self::CLASS_DEF,
1536 ],
1537 self::TYPE_LITERAL => [
1538 self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
1539 ],
1540 ],
1541 // Class definition (after the class keyword). Expects an identifier, or the extends
1542 // keyword followed by an expression (or both), followed by {, which starts an object
1543 // literal. The object literal's closing } will pop the stack, so the state to return
1544 // to after the class definition should be pushed to the stack first.
1545 self::CLASS_DEF => [
1546 self::TYPE_BRACE_OPEN => [
1547 self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
1548 ],
1549 self::TYPE_PAREN_OPEN => [
1550 self::ACTION_PUSH => self::CLASS_DEF,
1551 self::ACTION_GOTO => self::PAREN_EXPRESSION,
1552 ],
1553 self::TYPE_FUNC => [
1554 self::ACTION_PUSH => self::CLASS_DEF,
1555 self::ACTION_GOTO => self::FUNC,
1556 ],
1557 ],
1558 // Import or export declaration
1559 self::IMPORT_EXPORT => [
1560 self::TYPE_SEMICOLON => [
1561 self::ACTION_GOTO => self::STATEMENT,
1562 ],
1563 self::TYPE_VAR => [
1564 self::ACTION_GOTO => self::EXPRESSION,
1565 ],
1566 self::TYPE_FUNC => [
1567 self::ACTION_PUSH => self::EXPRESSION_OP,
1568 self::ACTION_GOTO => self::FUNC,
1569 ],
1570 self::TYPE_CLASS => [
1571 self::ACTION_PUSH => self::EXPRESSION_OP,
1572 self::ACTION_GOTO => self::CLASS_DEF,
1573 ],
1574 self::TYPE_SPECIAL => [
1575 'default' => [
1576 self::ACTION_GOTO => self::EXPRESSION,
1577 ],
1578 // Stay in this state for *, as, from
1579 '*' => [],
1580 'as' => [],
1581 'from' => [],
1582 ],
1583 ],
1584 // Used in template string-specific code below
1585 self::TEMPLATE_STRING_HEAD => [
1586 self::TYPE_LITERAL => [
1587 self::ACTION_PUSH => self::TEMPLATE_STRING_TAIL,
1588 self::ACTION_GOTO => self::EXPRESSION,
1589 ],
1590 ],
1591 ];
1592
1593 /**
1594 * @var array $semicolon
1595 *
1596 * Rules for when semicolon insertion is appropriate. Semicolon insertion happens if we are
1597 * in one of these states, and encounter one of these tokens preceded by a newline.
1598 *
1599 * This array is augmented by ensureExpandedStates().
1600 */
1601 private static $semicolon = [
1602 self::EXPRESSION_NO_NL => [
1603 self::TYPE_UN_OP => true,
1604 // BIN_OP seems impossible at the start of an expression, but it can happen in
1605 // yield *foo
1606 self::TYPE_BIN_OP => true,
1607 self::TYPE_INCR_OP => true,
1608 self::TYPE_ADD_OP => true,
1609 self::TYPE_BRACE_OPEN => true,
1610 self::TYPE_PAREN_OPEN => true,
1611 self::TYPE_RETURN => true,
1612 self::TYPE_IF => true,
1613 self::TYPE_DO => true,
1614 self::TYPE_VAR => true,
1615 self::TYPE_FUNC => true,
1616 self::TYPE_CLASS => true,
1617 self::TYPE_LITERAL => true,
1618 self::TYPE_ASYNC => true,
1619 ],
1620 self::EXPRESSION_TERNARY_NO_NL => [
1621 self::TYPE_UN_OP => true,
1622 // BIN_OP seems impossible at the start of an expression, but it can happen in
1623 // yield *foo
1624 self::TYPE_BIN_OP => true,
1625 self::TYPE_INCR_OP => true,
1626 self::TYPE_ADD_OP => true,
1627 self::TYPE_BRACE_OPEN => true,
1628 self::TYPE_PAREN_OPEN => true,
1629 self::TYPE_RETURN => true,
1630 self::TYPE_IF => true,
1631 self::TYPE_DO => true,
1632 self::TYPE_VAR => true,
1633 self::TYPE_FUNC => true,
1634 self::TYPE_CLASS => true,
1635 self::TYPE_LITERAL => true,
1636 self::TYPE_ASYNC => true,
1637 ],
1638 self::PAREN_EXPRESSION_NO_NL => [
1639 self::TYPE_UN_OP => true,
1640 // BIN_OP seems impossible at the start of an expression, but it can happen in
1641 // yield *foo
1642 self::TYPE_BIN_OP => true,
1643 self::TYPE_INCR_OP => true,
1644 self::TYPE_ADD_OP => true,
1645 self::TYPE_BRACE_OPEN => true,
1646 self::TYPE_PAREN_OPEN => true,
1647 self::TYPE_RETURN => true,
1648 self::TYPE_IF => true,
1649 self::TYPE_DO => true,
1650 self::TYPE_VAR => true,
1651 self::TYPE_FUNC => true,
1652 self::TYPE_CLASS => true,
1653 self::TYPE_LITERAL => true,
1654 self::TYPE_ASYNC => true,
1655 ],
1656 self::PROPERTY_EXPRESSION_NO_NL => [
1657 self::TYPE_UN_OP => true,
1658 // BIN_OP seems impossible at the start of an expression, but it can happen in
1659 // yield *foo
1660 self::TYPE_BIN_OP => true,
1661 self::TYPE_INCR_OP => true,
1662 self::TYPE_ADD_OP => true,
1663 self::TYPE_BRACE_OPEN => true,
1664 self::TYPE_PAREN_OPEN => true,
1665 self::TYPE_RETURN => true,
1666 self::TYPE_IF => true,
1667 self::TYPE_DO => true,
1668 self::TYPE_VAR => true,
1669 self::TYPE_FUNC => true,
1670 self::TYPE_CLASS => true,
1671 self::TYPE_LITERAL => true,
1672 self::TYPE_ASYNC => true,
1673 ],
1674 self::EXPRESSION_OP => [
1675 self::TYPE_UN_OP => true,
1676 self::TYPE_INCR_OP => true,
1677 self::TYPE_BRACE_OPEN => true,
1678 self::TYPE_RETURN => true,
1679 self::TYPE_IF => true,
1680 self::TYPE_DO => true,
1681 self::TYPE_VAR => true,
1682 self::TYPE_FUNC => true,
1683 self::TYPE_CLASS => true,
1684 self::TYPE_LITERAL => true,
1685 self::TYPE_ASYNC => true,
1686 ],
1687 self::EXPRESSION_END => [
1688 self::TYPE_UN_OP => true,
1689 self::TYPE_INCR_OP => true,
1690 self::TYPE_ADD_OP => true,
1691 self::TYPE_BRACE_OPEN => true,
1692 self::TYPE_PAREN_OPEN => true,
1693 self::TYPE_RETURN => true,
1694 self::TYPE_IF => true,
1695 self::TYPE_DO => true,
1696 self::TYPE_VAR => true,
1697 self::TYPE_FUNC => true,
1698 self::TYPE_CLASS => true,
1699 self::TYPE_LITERAL => true,
1700 self::TYPE_ASYNC => true,
1701 ],
1702 self::PAREN_EXPRESSION_OP_NO_NL => [
1703 self::TYPE_FUNC => true,
1704 ]
1705 ];
1706
1707 /**
1708 * @var array $divStates
1709 *
1710 * States in which a / is a division operator. In all other states, it's the start of a regex.
1711 *
1712 * This array is augmented by self::ensureExpandedStates().
1713 */
1714 private static $divStates = [
1715 self::EXPRESSION_OP => true,
1716 self::EXPRESSION_TERNARY_OP => true,
1717 self::PAREN_EXPRESSION_OP => true,
1718 self::PROPERTY_EXPRESSION_OP => true,
1719 self::PROPERTY_EXPRESSION_ASYNC => true
1720 ];
1721
1722 /**
1723 * Add copies of all states but with negative numbers to self::$model (if not already present),
1724 * to represent generator function states.
1725 */
1726 private static function ensureExpandedStates() {
1727 // Already done?
1728 if ( self::$expandedStates ) {
1729 return;
1730 }
1731 self::$expandedStates = true;
1732
1733 // Add copies of all states (except FUNC and GENFUNC) with negative numbers.
1734 // These negative states represent states inside generator functions. When in these states,
1735 // TYPE_YIELD is treated as TYPE_RETURN, otherwise as TYPE_LITERAL
1736 foreach ( self::$model as $state => $transitions ) {
1737 if ( $state === self::FUNC || $state === self::GENFUNC ) {
1738 continue;
1739 }
1740 foreach ( $transitions as $tokenType => $actions ) {
1741 foreach ( $actions as $action => $target ) {
1742 if ( !is_array( $target ) ) {
1743 self::$model[-$state][$tokenType][$action] = (
1744 $target === self::FUNC ||
1745 $target === true ||
1746 $target === self::GENFUNC
1747 ) ? $target : -$target;
1748 continue;
1749 }
1750
1751 foreach ( $target as $subaction => $subtarget ) {
1752 self::$model[-$state][$tokenType][$action][$subaction] = (
1753 $subtarget === self::FUNC ||
1754 $subtarget === true ||
1755 $subtarget === self::GENFUNC
1756 ) ? $subtarget : -$subtarget;
1757 }
1758 }
1759 }
1760 }
1761 // Special cases:
1762 // '{' in a property assignment starts a method, so it shouldn't be flipped
1763 self::$model[-self::PROPERTY_ASSIGNMENT][self::TYPE_BRACE_OPEN][self::ACTION_GOTO] = self::STATEMENT;
1764
1765 // Also add negative versions of states to the other arrays
1766 foreach ( self::$semicolon as $state => $value ) {
1767 self::$semicolon[-$state] = $value;
1768 }
1769 foreach ( self::$divStates as $state => $value ) {
1770 self::$divStates[-$state] = $value;
1771 }
1772 }
1773
1774 /**
1775 * Returns minified JavaScript code.
1776 *
1777 * @see MinifierState::setErrorHandler
1778 * @param string $s JavaScript code to minify
1779 * @param callable|null $onError Called with a ParseError object
1780 * @return string Minified code
1781 */
1782 public static function minify( $s, $onError = null ) {
1783 return self::minifyInternal( $s, null, $onError );
1784 }
1785
1786 /**
1787 * Create a minifier state object without source map capabilities
1788 *
1789 * Example:
1790 *
1791 * JavaScriptMinifier::createMinifier()
1792 * ->addSourceFile( 'file.js', $source )
1793 * ->getMinifiedOutput();
1794 *
1795 * @return JavaScriptMinifierState
1796 */
1797 public static function createMinifier() {
1798 return new JavaScriptMinifierState;
1799 }
1800
1801 /**
1802 * Create a minifier state object with source map capabilities
1803 *
1804 * Example:
1805 *
1806 * $mapper = JavaScriptMinifier::createSourceMapState()
1807 * ->addSourceFile( 'file1.js', $source1 )
1808 * ->addOutput( "\n\n" )
1809 * ->addSourceFile( 'file2.js', $source2 );
1810 * $out = $mapper->getMinifiedOutput();
1811 * $map = $mapper->getSourceMap()
1812 *
1813 * @return JavaScriptMapperState
1814 */
1815 public static function createSourceMapState() {
1816 return new JavaScriptMapperState;
1817 }
1818
1819 /**
1820 * Create a MinifierState that doesn't actually minify
1821 *
1822 * @return IdentityMinifierState
1823 */
1824 public static function createIdentityMinifier() {
1825 return new IdentityMinifierState;
1826 }
1827
1828 /**
1829 * Minify with optional source map.
1830 *
1831 * @internal
1832 *
1833 * @param string $s
1834 * @param MappingsGenerator|null $mapGenerator
1835 * @param callable|null $onError
1836 * @param callable|null $onDebug See augmentDebugContext() for callback parameter
1837 * @return string
1838 */
1839 public static function minifyInternal( $s, $mapGenerator = null, $onError = null, $onDebug = null ) {
1840 self::ensureExpandedStates();
1841
1842 // Here's where the minifying takes place: Loop through the input, looking for tokens
1843 // and output them to $out, taking actions to the above defined rules when appropriate.
1844 $error = null;
1845 $out = '';
1846 $pos = 0;
1847 $length = strlen( $s );
1848 $lineLength = 0;
1849 $dotlessNum = false;
1850 $lastDotlessNum = false;
1851 $newlineFound = true;
1852 $state = self::STATEMENT;
1853 $stack = [];
1854 // Optimization: calling end( $stack ) repeatedly is expensive
1855 $topOfStack = null;
1856 // Pretend that we have seen a semicolon yet
1857 $last = ';';
1858 while ( $pos < $length ) {
1859 // First, skip over any whitespace and multiline comments, recording whether we
1860 // found any newline character
1861 $skip = strspn( $s, " \t\n\r\xb\xc", $pos );
1862 if ( !$skip ) {
1863 $ch = $s[$pos];
1864 if ( $ch === '/' && substr( $s, $pos, 2 ) === '/*' ) {
1865 // Multiline comment. Search for the end token or EOT.
1866 $end = strpos( $s, '*/', $pos + 2 );
1867 $skip = $end === false ? $length - $pos : $end - $pos + 2;
1868 }
1869 }
1870 if ( $skip ) {
1871 // The semicolon insertion mechanism needs to know whether there was a newline
1872 // between two tokens, so record it now.
1873 if ( !$newlineFound && strcspn( $s, "\r\n", $pos, $skip ) !== $skip ) {
1874 $newlineFound = true;
1875 }
1876 if ( $mapGenerator ) {
1877 $mapGenerator->consumeSource( $skip );
1878 }
1879 $pos += $skip;
1880 continue;
1881 }
1882 // Handle C++-style comments and html comments, which are treated as single line
1883 // comments by the browser, regardless of whether the end tag is on the same line.
1884 // Handle --> the same way, but only if it's at the beginning of the line
1885 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable
1886 if ( ( $ch === '/' && substr( $s, $pos, 2 ) === '//' )
1887 || ( $ch === '<' && substr( $s, $pos, 4 ) === '<!--' )
1888 || ( $ch === '-' && $newlineFound && substr( $s, $pos, 3 ) === '-->' )
1889 ) {
1890 $skip = strcspn( $s, "\r\n", $pos );
1891 if ( $mapGenerator ) {
1892 $mapGenerator->consumeSource( $skip );
1893 }
1894 $pos += $skip;
1895 continue;
1896 }
1897
1898 // Find out which kind of token we're handling.
1899 // Note: $end must point past the end of the current token
1900 // so that `substr($s, $pos, $end - $pos)` would be the entire token.
1901 // In order words, $end will be the offset of the last relevant character
1902 // in the stream + 1, or simply put: The offset of the first character
1903 // of any next token in the stream.
1904 $end = $pos + 1;
1905 // Handle string literals
1906 if ( $ch === "'" || $ch === '"' ) {
1907 // Search to the end of the string literal, skipping over backslash escapes
1908 $search = $ch . '\\';
1909 do {
1910 // Speculatively add 2 to the end so that if we see a backslash,
1911 // the next iteration will start 2 characters further (one for the
1912 // backslash, one for the escaped character).
1913 // We'll correct this outside the loop.
1914 $end += strcspn( $s, $search, $end ) + 2;
1915 // If the last character in our search for a quote or a backlash
1916 // matched a backslash and we haven't reached the end, keep searching..
1917 } while ( $end - 2 < $length && $s[$end - 2] === '\\' );
1918 // Correction (1): Undo speculative add, keep only one (end of string literal)
1919 $end--;
1920 if ( $end > $length ) {
1921 // Correction (2): Loop wrongly assumed an end quote ended the search,
1922 // but search ended because we've reached the end. Correct $end.
1923 // TODO: This is invalid and should throw.
1924 $end--;
1925 }
1926
1927 // Handle template strings, either from "`" to begin a new string,
1928 // or continuation after the "}" that ends a "${"-expression.
1929 } elseif ( $ch === '`' || ( $ch === '}' && $topOfStack === self::TEMPLATE_STRING_TAIL ) ) {
1930 if ( $ch === '}' ) {
1931 // Pop the TEMPLATE_STRING_TAIL state off the stack
1932 // We don't let it get popped off the stack the normal way, to avoid the newline
1933 // and comment stripping code above running on the continuation of the literal
1934 array_pop( $stack );
1935 // Also pop the previous state off the stack
1936 $state = array_pop( $stack );
1937 $topOfStack = end( $stack );
1938 }
1939 // Search until we reach either a closing ` or a ${, skipping over backslash escapes
1940 // and $ characters followed by something other than { or `
1941 do {
1942 $end += strcspn( $s, '`$\\', $end ) + 1;
1943 if ( $end - 1 < $length && $s[$end - 1] === '`' ) {
1944 // End of the string, stop
1945 // We don't do this in the while() condition because the $end++ in the
1946 // backslash escape branch makes it difficult to do so without incorrectly
1947 // considering an escaped backtick (\`) the end of the string
1948 break;
1949 }
1950 if ( $end - 1 < $length && $s[$end - 1] === '\\' ) {
1951 // Backslash escape. Skip the next character, and keep going
1952 $end++;
1953 continue;
1954 }
1955 if ( $end < $length && $s[$end - 1] === '$' && $s[$end] === '{' ) {
1956 // Beginning of an expression in ${ ... }. Skip the {, and stop
1957 $end++;
1958 // Push the current state to the stack. We'll pop this off later when hitting
1959 // the end of this template string
1960 $stack[] = $state;
1961 $topOfStack = $state;
1962 // Change the state to TEMPLATE_STRING_HEAD. The token type will be detected
1963 // as TYPE_LITERAL, and this will cause the state machine to expect an
1964 // expression, then go to the TEMPLATE_STRING_TAIL state when it hits the }
1965 $state = self::TEMPLATE_STRING_HEAD;
1966 break;
1967 }
1968 } while ( $end - 1 < $length );
1969 if ( $end > $length ) {
1970 // Loop wrongly assumed an end quote or ${ ended the search,
1971 // but search ended because we've reached the end. Correct $end.
1972 // TODO: This is invalid and should throw.
1973 $end--;
1974 }
1975
1976 // We have to distinguish between regexp literals and division operators
1977 // A division operator is only possible in certain states
1978 } elseif ( $ch === '/' && !isset( self::$divStates[$state] ) ) {
1979 // Regexp literal
1980 for ( ; ; ) {
1981 // Search until we find "/" (end of regexp), "\" (backslash escapes),
1982 // or "[" (start of character classes).
1983 do {
1984 // Speculatively add 2 to ensure next iteration skips
1985 // over backslash and escaped character.
1986 // We'll correct this outside the loop.
1987 $end += strcspn( $s, '/[\\', $end ) + 2;
1988 // If backslash escape, keep searching...
1989 } while ( $end - 2 < $length && $s[$end - 2] === '\\' );
1990 // Correction (1): Undo speculative add, keep only one (end of regexp)
1991 $end--;
1992 if ( $end > $length ) {
1993 // Correction (2): Loop wrongly assumed end slash was seen
1994 // String ended without end of regexp. Correct $end.
1995 // TODO: This is invalid and should throw.
1996 $end--;
1997 break;
1998 }
1999 if ( $s[$end - 1] === '/' ) {
2000 break;
2001 }
2002 // (Implicit else), we must've found the start of a char class,
2003 // skip until we find "]" (end of char class), or "\" (backslash escape)
2004 do {
2005 // Speculatively add 2 for backslash escape.
2006 // We'll substract one outside the loop.
2007 $end += strcspn( $s, ']\\', $end ) + 2;
2008 // If backslash escape, keep searching...
2009 } while ( $end - 2 < $length && $s[$end - 2] === '\\' );
2010 // Correction (1): Undo speculative add, keep only one (end of regexp)
2011 $end--;
2012 if ( $end > $length ) {
2013 // Correction (2): Loop wrongly assumed "]" was seen
2014 // String ended without ending char class or regexp. Correct $end.
2015 // TODO: This is invalid and should throw.
2016 $end--;
2017 break;
2018 }
2019 }
2020 // Search past the regexp modifiers (gi)
2021 while ( $end < $length && ctype_alpha( $s[$end] ) ) {
2022 $end++;
2023 }
2024 } elseif (
2025 $ch === '0'
2026 && ( $pos + 1 < $length ) && ( $s[$pos + 1] === 'x' || $s[$pos + 1] === 'X' )
2027 ) {
2028 // Hex numeric literal
2029 // x or X
2030 $end++;
2031 $len = strspn( $s, '0123456789ABCDEFabcdef', $end );
2032 if ( !$len && !$error ) {
2033 $error = new ParseError(
2034 'Expected a hexadecimal number but found ' . substr( $s, $pos, 5 ),
2035 $pos,
2036 );
2037 }
2038 $end += $len;
2039 } elseif (
2040 // Optimisation: This check must accept only ASCII digits 0-9.
2041 // Avoid ctype_digit() because it is slower and also accepts locale-specific digits.
2042 // Using is_numeric() might seem wrong also as it accepts negative numbers, decimal
2043 // numbers, and exponents (e.g. strings like "+012.34e6"). But, it is fine here
2044 // because we know $ch is a single character, and we believe the only single
2045 // characters that is_numeric() accepts are ASCII digits 0-9.
2046 is_numeric( $ch )
2047 || ( $ch === '.' && $pos + 1 < $length && is_numeric( $s[$pos + 1] ) )
2048 ) {
2049 $end += strspn( $s, '0123456789', $end );
2050 $decimal = strspn( $s, '.', $end );
2051 if ( $decimal ) {
2052 // Valid: "5." (number literal, optional fraction)
2053 // Valid: "5.42" (number literal)
2054 // Valid: "5..toString" (number literal "5.", followed by member expression).
2055 // Invalid: "5..42"
2056 // Invalid: "5...42"
2057 // Invalid: "5...toString"
2058 $fraction = strspn( $s, '0123456789', $end + $decimal );
2059 if ( $decimal === 2 && !$fraction ) {
2060 // Rewind one character, so that the member expression dot
2061 // will be parsed as the next token (TYPE_DOT).
2062 $decimal = 1;
2063 }
2064 if ( $decimal > 1 && !$error ) {
2065 $error = new ParseError( 'Too many decimal points', $end );
2066 }
2067 $end += $decimal + $fraction;
2068 } else {
2069 $dotlessNum = true;
2070 }
2071 $exponent = strspn( $s, 'eE', $end );
2072 if ( $exponent ) {
2073 if ( $exponent > 1 && !$error ) {
2074 $error = new ParseError( 'Number with several E', $end );
2075 }
2076 $end += $exponent;
2077
2078 // + sign is optional; - sign is required.
2079 $end += strspn( $s, '-+', $end );
2080 $len = strspn( $s, '0123456789', $end );
2081 if ( !$len && !$error ) {
2082 $error = new ParseError(
2083 'Missing decimal digits after exponent',
2084 $pos
2085 );
2086 }
2087 $end += $len;
2088 }
2089 } elseif ( isset( self::$opChars[$ch] ) ) {
2090 // Punctuation character. Search for the longest matching operator.
2091 for ( $tokenLength = self::LONGEST_PUNCTUATION_TOKEN; $tokenLength > 1; $tokenLength-- ) {
2092 if (
2093 $pos + $tokenLength <= $length &&
2094 isset( self::$tokenTypes[ substr( $s, $pos, $tokenLength ) ] )
2095 ) {
2096 $end = $pos + $tokenLength;
2097 break;
2098 }
2099 }
2100 } else {
2101 // Identifier or reserved word. Search for the end by excluding whitespace and
2102 // punctuation.
2103 $end += strcspn( $s, " \t\n.;,=<>+-{}()[]?:*/%'\"`!&|^~\xb\xc\r", $end );
2104 }
2105
2106 // Now get the token type from our type array
2107 // so $end - $pos == strlen( $token )
2108 $token = substr( $s, $pos, $end - $pos );
2109 $type = isset( self::$model[$state][self::TYPE_SPECIAL][$token] )
2110 ? self::TYPE_SPECIAL
2111 : self::$tokenTypes[$token] ?? self::TYPE_LITERAL;
2112 if ( $type === self::TYPE_YIELD ) {
2113 // yield is treated as TYPE_RETURN inside a generator function (negative state)
2114 // but as TYPE_LITERAL when not in a generator function (positive state)
2115 $type = $state < 0 ? self::TYPE_RETURN : self::TYPE_LITERAL;
2116 }
2117
2118 $pad = '';
2119
2120 if ( $newlineFound && isset( self::$semicolon[$state][$type] ) ) {
2121 // This token triggers the semicolon insertion mechanism of javascript. While we
2122 // could add the ; token here ourselves, keeping the newline has a few advantages.
2123 $pad = "\n";
2124 $state = $state < 0 ? -self::STATEMENT : self::STATEMENT;
2125 $lineLength = 0;
2126 // This check adds a new line if we have exceeded the max length and only does this if
2127 // a newline was found in this this position, if it wasn't, it uses the next available
2128 // line break
2129 } elseif ( $newlineFound &&
2130 $lineLength + $end - $pos > self::$maxLineLength &&
2131 !isset( self::$semicolon[$state][$type] ) &&
2132 $type !== self::TYPE_INCR_OP &&
2133 $type !== self::TYPE_ARROW
2134 ) {
2135 $pad = "\n";
2136 $lineLength = 0;
2137 // Check, whether we have to separate the token from the last one with whitespace
2138 } elseif ( !isset( self::$opChars[$last] ) && !isset( self::$opChars[$ch] ) ) {
2139 $pad = ' ';
2140 $lineLength++;
2141 // Don't accidentally create ++, -- or // tokens
2142 } elseif ( $last === $ch && ( $ch === '+' || $ch === '-' || $ch === '/' ) ) {
2143 $pad = ' ';
2144 $lineLength++;
2145 // Don't create invalid dot notation after number literal (T303827).
2146 // Keep whitespace in "42. foo".
2147 // But keep minifying "foo.bar", "42..foo", and "42.0.foo" per $opChars.
2148 } elseif ( $lastDotlessNum && $type === self::TYPE_DOT ) {
2149 $pad = ' ';
2150 $lineLength++;
2151 }
2152
2153 if ( $onDebug ) {
2154 $onDebug( self::augmentDebugContext( [
2155 'stack' => $stack,
2156 'last' => $last,
2157 'state' => $state,
2158 'pos' => $pos,
2159 'ch' => $ch,
2160 'token' => $token,
2161 'type' => $type,
2162 ] ) );
2163 }
2164
2165 if ( $mapGenerator ) {
2166 $mapGenerator->outputSpace( $pad );
2167 $mapGenerator->outputToken( $token );
2168 $mapGenerator->consumeSource( $end - $pos );
2169 }
2170 $out .= $pad;
2171 $out .= $token;
2172 $lineLength += $end - $pos;
2173 $last = $s[$end - 1];
2174 $pos = $end;
2175 $newlineFound = false;
2176 $lastDotlessNum = $dotlessNum;
2177 $dotlessNum = false;
2178
2179 // Now that we have output our token, transition into the new state.
2180 $actions = $type === self::TYPE_SPECIAL ?
2181 self::$model[$state][$type][$token] :
2182 self::$model[$state][$type] ?? [];
2183 if ( isset( $actions[self::ACTION_PUSH] ) &&
2184 count( $stack ) < self::STACK_LIMIT
2185 ) {
2186 $topOfStack = $actions[self::ACTION_PUSH];
2187 $stack[] = $topOfStack;
2188 }
2189 if ( $stack && isset( $actions[self::ACTION_POP] ) ) {
2190 $state = array_pop( $stack );
2191 $topOfStack = end( $stack );
2192 } elseif ( isset( $actions[self::ACTION_GOTO] ) ) {
2193 $state = $actions[self::ACTION_GOTO];
2194 }
2195 }
2196 if ( $onError && $error ) {
2197 $onError( $error );
2198 }
2199 return $out;
2200 }
2201
2202 /**
2203 * Replace integer values with the corresponding class constant names
2204 *
2205 * @param array $context
2206 * - int[] 'stack' List of states (class constants)
2207 * - string 'last' Previous character from input stream
2208 * - int 'state' Current state as result of previous character (class constant)
2209 * - int 'pos' Offset of current character in input stream
2210 * - string 'ch' Current character in input stream, first character of current token
2211 * - string 'token' Current token from input stream
2212 * - int 'type' Current type as interpreted from the current character
2213 *
2214 * @return array The $context, with any integer class constants replaced by
2215 * their corresponding class constant name as a string (if found), or else
2216 * their given integer value.
2217 */
2218 private static function augmentDebugContext( array $context ) {
2219 $self = new ReflectionClass( self::class );
2220 foreach ( $self->getConstants() as $name => $value ) {
2221 foreach ( $context['stack'] as $i => $state ) {
2222 if ( $value === $state ) {
2223 $context['stack'][$i] = $name;
2224 }
2225 }
2226 if ( $value === $context['state'] ) {
2227 $context['state'] = $name;
2228 }
2229 if ( $value === $context['type'] ) {
2230 $context['type'] = $name;
2231 }
2232 }
2233
2234 return $context;
2235 }
2236 }