Repository navigation
Expand file tree
/
Copy pathjavascript.ts
More file actions
1099 lines (1039 loc) · 76.9 KB
/
Copy pathjavascript.ts
File metadata and controls
1099 lines (1039 loc) · 76.9 KB
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
// JavaScript grammar for Monogram — the STANDALONE BASE of the ECMAScript family.
//
// JavaScript is the syntactic SUBSET of TypeScript (TS = JS + a type layer), so
// this file owns the shared, type-free ECMAScript *vocabulary* — the token set,
// the `notReserved`/`notReservedExpr` reserved-word guards, the precedence ladder
// (`ecmaPrec`), and the JS scope map (`jsScopes`) — and exports it. The TS grammar
// (the sibling superset file) then imports that vocabulary and EXTENDS it with the
// type layer. The dependency runs subset → superset only: this file imports nothing
// from the TS grammar (it has no type knowledge) and must stand alone — its only
// import is the engine's combinator API.
//
// The rules are NOT shared either direction: combinator rules bind their
// references at definition time, so a JS rule must reference the OTHER JS rule
// consts — it can't reuse TS's rule objects (and vice-versa). Each file therefore
// keeps its own rule consts; only the vocabulary above is shared.
//
// This grammar is the TS grammar with every type construct removed: no type
// annotations, type parameters/arguments, `interface`/`type`/`enum`/`namespace`/
// `declare`/`module`-as-type declarations, `as`/`satisfies`/`<T>`-cast/non-null
// (`!`) expressions, and no type-optional `?` on params/members. Everything that
// is real JavaScript is kept: functions/arrows/classes (static, async, accessor,
// get/set, generators, private `#x`, static blocks), the full expression and
// operator set (`??`, `?.`, `**`, optional chaining), destructuring, template
// literals (tagged + interpolated), regex, every module import/export VALUE form,
// control flow, numeric literals, and Stage-3 decorators on classes.
import {
token, rule, defineGrammar,
left, right, none, noUnaryLhs, lhsAssignTarget, prefixAssignTarget, postfixAssignTarget,
op, prefix, postfix, sameLine,
sep, opt, many, many1, alt, exclude, not, reservableNot, capExpr, treeSitterRelax, arm, field,
awaitCtx, yieldCtx, asyncGenCtx, resetCtx, when,
altPattern, optPattern, seq, oneOf, noneOf, range, anyChar, star, plus, repeat, notFollowedBy, start,
type Element, type Alternative,
} from './src/api.ts';
// Build the four async×generator arms of a `function` form, routing each arm's params
// and body to its [Await]/[Yield] family: plain resets to none, generator -> yield,
// async -> await, async-generator -> both. `nameParts` is spread in after `function`
// (and `*` for the generator arms); `body` is the function body element. `async` and
// `function` share a line (`async [no LineTerminator here] function`): after a line break
// `async` is an identifier and `function` begins the next statement. Param/Block
// resolve at thunk-eval time (defined below), so this is safe to call inside a rule().
function fnArms(nameParts: Element[], body: Element) {
return [
arm('function_', 'function', ...nameParts, '(', sep(resetCtx(field('parameter', Param)), ','), ')', resetCtx(field('body', body))),
arm('generator', 'function', field('asterisk', '*'), ...nameParts, '(', sep(yieldCtx(field('parameter', Param)), ','), ')', yieldCtx(field('body', body))),
arm('asyncFunction', field('asyncKeyword', 'async'), sameLine, 'function', ...nameParts, '(', sep(awaitCtx(field('parameter', Param)), ','), ')', awaitCtx(field('body', body))),
arm('asyncGenerator', field('asyncKeyword', 'async'), sameLine, 'function', field('asterisk', '*'), ...nameParts, '(', sep(asyncGenCtx(field('parameter', Param)), ','), ')', asyncGenCtx(field('body', body))),
];
}
// How a statement ends (ECMAScript 12.10, Automatic Semicolon Insertion; tsc's parseSemicolon):
// a `;`, or nothing when the next token is on a later line, is `}`, or is the end of the input
// (`not(sameLine)` holds there too). A same-line token can not end it, so two statements on one
// line need a `;` between them. A statement or declaration that ends in a block needs nothing.
// The one definition, for both grammars: TypeScript's declarations and members end this way too.
export const asi = () => alt([';'], [not(sameLine)], [not(not('}'))]);
// ── Tokens ──
// IdentifierName, ASCII + `\u`-escape forms. The `\uXXXX` / `\u{cp}` alternatives let an
// identifier (or a private `#name`) spell any character via an escape — e.g. `\u{6F}_` or
// `#\u{6F}_` — which the spec permits anywhere an IdentifierStart/Part is allowed. Non-ASCII
// *literal* identifier characters (`℘`, accented letters, ZWNJ/ZWJ, combining marks) are matched
// by the lexer's Unicode ID_Start/ID_Continue fallback instead (no /u flag is compiled here —
// YAML's token patterns use escapes that /u rejects — so \p{…} cannot live in these patterns).
const digit = range('0', '9');
const hexDigit = oneOf(digit, range('A', 'F'), range('a', 'f'));
const idStart = oneOf(range('a', 'z'), range('A', 'Z'), '_', '$');
const idCont = oneOf(range('a', 'z'), range('A', 'Z'), digit, '_', '$');
const lineTerminator = oneOf('\n', '\r', '\u2028', '\u2029');
const hspace = oneOf(' ', '\t');
const uEsc = altPattern(seq('\\u', repeat(hexDigit, 4, 4)), seq('\\u{', plus(hexDigit), '}'));
const identStart = altPattern(idStart, uEsc);
const identPart = altPattern(idCont, uEsc);
const numericTailGuard = notFollowedBy(oneOf(range('0', '9'), range('A', 'Z'), range('a', 'z'), '_', '$', '\\'));
const digits = seq(plus(digit), star(seq('_', plus(digit))));
const Ident = token(seq(identStart, star(identPart)), { identifier: true });
// Numeric tokens end with `(?![0-9A-Za-z_$\\])`: the spec rule that a numeric literal
// may not be immediately followed by an IdentifierStart or DecimalDigit. Without it,
// `0b2`/`0B1102110`/`0o81010` would munch a valid prefix (`0b1`, `0B110`) and leave the
// rest as a second token, so the file parses as two statements instead of being rejected.
// With it the bad literal matches no token and the lexer throws — the correct rejection.
// (ASCII IdentifierStart + `\` for `\u`-escapes; the lexer compiles patterns without the
// /u flag so \p{L} is unavailable, and every affected conformance case is ASCII.)
// Radix literals may carry a trailing `n` BigInt suffix (`0x5an`/`0o17n`/`0b101n`); the `n` is
// only valid here (radix forms have no fractional part), so it lives on each radix token rather
// than on the decimal `BigInt_` below. The shared `(?!IdentifierStart|DecimalDigit)` tail still
// rejects a stray trailing identifier (`0x5anabc`).
const HexNumber = token(seq('0', oneOf('x', 'X'), plus(hexDigit), star(seq('_', plus(hexDigit))), optPattern('n'), numericTailGuard), { scope: 'constant.numeric.hex' });
const OctalNumber = token(seq('0', oneOf('o', 'O'), plus(range('0', '7')), star(seq('_', plus(range('0', '7')))), optPattern('n'), numericTailGuard), { scope: 'constant.numeric.octal' });
const BinaryNumber = token(seq('0', oneOf('b', 'B'), plus(oneOf('0', '1')), star(seq('_', plus(oneOf('0', '1')))), optPattern('n'), numericTailGuard), { scope: 'constant.numeric.binary' });
// DecimalLiteral, including the leading-dot form (`.5`, `.0e1`): an integer part with optional
// fraction/exponent, OR a bare fraction `.digits` with optional exponent. Same trailing guard.
// Scope is set explicitly (not inferred from a `[0-9]`-leading pattern) because the leading-dot
// alternative makes the pattern start with `(?:` — gen-textmate's decimal-numeric detector keys on a
// `[0-9]`/`\d` prefix, so without this the token would lose its `constant.numeric` scope.
// `. DecimalDigits?`: a separator stands between digits only (`1._5`, `1.5_` match no token). Written
// with a lookahead rather than an optional DecimalDigits so the portable lexers never back up into it.
const fracTail = seq('.', notFollowedBy('_'), star(digit), star(seq('_', plus(digit))));
const expTail = seq(oneOf('e', 'E'), optPattern(oneOf('+', '-')), digits);
// A decimal integer part is a single `0` or a `[1-9]`-led run — a leading `0` followed by
// a digit (legacy octal `0123`, leading-zero decimal `09`) is NOT a decimal literal: with
// intPart='0', the trailing digit trips numericTailGuard so the token fails to match and
// the lexer rejects it (tsc's scanner behavior). fracTail/expTail keep `digits` (leading
// zeros legal there: `0.012`, `1e007`).
const intPart = altPattern('0', seq(range('1', '9'), star(digit), star(seq('_', plus(digit)))));
// Owed: `1._5` and `0.n` are `1.` / `0.` and an IdentifierStart (the longest literal, then no token),
// but the token backs up to `1` and `._5`; refusing that (an integer part not followed by `.`) makes
// those inputs lex errors, after which the portable lexers' relex of an edit before them (`.1._x`)
// keeps the error (portable-targets/edits: edited rejects, fresh accepts): the lexer lane's.
const Number_ = token(seq(altPattern(seq(intPart, optPattern(fracTail)), seq('.', digits)), optPattern(expTail), numericTailGuard), { scope: 'constant.numeric.decimal' });
// A decimal BigInt (`DecimalBigIntegerLiteral : 0 BigIntLiteralSuffix | NonZeroDigit DecimalDigits? BigIntLiteralSuffix`):
// the integer part of a decimal literal, so no leading zero (`010n`, `00n` match no token).
const BigInt_ = token(seq(intPart, 'n', numericTailGuard), { scope: 'constant.numeric.bigint' });
// Annex B.1.1 (JavaScript only: TypeScript's scanner refuses both): LegacyOctalIntegerLiteral
// `0 OctalDigit+` (`010` is 8) and NonOctalDecimalIntegerLiteral, a `0`-led digit run with an `8`
// or `9` (`08`, `019`), which is a DecimalIntegerLiteral and so takes a fraction and an exponent
// (`08.5`, `09e1`). Neither has separators. Both are refused in strict mode code (an early error).
const LegacyNumber = token(altPattern(
seq('0', star(range('0', '7')), oneOf('8', '9'), star(digit), optPattern(fracTail), optPattern(expTail), numericTailGuard),
seq('0', plus(range('0', '7')), numericTailGuard),
), { scope: 'constant.numeric.decimal' });
// A well-formed JS escape, used in the string-body pattern below. `\u`/`\x` must
// match their strict forms — a `\u{cp}` with cp ≤ 0x10FFFF, a 4-hex `\uXXXX`, or a
// 2-hex `\xXX` — while `\` + any *other* char (\n, \\, \q non-escape, line
// continuation) stays valid via `[^ux]`. A malformed `\u`/`\x` (e.g. `\u{110000}`,
// `\u{r}`, `\u{}`, `\u{67`) matches no escape, so the string matches no token and the
// lexer throws — TS's exact rejection. The in-range codepoint is `0*` leading zeros
// then 1–5 hex (0–0xFFFFF) or `10`+4 hex (0x100000–0x10FFFF).
const codePoint = seq(star('0'), altPattern(repeat(hexDigit, 1, 5), seq('10', repeat(hexDigit, 4, 4))));
const escape = seq('\\', altPattern(seq('u{', codePoint, '}'), seq('u', repeat(hexDigit, 4, 4)), seq('x', repeat(hexDigit, 2, 2)), '\r\n', seq('\r', notFollowedBy('\n')), noneOf('u', 'x', '\r')));
const highlightedEscape = seq('\\', altPattern(
oneOf('n', 'r', 't', 'b', 'f', 'v', '0', "'", '"', '\\'),
seq('x', repeat(hexDigit, 2, 2)),
seq('u', repeat(hexDigit, 4, 4)),
seq('u{', plus(hexDigit), '}'),
));
// DoubleStringCharacter / SingleStringCharacter: any code point but the quote, `\`, LF and CR (LS / PS
// are allowed since ES2019), or an escape; a LineContinuation is `\` and a LineTerminatorSequence,
// CR LF included (`\` CR is a continuation only where no LF follows: each escape ends in one place,
// which the portable lexers' scan needs). A JSX attribute value is its own token (JSXString), which may span lines.
const String_ = token(altPattern(seq('"', star(altPattern(noneOf('"', '\\', '\n', '\r'), escape)), '"'), seq("'", star(altPattern(noneOf("'", '\\', '\n', '\r'), escape)), "'")), {
string: true,
escape: highlightedEscape,
});
const Template = token(seq('`', star(altPattern(noneOf('`', '\\', '$'), seq('\\', noneOf(lineTerminator)), seq('$', notFollowedBy('{')))), '`'), {
escape: seq('\\', altPattern(
oneOf('n', 'r', 't', 'b', 'f', 'v', '0', "'", '"', '\\', '`', '$'),
seq('x', repeat(hexDigit, 2, 2)),
seq('u', repeat(hexDigit, 4, 4)),
seq('u{', plus(hexDigit), '}'),
)),
// Same well-formed-escape rule as strings; the lexer rejects a malformed `\u`/`\x`
// in an *untagged* template (`\u{110000}`, `\u{r}`), but allows it when tagged.
escapeValid: escape,
template: { open: '`', interpOpen: '${', interpClose: '}' },
});
const regexEscape = seq('\\', noneOf(lineTerminator));
const regexClassBody = star(altPattern(noneOf(']', '\\', lineTerminator), regexEscape));
// RegularExpressionChar; the FIRST char additionally excludes `*` (RegularExpressionFirstChar)
// so `/*` is never a regex start — it is a block-comment open, and an unterminated `/* … /`
// is a lexical error, NOT a regex literal. (A `*` anywhere after the first char stays legal:
// `/a*/`.) Body is one-or-more total, so `//` remains a LineComment as before.
const regexChar = altPattern(noneOf('/', '\\', '[', lineTerminator), regexEscape, seq('[', regexClassBody, ']'));
const regexFirstChar = altPattern(noneOf('/', '\\', '[', '*', lineTerminator), regexEscape, seq('[', regexClassBody, ']'));
const Regex_ = token(seq('/', regexFirstChar, star(regexChar), '/', star(identPart)), { // flags: maximal-munch any IdentifierPart run (tsc lexes flags leniently; validity is a checker rule)
regex: true,
regexContext: {
divisionAfterTypes: ['Ident', 'Number', 'LegacyNumber', 'HexNumber', 'OctalNumber', 'BinaryNumber', 'String', 'Template', 'BigInt', 'PrivateField'],
divisionAfterTexts: [')', ']', 'this', 'super', 'true', 'false', 'null', 'undefined'],
regexAfterTexts: ['in', 'of', 'instanceof', 'typeof', 'delete', 'void', 'await', 'yield', 'throw', 'return', 'case', 'do', 'else', 'new', 'default'],
// `kw ( … )` heads (control-flow): the closing `)` is a statement head, not a
// value, so `if (a) /re/` parses `/re/` as a regex rather than division.
regexAfterParenKeywords: ['if', 'while', 'for', 'with'],
// member accessors: after one, those keywords are property NAMES, so
// `obj.for(x) / y` stays a method call + division.
memberAccessTexts: ['.', '?.'],
// `!` is BOTH prefix logical-not and (in TS) postfix non-null. It is value-producing —
// so a following `/` is division (`x! / y`) and a following template is tagged — only in
// its postfix form (after a value); as prefix-not a following `/` is a regex (`!/re/`).
// Resolved per-occurrence from the preceding context (the lexer/highlighter check whether
// the `!` itself follows a value). In valid JS `!` is only ever prefix, so this is inert
// for JS (a `!` never follows a value) — it earns its keep in the shared TS layer. `++` / `--`
// are the same: postfix after a value (`a++ / 2` divides), prefix otherwise (`++/r/.lastIndex`).
postfixAfterValueTexts: ['!', '++', '--'],
},
});
// `@name`: the `@` of a decorator and its first IdentifierName, one token so the highlighters scope
// it as a decorator. It admits the `\u`-escape forms of `Ident`; a name with a character beyond ASCII
// (`@℘`, `@a℘`) is no Decorator token (the lookahead), so it lexes as `@` and an Ident, which reads
// it whole by the lexer's Unicode fallback. The dotted
// segments, the arguments and a `@` that trivia separates from its name (`@ a`) are the parser's
// (DecoratorExpr).
const Decorator = token(seq('@', identStart, star(identPart)), { scope: 'entity.name.function.decorator' });
// PrivateIdentifier: `#` + an IdentifierName, so it admits the same `\u`-escape forms as `Ident`
// (`#\u{6F}_`). A non-ASCII literal `#name` (`#℘`, `#ZWNJ`) is handled by the lexer's Unicode
// fallback, which recognises this token's leading `#` as a name prefix.
const PrivateField = token(seq('#', identStart, star(identPart)), { scope: 'variable.other.property', identifierPrefix: '#' });
const Shebang = token(seq(start(), '#!', star(noneOf(lineTerminator))), { skip: true, scope: 'comment.line.shebang' });
const JSDoc = token(seq('/**', notFollowedBy('/'), star(seq(notFollowedBy('*/'), anyChar()), { greedy: false }), '*/'), { skip: true, scope: 'comment.block.documentation', embed: 'jsdoc' });
const TripleSlash = token(seq('///', star(hspace), '<', star(noneOf(lineTerminator))), { skip: true, scope: 'comment.line.triple-slash' });
// SingleLineCommentChar / HashbangComment: any character but a LineTerminator (LF, CR, LS, PS).
const LineComment = token(seq('//', star(noneOf(lineTerminator))), { skip: true });
const BlockComment = token(seq('/*', star(seq(notFollowedBy('*/'), anyChar()), { greedy: false }), '*/'), { skip: true });
// The token consts, reserved-word guards, precedence ladder, and scope map are
// pure ECMAScript vocabulary — no rule wiring — so the TS grammar imports them from
// here and extends them rather than duplicating them.
export {
Shebang, JSDoc, TripleSlash, LineComment, BlockComment,
Ident, HexNumber, OctalNumber, BinaryNumber, BigInt_,
Number_, String_, Template, Regex_, Decorator, PrivateField,
};
// Where a statement may not be an expression statement (14.5, `ExpressionStatement : [lookahead ∉
// { {, function, async [no LineTerminator here] function, class, let [ }] Expression ;`): there
// `{` begins a block, `function` / `async function` / `class` a declaration, and `let [` a lexical
// declaration, so what an expression would read beyond them (`{}.x`, `class C {}.name`) is the
// next statement or no statement. A decorator begins a declaration too: the decorators proposal
// (ecma262 PR #2417) gives ClassDeclaration and ClassExpression the same `DecoratorList?` and adds
// `@` to the like lookahead of `export default`, and tsc's parseStatement routes `@` to
// parseDeclaration. Both grammars' expression statement is this one arm; an expression elsewhere
// (`({}).x`, `f({}.x)`, `x = @dec class {}`) is not restricted.
export const expressionStatement = (Expr: Element) => [
not(alt('{', 'function', 'class', '@', Decorator)), not(alt(['async', sameLine, 'function'], ['let', '['])),
field('first', Expr), many(field('comma', ','), field('rest', Expr)), asi(),
];
// An arrow's body (`ConciseBody : [lookahead ≠ {] ExpressionBody | { FunctionBody }`): after `=>`
// a `{` begins the function body, never an object literal, so `() => { b() {} }.x` has no parse.
export const conciseBody = (Block: Element, Expr: Element) => alt(arm('block', Block), arm('expression', not('{'), Expr));
// `export Declaration` / `export VariableStatement` (16.2.3 ExportDeclaration): what follows
// `export` is a declaration, never another statement, so `export if (x) {}`, `export x;`,
// `export ;`, `export let: x;` (a labelled statement), `export let.x = 1;` (an expression) have
// no parse and `export { a }` is the export list, not `export` and a block. A declaration is
// `Decl` (`$`) or one of the declaring productions themselves (`forms`: `var` / `let` / `const`,
// and in TypeScript `using`, which tsc's parser takes there), never a statement that merely
// begins with their word.
export const exportDeclaration = ($: Element, forms: Alternative[]) =>
arm('exportDeclaration', field('exportKeyword', 'export'), field('declaration', alt(arm('decl', field('decl', $)), ...forms)));
// VariableStatement / LexicalDeclaration (14.3): a VariableDeclarationList / BindingList has one
// or more declarations and no trailing comma, so `var;`, `const;` and `var a,` have no parse.
// `kinds` are the words (`var` is a Statement, `let` / `const` a Declaration); `Binding` is the
// grammar's.
export const variableDeclaration = (kinds: string[], Binding: Element) =>
[field('kind', alt(...kinds)), field('binding', Binding), many(field('bindingComma', ','), field('bindingRest', Binding)), asi()];
// A module specifier and its import attributes (16.2.2 `FromClause : from ModuleSpecifier`,
// `WithClause : with { } | with { WithEntries ,opt }`, `WithEntries : AttributeKey : StringLiteral`,
// `AttributeKey : IdentifierName | StringLiteral`): the specifier and every attribute value are
// string literals (`import x from y`, `with { type: 1 }` have no parse), and nothing restricts a
// line break before `with`. tsc's parser takes any expression for both, the legacy `assert` for
// `with`, and no `with` past a line break (docs/CORRECTNESS.md).
// tree-sitter renders no attributes: after a specifier, `with` would also begin a `with`
// statement past an inserted `;`, which a CFG without lookahead cannot tell apart.
const withEntry = (rest = '') => [field(`attributeKey${rest}`, alt(Ident, String_)), ':', field(`attributeValue${rest}`, String_)];
export const moduleSpecifier = [field('source', String_), treeSitterRelax(opt(field('withKeyword', 'with'), '{', opt(...withEntry(), many(',', ...withEntry('Rest')), opt(',')), field('attributesClose', '}')), [])];
// ── Always-reserved words ──
// The `Ident` token deliberately swallows keywords (they lex as identifiers), so
// every keyword can otherwise fall back to a bare identifier. These words are
// reserved in EVERY context (ECMAScript ReservedWord ∪ TS's always-reserved), so
// they are valid as an identifier NOWHERE — not as an expression, a shorthand
// property, or a binding name. `notReserved` is a zero-width guard placed before an
// identifier position to forbid exactly these. Excluded on purpose: contextual
// keywords (as/async/from/type/of/…) and strict-mode-only reserved words
// (let/static/implements/yield/await/…) — those ARE valid identifiers in some
// context a CFG can't detect (sloppy mode, non-generator/non-async), so forbidding
// them here would reject valid code (`var let = 1`, `function f(yield) {}`).
// NOT reservable: tsc's PARSER accepts await/yield (and let/static/…) as binding
// identifiers even inside an async/generator body — the "reserved word" rule there is
// a CHECKER diagnostic, not a parse error (`async function f(){ let await = 1 }`,
// `function* g(){ function yield(){} }` both parse). The [Await]/[Yield] reservation
// that IS a parse error lives at expression position (notReservedExpr), where `await`
// must be the operator and so needs an operand.
export const reservedWords = [
'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', 'default',
'delete', 'do', 'else', 'enum', 'export', 'extends', 'false', 'finally', 'for',
'function', 'if', 'import', 'in', 'instanceof', 'new', 'null', 'return', 'super',
'switch', 'this', 'throw', 'true', 'try', 'typeof', 'var', 'void', 'while', 'with',
];
export const notReserved = not(alt(...reservedWords));
// A BindingIdentifier: the name a binding introduces (a parameter, a variable, a destructured
// or rest name) is an identifier that is not a reserved word. This is the one definition every
// binding position spreads in; tsc's parser decides it once too (parseBindingIdentifier, which
// takes an identifier or any keyword after LastReservedWord, in every context), so contextual
// and strict-mode-only words stay names (`function f(yield, let, of) {}`).
export const bindingIdentifier = [notReserved, Ident];
// The same, as a function's or class's name: the child `name`.
export const bindingName = [notReserved, field('name', Ident)];
// ── The `for` statement (14.7.4 ForStatement, 14.7.5 ForInOfStatement) ──
// Both grammars build their ForBinding, ForHead and ForAwaitHead rules and their `for` statement
// from these, each with its own rules (TypeScript adds a binding's type annotation).
//
// A declaration in a head: `var`, `let` or `const` (ForBinding, a VariableDeclaration or
// LexicalBinding), or `using` / `await using` (explicit resource management, ForDeclaration[+Using]),
// then its bindings. A binding's initializer is Initializer[~In], so in `for (var a = 1 in xs)` the
// `in` is the head's. A `using` binding is a BindingIdentifier (ForBinding[~Pattern]), so `for (using
// [a] of b)` loops over `using[a]`, and there is no `using` for-in (that arm's ForDeclaration is
// [~Using]). `for (using of of …)` has no parse tree: the `using` declaration is suppressed by the
// spec's `[lookahead ≠ using of]` and `using` as an identifier then fails, so the guard is that
// triple; `for (using of ;…)` (a binding named `of`) and `for (await using of of …)` stay valid.
export const forBindingArms = (BindingPattern: Element, Expr: Element, annotation: Element[] = []) => [
[field('name', alt(bindingIdentifier, BindingPattern)), ...annotation, opt('=', exclude('in', field('value', Expr)))],
];
// `tails` are what may follow the first binding of a `var` / `let` / `const` declaration and of a
// `using` one: both grammars' heads parse it once and then branch.
const forDeclaration = (ForBinding: Element, tails: Alternative[], usingTails: Alternative[]) => alt(
[field('kind', alt('let', 'const', 'var')), field('binding', ForBinding), field('tail', alt(...tails))],
[not(['using', 'of', 'of']), field('kind', alt('using', ['await', sameLine, 'using'])), sameLine, not(alt('[', '{')), field('binding', ForBinding), field('tail', alt(...usingTails))],
);
// The head inside `for ( … )`:
// - `for ( [lookahead ≠ let [] Expression[~In]opt ; Expression opt ; Expression opt )` and the
// declared C-style head: a first expression or initializer reads no `in` operator, so `for (a =
// b in c;;)` has no parse (each comma operand is its own [~In] Pratt call);
// - `for ( [lookahead ≠ let [] LeftHandSideExpression in Expression )`: the left side reads no `in`
// either, so the `in` is the head's (`for (key in obj)`); the object is a full Expression;
// - `for ( [lookahead ∉ { let, async of }] LeftHandSideExpression of AssignmentExpression )`: no
// comma after `of`; `for (async of => {};;)` is a C-style head;
// - a declaration before `in` / `of` has one binding.
export const forHeadArms = (Expr: Element, ForBinding: Element) => {
const cTail = [';', opt(field('condition', Expr), many(field('conditionComma', ','), field('conditionRest', Expr))), ';', opt(field('increment', Expr), many(field('incrementComma', ','), field('incrementRest', Expr)))]; // `; cond ; update`
const cStyle = arm('classic', many(field('bindingComma', ','), field('bindingRest', ForBinding)), ...cTail);
const forIn = arm('in_', 'in', field('right', Expr), many(field('rightComma', ','), field('rightRest', Expr))), forOf = arm('of', 'of', field('right', Expr));
return [
arm('declaration', forDeclaration(ForBinding, [cStyle, forIn, forOf], [cStyle, forOf])),
arm('classic', not(['let', '[']), opt(exclude('in', field('initializer', Expr)), many(field('initializerComma', ','), exclude('in', field('initializerRest', Expr)))), ...cTail),
arm('in_', not(['let', '[']), exclude('in', field('left', Expr)), 'in', field('right', Expr), many(field('rightComma', ','), field('rightRest', Expr))),
arm('of', not(alt('let', ['async', 'of'])), exclude('in', field('left', Expr)), 'of', field('right', Expr)),
];
};
// The head inside `for await ( … )`: only the `of` forms, and its left side's lookahead is
// `[lookahead ≠ let]` alone, so `for await (async of x)` is a loop over `x`.
export const forAwaitHeadArms = (Expr: Element, ForBinding: Element) => [
arm('declaration', forDeclaration(ForBinding, [arm('of', 'of', field('right', Expr))], [arm('of', 'of', field('right', Expr))])),
arm('of', not('let'), exclude('in', field('left', Expr)), 'of', field('right', Expr)),
];
export const forStatement = ($: Element, ForHead: Element, ForAwaitHead: Element) =>
['for', alt(['(', field('head', ForHead), ')'], [field('awaitKeyword', 'await'), '(', field('head', ForAwaitHead), ')']), field('body', $)];
// The guard of the *expression* identifier (IdentifierReference, and an arrow's lone
// BindingIdentifier): `Identifier : IdentifierName but not ReservedWord`, so no reserved word is
// one. A reserved word with no expression form of its own is no expression (`in;`, `x =
// default`, `f(export)`, `const;`, `void ;`, a colon-less `case`, `x = var`); one that begins an
// expression of its own (`this`, `true` / `false` / `null`, `super.x`, `new X`) is that
// expression's arm alone, never also an identifier (no tie for declaration order to decide) and
// never an arrow's parameter (`true => 1`, `this => 1`, `super => 1`). `undefined` is no reserved
// word: an ordinary identifier. Reservable: it also refuses the context's operator words
// (`await` / `yield`). TypeScript's guard leaves `import` out: `import` with no or a spread
// argument (`import()`, `import(...a)`) is read through this fallback, as tsc's parser does in
// files it accepts. Owed: that `import` is tsc-parser tolerance, not the productions.
export const notReservedExpr = reservableNot(alt(...reservedWords.filter((w) => w !== 'import')));
// JavaScript's: `import` is an expression only as ImportCall and `import.meta` (13.3.10
// `ImportCall : import ( AssignmentExpression ,opt ) | import ( AssignmentExpression ,
// AssignmentExpression ,opt )`), so `import()`, `import(a, b, c)` and `import(...a)` have no parse.
const notReservedJsExpr = reservableNot(alt(...reservedWords));
// A computed member's index (13.3 `MemberExpression : MemberExpression [ Expression ]`, `SuperProperty
// : super [ Expression ]`, `OptionalChain : ?. [ Expression ]`): an Expression, so a comma list
// (`a[0, 1]`, `a?.[0, 1]`); each operand is its own AssignmentExpression call.
export const bracketIndex = (E: Element) => ['[', E, many(field('exprComma', ','), field('exprRest', E)), ']'];
// ImportCall's arguments (`( AssignmentExpression ,opt )`, `( AssignmentExpression ,
// AssignmentExpression ,opt )`): no spread, no empty list.
const importCallArgs = ($: Element) => ['(', not('...'), field('source', $), opt(',', opt(not('...'), field('options', $), opt(','))), ')'];
// ── Precedence ladder (shared ECMAScript operator precedence) ──
// Binding powers for the ALTERNATIVE-form LEDs (rule alternatives `[$, connector, …]`).
// The conditional sits between the assignment levels and `??` (its branches stay full
// assignment expressions); `in`/`instanceof` sit AT the relational level and left-chain
// their right operand like the ladder relationals they are.
export const jsLedPrecs = [
{ connector: '?', below: '??' },
{ connector: 'in', sameAs: '<', chainRhs: true },
{ connector: 'instanceof', sameAs: '<', chainRhs: true },
];
// The ladder, with the `await` operator as each grammar reads it: JavaScript's is gated (jsPrec),
// TypeScript's is tsc's parser's, an operator everywhere (ecmaPrec; the checker reports it outside
// an async function).
const ecmaPrecWith = (awaitOp: string | ReturnType<typeof when>) => [
// Assignment operators require a LeftHandSideExpression target (ECMAScript
// AssignmentTargetType): `-x = 1`, `++x = 1`, `x++ = 1` are syntax errors; `x = 1`,
// `x.y = 1`, `(x++) = 1` (a parenthesized cover) are fine. The logical assignments are three more
// AssignmentExpression arms (13.15 `LeftHandSideExpression &&= AssignmentExpression`, `||=`, `??=`):
// one level, so each operator's right operand is a full AssignmentExpression (`c ||= a = b`).
right(lhsAssignTarget('=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '>>>=', '&=', '|=', '^=', '??=', '||=', '&&=')),
left('??'),
left('||'),
left('&&'),
left('|'),
left('^'),
left('&'),
none('==', '!=', '===', '!=='),
none('<', '>', '<=', '>='),
left('<<', '>>', '>>>'),
left('+', '-'),
left('*', '/', '%'),
right(noUnaryLhs('**')), // `-x ** y` is a syntax error: a unary-prefix expr can't be a `**` LHS
right(prefix('!', '~', '+', '-', 'typeof', 'void', 'delete', awaitOp)),
// prefix `++`/`--` (update prefixes): the spec operand is a UnaryExpression
// (`UpdateExpression : ++ UnaryExpression`) whose AssignmentTargetType must be simple — an
// early error, like `a + b = c`, enforced by the same target check: `++-x`, `++x++`,
// `++ ++x` reject (tsc reads the operand as a LeftHandSideExpression).
right(prefixAssignTarget('++', '--')),
// postfix `++`/`--` operand IS a LeftHandSideExpression in the grammar
// (`UpdateExpression : LeftHandSideExpression [no LT] ++`), so `++x++`, `x++ ++` are genuine
// PRODUCTION-violations (operand `++x`/`x++` is an UpdateExpression, not a LHS) — no parse
// tree exists, so the CST producer correctly rejects them. (Asymmetric with the prefix above
// by the grammar's own slot types: prefix operand = UnaryExpression, postfix operand = LHS.)
left(postfixAssignTarget('++', '--')),
];
export const ecmaPrec = ecmaPrecWith('await');
// `await` is the operator (AwaitExpression) only where [+Await] holds; elsewhere it is an
// IdentifierReference (`await(x)` in a script is a call). `~syncFn` and not `+await`: the top
// level reads both goals until the goals land (grammar-utils.ts CTX_PARAMS, a transition).
const jsPrec = ecmaPrecWith(when('~syncFn', 'await'));
// ── Decorators ──
// Stage-3 / JS-real decorators: `@dec` and `@dec(args)`. The TS-only
// `@dec<T>(args)` type-argument form is dropped.
// The decorators proposal's productions: `Decorator : @ DecoratorMemberExpression | @
// DecoratorParenthesizedExpression | @ DecoratorCallExpression`, `DecoratorMemberExpression :
// IdentifierReference | DecoratorMemberExpression . IdentifierName | DecoratorMemberExpression .
// PrivateIdentifier`, `DecoratorCallExpression : DecoratorMemberExpression Arguments`,
// `DecoratorParenthesizedExpression : ( Expression )`. The head is the Decorator token (`@a`), or a
// `@` and an Ident when trivia separates them (`@ a`); the dotted segments are tokens of their own
// (`@a . b`, `@a.℘`; `@a.` has no parse).
const decoratorHead = alt(Decorator, ['@', notReservedJsExpr, Ident]);
const DecoratorExpr = rule(() => [
['@', '(', Expr, many(',', Expr), ')'], // @(expr)
[decoratorHead, many('.', alt(Ident, PrivateField)), opt('(', sep(Expr, ','), ')')], // @dec | @a.b.#c | @dec(args)
]);
// ── Expressions ──
const Prop = rule(() => {
// ( … ) { … }, params+body routed to a [Await]/[Yield] family (see memTail); the
// MemberName stays outside it (a computed key inherits the enclosing context).
const propTail = (ctx: (...items: Element[]) => Element) => [field('paren', '('), sep(ctx(field('parameter', Param)), ','), ')', ctx(field('body', Block))];
return [
arm('spread', '...', field('argument', Expr)),
// accessor (get/set) — get/set bodies are plain (reset)
arm('accessor', field('kind', alt('get', 'set')), field('key', MemberName), field('paren', '('), opt(sep(resetCtx(field('parameter', Param)), ',')), ')', resetCtx(field('body', Block))),
// method, 4-way split on async × generator (each routes params+body to its family)
arm('asyncGeneratorMethod', 'async', '*', field('key', MemberName), ...propTail(asyncGenCtx)),
arm('asyncMethod', 'async', field('key', MemberName), ...propTail(awaitCtx)),
arm('generatorMethod', '*', field('key', MemberName), ...propTail(yieldCtx)),
arm('method', field('key', MemberName), ...propTail(resetCtx)),
// value property — any member name incl computed `[e]: v` (MemberName covers `[Expr]`)
arm('init', field('key', MemberName), ':', field('value', Expr)),
// computed comma list (lenient): `[a, b]: v`. One expression in the brackets is the
// value property above (`[e]` is a MemberName), so the list has at least one comma.
arm('computedList', '[', Expr, many1(',', Expr), ']', ':', Expr),
// shorthand (Ident only): x | x = v — a reserved word here is invalid
// (`var v = { class }`); a reserved word as a property KEY (`{ class: 1 }`) is fine,
// already handled by the `[MemberName, ':', Expr]` branch above.
arm('shorthand', notReserved, field('name', Ident), field('initializer', alt(arm('assign', '=', field('value', Expr)), arm('none')))),
];
});
// A class heritage (`extends LeftHandSideExpression`) and a `new` callee (a MemberExpression)
// are ECMAScript productions, so their forms are stated here once and TypeScript's rules are
// built from them. A rule binds the rules it names when it is defined, so each grammar builds
// its own rule from these arms with its own `Expr` (and passes the forms its type layer adds).
//
// Heritage: `extends` takes a LeftHandSideExpression (15.7 `ClassHeritage : extends
// LeftHandSideExpression`), not an operator expression: a NewExpression or a CallExpression or an
// OptionalExpression. A NewExpression is a MemberExpression or `new NewExpression`, which is the
// grammar's `new` callee rule (`NewTarget`: every primary expression but a class expression,
// `super.x`, `new.target`, `import.meta`, the member and tagged-template forms, and `new` with or
// without Arguments: `extends function(){}`, `extends new B`, `extends [].x`, `extends {}`); a
// class expression (`classExpression`, which carries the grammar's own class body) is the rule's
// own arm, as it is the expression rule's. After it come the call tail and, past a call, the
// member, optional-chain and tagged-template tails (`extends f().x`, `extends a?.b`). A literal
// base parses; rejecting it is a checker's job. An arrow, a conditional or a binary operator
// is no LeftHandSideExpression (`extends a ? b : c {}` has no parse).
export const classHeritageArms = ($: Element, Expr: Element, NewTarget: Element, classExpression: Alternative, typeArgs: Alternative[] = []) => [
NewTarget,
classExpression,
[$, '.', alt(Ident, PrivateField)],
[$, '?.', alt(Ident, PrivateField, ['(', sep(Expr, ','), ')'], ['[', Expr, ']'], Template)], // `?.[` one expression: see Expr's
[$, ...bracketIndex(Expr)],
...typeArgs,
[$, '(', sep(Expr, ','), ')'],
[$, Template],
];
// The callee of `new` is a MemberExpression, never a CallExpression (`new a.b()` constructs
// `a.b`), so it has its own rule: Expr's call LED must not reach it. A template is part of it
// twice over — `PrimaryExpression : TemplateLiteral` and `MemberExpression : MemberExpression
// TemplateLiteral` — so in `new tag`x`()` the tagged template is the callee and `( )` are the
// `new` Arguments. A `new` with its Arguments is a MemberExpression too (`MemberExpression :
// new MemberExpression Arguments`), and one without is a NewExpression, so `new new Foo()()` and
// `new new Foo` chain: `newArgs` is the inner `new`'s optional Arguments, `taggedTemplate` any
// further tagged-template form of the grammar.
//
// The other MemberExpression forms (ECMA-262 13.3), each as the expression NUD / LED writes it:
// - PrimaryExpression: `this`, the literals (`new null()` and `new 1` are syntax; that they throw is
// run time), an identifier that is not a reserved word (`identGuard`, the expression NUD's: `new
// if()` has no derivation, and `new this` is ThisExpression, not an identifier), array and
// object literals (`Prop`, the grammar's property rule), function expressions (`fnExprArms`, the
// grammar's four forms), a template, a parenthesised expression. A class expression is the
// expression rule's own `new class …` arms.
// - SuperProperty (`new super.x()`, `new super[x]`), MetaProperty (`new new.target()`, `new
// import.meta.x()`), and the member forms, a private name included (`new X.#f()`).
export const newTargetArms = ($: Element, Expr: Element, newArgs: Element, { identGuard, Prop, fnExprArms, taggedTemplate = [], literals = [] }: { identGuard: Element; Prop: Element; fnExprArms: Alternative[]; taggedTemplate?: Alternative[]; literals?: Element[] }) => [
'true', 'false', 'null', 'this',
arm('ident', identGuard, Ident),
Number_, HexNumber, OctalNumber, BinaryNumber, BigInt_, String_, Regex_, ...literals,
arm('superMember', field('superKeyword', 'super'), alt(['.', Ident], bracketIndex(Expr))),
arm('newDotTarget', field('newKeyword', 'new'), '.', field('targetKeyword', 'target')),
arm('importMeta', 'import', '.', 'meta'),
['new', not('<'), $, newArgs],
Template,
arm('array', field('bracket', '['), opt(field('element', Expr)), many(field('comma', ','), opt(field('elementRest', Expr))), ']'),
arm('object', '{', sep(field('property', Prop), ','), '}'),
...fnExprArms,
[$, '.', alt(Ident, PrivateField)],
[$, ...bracketIndex(Expr)],
[$, Template],
...taggedTemplate,
['(', field('first', Expr), many(field('comma', ','), field('rest', Expr)), ')'],
];
const ClassHeritage = rule($ => classHeritageArms($, Expr, NewTarget,
['class', opt(...bindingName), opt('extends', field('superClass', $)), field('bodyOpen', '{'), many(field('member', ClassMember)), field('bodyClose', '}')]));
const NewTarget = rule($ => newTargetArms($, Expr, opt('(', sep(Expr, ','), ')'),
{ identGuard: notReservedJsExpr, Prop, fnExprArms: fnArms([opt(...bindingName)], Block), literals: [LegacyNumber] }));
const Expr = rule($ => [
// The keyword-valued literals and `this` (reserved words, so the identifier below refuses
// them: each is its own arm alone).
'true', 'false', 'null', 'this',
// `super` is a CONSTRAINED primary (mirrors tsc's parseSuperExpression): MUST be
// immediately followed by a call `(args)`, member `.name`/`.#priv`, or element `[expr]`.
[field('superKeyword', 'super'), alt(['(', sep($, ','), ')'], ['.', alt(Ident, PrivateField)], bracketIndex($))],
// A standalone identifier expression (IdentifierReference): never a reserved word (see
// notReservedExpr), so `catch(x){}` with no `try`, `void ;` and `throw ;` are no expression.
[notReservedJsExpr, Ident],
Number_,
String_,
Template,
Regex_,
[$, op, $],
[prefix, $],
// Postfix `++`/`--` is a RESTRICTED production (`LeftHandSideExpression [no LineTerminator
// here] ++`): after a line break the operator is not this operand's — `x\n++\ny` is
// `x; ++y`, and `x\n++\n++\ny` is `x; ++ ++y` (rejected by the prefix target check).
[$, sameLine, postfix],
['...', $],
[$, '(', sep($, ','), ')'],
[$, '.', alt(Ident, PrivateField)],
// optional chaining: ?.x | ?.#x | ?.(args) | ?.[i] | ?.`…`. A private member `?.#x` IS
// valid current ECMAScript (V8 + Babel accept it; tsc's lone parse rejection is a bug being
// removed in TS#60263) — so PrivateField stays. The CST producer models the syntax; it does
// not adjudicate tsc-only restrictions.
// Owed: `a?.[0, 1]` (OptionalChain `?. [ Expression ]`): TypeScript's v1 ESTree shape
// (test/fixtures/shape-typescript.ts estreeOptionalChain and its Rust stream type) tells this branch
// from a call by its kids, so both grammars keep one expression here until that shape names it.
[$, '?.', alt(Ident, PrivateField, ['(', sep($, ','), ')'], ['[', $, ']'], Template)],
[$, ...bracketIndex($)],
[$, '?', field('then', $), ':', field('otherwise', $)],
[$, 'instanceof', $],
[$, 'in', $],
[$, Template],
// `new.target` meta-property — the only `new` form not followed by a target; matched by a
// dedicated arm (NOT the bare identifier nud, which excludes `new`) so a failed `new T` arm
// can't slide `new` in as an Ident (`new <T>Foo()` → the comparison `(new < T) > Foo()`).
arm('newDotTarget', field('newKeyword', 'new'), '.', field('targetKeyword', 'target')),
// new T | new T(args). An optional chain may NOT follow a bare `new` (no Arguments): a
// NewExpression is not a valid `?.` base, so `new a?.b` / `new class{}?.x` have no parse tree
// (tsc + V8 + babel all reject). `not('?.')` guards the no-call exit; `new a()?.b` chains via
// the outer `?.` LED unchanged.
arm('construct', 'new', not('<'), NewTarget, alt(['(', sep($, ','), ')'], not('?.'))),
arm('newNamedClass', 'new', field('classKeyword', 'class'), ...bindingName, opt('extends', field('superClass', ClassHeritage)), field('bodyOpen', '{'), many(field('member', ClassMember)), field('bodyClose', '}'), field('newArguments', alt(arm('paren', '(', sep(field('argument', $), ','), ')'), arm('none', not('?.'))))),
arm('newAnonymousClass', 'new', field('classKeyword', 'class'), opt('extends', field('superClass', ClassHeritage)), field('bodyOpen', '{'), many(field('member', ClassMember)), field('bodyClose', '}'), field('newArguments', alt(arm('paren', '(', sep(field('argument', $), ','), ')'), arm('none', not('?.'))))),
// array literal `[a, , ...b,]`: an element or a hole, then `,` and an element or a hole, the
// ArrayBindingPattern's form. Each element is parsed once: `many(opt($), ','), opt($)` parsed
// the last one in the repetition, found no `,`, and parsed it again (twice per nesting level).
arm('array', field('bracket', '['), opt(field('element', $)), many(field('comma', ','), opt(field('elementRest', $))), ']'),
arm('object', '{', sep(field('property', Prop), ','), '}'),
// Arrow functions, async/non-async SPLIT so the [Await] grammar parameter can route
// each arm's params + body to the right context word (awaitCtx / resetCtx): an async
// arrow's params and body are await-context (`async (a = await) =>` rejects — await
// needs an operand), a plain arrow's body resets to none.
// capExpr('?'): an ArrowFunction is the LOWEST-precedence AssignmentExpression — it can be
// neither the operand of a binary/logical/conditional operator nor an assignment target, so
// each arm is capped BELOW the conditional `?`: it parses only at an assignment-or-looser
// minBp and, once parsed, admits no led (`() => {} || a` rejects, NOT `(() => {}) || a`). A
// `||`/`?:` INSIDE an expression body (`() => a || b`) is unaffected — parsed by the body `$`.
// The body is a ConciseBody (`conciseBody`): `() => {}` is a block body, not an object literal
// that absorbs a trailing `|| a` / `.x`, also where the block does not parse.
arm('asyncArrow', capExpr('?', field('asyncKeyword', 'async'), '(', sep(awaitCtx(field('parameter', Param)), ','), ')', field('arrow', '=>'), awaitCtx(field('body', conciseBody(Block, $))))),
arm('arrow', capExpr('?', '(', sep(field('parameter', Param), ','), ')', field('arrow', '=>'), resetCtx(field('body', conciseBody(Block, $))))),
// async arrow with a BARE parameter: `async err => …` (ES2017). `async` and the
// parameter must share a line (`async\nx => …` is `async;` then a plain arrow —
// the spec's [no LineTerminator here] between async and the binding identifier).
arm('asyncArrowIdentifier', capExpr('?', field('asyncKeyword', 'async'), sameLine, awaitCtx(notReservedJsExpr, field('parameter', Ident)), field('arrow', '=>'), awaitCtx(field('body', conciseBody(Block, $))))),
arm('arrowIdentifier', capExpr('?', notReservedJsExpr, field('parameter', Ident), field('arrow', '=>'), resetCtx(field('body', conciseBody(Block, $))))),
// YieldExpression is an AssignmentExpression[+Yield] only: elsewhere `yield` is an identifier
// (`iter = yield();` calls it; `yield * x = 1` multiplies, then fails as an assignment target).
arm('yield_', capExpr('?', when('+yield', 'yield', alt(arm('star', sameLine, '*', $), arm('expr', opt(sameLine, $)))))), // yield e | yield* e (delegate) | yield
['(', field('first', $), many(field('comma', ','), field('rest', $)), ')'],
// ImportCall: one or two AssignmentExpressions, no spread (the `...` arm is Arguments'), and ImportMeta.
// A phase import call (import defer and source phase imports, Stage 3: `ImportCall : import .
// ImportPhase ( AssignmentExpression ,opt )`, and with options as ImportCall) takes the same
// arguments; `import.defer` / `import.source` are no expression of their own (`typeof
// import.source`, `new import.defer(x)` have no parse). After `.` one token decides the form.
arm('import_', field('importKeyword', 'import'), field('form', alt(
arm('call', ...importCallArgs($)),
arm('dot', '.', field('property', alt(
arm('meta', field('metaKeyword', 'meta')),
arm('phaseCall', field('phase', alt('defer', 'source')), ...importCallArgs($)),
))),
))),
PrivateField,
HexNumber, OctalNumber, BinaryNumber, BigInt_, LegacyNumber,
// function expression, 4-way split on async × generator (see fnArms).
...fnArms([opt(...bindingName)], Block),
// named vs anonymous kept separate (greedy opt(Ident) would eat a leading
// `extends`). A ClassExpression takes a DecoratorList, as a ClassDeclaration does: any number
// of decorators (`x = @a @b class {}`).
arm('namedClass', many(field('decorator', DecoratorExpr)), 'class', ...bindingName, opt('extends', field('superClass', ClassHeritage)), field('bodyOpen', '{'), many(field('member', ClassMember)), field('bodyClose', '}')),
arm('anonymousClass', many(field('decorator', DecoratorExpr)), 'class', opt('extends', field('superClass', ClassHeritage)), field('bodyOpen', '{'), many(field('member', ClassMember)), field('bodyClose', '}')),
]);
// ── Statements ──
const Block = rule(() => [
['{', many(Stmt), '}'],
]);
// ── Destructuring Patterns ──
const BindingProperty = rule(() => [
// `name: elem` — the KEY is a PropertyName, so a reserved word is allowed here
// (`{ while: y }`); the bound name inside `elem` is guarded by BindingElement.
arm('identPair', field('key', Ident), ':', field('value', BindingElement)),
// shorthand `a` / shorthand-with-default `a = 1` — the name is a BindingIdentifier,
// so a reserved word is invalid (`{ while }`, `{ class }`).
arm('shorthand', ...bindingName, opt('=', field('value', Expr))),
arm('pair', field('key', alt(String_, Number_, HexNumber, OctalNumber, BinaryNumber, BigInt_, LegacyNumber, arm('computed', field('bracket', '['), field('expr', Expr), field('bracketClose', ']')))), ':', field('value', BindingElement)), // "s"/0/0x1/1n/[e]: elem (a LiteralPropertyName or a ComputedPropertyName)
arm('rest', field('ellipsis', '...'), field('argument', alt(bindingIdentifier, BindingPattern))), // ...rest | ...{ a }
]);
const BindingElement = rule(() => [
[field('name', alt(bindingIdentifier, BindingPattern)), opt('=', field('value', Expr))], // a | { a } (optionally = default)
]);
const ArrayBindingElement = rule(() => [
BindingElement,
arm('rest', field('ellipsis', '...'), field('argument', alt(bindingIdentifier, BindingPattern))), // [...rest] | [...{ a }]
]);
const BindingPattern = rule(() => [
arm('object', '{', sep(field('property', BindingProperty), ','), '}'), // { a, b: c, ...rest }
// [a, , b, ...rest]: a hole is an absent element. The `[` and each `,` are named: a hole's
// position is the end of the token before it (tsc's OmittedExpression).
arm('array', field('bracket', '['), opt(field('element', ArrayBindingElement)), many(field('comma', ','), opt(field('elementRest', ArrayBindingElement))), ']'),
]);
// ── Bindings & Parameters ──
const Binding = rule(() => [
[field('name', alt(bindingIdentifier, BindingPattern)), opt('=', field('value', Expr))],
]);
// A binding in a for-head (`forBindingArms`): Binding with a no-`in` initializer.
const ForBinding = rule(() => forBindingArms(BindingPattern, Expr));
// A parameter (FormalParameter) is a binding element: its name is a BindingIdentifier, so no
// reserved word names a parameter (`function f(in) {}`, `(class) => 1`), `this` included (a
// `this` parameter is TypeScript's).
const Param = rule(() => {
const body = alt(
arm('single', field('name', alt(bindingIdentifier, BindingPattern)), opt('=', field('value', Expr))),
arm('rest', field('ellipsis', '...'), field('name', alt(bindingIdentifier, BindingPattern)), opt('=', field('value', Expr))), // rest (an initializer is a CHECKER error in tsc, not a parse error)
);
return [
arm('plain', opt(field('decorator', DecoratorExpr)), field('binding', body)),
];
});
const ForHead = rule(() => forHeadArms(Expr, ForBinding));
const ForAwaitHead = rule(() => forAwaitHeadArms(Expr, ForBinding));
const SwitchCase = rule(() => [
['case', Expr, many(',', Expr), ':'],
['default', ':'],
Stmt,
]);
// A catch clause's parameter (14.15 `CatchParameter : BindingIdentifier | BindingPattern`), not
// a FormalParameter: no initializer, rest or decorator (`catch (e = 1)`, `catch (...e)`, `catch
// (@d e)` have no parse).
const CatchParameter = rule(() => [
bindingIdentifier,
BindingPattern,
]);
// Statement (14): what a sub-statement position holds (the body of `if` / `else`, of a loop, of
// `with`, after a label), and one kind of StatementListItem (`Stmt`). It holds no Declaration:
// there `let` is the identifier, so `if (f) let \n{}` is the expression statement `let` (ended by
// ASI) and then a block, and `if (x) let y = 1;`, `while (x) class C {}` have no parse. The one
// declaration a clause takes is a plain FunctionDeclaration: after a label (LabelledItem) and, in
// an `if` clause, by Annex B (B.3.3; sloppy mode only, and strictness is not tracked here, so it
// is accepted everywhere); no generator, async function or class (`a: function* g() {}`).
const clause = ($: Element) => alt(arm('statement', $), fnArms(bindingName, Block)[0]);
const Statement = rule($ => [
Block,
// A VariableStatement; `let;` is the expression statement of the identifier `let` (sloppy mode).
arm('variable', ...variableDeclaration(['var'], Binding)),
['if', '(', field('condition', Expr), many(field('conditionComma', ','), field('conditionRest', Expr)), ')', field('consequence', clause($)), opt('else', field('alternative', clause($)))],
forStatement($, ForHead, ForAwaitHead),
['while', '(', field('condition', Expr), many(field('conditionComma', ','), field('conditionRest', Expr)), ')', field('body', $)],
// A do-while's `;` is optional even on its line (12.10.1: a `;` is inserted after the `)`
// that ends it), so `do x; while (y) z` is two statements.
['do', field('body', $), 'while', '(', field('condition', Expr), many(field('conditionComma', ','), field('conditionRest', Expr)), ')', opt(';')],
['switch', '(', Expr, many(',', Expr), ')', '{', many(SwitchCase), '}'],
// The expression is a RESTRICTED production (`return [no LineTerminator here] Expression`):
// after a line break it starts the next statement, so `return` ⏎ `var b` is two.
['return', opt(sameLine, field('argument', Expr), many(field('argumentComma', ','), field('argumentRest', Expr))), asi()],
['throw', field('argument', Expr), many(field('argumentComma', ','), field('argumentRest', Expr)), asi()],
// The label is a RESTRICTED production (`break [no LineTerminator here] Label`)
// and a label can't be a reserved word — without both, `break` ⏎ `case "X":`
// inside a switch eats `case` as the label and the whole switch cascades.
['break', opt(sameLine, notReserved, field('label', Ident)), asi()],
['continue', opt(sameLine, notReserved, field('label', Ident)), asi()],
// TryStatement (14.15): a Catch, a Finally, or both, so `try {}` has no parse.
['try', field('body', Block), not(not(alt('catch', 'finally'))), opt('catch', opt('(', field('parameter', CatchParameter), ')'), field('handler', Block)), opt('finally', field('finalizer', Block))],
arm('labelled', notReserved, field('label', Ident), ':', field('item', clause($))), // LabelledItem
arm('empty', ';'),
['debugger', asi()],
// `with ( Expression ) Statement` (14.11): the object is an Expression, a comma list included.
['with', '(', field('object', Expr), many(field('objectComma', ','), field('objectRest', Expr)), ')', field('body', $)],
// Not after a start that begins a block or a declaration (`expressionStatement`): without the
// lookahead, longest match lets the expression reading win wherever a tail makes it longer
// (`{} .b`, `function f(){}\n(g)()`, `@dec class {}.x`).
expressionStatement(Expr),
]);
// StatementListItem (14.2: `Statement | Declaration`): an element of a block's, a case clause's
// or the program's list. The Declarations a list adds to the Statements: a LexicalDeclaration
// (`let` / `const`, and `using` / `await using` from explicit resource management) and `Decl`
// (functions, classes, and the module items). Where both readings start with `let`, the longer
// is the derivation (`let\nx = 1` declares; `let;` is the identifier).
const Stmt = rule(() => [
Statement,
arm('variable', ...variableDeclaration(['let', 'const'], Binding)),
arm('using', opt(field('awaitKeyword', 'await'), sameLine), field('usingKeyword', 'using'), sameLine, field('binding', Binding), many(field('bindingComma', ','), field('bindingRest', Binding)), asi()),
Decl,
]);
// ── Declarations ──
const MemberName = rule(() => [
Ident,
PrivateField,
String_,
Number_,
HexNumber,
OctalNumber,
BinaryNumber,
BigInt_,
LegacyNumber,
arm('computed', field('bracket', '['), field('expr', Expr), field('bracketClose', ']')),
]);
// ── Member words ──
// A word that begins a member production (`static`, `async`, `accessor`, `get`, `set`, and
// TypeScript's modifiers, which build on this) is that keyword only where the start of a member
// follows it; otherwise it is the member's NAME (`static() {}`, `get = 1`, `static static;`,
// `@d accessor;`). The two readings part at that one token: the keyword reading goes on with a
// ClassElementName (an identifier name, a string, a number, a `#name`, `[`) or a `*`, the name
// reading with anything else (`(`, `=`, `;`, `}`, or, past a line break, the next member). What
// differs per word is one further condition, `memberWordRules`:
// static none: `static [ClassElementName | * | {]`, on any line (`static⏎x` is a
// static `x`; the static block `static {` is its own production)
// get / set the start is not `*` (`get ClassElementName`, on any line: `get⏎x() {}` is
// a getter, `get⏎*x() {}` a field `get` and a generator)
// every other the start is on the word's own line (`async [no LineTerminator here]`,
// `accessor [no LineTerminator here]`: `async⏎x() {}` is a field `async`)
// A word in no production takes a further token only where its own arm reads one (`accessor`
// a ClassElementName, no `*`), so the keyword reading fails there and so does the name reading,
// which cannot take a same-line member start either.
export const memberStart = alt(Ident, PrivateField, String_, Number_, HexNumber, OctalNumber, BinaryNumber, BigInt_, '[', '*');
export const memberAhead = not(not(memberStart));
export const memberWordRules: Record<string, Element[]> = { static: [], get: [not('*')], set: [not('*')] };
// `ahead` is the follower test; TypeScript's parameter modifiers pass their own (a parameter may
// also start with `{` or `...`).
export const memberWord = (words: string[], rules: Record<string, Element[]>, ahead: Element = memberAhead) => {
const sameLineWords = words.filter(w => !(w in rules));
return alt([
alt(
...words.filter(w => w in rules).map(w => [w, ...rules[w]]),
...(sameLineWords.length ? [[alt(...sameLineWords), sameLine]] : []),
),
ahead,
]);
};
const staticWord = memberWord(['static'], memberWordRules);
const asyncWord = memberWord(['async'], memberWordRules);
const accessorWord = memberWord(['accessor'], memberWordRules);
const getSetWord = memberWord(['get', 'set'], memberWordRules);
// Class member ( params ) body, with params+body routed to a [Await]/[Yield] family:
// plain methods reset (a method body has its OWN, non-inherited context — the spec's
// implicit function boundary), generators yield, async await, async-generators both.
// The MemberName stays OUTSIDE the family: a computed key `[e]` is evaluated in the
// ENCLOSING context, so it must inherit, not reset.
// A method, accessor or constructor has a body (MethodDefinition), after which nothing is
// needed: a `;` there is an empty class element of its own. A field ends as a statement does.
const memTail = (ctx: (...items: Element[]) => Element) => [field('paren', '('), sep(ctx(field('parameter', Param)), ','), ')', ctx(field('body', Block))];
// A class element (15.7 ClassElement, with the decorators proposal's DecoratorList and
// `accessor`): `;`, a static block, or a member's decorators, at most one `static`, and then one
// production, each word of which is a keyword only where `memberWord` says so. Inner alt() is
// first-match; its branches part at their first token (the word, `*`, or a name), so the first
// that matches is the only one. A member with no prefix whose name is the identifier
// `constructor` is the constructor (its keyword leaf); with a prefix it is a method named
// `constructor`.
const memberKinds = [
arm('accessorField', field('accessorWord', accessorWord), field('key', MemberName), opt('=', resetCtx(field('value', Expr))), asi()),
arm('async', field('asyncWord', asyncWord), field('form', alt(
arm('generatorMethod', field('asterisk', '*'), field('key', MemberName), ...memTail(asyncGenCtx)),
arm('method', field('key', MemberName), ...memTail(awaitCtx)),
))),
arm('generatorMethod', field('asterisk', '*'), field('key', MemberName), ...memTail(yieldCtx)),
arm('accessor', field('kind', getSetWord), field('key', MemberName), field('paren', '('), opt(sep(resetCtx(field('parameter', Param)), ',')), ')', resetCtx(field('body', Block))),
// Where `get` / `set` is the accessor keyword it is no name (`get⏎x;` has no derivation; the
// same-line words cannot end a field before a member start anyway).
arm('named', not(getSetWord), field('key', MemberName), field('tail', alt(
arm('method', ...memTail(resetCtx)), // method (requires `(`)
// field; it ends as a statement does
arm('field', opt('=', resetCtx(field('value', Expr))), asi()),
))),
];
const ClassMember = rule(() => [
arm('empty', ';'), // SemicolonClassElement: `class C { ; }`
arm('staticBlock', 'static', awaitCtx(field('body', Block))), // ClassStaticBlock: the body is [+Await] (await reserved)
arm('member', field('form', alt(
arm('constructor', field('constructorKeyword', 'constructor'), ...memTail(resetCtx)),
arm('element', many(field('decorator', DecoratorExpr)), opt(field('staticWord', staticWord)), field('member', alt(...memberKinds))),
))),
]);
const ImportSpecifier = rule(() => [
// The spec's two arms: ModuleExportName `as` ImportedBinding | ImportedBinding. The local name
// is a BindingIdentifier; the name read from the module, before `as`, is an IdentifierName:
// `import { if as x }`, not `import { if }` / `{ x as if }`.
arm('renamed', field('imported', Ident), 'as', notReserved, field('local', Ident)),
arm('plain', notReserved, field('local', Ident)),
// arbitrary module namespace identifier (ES2022): `import { "str" as x }` — the
// string form requires the rename (the local binding must be an identifier).
arm('stringRenamed', field('imported', String_), 'as', notReserved, field('local', Ident)),
]);
// Export specifiers are WIDER than import ones: a ModuleExportName (identifier or
// string) is valid on BOTH sides and may stand alone (`export { x as "s" }`,
// `export { "a" as "b" } from "m"`).
const ExportSpecifier = rule(() => [
arm('specifier', field('local', alt(Ident, String_)), opt('as', field('exported', alt(Ident, String_)))),
]);
// `{ a, b as c }` (NamedImports) and `* as ns` (NameSpaceImport), alone or after a default binding.
const namedImports = () => [field('brace', '{'), sep(field('specifier', ImportSpecifier), ','), field('braceClose', '}')];
const namespaceImport = () => [field('star', '*'), 'as', notReserved, field('namespace', Ident)];
const ImportClause = rule(() => [
// import defer (Stage 3): `import defer NameSpaceImport FromClause`. `defer` is the phase only
// before `* as`; elsewhere it is a name (`import defer from "m"`, `import defer, { a } from "m"`).
arm('deferNamespace', field('deferKeyword', 'defer'), ...namespaceImport()),
// default import, optionally followed by named `{…}` or namespace `* as x`
arm('defaultBinding', notReserved, field('name', Ident), opt(',', field('bindings', alt(arm('named', ...namedImports()), arm('namespace', ...namespaceImport()))))),
arm('named', ...namedImports()),
arm('namespace', ...namespaceImport()),
]);
// A class declaration from its `class` keyword on. Decl writes it once bare and once after a
// declaration's decorators (below); gen-textmate expands it to recover the `class Ident … { … }` shape
// for highlighting.
const classDecl = ['class', ...bindingName, opt('extends', field('superClass', ClassHeritage)), field('bodyOpen', '{'), many(field('member', ClassMember)), field('bodyClose', '}')];
const Decl = rule($ => [
// Function declarations live here (not in Stmt) so that at statement level a
// leading `function` is preferred as a declaration over an IIFE expression-
// statement: Program tries Decl before Stmt, so `function f(){}\n()=>{}` parses
// as a declaration + arrow rather than longest-matching `function f(){}()` (IIFE).
...fnArms(bindingName, Block),
classDecl, // a class with no decorator; with them, the decorator arm below
exportDeclaration($, [arm('variable', ...variableDeclaration(['var', 'let', 'const'], Binding))]),
// A declaration's decorators, parsed ONCE, then what they decorate. Under the decorators
// proposal a DecoratorList belongs to a class: ClassDeclaration takes it (`@dec class C {}` is
// one declaration), and so does an exported class, written before `export` or after it, not
// both (`DecoratorList export [lookahead ≠ @] ClassDeclaration`, and the same with `default`).
// So after the decorators come a class, or `export` / `export default` and an undecorated
// class, read by the export arms (`$`); no other declaration takes decorators (`@dec function
// f() {}`, `@dec export var x`, `@dec export { a }`, `@dec export default x`, `@dec export
// @dec class C {}` have no parse). The inline alt is ordered, so a class is the first branch
// and the only one that parses it.
arm('decorated', many1(field('decorator', DecoratorExpr)), field('declaration', alt(arm('class_', ...classDecl), arm('export_', not(not(['export', opt('default'), 'class'])), field('decl', $))))),
// A declaration that ends in a block needs nothing after it; every other one ends as a
// statement does. After `default`, `function` and `class` start a declaration
// (`export default [lookahead ∉ { function, async function, class }] AssignmentExpression`;
// the inline alt is ordered, so their arms come first), so `export default class {} g()` is two.
arm('exportDefault', field('exportKeyword', 'export'), field('defaultKeyword', 'default'), field('declaration', alt(
...fnArms([opt(...bindingName)], Block), // function
arm('namedClass', many(field('decorator', DecoratorExpr)), 'class', ...bindingName, opt('extends', field('superClass', ClassHeritage)), field('bodyOpen', '{'), many(field('member', ClassMember)), field('bodyClose', '}')), // named class
arm('anonymousClass', many(field('decorator', DecoratorExpr)), 'class', opt('extends', field('superClass', ClassHeritage)), field('bodyOpen', '{'), many(field('member', ClassMember)), field('bodyClose', '}')), // anonymous class
arm('expression', field('expr', Expr), asi()), // catch-all: export default <expr>
))),
arm('exportAll', field('exportKeyword', 'export'), field('star', '*'), field('form', alt(arm('plain', 'from', ...moduleSpecifier), arm('aliased', 'as', field('exported', alt(Ident, String_)), 'from', ...moduleSpecifier))), asi()), // the alias is a ModuleExportName
arm('exportNamed', field('exportKeyword', 'export'), field('brace', '{'), sep(field('specifier', ExportSpecifier), ','), field('braceClose', '}'), opt('from', ...moduleSpecifier), asi()),
arm('importDeclaration', field('importKeyword', 'import'), field('form', alt(
arm('clause', field('clause', ImportClause), 'from', ...moduleSpecifier), // import X from "m"
// source phase imports (Stage 3): `import source ImportedBinding FromClause`; `import source
// from "m"` is the default import of `source` (the clause arm), `import source from from "m"` this one.
arm('source', field('sourceKeyword', 'source'), notReserved, field('name', Ident), 'from', ...moduleSpecifier),
arm('bare', ...moduleSpecifier), // import "m"
)), asi()),
]);
// ── Entry ──
const Program = rule(() => [
many(alt(Decl, Stmt)), // Decl first: prefer declaration over IIFE expression-statement
]);
// ── Scope map ──
// The JS scope map: scopes every real-JS token. The TS grammar imports this and
// extends it with the type-only scope keys (storage.type.interface/type/enum/
// namespace, the keyof/as/satisfies/is/infer/asserts keyword.operator.expression
// entries, support.type.primitive) — those names are plain identifiers in JS, not
// types, so they are deliberately absent here.
export const jsScopes = {
'keyword.control.conditional': ['if', 'else'],
'keyword.control.switch': ['switch', 'case', 'default'],
'keyword.control.loop': ['for', 'while', 'do', 'in', 'of', 'break', 'continue'],
'keyword.control.flow': ['return', 'await', 'yield'],
'keyword.control.trycatch': ['try', 'catch', 'finally', 'throw'],
'keyword.control': ['debugger', 'with'],
'keyword.control.import': ['import'],
// `defer` before a namespace import, scoped by position (gen-textmate's phase-modifier handling), as in TypeScript.
'keyword.control.import.phase': ['defer'],
'keyword.control.export': ['export'],
'keyword.control.from': ['from'],
'storage.type': ['let', 'const', 'var', 'using'],
// `const` also carries this marker subtype — a no-op for the keyword's own color
// (`storage.type` wins, being declared first), but it tells the highlighter generator
// that a `const` binding names a CONSTANT. See gen-textmate's const-binding rule.
'storage.type.const': ['const'],
'storage.type.function': ['function', 'constructor'],
'storage.type.class': ['class'],
'storage.modifier': ['static', 'async', 'accessor'],
'storage.type.property': ['get', 'set'],
'keyword.other.extends': ['extends'],
'keyword.operator.expression': ['instanceof', 'new', 'delete', 'void', 'typeof'],
'keyword.operator.assignment': ['=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '>>>=', '&=', '|=', '^=', '??=', '||=', '&&='],
'keyword.operator.comparison': ['==', '!=', '===', '!=='],
'keyword.operator.relational': ['<', '>', '<=', '>='],
'keyword.operator.logical': ['||', '&&', '??'],
'keyword.operator.arithmetic': ['+', '-', '*', '/', '%', '**'],
'keyword.operator.increment-decrement': ['++', '--'],
'keyword.operator.logical.prefix': ['!', '~'],
'keyword.operator.bitwise': ['|', '&', '^'],
'keyword.operator.bitwise.shift': ['<<', '>>', '>>>'],
'storage.type.function.arrow': ['=>'],
'punctuation.bracket.round': ['(', ')'],
'punctuation.bracket.curly': ['{', '}'],
'punctuation.bracket.square': ['[', ']'],
'punctuation.accessor': ['.'],
'punctuation.accessor.optional': ['?.'],
'punctuation.terminator.statement': [';'],
'punctuation.separator.comma': [','],
'constant.language.boolean.true': ['true'],
'constant.language.boolean.false': ['false'],
'constant.language.null': ['null', 'undefined'],
'variable.language.this': ['this'],
'variable.language.super': ['super'],
'support.class': ['Promise', 'Array', 'Map', 'Set', 'WeakMap', 'WeakSet', 'Error', 'RegExp', 'Date', 'Object', 'Function', 'Symbol'],
'support.variable': ['console', 'window', 'document', 'process', 'module', 'require', 'exports', 'global', 'globalThis'],
'support.variable.property': ['.length', '.prototype', '.constructor'],
};
// Repository-key NAMING CONSTRAINT (官方命名「限制器」) — the SHARED ECMAScript half. For Monogram's
// source.js to be a repository-level DROP-IN for VS Code's official JavaScript grammar (and for the
// shared half of source.ts), the official repository KEY NAMES that external grammars `#include`
// (`source.js#qstring-double`, `#comment`, `#punctuation-comma`, …) must be the names Monogram
// NATIVELY emits. Monogram derives those keys under its OWN structural names (`#string-double`,
// `#linecomment`/`#blockcomment`, `#scope-punctuation-separator-comma`, …), so this maps each
// OFFICIAL name → the structural key(s) gen-textmate derived for the SAME construct; gen-textmate projects the
// repository through it at generation time, emitting the canonical name NATIVELY (a STRING value
// RENAMES the structural key — its old name ceases to exist — and rewrites every `#…` reference; an
// ARRAY value SYNTHESISES the `{patterns:[…]}` union the official grammar itself writes, resolving
// each member through the 1:1 renames first and dropping members absent from this grammar). It is
// PURELY a naming projection — no `match`/`begin`/`name` changes — so emitted tokenization is
// byte-for-byte unchanged (verified: test/repo-compat.ts + the byte-identical scope-array diff). The
// NAMES are ECMAScript DATA and belong here (the grammar definition may know JS — it already carries