-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJavaScriptMinifier.php
1681 lines (1636 loc) · 56.4 KB
/
JavaScriptMinifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright 2011 Paul Copperman <paul.copperman@gmail.com>
* Copyright 2018 Timo Tijhof
* Copyright 2021 Roan Kattouw <roan.kattouw@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @file
* @license Apache-2.0
* @license MIT
* @license GPL-2.0-or-later
* @license LGPL-2.1-or-later
*/
/**
* JavaScript Minifier
*
* This class is meant to safely minify JavaScript code, while leaving syntactically correct
* programs intact. Other libraries, such as JSMin require a certain coding style to work
* correctly. OTOH, libraries like jsminplus, that do parse the code correctly are rather
* slow, because they construct a complete parse tree before outputting the code minified.
* So this class is meant to allow arbitrary (but syntactically correct) input, while being
* fast enough to be used for on-the-fly minifying.
*
* This class was written with ECMA-262 Edition 6 in mind ("ECMAScript 6"). Parsing features
* new to later editions of ECMAScript might not be supported. It's assumed that the input is
* syntactically correct; if it's not, this class may not detect that, and may produce incorrect
* output.
*
* See <https://262.ecma-international.org/6.0/>.
*/
class JavaScriptMinifier {
/* Parsing states.
* The state machine is necessary to decide whether to parse a slash as division
* operator or as regexp literal, and to know where semicolon insertion is possible.
* States are generally named after the next expected item. We only distinguish states when the
* distinction is relevant for our purpose. The meaning of these states is documented
* in $model below.
*
* Negative numbers are used to indicate that the state is inside a generator function,
* which changes the behavior of 'yield'
*/
private const STATEMENT = 1;
private const CONDITION = 2;
private const FUNC = 3;
private const GENFUNC = 4;
private const PROPERTY_ASSIGNMENT = 5;
private const EXPRESSION = 6;
private const EXPRESSION_NO_NL = 7;
private const EXPRESSION_OP = 8;
private const EXPRESSION_DOT = 9;
private const EXPRESSION_END = 10;
private const EXPRESSION_ARROWFUNC = 11;
private const EXPRESSION_TERNARY = 12;
private const EXPRESSION_TERNARY_OP = 13;
private const EXPRESSION_TERNARY_DOT = 14;
private const EXPRESSION_TERNARY_ARROWFUNC = 15;
private const PAREN_EXPRESSION = 16;
private const PAREN_EXPRESSION_OP = 17;
private const PAREN_EXPRESSION_DOT = 18;
private const PAREN_EXPRESSION_ARROWFUNC = 19;
private const PROPERTY_EXPRESSION = 20;
private const PROPERTY_EXPRESSION_OP = 21;
private const PROPERTY_EXPRESSION_DOT = 22;
private const PROPERTY_EXPRESSION_ARROWFUNC = 23;
private const CLASS_DEF = 24;
private const IMPORT_EXPORT = 25;
private const TEMPLATE_STRING_HEAD = 26;
private const TEMPLATE_STRING_TAIL = 27;
/* Token types */
private const TYPE_UN_OP = 101; // unary operators
private const TYPE_INCR_OP = 102; // ++ and --
private const TYPE_BIN_OP = 103; // binary operators (except .)
private const TYPE_ADD_OP = 104; // + and - which can be either unary or binary ops
private const TYPE_DOT = 105; // .
private const TYPE_HOOK = 106; // ?
private const TYPE_COLON = 107; // :
private const TYPE_COMMA = 108; // ,
private const TYPE_SEMICOLON = 109; // ;
private const TYPE_BRACE_OPEN = 110; // {
private const TYPE_BRACE_CLOSE = 111; // }
private const TYPE_PAREN_OPEN = 112; // ( and [
private const TYPE_PAREN_CLOSE = 113; // ) and ]
private const TYPE_ARROW = 114; // =>
private const TYPE_RETURN = 115; // keywords: break, continue, return, throw
private const TYPE_IF = 116; // keywords: catch, for, with, switch, while, if
private const TYPE_DO = 117; // keywords: case, finally, else, do, try
private const TYPE_VAR = 118; // keywords: var, let, const
private const TYPE_YIELD = 119; // keywords: yield
private const TYPE_FUNC = 120; // keywords: function
private const TYPE_CLASS = 121; // keywords: class
private const TYPE_LITERAL = 122; // all literals, identifiers, unrecognised tokens, and other keywords
private const TYPE_SPECIAL = 123; // For special treatment of tokens that usually mean something else
private const ACTION_GOTO = 201; // Go to another state
private const ACTION_PUSH = 202; // Push a state to the stack
private const ACTION_POP = 203; // Pop the state from the top of the stack, and go to that state
// Limit to avoid excessive memory usage
private const STACK_LIMIT = 1000;
// Length of the longest token in $tokenTypes made of punctuation characters,
// as defined in $opChars. Update this if you add longer tokens to $tokenTypes.
//
// Currently the longest punctuation token is `>>>=`, which is 4 characters.
private const LONGEST_PUNCTUATION_TOKEN = 4;
/**
* @var int $maxLineLength
*
* Maximum line length
*
* This is not a strict maximum, but a guideline. Longer lines will be
* produced when literals (e.g. quoted strings) longer than this are
* encountered, or when required to guard against semicolon insertion.
*
* This is a private member (instead of constant) to allow tests to
* set it to 1, to verify ASI and line-breaking behaviour.
*/
private static $maxLineLength = 1000;
private static $expandedStates = false;
/**
* @var array $opChars
*
* Characters which can be combined without whitespace between them.
*/
private static $opChars = [
// ECMAScript 6.0 § 11.7 Punctuators
// Unlike the spec, these are individual symbols, not sequences.
'{' => true,
'}' => true,
'(' => true,
')' => true,
'[' => true,
']' => true,
// Dots have a special case after $dotlessNum which require whitespace
'.' => true,
';' => true,
',' => true,
'<' => true,
'>' => true,
'=' => true,
'!' => true,
'+' => true,
'-' => true,
'*' => true,
'%' => true,
'&' => true,
'|' => true,
'^' => true,
'~' => true,
'?' => true,
':' => true,
'/' => true,
// ECMAScript 6.0 § 11.8.4 String Literals
'"' => true,
"'" => true,
// ECMAScript 6.0 § 11.8.6 Template Literal Lexical Components
'`' => true,
];
/**
* @var array $tokenTypes
*
* Tokens and their types.
*/
private static $tokenTypes = [
// ECMAScript 6.0 § 12.5 Unary Operators
// UnaryExpression includes PostfixExpression, which includes 'new'.
'new' => self::TYPE_UN_OP,
'delete' => self::TYPE_UN_OP,
'void' => self::TYPE_UN_OP,
'typeof' => self::TYPE_UN_OP,
'~' => self::TYPE_UN_OP,
'!' => self::TYPE_UN_OP,
// ECMAScript 6.0 § 12.2 Primary Expression, among others
'...' => self::TYPE_UN_OP,
// ECMAScript 6.0 § 12.7 Additive Operators
'++' => self::TYPE_INCR_OP,
'--' => self::TYPE_INCR_OP,
'+' => self::TYPE_ADD_OP,
'-' => self::TYPE_ADD_OP,
// ECMAScript 6.0 § 12.6 Multiplicative Operators
'*' => self::TYPE_BIN_OP,
'/' => self::TYPE_BIN_OP,
'%' => self::TYPE_BIN_OP,
// ECMAScript 6.0 § 12.8 Bitwise Shift Operators
'<<' => self::TYPE_BIN_OP,
'>>' => self::TYPE_BIN_OP,
'>>>' => self::TYPE_BIN_OP,
// ECMAScript 6.0 § 12.9 Relational Operators
'<' => self::TYPE_BIN_OP,
'>' => self::TYPE_BIN_OP,
'<=' => self::TYPE_BIN_OP,
'>=' => self::TYPE_BIN_OP,
'instanceof' => self::TYPE_BIN_OP,
'in' => self::TYPE_BIN_OP,
// ECMAScript 6.0 § 12.10 Equality Operators
'==' => self::TYPE_BIN_OP,
'!=' => self::TYPE_BIN_OP,
'===' => self::TYPE_BIN_OP,
'!==' => self::TYPE_BIN_OP,
// ECMAScript 6.0 § 12.11 Binary Bitwise Operators
'&' => self::TYPE_BIN_OP,
'^' => self::TYPE_BIN_OP,
'|' => self::TYPE_BIN_OP,
// ECMAScript 6.0 § 12.12 Binary Logical Operators
'&&' => self::TYPE_BIN_OP,
'||' => self::TYPE_BIN_OP,
// ECMAScript 6.0 § 12.13 Conditional Operator
// Also known as ternary.
'?' => self::TYPE_HOOK,
':' => self::TYPE_COLON,
// ECMAScript 6.0 § 12.14 Assignment Operators
'=' => self::TYPE_BIN_OP,
'*=' => self::TYPE_BIN_OP,
'/=' => self::TYPE_BIN_OP,
'%=' => self::TYPE_BIN_OP,
'+=' => self::TYPE_BIN_OP,
'-=' => self::TYPE_BIN_OP,
'<<=' => self::TYPE_BIN_OP,
'>>=' => self::TYPE_BIN_OP,
'>>>=' => self::TYPE_BIN_OP,
'&=' => self::TYPE_BIN_OP,
'^=' => self::TYPE_BIN_OP,
'|=' => self::TYPE_BIN_OP,
// ECMAScript 6.0 § 12.15 Comma Operator
',' => self::TYPE_COMMA,
// The keywords that disallow LineTerminator before their
// (sometimes optional) Expression or Identifier.
//
// keyword ;
// keyword [no LineTerminator here] Identifier ;
// keyword [no LineTerminator here] Expression ;
//
// See also ECMAScript 6.0 § 11.9.1 Rules of Automatic Semicolon Insertion
'continue' => self::TYPE_RETURN,
'break' => self::TYPE_RETURN,
'return' => self::TYPE_RETURN,
'throw' => self::TYPE_RETURN,
// yield is only a keyword inside generator functions, otherwise it's an identifier
// This is handled with the negative states hack: if the state is negative, TYPE_YIELD
// is treated as TYPE_RETURN, if it's positive it's treated as TYPE_LITERAL
'yield' => self::TYPE_YIELD,
// The keywords require a parenthesised Expression or Identifier
// before the next Statement.
//
// keyword ( Expression ) Statement
// keyword ( Identifier ) Statement
//
// See also ECMAScript 6.0:
// - § 13.6 The if Statement
// - § 13.7 Iteration Statements (do, while, for)
// - § 12.10 The with Statement
// - § 12.11 The switch Statement
// - § 12.13 The throw Statement
'if' => self::TYPE_IF,
'catch' => self::TYPE_IF,
'while' => self::TYPE_IF,
'for' => self::TYPE_IF,
'switch' => self::TYPE_IF,
'with' => self::TYPE_IF,
// The keywords followed by a Statement, Expression, or Block.
//
// else Statement
// do Statement
// case Expression
// try Block
// finally Block
//
// See also ECMAScript 6.0:
// - § 13.6 The if Statement (else)
// - § 13.7 Iteration Statements (do, while, for)
// - § 13.12 The switch Statement (case)
// - § 13.15 The try Statement
'else' => self::TYPE_DO,
'do' => self::TYPE_DO,
'case' => self::TYPE_DO,
'try' => self::TYPE_DO,
'finally' => self::TYPE_DO,
// Keywords followed by a variable declaration
// This is different from the group above, because a { begins
// object destructuring, rather than a block
'var' => self::TYPE_VAR,
'let' => self::TYPE_VAR,
'const' => self::TYPE_VAR,
// ECMAScript 6.0 § 14.1 Function Definitions
'function' => self::TYPE_FUNC,
// ECMAScript 6.0 § 14.2 Arrow Function Definitions
'=>' => self::TYPE_ARROW,
// Class declaration or expression:
// class Identifier { ClassBody }
// class { ClassBody }
// class Identifier extends Expression { ClassBody }
// class extends Expression { ClassBody }
'class' => self::TYPE_CLASS,
// ECMAScript 6.0 § 12.3 Left-Hand-Side Expressions (MemberExpression)
// A dot can also be part of a DecimalLiteral, but in that case we handle the entire
// DecimalLiteral as one token. A separate '.' token is always part of a MemberExpression.
'.' => self::TYPE_DOT,
// Can be one of:
// - Block (ECMAScript 6.0 § 13.2 Block)
// - ObjectLiteral (ECMAScript 6.0 § 12.2 Primary Expression)
'{' => self::TYPE_BRACE_OPEN,
'}' => self::TYPE_BRACE_CLOSE,
// Can be one of:
// - Parenthesised Identifier or Expression after a
// TYPE_IF or TYPE_FUNC keyword.
// - PrimaryExpression (ECMAScript 6.0 § 12.2 Primary Expression)
// - CallExpression (ECMAScript 6.0 § 12.3 Left-Hand-Side Expressions)
// - Beginning of an ArrowFunction (ECMAScript 6.0 § 14.2 Arrow Function Definitions)
'(' => self::TYPE_PAREN_OPEN,
')' => self::TYPE_PAREN_CLOSE,
// Can be one of:
// - ArrayLiteral (ECMAScript 6.0 § 12.2 Primary Expressions)
// - ComputedPropertyName (ECMAScript 6.0 § 12.2.6 Object Initializer)
'[' => self::TYPE_PAREN_OPEN,
']' => self::TYPE_PAREN_CLOSE,
// Can be one of:
// - End of any statement
// - EmptyStatement (ECMAScript 6.0 § 13.4 Empty Statement)
';' => self::TYPE_SEMICOLON,
];
/**
* @var array $model
*
* The main table for the state machine. Defines the desired action for every state/token pair.
*
* The state pushed onto the stack by ACTION_PUSH will be returned to by ACTION_POP.
* A state/token pair may not specify both ACTION_POP and ACTION_GOTO. If that does happen,
* ACTION_POP takes precedence.
*
* This table is augmented by self::ensureExpandedStates().
*/
private static $model = [
// Statement - This is the initial state.
self::STATEMENT => [
self::TYPE_UN_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_INCR_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_BRACE_OPEN => [
// Use of '{' in statement context, creates a Block.
self::ACTION_PUSH => self::STATEMENT,
],
self::TYPE_BRACE_CLOSE => [
// Ends a Block
self::ACTION_POP => true,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_RETURN => [
self::ACTION_GOTO => self::EXPRESSION_NO_NL,
],
self::TYPE_IF => [
self::ACTION_GOTO => self::CONDITION,
],
self::TYPE_VAR => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::STATEMENT,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::STATEMENT,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_SPECIAL => [
'import' => [
self::ACTION_GOTO => self::IMPORT_EXPORT,
],
'export' => [
self::ACTION_GOTO => self::IMPORT_EXPORT,
],
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
],
// The state after if/catch/while/for/switch/with
// Waits for an expression in parentheses, then goes to STATEMENT
self::CONDITION => [
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::STATEMENT,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
],
// The state after the function keyword. Waits for {, then goes to STATEMENT.
// The function body's closing } will pop the stack, so the state to return to
// after the function should be pushed to the stack first
self::FUNC => [
// Needed to prevent * in an expression in the argument list from improperly
// triggering GENFUNC
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::FUNC,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_BRACE_OPEN => [
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_SPECIAL => [
'*' => [
self::ACTION_GOTO => self::GENFUNC,
],
],
],
// After function*. Waits for { , then goes to a generator function statement.
self::GENFUNC => [
self::TYPE_BRACE_OPEN => [
// Note negative value: generator function states are negative
self::ACTION_GOTO => -self::STATEMENT
],
],
// Property assignment - This is an object literal declaration.
// For example: `{ key: value, key2, [computedKey3]: value3, method4() { ... } }`
self::PROPERTY_ASSIGNMENT => [
// Note that keywords like if, class, var, delete, instanceof etc. can be used as keys,
// and should be treated as literals here, as they are in EXPRESSION_DOT. In this state,
// that is implicitly true because TYPE_LITERAL has no action, so it stays in this state.
// If we later add a state transition for TYPE_LITERAL, that same transition should
// also be applied to TYPE_RETURN, TYPE_IF, TYPE_DO, TYPE_VAR, TYPE_FUNC and TYPE_CLASS.
self::TYPE_COLON => [
self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
],
// For {, which begins a method
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::PROPERTY_ASSIGNMENT,
// This is not flipped, see "Special cases" below
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_BRACE_CLOSE => [
self::ACTION_POP => true,
],
// For [, which begins a computed key
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::PROPERTY_ASSIGNMENT,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_SPECIAL => [
'*' => [
self::ACTION_PUSH => self::PROPERTY_ASSIGNMENT,
self::ACTION_GOTO => self::GENFUNC,
],
],
],
// Place in an expression where we expect an operand or a unary operator: the start
// of an expression or after an operator. Note that unary operators (including INCR_OP
// and ADD_OP) cause us to stay in this state, while operands take us to EXPRESSION_OP
self::EXPRESSION => [
self::TYPE_SEMICOLON => [
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
],
self::TYPE_BRACE_CLOSE => [
self::ACTION_POP => true,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
],
// An expression immediately after return/throw/break/continue, where a newline
// is not allowed. This state is identical to EXPRESSION, except that semicolon
// insertion can happen here, and we never stay here: in cases where EXPRESSION would
// do nothing, we go to EXPRESSION.
self::EXPRESSION_NO_NL => [
self::TYPE_UN_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_INCR_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
// BIN_OP seems impossible at the start of an expression, but it can happen in
// yield *foo
self::TYPE_BIN_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_SEMICOLON => [
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
],
self::TYPE_BRACE_CLOSE => [
self::ACTION_POP => true,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
],
// Place in an expression after an operand, where we expect an operator
self::EXPRESSION_OP => [
self::TYPE_BIN_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_DOT => [
self::ACTION_GOTO => self::EXPRESSION_DOT,
],
self::TYPE_HOOK => [
self::ACTION_PUSH => self::EXPRESSION,
self::ACTION_GOTO => self::EXPRESSION_TERNARY,
],
self::TYPE_COLON => [
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_COMMA => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_SEMICOLON => [
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_ARROW => [
self::ACTION_GOTO => self::EXPRESSION_ARROWFUNC,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_BRACE_CLOSE => [
self::ACTION_POP => true,
],
],
// State after a dot (.). Like EXPRESSION, except that many keywords behave like literals
// (e.g. class, if, else, var, function) because they're not valid as identifiers but are
// valid as property names.
self::EXPRESSION_DOT => [
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
// The following are keywords behaving as literals
self::TYPE_RETURN => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
self::TYPE_IF => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
self::TYPE_DO => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
self::TYPE_VAR => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
self::TYPE_FUNC => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
self::TYPE_CLASS => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
// We don't expect real unary/binary operators here, but some keywords
// (new, delete, void, typeof, instanceof, in) are classified as such, and they can be
// used as property names
self::TYPE_UN_OP => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
self::TYPE_BIN_OP => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
],
// State after the } closing an arrow function body: like STATEMENT except
// that it has semicolon insertion, COMMA can continue the expression, and after
// a function we go to STATEMENT instead of EXPRESSION_OP
self::EXPRESSION_END => [
self::TYPE_UN_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_INCR_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_COMMA => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_SEMICOLON => [
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::STATEMENT,
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_BRACE_CLOSE => [
self::ACTION_POP => true,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_RETURN => [
self::ACTION_GOTO => self::EXPRESSION_NO_NL,
],
self::TYPE_IF => [
self::ACTION_GOTO => self::CONDITION,
],
self::TYPE_VAR => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::STATEMENT,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::STATEMENT,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
],
// State after =>. Like EXPRESSION, except that { begins an arrow function body
// rather than an object literal.
self::EXPRESSION_ARROWFUNC => [
self::TYPE_UN_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_INCR_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::EXPRESSION,
],
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_END,
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::EXPRESSION_OP,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::EXPRESSION_OP,
],
],
// Expression after a ? . This differs from EXPRESSION because a : ends the ternary
// rather than starting STATEMENT (outside a ternary, : comes after a goto label)
// The actual rule for : ending the ternary is in EXPRESSION_TERNARY_OP.
self::EXPRESSION_TERNARY => [
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
],
// Like EXPRESSION_OP, but for ternaries, see EXPRESSION_TERNARY
self::EXPRESSION_TERNARY_OP => [
self::TYPE_BIN_OP => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY,
],
self::TYPE_DOT => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_DOT,
],
self::TYPE_HOOK => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY,
self::ACTION_GOTO => self::EXPRESSION_TERNARY,
],
self::TYPE_COMMA => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY,
],
self::TYPE_ARROW => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_ARROWFUNC,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_COLON => [
self::ACTION_POP => true,
],
],
// Like EXPRESSION_DOT, but for ternaries, see EXPRESSION_TERNARY
self::EXPRESSION_TERNARY_DOT => [
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
// The following are keywords behaving as literals
self::TYPE_RETURN => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
self::TYPE_IF => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
self::TYPE_DO => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
self::TYPE_VAR => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
self::TYPE_FUNC => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
self::TYPE_CLASS => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
// We don't expect real unary/binary operators here, but some keywords
// (new, delete, void, typeof, instanceof, in) are classified as such, and they can be
// used as property names
self::TYPE_UN_OP => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
self::TYPE_BIN_OP => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
],
// Like EXPRESSION_ARROWFUNC, but for ternaries, see EXPRESSION_TERNARY
self::EXPRESSION_TERNARY_ARROWFUNC => [
self::TYPE_UN_OP => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY,
],
self::TYPE_INCR_OP => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY,
],
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::EXPRESSION_TERNARY_OP,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::EXPRESSION_TERNARY_OP,
],
],
// Expression inside parentheses. Like EXPRESSION, except that ) ends this state
// This differs from EXPRESSION because semicolon insertion can't happen here
self::PAREN_EXPRESSION => [
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_PAREN_CLOSE => [
self::ACTION_POP => true,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
],
// Like EXPRESSION_OP, but in parentheses, see PAREN_EXPRESSION
self::PAREN_EXPRESSION_OP => [
self::TYPE_BIN_OP => [
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_DOT => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_DOT,
],
self::TYPE_HOOK => [
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_COLON => [
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_COMMA => [
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_SEMICOLON => [
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_ARROW => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_ARROWFUNC,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_PAREN_CLOSE => [
self::ACTION_POP => true,
],
],
// Like EXPRESSION_DOT, but in parentheses, see PAREN_EXPRESSION
self::PAREN_EXPRESSION_DOT => [
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
// The following are keywords behaving as literals
self::TYPE_RETURN => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
self::TYPE_IF => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
self::TYPE_DO => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
self::TYPE_VAR => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
self::TYPE_FUNC => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
self::TYPE_CLASS => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
// We don't expect real unary/binary operators here, but some keywords
// (new, delete, void, typeof, instanceof, in) are classified as such, and they can be
// used as property names
self::TYPE_UN_OP => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
self::TYPE_BIN_OP => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
],
// Like EXPRESSION_ARROWFUNC, but in parentheses, see PAREN_EXPRESSION
self::PAREN_EXPRESSION_ARROWFUNC => [
self::TYPE_UN_OP => [
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_INCR_OP => [
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
self::ACTION_GOTO => self::STATEMENT,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::PAREN_EXPRESSION_OP,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::PAREN_EXPRESSION_OP,
],
],
// Expression as the value of a key in an object literal. Like EXPRESSION, except that
// a comma (in PROPERTY_EXPRESSION_OP) goes to PROPERTY_ASSIGNMENT instead
self::PROPERTY_EXPRESSION => [
self::TYPE_BRACE_OPEN => [
self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
],
self::TYPE_BRACE_CLOSE => [
self::ACTION_POP => true,
],
self::TYPE_PAREN_OPEN => [
self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
self::ACTION_GOTO => self::PAREN_EXPRESSION,
],
self::TYPE_FUNC => [
self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
self::ACTION_GOTO => self::FUNC,
],
self::TYPE_CLASS => [
self::ACTION_PUSH => self::PROPERTY_EXPRESSION_OP,
self::ACTION_GOTO => self::CLASS_DEF,
],
self::TYPE_LITERAL => [
self::ACTION_GOTO => self::PROPERTY_EXPRESSION_OP,
],
],
// Like EXPRESSION_OP, but in a property expression, see PROPERTY_EXPRESSION
self::PROPERTY_EXPRESSION_OP => [
self::TYPE_BIN_OP => [
self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
],
self::TYPE_ADD_OP => [
self::ACTION_GOTO => self::PROPERTY_EXPRESSION,
],
self::TYPE_DOT => [
self::ACTION_GOTO => self::PROPERTY_EXPRESSION_DOT,
],
self::TYPE_HOOK => [
self::ACTION_PUSH => self::PROPERTY_EXPRESSION,
self::ACTION_GOTO => self::EXPRESSION_TERNARY,
],
self::TYPE_COMMA => [
self::ACTION_GOTO => self::PROPERTY_ASSIGNMENT,
],
self::TYPE_ARROW => [
self::ACTION_GOTO => self::PROPERTY_EXPRESSION_ARROWFUNC,
],
self::TYPE_BRACE_OPEN => [