-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtypescript.ts
More file actions
624 lines (577 loc) · 32.6 KB
/
typescript.ts
File metadata and controls
624 lines (577 loc) · 32.6 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
import {
rule, defineGrammar,
op, prefix, postfix, sameLine,
sep, opt, many, many1, alt, exclude, not,
} from './src/api.ts';
// JavaScript is the SUBSET / base of the ECMAScript family; TypeScript is the
// SUPERSET (JS + a type layer). The shared, type-free vocabulary — token consts,
// the `notReserved`/`notReservedExpr` reserved-word guards, the precedence ladder
// (`ecmaPrec`), and the JS scope map (`jsScopes`) — is OWNED by javascript.ts and
// imported here, then extended below with the type layer. Rules are NOT shared
// either direction (combinator rules bind their references at definition time), so
// this file keeps its own rule consts.
import {
Shebang, JSDoc, TripleSlash, LineComment, BlockComment,
Ident, HexNumber, OctalNumber, BinaryNumber, BigInt_,
Number_, String_, Template, Regex_, Decorator, PrivateField,
notReserved, notReservedExpr, ecmaPrec, jsScopes, jsBaseCanonical,
} from './javascript.ts';
// ── Type query reference (typeof's argument: just dotted identifiers) ──
const TypeofRef = rule($ => [
Ident,
[$, '.', Ident],
]);
// ── Decorators ──
// A decorator is `@ DecoratorMemberExpression`/`DecoratorCallExpression` (the `@name`
// head already lexed by the Decorator token, dotted segments included). After the head,
// these tail forms may follow and chain (`@x!.y`, `@x.y!`, `@x!()`): a call `(args)` /
// typed call `<T>(args)`, a non-null `!`, and a member `.m`. ELEMENT access `[i]` is
// deliberately EXCLUDED — TS's decorator grammar omits it precisely so a computed class
// member after a decorator (`@dec ["m"]() {}`, `@dec [field] = 1`) is not swallowed as
// `@dec[...]` (tsc rejects a real `@arr[0]` decorator). `many` so a whole chain is allowed.
const DecoratorExpr = rule($ => [
[Decorator, many(alt(
['(', sep(Expr, ','), ')'], // call: (args)
['<', sep(Type, ','), '>', '(', sep(Expr, ','), ')'], // typed call: <T>(args)
'!', // non-null assertion
['.', alt(Ident, PrivateField)], // member access
))],
]);
// ── Types ──
const TypeMember = rule($ => {
const callSig = [opt(TypeParams), '(', sep(Param, ','), ')', opt(':', Type)]; // `<T>( … ): Ret`
const propOrMethod = alt(callSig, [opt(':', Type)]); // after a name: method (callSig) | property
return [
// call / construct signature (no member name): a construct sig is just a
// call sig with an optional leading `new`.
[opt('new'), ...callSig],
// index signature | mapped type | computed member — share `(+/-)? readonly? [`,
// then (for index/mapped) share the leading `Ident` and branch on `in` vs `:`.
[opt(alt('+', '-')), opt('readonly'), '[', alt(
[Ident, alt(
['in', Type, opt('as', Type), ']', opt(alt('+', '-')), opt('?'), ':', Type], // mapped: K in T (as U)?
[':', Type, ']', opt(':', Type)], // index: k: T
)],
[Expr, ']', opt('?'), propOrMethod], // computed: expr
[']', opt(':', Type)], // empty index sig: [] / []: T
)],
// readonly property (the readonly index signature is the bracketed branch above)
['readonly', Ident, opt('?'), ':', Type],
// named member — share the name + `?`, then branch property | method
[alt(Ident, Number_, String_, PrivateField), opt('?'), propOrMethod],
];
});
const Type = rule($ => {
const fnType = [opt(TypeParams), '(', sep(Param, ','), ')', '=>', $]; // (a: T) => R / <T>(…) => R
return [
[Ident, opt('is', $)], // T | type predicate `x is T`
[$, '<', sep($, ','), '>'],
[$, sameLine, '[', ']'], // array type T[] — `[` must be on the same line (no ASI)
[$, '|', $],
[$, '&', $],
['|', $], // leading pipe: type T = | A | B
['&', $], // leading amp: type T = & A & B
['keyof', $],
['typeof', TypeofRef],
['readonly', $],
['(', $, ')'],
fnType, // function type
[opt('abstract'), 'new', ...fnType], // constructor type (= `new` + function type)
// tuple element: `...`? (name `?`? `:`)? `...`? Type `?`? — the second `...`
// covers a named rest member `n: ...T[]` (TS: RestType after the label); the
// trailing `?` covers optional members `n: T?` / `T?` (TS: OptionalType).
['[', many(opt('...'), opt(Ident, opt('?'), ':'), opt('...'), $, opt('?'), opt(',')), ']'],
['{', many(TypeMember, opt(alt(';', ','))), '}'],
['asserts', Ident, opt('is', $)],
[$, 'extends', $, '?', $, ':', $],
// infer U | infer U extends T | infer U extends T ? X : Y (conditional binds to the infer)
['infer', Ident, opt('extends', $, opt('?', $, ':', $))],
String_,
Number_,
HexNumber, OctalNumber, BinaryNumber, BigInt_,
['-', alt(Number_, BigInt_)],
'true', 'false', 'null', 'undefined', 'void', 'this',
['unique', 'symbol'],
['import', '(', $, ')'],
Template,
[$, sameLine, '[', $, ']'], // indexed access T[K] — `[` must be on the same line (no ASI)
[$, '.', Ident],
];
}, { type: true });
// ── Expressions ──
const Prop = rule($ => {
const method = ['(', sep(Param, ','), ')', opt(':', Type), Block]; // ( … ): T { … }
return [
['...', Expr], // spread
// accessor (get/set), optionally with an accessibility modifier (lenient)
[opt(alt('public', 'private', 'protected')), alt('get', 'set'), MemberName, '(', opt(sep(Param, ',')), ')', opt(':', Type), Block],
// method: async?/generator?, any member name (incl `#x`, computed `[e]`), then ( … ) { … }
[opt('async'), opt('*'), MemberName, opt(TypeParams), ...method],
// value property — any member name incl computed `[e]: v` (MemberName covers `[Expr]`)
[MemberName, ':', Expr],
['[', Expr, many(',', Expr), ']', ':', Expr], // computed comma list (lenient)
// shorthand (Ident only): x | x = v | 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.
[notReserved, Ident, alt(['=', Expr], ['?', opt(':', Expr)], [])],
];
});
const ClassHeritage = rule($ => [
Ident,
// The heritage clause is a LeftHandSideExpression, not just a dotted name: a
// parenthesized expression (`extends (B)`, `extends (cond ? A : B)`) and a class
// EXPRESSION (`extends class {}`, `extends class Q extends P {}`) are both valid
// bases. (Before, only `Ident` was a base, so `extends (B)` was rejected and
// `extends class {}` only "worked" by mis-reading `class` as the superclass name.)
['(', Expr, ')'],
['class', opt(notReserved, Ident), opt(TypeParams), opt('extends', $), opt('implements', sep(Type, ',')), '{', many(ClassMember), '}'],
[$, '.', Ident],
[$, '<', sep(Type, ','), '>'],
[$, '(', sep(Expr, ','), ')'],
]);
const NewTarget = rule($ => [
Ident,
[$, '.', Ident],
[$, '[', Expr, ']'],
['(', Expr, ')'],
]);
const Expr = rule($ => [
// A standalone identifier expression, but never a reserved word that has no expression
// role (see notReservedExpr). This kills the bare-identifier fallback for keywords like
// `catch`/`throw` and the prefix operators `void`/`typeof`/`delete`, so `catch(x){}`
// with no `try`, `void ;`, and `throw ;` are rejected as TS does. (`enum` is included —
// it previously had its own `not('enum')` guard for the same reason.)
[notReservedExpr, Ident],
Number_,
String_,
Template,
Regex_,
'true', 'false', 'null', 'undefined', 'this', 'super',
[$, op, $],
[prefix, $],
[$, postfix],
['...', $],
// typed call / tagged template: f<T>(…) | f<T>`…` — a call/tag may itself be
// continued by member access (`f<T>().x`), so this is an ordinary access tail.
[$, '<', sep(Type, ','), '>', alt(['(', sep($, ','), ')'], Template)],
// bare instantiation `f<T>` (no call/tag): allowed only when the next token
// can't start an expression — otherwise `<`/`>` were comparisons (`f < a, b > 7`),
// the disambiguation TS makes via canFollowTypeArgumentsInExpression. Ending in a
// negative lookahead, this LED closes the access tail (it asserts nothing follows),
// so a `.`/`?.` property access can't chain off it: `Foo<T>.Bar` is rejected
// (TS1477 — a bare instantiation is not a valid base for property access). A `[`,
// `(`, or `` ` `` continuation still reparses the `<…>` as comparisons (those start
// an expression, so `not($)` fails the bare arm), matching TS.
[$, '<', sep(Type, ','), '>', not(Expr)],
[$, '(', sep($, ','), ')'],
[$, '.', alt(Ident, PrivateField)],
// optional chaining: ?.x | ?.#x | ?.(args) | ?.[i] | ?.`…`
[$, '?.', alt(Ident, PrivateField, ['(', sep($, ','), ')'], ['[', $, ']'], Template)],
[$, '[', $, ']'],
[$, '!'], // TS non-null assertion — a LHS-chain tail (access can follow: `x!.y`, `x!()`), unlike update `++`/`--`
[$, '?', $, ':', $],
[$, 'as', Type],
[$, 'instanceof', $],
[$, 'in', $],
[$, Template],
// new T | new T(args) | new T<X> | new T<X>(args)
['new', NewTarget, opt(alt(
['<', sep(Type, ','), '>', opt('(', sep($, ','), ')')],
['(', sep($, ','), ')'],
))],
['new', 'class', notReserved, Ident, opt(TypeParams), opt('extends', ClassHeritage), opt('implements', sep(Type, ',')), '{', many(ClassMember), '}', opt('(', sep($, ','), ')')],
['new', 'class', opt(TypeParams), opt('extends', ClassHeritage), opt('implements', sep(Type, ',')), '{', many(ClassMember), '}', opt('(', sep($, ','), ')')],
['[', many(opt($), ','), opt($), ']'],
['{', sep(Prop, ','), '}'],
[opt('async'), opt(TypeParams), '(', sep(Param, ','), ')', opt(':', Type), '=>', alt($, Block)],
[Ident, '=>', alt($, Block)],
['yield', alt(['*', $], [opt($)])], // yield e | yield* e (delegate) | yield
['(', $, many(',', $), ')'],
[$, 'satisfies', Type],
['import', alt(['(', $, ')'], ['.', 'meta'])],
PrivateField,
HexNumber, OctalNumber, BinaryNumber, BigInt_,
[opt('async'), 'function', opt('*'), opt(notReserved, Ident), opt(TypeParams), '(', sep(Param, ','), ')', opt(':', Type), Block],
// named vs anonymous kept separate (greedy opt(Ident) would eat a leading
// `extends`/`implements`); decorator dimension is a `many` (a class expression may
// carry ≥2 decorators, `x = @d @d class C {}`, like the declaration arm below).
[many(DecoratorExpr), 'class', notReserved, Ident, opt(TypeParams), opt('extends', ClassHeritage), opt('implements', sep(Type, ',')), '{', many(ClassMember), '}'],
[many(DecoratorExpr), 'class', opt(TypeParams), opt('extends', ClassHeritage), opt('implements', sep(Type, ',')), '{', many(ClassMember), '}'],
['<', Type, '>', $],
]);
// ── 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.
[Ident, ':', BindingElement],
// shorthand `a` / shorthand-with-default `a = 1` — the name is a BindingIdentifier,
// so a reserved word is invalid (`{ while }`, `{ class }`).
[notReserved, Ident, opt('=', Expr)],
[alt(String_, Number_, ['[', Expr, ']']), ':', BindingElement], // "s"/0/[e]: elem
['...', alt([notReserved, Ident], BindingPattern)], // ...rest | ...{ a }
]);
const BindingElement = rule($ => [
[alt([notReserved, Ident], BindingPattern), opt('=', Expr)], // a | { a } (optionally = default)
]);
const ArrayBindingElement = rule($ => [
BindingElement,
['...', alt([notReserved, Ident], BindingPattern)], // [...rest] | [...{ a }]
]);
const BindingPattern = rule($ => [
['{', sep(BindingProperty, ','), '}'], // { a, b: c, ...rest }
['[', opt(ArrayBindingElement), many(',', opt(ArrayBindingElement)), ']'], // [a, , b, ...rest]
]);
// ── Bindings & Parameters ──
const Binding = rule($ => [
[alt([notReserved, Ident, opt('!')], BindingPattern), opt(':', Type), opt('=', Expr)],
]);
// A binding in a for-head: identical to Binding except the initializer is a
// no-`in` expression, so `for (var a = 1 in xs)` reads `a = 1` then the for-in
// `in` (TS's [~In] grammar), rather than greedily parsing `1 in xs`.
const ForBinding = rule($ => [
[alt([notReserved, Ident, opt('!')], BindingPattern), opt(':', Type), opt('=', exclude('in', Expr))],
]);
const Param = rule($ => {
const tail = [opt('?'), opt(':', Type), opt('=', Expr)]; // ? : T = E
const body = alt(
// NOTE: a plain parameter name is NOT reserved-guarded — `this` is a valid first
// parameter even without an annotation (`function f(this, a)`: the implicit-any
// `this`-param), and `this` is an always-reserved word; guarding here would reject
// that valid form. (A truly reserved param name like `function f(while)` stays an
// accepted over-accept; it's out of this gap's scope.)
[Ident, ...tail],
[BindingPattern, ...tail],
// a rest element, by contrast, can never validly be a reserved word (`...while`),
// and `...this` is invalid too, so guarding the rest name is FN-safe.
['...', alt([notReserved, Ident], BindingPattern), opt('?'), opt(':', Type)], // rest
);
return [
['this', ':', Type],
// optional decorators + optional parameter-property modifiers, then the binding.
// many1 → with modifiers; the no-modifier branch also catches a param NAMED
// like a modifier (`public: T`), which many() would otherwise eat.
[opt(DecoratorExpr), many1(alt('public', 'private', 'protected', 'readonly')), body],
[opt(DecoratorExpr), body],
];
});
const ForHead = rule($ => {
const cTail = [';', opt(Expr, many(',', Expr)), ';', opt(Expr, many(',', Expr))]; // `; cond ; update`
return [
// declared head: `let/const/var/using/await using <bindings>` then C-style or in/of.
// ForBinding gives a no-`in` initializer so `for (var a = 1 in xs)` parses.
[alt('let', 'const', 'var', 'using', ['await', 'using']), sep(ForBinding, ','), alt(
cTail,
[alt('in', 'of'), Expr],
)],
[opt(Expr, many(',', Expr)), ...cTail], // C-style, no declaration: `for (i=0; …; …)` / `for (;;)`
[Expr, alt('in', 'of'), Expr], // for-in/of, no declaration: `for (x of xs)`
];
});
const SwitchCase = rule($ => [
['case', Expr, many(',', Expr), ':'],
['default', ':'],
Stmt,
]);
const Stmt = rule($ => [
Block,
[alt('let', 'const', 'var'), sep(Binding, ','), opt(';')],
['if', '(', Expr, many(',', Expr), ')', $, opt('else', $)],
['for', opt('await'), '(', ForHead, ')', $],
['while', '(', Expr, many(',', Expr), ')', $],
['do', $, 'while', '(', Expr, many(',', Expr), ')', opt(';')],
['switch', '(', Expr, many(',', Expr), ')', '{', many(SwitchCase), '}'],
['return', opt(Expr, many(',', Expr)), opt(';')],
['throw', Expr, many(',', Expr), opt(';')],
['break', opt(Ident), opt(';')],
['continue', opt(Ident), opt(';')],
['try', Block, opt('catch', opt('(', alt(Param, BindingPattern), ')'), Block), opt('finally', Block)],
[Ident, ':', $],
';',
['debugger', opt(';')],
['with', '(', Expr, ')', $],
[opt('await'), 'using', sep(Binding, ','), opt(';')],
Decl,
[Expr, many(',', Expr), opt(';')],
]);
// ── Type Parameters ──
const TypeParam = rule($ => {
// TS parses any modifier soup before a type-param name (variance `in`/`out`,
// `const`, even bogus `public`), then reports invalid ones post-parse. A param
// can also be NAMED like a modifier — `<in>`, `<out = any>`, and even the
// variance-modified `<in in>` / `<out out>` (first `in`/`out` is the modifier,
// second is the name). Longest-match picks among:
const tail = [opt('extends', Type), opt('=', Type)];
const mod = alt('const', 'in', 'out', 'public', 'private', 'protected', 'readonly');
const name = alt(Ident, 'in', 'out'); // a name may itself be a contextual variance keyword
return [
[many1(mod), Ident, ...tail], // modifier soup + real-ident name: `<const in T>`, `<in T>`
[mod, name, ...tail], // single modifier + in/out-named param: `<in in>`, `<out out>`
[name, ...tail], // bare name, incl. `<in>`, `<out>`: `<T>`, `<in = any>`
];
});
const TypeParams = rule($ => [
['<', sep(TypeParam, ','), '>'],
]);
// ── Declarations ──
const InterfaceMember = rule($ => {
const callSig = [opt(TypeParams), '(', sep(Param, ','), ')', opt(':', Type)]; // `<T>( … ): Ret`
const propOrMethod = alt(callSig, [opt(':', Type)]); // after a name: method | property (bare = implicit any)
return [
// call / construct signature (construct = call sig with a leading `new`)
[opt('new'), ...callSig],
// getter / setter (`get`/`set` as a member NAME falls through to the named branch)
[alt('get', 'set'), MemberName, '(', sep(Param, ','), ')', opt(':', Type)],
// mapped type: static? (+/-)? readonly? [ K in T (as U)? ] (+/-)? ?? : T
[opt('static'), opt(alt('+', '-')), opt('readonly'), '[', Ident, 'in', Type, opt('as', Type), ']', opt(alt('+', '-')), opt('?'), ':', Type],
// readonly property (readonly index sig is the bracketed branch below)
['readonly', MemberName, opt('?'), ':', Type],
// named / computed member (MemberName includes `[Expr]`) — branch property | method.
// Placed before the index signature so a bare `[expr]` parses as a computed
// property (TS: `[p]` is a computed property, not an indexer).
[MemberName, opt('?'), propOrMethod],
// index signature: static? readonly? [ Param,* ] (: T)? — TS parses the brackets
// as a full parameter list, so `[]`, `[a?]`, `[public a]`, `[a: T, b: U]` all parse
// (the extra forms are grammar-errors TS reports post-parse, but the parser accepts).
[opt('static'), opt('readonly'), '[', sep(Param, ','), ']', opt(':', Type)],
];
});
const MemberName = rule($ => [
Ident,
PrivateField,
String_,
Number_,
HexNumber,
OctalNumber,
BinaryNumber,
BigInt_,
['[', Expr, ']'],
]);
// Branched: parse the modifier list ONCE, then branch on the member kind, so a
// member's shared `modifiers …` prefix isn't re-parsed per alternative. Inner
// alt() is first-match, so branches are ordered specific-before-general
// (generator/accessor/index-sig before the MemberName method/field split).
const Modifier = alt('public', 'private', 'protected', 'static', 'abstract', 'readonly', 'override', 'accessor', 'async');
const callTail = ['(', sep(Param, ','), ')', opt(':', Type), opt(Block), opt(';')] as const;
const ClassMember = rule($ => [
DecoratorExpr,
['constructor', '(', sep(Param, ','), ')', Block, opt(';')],
['static', Block],
[
many(Modifier),
alt(
['*', MemberName, opt('?'), opt(TypeParams), ...callTail], // generator method
[alt('get', 'set'), MemberName, '(', opt(sep(Param, ',')), ')', opt(':', Type), opt(Block), opt(';')], // accessor
['[', Ident, ':', Type, ']', ':', Type, opt(';')], // index signature
[MemberName, alt(
[opt('?'), opt(TypeParams), ...callTail], // method (requires `(`)
[opt('!'), opt('?'), opt(':', Type), opt('=', Expr), opt(';')], // field (all-optional → catch-all)
)],
),
],
// Fallbacks for a member NAMED like a modifier (`static = 1`, `get = 1`, `async() {}`):
// many(Modifier) would eat the name, so the member kind alt fails and we land here.
[MemberName, opt('!'), opt('?'), opt(':', Type), opt('=', Expr), opt(';')],
[MemberName, opt('?'), opt(TypeParams), '(', sep(Param, ','), ')', opt(':', Type), opt(Block), opt(';')],
]);
const EnumMember = rule($ => [
[MemberName, opt('=', Expr)],
]);
const ImportSpecifier = rule($ => [
[Ident, opt('as', Ident)],
]);
const ImportClause = rule($ => [
// deferred import (TS 5.9): `import defer * as ns from "m"`. `defer` is a phase
// modifier here, ONLY valid immediately before the namespace `* as`. As a keyword
// literal it still lexes as an Ident, so `defer` stays an ordinary binding name in
// every other position (`const defer = 1`, `import defer from "m"`, `defer()`).
['defer', '*', 'as', Ident],
// default import, optionally followed by named `{…}` or namespace `* as x`
[Ident, opt(',', alt(['{', sep(ImportSpecifier, ','), '}'], ['*', 'as', Ident]))],
['{', sep(ImportSpecifier, ','), '}'],
['*', 'as', Ident],
]);
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).
[opt('async'), 'function', opt('*'), notReserved, Ident, opt(TypeParams), '(', sep(Param, ','), ')', opt(':', Type), alt(Block, opt(';'))],
// The declaration NAME slots below carry `notReserved` (same guard as the type-alias
// name): a reserved word is not a legal declaration name (`interface void {}`,
// `class while {}`, `enum for {}`, `namespace debugger {}` — all TS errors), while a
// contextual keyword name (`interface any`, `class string`, `enum number`) stays valid.
['interface', notReserved, Ident, opt(TypeParams), opt('extends', sep(Type, ',')), '{', many(InterfaceMember, opt(alt(';', ','))), '}'],
['type', notReserved, Ident, opt(TypeParams), '=', Type, opt(';')], // type-alias name can't be a reserved word (`type void = …`); contextual type keywords (`string`/`any`/…) stay valid
// class decl: optional decorators + optional `abstract`. gen-tm expands the
// opt()/many() to recover the `class Ident … { … }` shape for highlighting.
[many(DecoratorExpr), opt('abstract'), 'class', notReserved, Ident, opt(TypeParams), opt('extends', ClassHeritage), opt('implements', sep(Type, ',')), '{', many(ClassMember), '}'],
['enum', notReserved, Ident, '{', sep(EnumMember, ','), '}'],
['declare', 'function', opt('*'), notReserved, Ident, opt(TypeParams), '(', sep(Param, ','), ')', opt(':', Type), opt(';')],
['declare', alt($, Stmt)],
['namespace', notReserved, Ident, many('.', Ident), '{', many(Stmt), '}'], // dotted name: `namespace A.B.C { … }`
['module', alt([notReserved, Ident, many('.', Ident)], String_), '{', many(Stmt), '}'], // `module A.B.C { … }` | `module "x" { … }`
['export', alt($, Stmt)],
['export', 'default', alt(
[opt('async'), 'function', opt('*'), opt(notReserved, Ident), opt(TypeParams), '(', sep(Param, ','), ')', opt(':', Type), alt(Block, opt(';'))], // function
['abstract', 'class', notReserved, Ident, opt(TypeParams), opt('extends', ClassHeritage), opt('implements', sep(Type, ',')), '{', many(ClassMember), '}'], // named abstract class
['abstract', 'class', opt(TypeParams), opt('extends', ClassHeritage), opt('implements', sep(Type, ',')), '{', many(ClassMember), '}'], // anonymous abstract class
[Expr, opt(';')], // catch-all: export default <expr>
)],
['export', '*', alt(['from', String_, opt(';')], ['as', Ident, 'from', String_, opt(';')])],
['export', '{', sep(ImportSpecifier, ','), '}', opt('from', String_), opt(';')],
['export', '=', Expr, opt(';')],
['export', 'type', '{', sep(ImportSpecifier, ','), '}', opt('from', String_), opt(';')],
['const', 'enum', notReserved, Ident, '{', sep(EnumMember, ','), '}'],
['import', alt(
[ImportClause, 'from', String_, opt(';')], // import X from "m" (also `import type from "m"` = default named `type`)
['type', ImportClause, 'from', String_, opt(';')], // import type X from "m"
[Ident, '=', Expr, opt(';')], // import x = expr
[String_, opt(';')], // import "m"
)],
[many(DecoratorExpr), 'export', alt($, Stmt)],
]);
// ── Entry ──
const Program = rule($ => [
many(alt(Decl, Stmt)), // Decl first: prefer declaration over IIFE expression-statement
]);
// ── Grammar ──
export default defineGrammar({
name: 'typescript',
scopeName: 'source.ts',
tokens: {
// Comments must come before Regex_ to avoid /** ... */ being matched as regex
Shebang, JSDoc, TripleSlash, LineComment, BlockComment,
Ident, HexNumber, OctalNumber, BinaryNumber, BigInt: BigInt_,
Number: Number_, String: String_, Template, Regex: Regex_,
Decorator, PrivateField,
},
// The ECMAScript operator-precedence ladder is shared, owned by javascript.ts.
prec: ecmaPrec,
rules: {
Type, TypeMember, DecoratorExpr, TypeofRef,
Expr, Prop, MemberName, NewTarget, ClassHeritage,
Stmt, Block,
BindingProperty, BindingElement, ArrayBindingElement, BindingPattern,
Binding, ForBinding, Param, ForHead, SwitchCase,
TypeParams, TypeParam,
Decl, InterfaceMember, ClassMember, EnumMember,
ImportClause, ImportSpecifier,
Program,
},
// TypeScript EXTENDS the JS scope map (jsScopes, owned by javascript.ts) with the
// type layer. The shared entries are reused by reference; the TS-specific ones are
// inlined: four type-declaration keywords (storage.type.interface/type/enum/
// namespace), the type-operator keyword.operator.expression set (adds keyof/as/is/
// satisfies/asserts/infer over JS's), the widened storage.modifier (TS accessibility
// + readonly/abstract/override/declare) and keyword.other.extends (adds implements),
// and support.type.primitive (TS's primitive type names). The keys are written in
// jsScopes' original order — with the type-only keys interleaved at their TS
// positions — so the emitted grammar is byte-identical to the prior inline map (a
// bare `{ ...jsScopes, …TS-only }` spread would instead append the type-only keys at
// the end and flip `module`'s primary scope, changing the generated output).
scopes: {
'keyword.control.conditional': jsScopes['keyword.control.conditional'],
'keyword.control.switch': jsScopes['keyword.control.switch'],
'keyword.control.loop': jsScopes['keyword.control.loop'],
'keyword.control.flow': jsScopes['keyword.control.flow'],
'keyword.control.trycatch': jsScopes['keyword.control.trycatch'],
'keyword.control': jsScopes['keyword.control'],
'keyword.control.import': jsScopes['keyword.control.import'],
// `defer` is the TS 5.9 deferred-import phase modifier (`import defer * as ns`).
// It carries a keyword.control.import subtype, but — unlike `import` itself — it
// is NOT reserved (a valid identifier name everywhere else). gen-tm therefore
// scopes it POSITIONALLY (only right before the namespace `*`, via the
// import-export-all pattern), never in the flat keyword match. See gen-tm's
// phase-modifier handling.
'keyword.control.import.phase': ['defer'],
'keyword.control.export': jsScopes['keyword.control.export'],
'keyword.control.from': jsScopes['keyword.control.from'],
'storage.type': jsScopes['storage.type'],
'storage.type.const': jsScopes['storage.type.const'],
'storage.type.function': jsScopes['storage.type.function'],
'storage.type.class': jsScopes['storage.type.class'],
'storage.type.interface': ['interface'],
'storage.type.type': ['type'],
'storage.type.enum': ['enum'],
'storage.type.namespace': ['namespace', 'module'],
'storage.modifier': [
'public', 'private', 'protected',
'static', 'readonly', 'abstract', 'override', 'declare', 'async', 'accessor',
],
'storage.type.property': jsScopes['storage.type.property'],
'keyword.other.extends': ['extends', 'implements'],
'keyword.operator.expression': ['typeof', 'keyof', 'instanceof', 'as', 'new', 'delete', 'void', 'is', 'satisfies', 'asserts', 'infer'],
'keyword.operator.assignment': jsScopes['keyword.operator.assignment'],
'keyword.operator.comparison': jsScopes['keyword.operator.comparison'],
'keyword.operator.relational': jsScopes['keyword.operator.relational'],
'keyword.operator.logical': jsScopes['keyword.operator.logical'],
'keyword.operator.arithmetic': jsScopes['keyword.operator.arithmetic'],
'keyword.operator.increment-decrement': jsScopes['keyword.operator.increment-decrement'],
'keyword.operator.logical.prefix': jsScopes['keyword.operator.logical.prefix'],
'keyword.operator.bitwise': jsScopes['keyword.operator.bitwise'],
'keyword.operator.bitwise.shift': jsScopes['keyword.operator.bitwise.shift'],
'storage.type.function.arrow': jsScopes['storage.type.function.arrow'],
'punctuation.bracket.round': jsScopes['punctuation.bracket.round'],
'punctuation.bracket.curly': jsScopes['punctuation.bracket.curly'],
'punctuation.bracket.square': jsScopes['punctuation.bracket.square'],
'punctuation.accessor': jsScopes['punctuation.accessor'],
'punctuation.accessor.optional': jsScopes['punctuation.accessor.optional'],
'punctuation.terminator.statement': jsScopes['punctuation.terminator.statement'],
'punctuation.separator.comma': jsScopes['punctuation.separator.comma'],
'constant.language.boolean.true': jsScopes['constant.language.boolean.true'],
'constant.language.boolean.false': jsScopes['constant.language.boolean.false'],
'constant.language.null': jsScopes['constant.language.null'],
'variable.language.this': jsScopes['variable.language.this'],
'variable.language.super': jsScopes['variable.language.super'],
'support.type.primitive': ['string', 'number', 'boolean', 'object', 'symbol', 'bigint', 'any', 'unknown', 'never', 'void'],
'support.class': jsScopes['support.class'],
'support.variable': jsScopes['support.variable'],
'support.variable.property': jsScopes['support.variable.property'],
},
// Repository-key NAMING CONSTRAINT (官方命名「限制器」) — the part that makes Monogram's source.ts a
// REPOSITORY-LEVEL drop-in for VS Code's official TypeScript grammar. External grammars (Vue,
// Markdown, MDX, …) `#include` the official repository keys BY NAME (`source.ts#type`,
// `source.ts#qstring-double`, `source.ts#comment`, …). Monogram derives those keys under its OWN
// structural names (`#type-inner`, `#string-double`, `#linecomment`/`#blockcomment`, …), so the
// official names wouldn't resolve and an `#include` would silently no-op. This CONSTRAINS gen-tm's
// key emission: it maps each OFFICIAL name → the structural key(s) gen-tm derived for the SAME
// construct, and gen-tm 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, e.g. `#comment`/`#return-type`, resolving each member through the
// 1:1 renames first). It is purely a naming projection — no `match`/`begin`/`name` changes — so the
// emitted tokenization is byte-for-byte unchanged (verified: test/repo-compat.ts + the vue dual-host
// proof). gen-tm only looks up + substitutes, staying language-agnostic.
//
// The SHARED ECMAScript half (`type`, `qstring-*`, `punctuation-comma`/`-semicolon`/`-accessor`,
// `new-expr`, `regex`, `directives`, `parameter-name`, `comment`/`string`/`boolean-literal`/
// `numeric-literal` unions, `this-literal`/`super-literal`) is OWNED by javascript.ts (which owns the
// shared vocabulary) as `jsBaseCanonical`, imported and spread here; this file adds ONLY the TS-only
// entries (the type layer: type-parameters, casts, type-object, param/return type annotations,
// type-predicate). The structural source of an entry that doesn't exist in source.ts (none here —
// all verified present) would simply be SKIPPED by gen-tm. Official names that ALREADY name a real
// Monogram key are omitted: `expression`, `template`, and `namespace-declaration` (Monogram's
// `namespace`-keyword key) already match by name. (Monogram's `module-declaration` — the legacy
// `module X {}` form the official folds into `namespace-declaration` — stays Monogram-internal; a
// structural split, not a naming gap.)
canonicalRepoNames: {
...jsBaseCanonical,
// TS-only — the type layer (no JS counterpart).
'type-parameters': 'declaration-type-params',
'type-alias-declaration': 'type-declaration',
'type-object': 'type-object-type',
cast: 'type-cast',
'parameter-type-annotation': 'param-type-annotation',
'type-predicate-operator': 'is-typekw',
// Union (official wrapper key): members resolved through the renames above.
'return-type': ['type-annotation-return', 'decl-return-type'],
},
entry: Program,
// The expression rule — lets gen-tm derive a `#expression` sub-grammar (used by
// expression-only embeds like Vue's `{{ }}`, where statements are invalid).
expression: Expr,
});