-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathnodes.ts
2125 lines (1780 loc) · 80.6 KB
/
nodes.ts
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
/*!
* Copyright (c) 2020 Ron Buckton (rbuckton@chronicles.org)
*
* This file is licensed to you under the terms of the MIT License, found in the LICENSE file
* in the root of this repository or package.
*/
import { emptyIterable, first, forEach, last } from "./core";
import { DiagnosticMessages, LineMap } from "./diagnostics";
import { getNodeAccessor, setNodeAccessor, setSourceFileAccessor, setTriviaAccessor } from "./nodeInternal";
import { skipTrivia } from "./scanner";
import {
ArgumentOperatorKind,
AssertionKind,
BooleanKind,
CommentTriviaKind,
HtmlTagTriviaKind,
HtmlTriviaKind,
isHtmlTriviaKind,
LexicalSymbolKind,
LookaheadOperatorKind,
MetaElementKind,
OptionalSymbolKind,
PrimarySymbolKind,
ProductionBodyKind,
ProductionSeperatorKind,
ProseFragmentLiteralKind,
SourceElementKind,
SyntaxKind,
TokenKind,
TriviaKind
} from "./tokens";
import { TextRange } from "./types";
import { NodeVisitor } from "./visitor";
/** {@docCategory Nodes} */
export interface TextContent {
readonly text: string | undefined;
}
/** {@docCategory Nodes} */
export abstract class Node<TKind extends SyntaxKind = SyntaxKind> implements TextRange {
static {
setNodeAccessor({
setTextRange(node, pos, end) {
node._pos = pos;
node._end = end;
return node;
},
setDetachedTrivia(node, trivia) {
node._detachedTrivia = trivia;
},
setLeadingTrivia(node, trivia) {
node._leadingTrivia = trivia;
node._leadingHtmlTrivia = undefined;
},
setTrailingTrivia(node, trivia) {
node._trailingTrivia = trivia;
node._trailingHtmlTrivia = undefined
},
edgeCount(node) { return node.edgeCount; },
edgeName(node, offset) { return node.edgeName(offset); },
edgeValue(node, offset) { return node.edgeValue(offset); },
accept(node, visitor) { return node.accept(visitor); },
});
}
public readonly kind: TKind;
private _pos = 0;
private _end = 0;
private _leadingTrivia: readonly Trivia[] | undefined;
private _trailingTrivia: readonly Trivia[] | undefined;
private _detachedTrivia: readonly Trivia[] | undefined;
private _leadingHtmlTrivia: readonly HtmlTrivia[] | undefined;
private _trailingHtmlTrivia: readonly HtmlTrivia[] | undefined;
public constructor(kind: TKind) {
this.kind = kind;
}
public get pos() { return this._pos; };
public get end() { return this._end; };
/**
* Leading trivia is trivia that belongs to the beginning of the node:
* - An HTML close tag trivia, or any trivia preceding an HTML close tag trivia, is not leading trivia of the node.
* - An HTML open tag trivia, and any trivia following an HTML open tag trivia, is leading trivia of the node.
* - If the node has a preceding line break, then
* - Any other non-HTML tag trivia on the same line as the node that precedes the node is leading trivia of the node.
* - Any other non-HTML tag trivia on a line that precedes the node, but not preceding a blank line, is leading trivia of the node.
* - Otherwise,
* - Any other non-HTML tag trivia on the same line as the node that precedes the node is leading trivia, if there is no whitespace between
* that trivia and the node.
*/
public get leadingTrivia() { return this._leadingTrivia; }
/**
* Trailing trivia is trivia that belongs to the end of the node:
* - An HTML open tag trivia, or any trivia following an HTML open tag trivia, is not trailing trivia of the node.
* - An HTML close tag trivia, and any trivia preceding an HTML close tag trivia, is trailing trivia of the node.
* - If the node has a trailing line break, then
* - Any other non-HTML tag trivia on the same line as the node that follows the node is trailing trivia of the node.
* - Any other non-HTML tag trivia on a line that follows the node, but not following a blank line, is trailing trivia of the node.
* - Otherwise,
* - Any other non-HTML tag trivia on the same line as the node that follows the node is trailing trivia, if there is no whitespace between
* that trivia and the node.
*/
public get trailingTrivia() { return this._trailingTrivia; }
/**
* Detached trivia is any trivia that occurs prior to the node that is not the leading or trailing trivia of this
* or any other node.
*/
public get detachedTrivia() { return this._detachedTrivia; }
/** @deprecated Use {@link leadingTrivia} or {@link detachedTrivia} instead. */
public get leadingHtmlTrivia(): readonly HtmlTrivia[] | undefined { return this._leadingHtmlTrivia ||= this._leadingTrivia?.filter((trivia): trivia is HtmlTrivia => isHtmlTriviaKind(trivia.kind)); }
/** @deprecated Use {@link trailingTrivia} instead. */
public get trailingHtmlTrivia(): readonly HtmlTrivia[] | undefined { return this._trailingHtmlTrivia ||= this._trailingTrivia?.filter((trivia): trivia is HtmlTrivia => isHtmlTriviaKind(trivia.kind)); }
public getStart(sourceFile?: SourceFile) { return sourceFile ? skipTrivia(sourceFile.text, this.pos, this.end) : this.pos; }
public getEnd() { return this.end; }
public getWidth(sourceFile?: SourceFile) { return this.getEnd() - this.getStart(sourceFile); }
public getFullStart() { return this.pos; }
public getFullWidth() { return this.getEnd() - this.getFullStart(); }
public getText(sourceFile: SourceFile) { return sourceFile.text.slice(this.getStart(sourceFile), this.getEnd()); }
public getFullText(sourceFile: SourceFile) { return sourceFile.text.slice(this.getFullStart(), this.getEnd()); }
public get firstChild(): Node | undefined { return undefined; }
public get lastChild(): Node | undefined { return undefined; }
public forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined;
public forEachChild<T>(_cbNode: (node: Node) => T | undefined): T | undefined { return undefined; }
public children(): IterableIterator<Node> { return emptyIterable; }
protected get edgeCount(): number { return 0; }
protected edgeName(offset: number): string | undefined;
protected edgeName(_offset: number): string | undefined { return undefined; }
protected edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined;
protected edgeValue(_offset: number): Node | ReadonlyArray<Node> | undefined { return undefined; }
protected accept(visitor: NodeVisitor): Node { return visitor.visitExtension(this); }
}
/** {@docCategory Nodes} */
export abstract class TriviaBase<TKind extends TriviaKind> extends Node<TKind> {
static {
setTriviaAccessor({
setPrecedingFields(node, hasPrecedingLineTerminator, hasPrecedingBlankLine, hasPrecedingWhiteSpace) {
node._hasPrecedingLineTerminator = hasPrecedingLineTerminator;
node._hasPrecedingBlankLine = hasPrecedingBlankLine;
node._hasPrecedingWhiteSpace = hasPrecedingWhiteSpace;
},
setFollowingFields(node, hasFollowingLineTerminator, hasFollowingBlankLine, hasFollowingWhiteSpace) {
node._hasFollowingLineTerminator = hasFollowingLineTerminator;
node._hasFollowingBlankLine = hasFollowingBlankLine;
node._hasFollowingWhiteSpace = hasFollowingWhiteSpace;
}
});
}
private _hasPrecedingLineTerminator = false;
private _hasPrecedingBlankLine = false;
private _hasPrecedingWhiteSpace = false;
private _hasFollowingLineTerminator = false;
private _hasFollowingBlankLine = false;
private _hasFollowingWhiteSpace = false;
public get hasPrecedingLineTerminator() { return this._hasPrecedingLineTerminator; }
public get hasPrecedingBlankLine() { return this._hasPrecedingBlankLine; }
public get hasPrecedingWhiteSpace() { return this._hasPrecedingWhiteSpace; }
public get hasFollowingLineTerminator() { return this._hasFollowingLineTerminator; }
public get hasFollowingBlankLine() { return this._hasFollowingBlankLine; }
public get hasFollowingWhiteSpace() { return this._hasFollowingWhiteSpace; }
}
/** {@docCategory Nodes} */
export type Trivia =
| CommentTrivia
| HtmlTrivia
;
/** {@docCategory Nodes} */
export abstract class CommentTriviaBase<TKind extends CommentTriviaKind> extends TriviaBase<TKind> {
}
/** {@docCategory Nodes} */
export type CommentTrivia =
| SingleLineCommentTrivia
| MultiLineCommentTrivia
;
/**
* Represent a single-line comment trivia token.
* ```grammarkdown
* // comment
* ```
* {@docCategory Nodes}
*/
export class SingleLineCommentTrivia extends CommentTriviaBase<SyntaxKind.SingleLineCommentTrivia> {
public constructor() {
super(SyntaxKind.SingleLineCommentTrivia);
}
}
/**
* Represents a multi-line comment trivia token.
* {@docCategory Nodes}
*/
export class MultiLineCommentTrivia extends CommentTriviaBase<SyntaxKind.MultiLineCommentTrivia> {
public constructor() {
super(SyntaxKind.MultiLineCommentTrivia);
}
}
/** {@docCategory Nodes} */
export abstract class HtmlTriviaBase<TKind extends HtmlTriviaKind> extends TriviaBase<TKind> {
}
/** {@docCategory Nodes} */
export type HtmlTrivia =
| HtmlCommentTrivia
| HtmlOpenTagTrivia
| HtmlCloseTagTrivia
;
/**
* Represents an HTML comment trivia token:
* ```grammarkdown
* Production ::
* <!--before-->Nonterminal
* ```
* {@docCategory Nodes}
*/
export class HtmlCommentTrivia extends HtmlTriviaBase<SyntaxKind.HtmlCommentTrivia> {
public constructor() {
super(SyntaxKind.HtmlCommentTrivia);
}
}
/** {@docCategory Nodes} */
export abstract class HtmlTagTriviaBase<TKind extends HtmlTagTriviaKind> extends HtmlTriviaBase<TKind> {
public readonly tagName: string;
public constructor(kind: TKind, tagName: string) {
super(kind);
this.tagName = tagName;
}
}
/**
* Represents an HTML open-tag trivia token:
* ```grammarkdown
* Production ::
* <ins>Inserted</ins>
* <del>Deleted</del>
* ```
* {@docCategory Nodes}
*/
export class HtmlOpenTagTrivia extends HtmlTagTriviaBase<SyntaxKind.HtmlOpenTagTrivia> {
public constructor(tagName: string) {
super(SyntaxKind.HtmlOpenTagTrivia, tagName);
}
}
/**
* Represents an HTML close-tag trivia token:
* ```grammarkdown
* Production ::
* <ins>Inserted</ins>
* <del>Deleted</del>
* ```
* {@docCategory Nodes}
*/
export class HtmlCloseTagTrivia extends HtmlTagTriviaBase<SyntaxKind.HtmlCloseTagTrivia> {
public constructor(tagName: string) {
super(SyntaxKind.HtmlCloseTagTrivia, tagName);
}
}
/**
* Represents a token such as a keyword or operator.
* {@docCategory Nodes}
*/
export class Token<TKind extends TokenKind = TokenKind> extends Node<TKind> {
protected override accept(visitor: NodeVisitor): Token<TKind> { return visitor.visitToken(this); }
}
/**
* Represents a single- or double-quoted string literal (used by `@import` and `@line`)
* ```grammarkdown
* @import "file"
* ```
* {@docCategory Nodes}
*/
export class StringLiteral extends Node<SyntaxKind.StringLiteral> implements TextContent {
public readonly text: string | undefined;
public constructor(text: string | undefined) {
super(SyntaxKind.StringLiteral);
this.text = text;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitStringLiteral(this); }
}
/**
* Represents a number literal (used by `@line`)
* ```grammarkdown
* @line 500
* Production :: Nonterminal
* ```
* {@docCategory Nodes}
*/
export class NumberLiteral extends Node<SyntaxKind.NumberLiteral> implements TextContent {
public readonly text: string | undefined;
public constructor(text: string | undefined) {
super(SyntaxKind.NumberLiteral);
this.text = text;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitNumberLiteral(this); }
}
/**
* Represents a Unicode character literal in one of two forms:
* ```grammarkdown
* <TAB>
* U+0000
* ```
* {@docCategory Nodes}
*/
export class UnicodeCharacterLiteral extends Node<SyntaxKind.UnicodeCharacterLiteral> implements TextContent {
public readonly text: string | undefined;
public constructor(text: string | undefined) {
super(SyntaxKind.UnicodeCharacterLiteral);
this.text = text;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitUnicodeCharacterLiteral(this); }
}
/**
* Represents a terminal token in the grammar.
* ```grammarkdown
* `yield`
* ```
* {@docCategory Nodes}
*/
export class TerminalLiteral extends Node<SyntaxKind.TerminalLiteral> implements TextContent {
public readonly text: string | undefined;
public constructor(text: string | undefined) {
super(SyntaxKind.TerminalLiteral);
this.text = text;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitTerminalLiteral(this); }
}
/**
* Represents an identifier such as a Production or Parameter.
* {@docCategory Nodes}
*/
export class Identifier extends Node<SyntaxKind.Identifier> implements TextContent {
public readonly text: string | undefined;
public constructor(text: string | undefined) {
super(SyntaxKind.Identifier);
this.text = text;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitIdentifier(this); }
}
/** {@docCategory Nodes} */
export type TextContentNode =
| StringLiteral
| NumberLiteral
| Identifier
| UnicodeCharacterLiteral
| TerminalLiteral
| ProseFragmentLiteral<ProseFragmentLiteralKind>;
/**
* Represents a set of symbols in a `lookahead` assertion.
* ```grammarkdown
* [lookahead ∈ { `a`, `b` }]
* ```
* {@docCategory Nodes}
*/
export class SymbolSet extends Node<SyntaxKind.SymbolSet> {
public readonly openBraceToken: Token<SyntaxKind.OpenBraceToken>;
public readonly elements: ReadonlyArray<SymbolSpan> | undefined;
public readonly closeBraceToken: Token<SyntaxKind.CloseBraceToken> | undefined;
public constructor(openBraceToken: Token<SyntaxKind.OpenBraceToken>, elements: ReadonlyArray<SymbolSpan> | undefined, closeBraceToken: Token<SyntaxKind.CloseBraceToken> | undefined) {
super(SyntaxKind.SymbolSet);
this.openBraceToken = openBraceToken;
this.elements = elements;
this.closeBraceToken = closeBraceToken;
}
public override get firstChild(): Node | undefined { return this.openBraceToken; }
public override get lastChild(): Node | undefined { return this.closeBraceToken || last(this.elements) || this.openBraceToken; }
public override forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined {
return cbNode(this.openBraceToken)
|| (this.elements && forEach(this.elements, cbNode))
|| (this.closeBraceToken && cbNode(this.closeBraceToken));
}
public override * children(): IterableIterator<Node> {
yield this.openBraceToken;
if (this.elements) yield* this.elements;
if (this.closeBraceToken) yield this.closeBraceToken;
}
public update(elements: ReadonlyArray<SymbolSpan> | undefined) {
return elements !== this.elements
? setTextRange(new SymbolSet(this.openBraceToken, elements, this.closeBraceToken), this.pos, this.end)
: this;
}
protected override get edgeCount() { return 3; }
protected override edgeName(offset: number): string | undefined {
switch (offset) {
case 0: return "openBraceToken";
case 1: return "elements";
case 2: return "closeBraceToken";
}
return undefined;
}
protected override edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined {
switch (offset) {
case 0: return this.openBraceToken;
case 1: return this.elements;
case 2: return this.closeBraceToken;
}
return undefined;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitSymbolSet(this); }
}
/**
* Represents a set of constraints for a right-hand-side of a Production.
* ```grammarkdown
* Production[A] ::
* [+A] A
* [~A] B
* ```
* {@docCategory Nodes}
*/
export class Constraints extends Node<SyntaxKind.Constraints> {
public readonly openBracketToken: Token<SyntaxKind.OpenBracketToken>;
public readonly elements: ReadonlyArray<Argument> | undefined;
public readonly closeBracketToken: Token<SyntaxKind.CloseBracketToken> | undefined;
public constructor(openBracketToken: Token<SyntaxKind.OpenBracketToken>, elements: ReadonlyArray<Argument> | undefined, closeBracketToken: Token<SyntaxKind.CloseBracketToken> | undefined) {
super(SyntaxKind.Constraints);
this.openBracketToken = openBracketToken;
this.elements = elements;
this.closeBracketToken = closeBracketToken;
}
public override get firstChild(): Node | undefined { return this.openBracketToken; }
public override get lastChild(): Node | undefined { return this.closeBracketToken || last(this.elements); }
public override forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined {
return cbNode(this.openBracketToken)
|| (this.elements && forEach(this.elements, cbNode))
|| (this.closeBracketToken && cbNode(this.closeBracketToken));
}
public override * children(): IterableIterator<Node> {
yield this.openBracketToken;
if (this.elements) yield* this.elements;
if (this.closeBracketToken) yield this.closeBracketToken;
}
public update(elements: ReadonlyArray<Argument> | undefined) {
return elements !== this.elements
? setTextRange(new Constraints(this.openBracketToken, elements, this.closeBracketToken), this.pos, this.end)
: this;
}
protected override get edgeCount() { return 3; }
protected override edgeName(offset: number): string | undefined {
switch (offset) {
case 0: return "openBracketToken";
case 1: return "elements";
case 2: return "closeBracketToken";
}
return undefined;
}
protected override edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined {
switch (offset) {
case 0: return this.openBracketToken;
case 1: return this.elements;
case 2: return this.closeBracketToken;
}
return undefined;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitConstraints(this); }
}
//
// Symbols
//
/** {@docCategory Nodes} */
export abstract class LexicalSymbolBase<TKind extends LexicalSymbolKind> extends Node<TKind> {
}
/**
* Represents a placeholder symbol (`@`) used in some grammars.
* {@docCategory Nodes}
*/
export class PlaceholderSymbol extends LexicalSymbolBase<SyntaxKind.PlaceholderSymbol> {
public readonly placeholderToken: Token<SyntaxKind.AtToken>;
constructor (placeholderToken: Token<SyntaxKind.AtToken>) {
super(SyntaxKind.PlaceholderSymbol);
this.placeholderToken = placeholderToken;
}
public override get firstChild(): Node | undefined { return this.placeholderToken; }
public override get lastChild(): Node | undefined { return this.placeholderToken; }
public override forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined {
return cbNode(this.placeholderToken);
}
public override * children(): IterableIterator<Node> {
yield this.placeholderToken;
}
protected override get edgeCount() { return 1; }
protected override edgeName(offset: number): string | undefined { return offset === 0 ? "placeholderToken" : undefined; }
protected override edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined { return offset === 0 ? this.placeholderToken : undefined; }
protected override accept(visitor: NodeVisitor) { return visitor.visitPlaceholderSymbol(this); }
}
/**
* Represents a range of unicode characters.
* ```grammarkdown
* U+0000 through U+001F
* ```
* {@docCategory Nodes}
*/
export class UnicodeCharacterRange extends LexicalSymbolBase<SyntaxKind.UnicodeCharacterRange> {
public readonly left: UnicodeCharacterLiteral;
public readonly throughKeyword: Token<SyntaxKind.ThroughKeyword>;
public readonly right: UnicodeCharacterLiteral;
public constructor(left: UnicodeCharacterLiteral, throughKeyword: Token<SyntaxKind.ThroughKeyword>, right: UnicodeCharacterLiteral) {
super(SyntaxKind.UnicodeCharacterRange);
this.left = left;
this.throughKeyword = throughKeyword;
this.right = right;
}
public override get firstChild(): Node | undefined { return this.left; }
public override get lastChild(): Node | undefined { return this.right; }
public override forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined {
return cbNode(this.left)
|| cbNode(this.throughKeyword)
|| cbNode(this.right);
}
public override * children(): IterableIterator<Node> {
yield this.left;
yield this.throughKeyword;
yield this.right;
}
public update(left: UnicodeCharacterLiteral, right: UnicodeCharacterLiteral) {
return left !== this.left || right !== this.right
? setTextRange(new UnicodeCharacterRange(left, this.throughKeyword, right), this.pos, this.end)
: this;
}
protected override get edgeCount() { return 3; }
protected override edgeName(offset: number): string | undefined {
switch (offset) {
case 0: return "left";
case 1: return "throughKeyword";
case 2: return "right";
}
}
protected override edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined {
switch (offset) {
case 0: return this.left;
case 1: return this.throughKeyword;
case 2: return this.right;
}
return undefined;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitUnicodeCharacterRange(this); }
}
/**
* The \`but not\` operator allows you to reference a *Nonterminal* on the left, excluding some part of that production.
* ```grammarkdown
* A but not B
* ```
* {@docCategory Nodes}
*/
export class ButNotSymbol extends LexicalSymbolBase<SyntaxKind.ButNotSymbol> {
public readonly left: LexicalSymbol;
public readonly butKeyword: Token<SyntaxKind.ButKeyword> | undefined;
public readonly notKeyword: Token<SyntaxKind.NotKeyword> | undefined;
public readonly right: LexicalSymbol | undefined;
public constructor(left: LexicalSymbol, butKeyword: Token<SyntaxKind.ButKeyword> | undefined, notKeyword: Token<SyntaxKind.NotKeyword> | undefined, right: LexicalSymbol | undefined) {
super(SyntaxKind.ButNotSymbol);
this.left = left;
this.butKeyword = butKeyword;
this.notKeyword = notKeyword;
this.right = right;
}
public override get firstChild(): Node | undefined { return this.left; }
public override get lastChild(): Node | undefined { return this.right || this.notKeyword || this.butKeyword || this.left; }
public override forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined {
return cbNode(this.left)
|| (this.butKeyword && cbNode(this.butKeyword))
|| (this.notKeyword && cbNode(this.notKeyword))
|| (this.right && cbNode(this.right));
}
public override * children(): IterableIterator<Node> {
yield this.left;
if (this.butKeyword) yield this.butKeyword;
if (this.notKeyword) yield this.notKeyword;
if (this.right) yield this.right;
}
public update(left: LexicalSymbol, right: LexicalSymbol | undefined) {
return left !== this.left || right !== this.right
? setTextRange(new ButNotSymbol(left, this.butKeyword, this.notKeyword, right), this.pos, this.end)
: this;
}
protected override get edgeCount() { return 4; }
protected override edgeName(offset: number): string | undefined {
switch (offset) {
case 0: return "left";
case 1: return "butKeyword";
case 2: return "notKeyword";
case 3: return "right";
}
}
protected override edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined {
switch (offset) {
case 0: return this.left;
case 1: return this.butKeyword;
case 2: return this.notKeyword;
case 3: return this.right;
}
return undefined;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitButNotSymbol(this); }
}
/**
* Represents a section of Prose, which indicates handling of syntax outside the scope of the Grammarkdown parser.
* ```grammarkdown
* > This is a section of Prose with |Nonterminals| and `terminals`
* ```
* {@docCategory Nodes}
*/
export class Prose extends LexicalSymbolBase<SyntaxKind.Prose> {
public readonly greaterThanToken: Token<SyntaxKind.GreaterThanToken>;
public readonly fragments: ReadonlyArray<ProseFragment> | undefined;
public constructor(greaterThanToken: Token<SyntaxKind.GreaterThanToken>, fragments: ReadonlyArray<ProseFragment> | undefined) {
super(SyntaxKind.Prose);
this.greaterThanToken = greaterThanToken;
this.fragments = fragments;
}
public override get firstChild(): Node | undefined { return this.greaterThanToken; }
public override get lastChild(): Node | undefined { return last(this.fragments) || this.greaterThanToken; }
public override forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined {
return cbNode(this.greaterThanToken)
|| (this.fragments && forEach(this.fragments, cbNode));
}
public override * children(): IterableIterator<Node> {
yield this.greaterThanToken;
if (this.fragments) yield* this.fragments;
}
public update(fragments: ReadonlyArray<ProseFragment> | undefined) {
return fragments !== this.fragments
? setTextRange(new Prose(this.greaterThanToken, fragments), this.pos, this.end)
: this;
}
protected override get edgeCount() { return 2; }
protected override edgeName(offset: number): string | undefined {
switch (offset) {
case 0: return "greaterThanToken";
case 1: return "fragments";
}
return undefined;
}
protected override edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined {
switch (offset) {
case 0: return this.greaterThanToken;
case 1: return this.fragments;
}
return undefined;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitProse(this); }
}
/**
* Represents a set of symbols used to restrict a `but not` operator.
* ```grammarkdown
* A but not one of `a` or `b`
* ```
* {@docCategory Nodes}
*/
export class OneOfSymbol extends LexicalSymbolBase<SyntaxKind.OneOfSymbol> {
public readonly oneKeyword: Token<SyntaxKind.OneKeyword>;
public readonly ofKeyword: Token<SyntaxKind.OfKeyword> | undefined;
public readonly symbols: ReadonlyArray<LexicalSymbol> | undefined;
public constructor(oneKeyword: Token<SyntaxKind.OneKeyword>, ofKeyword: Token<SyntaxKind.OfKeyword> | undefined, symbols: ReadonlyArray<LexicalSymbol> | undefined) {
super(SyntaxKind.OneOfSymbol);
this.oneKeyword = oneKeyword;
this.ofKeyword = ofKeyword;
this.symbols = symbols;
}
public override get firstChild(): Node | undefined { return this.oneKeyword; }
public override get lastChild(): Node | undefined { return last(this.symbols) || this.ofKeyword || this.oneKeyword; }
public override forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined {
return cbNode(this.oneKeyword)
|| (this.ofKeyword && cbNode(this.ofKeyword))
|| (this.symbols && forEach(this.symbols, cbNode));
}
public override * children(): IterableIterator<Node> {
yield this.oneKeyword;
if (this.ofKeyword) yield this.ofKeyword;
if (this.symbols) yield* this.symbols;
}
public update(symbols: ReadonlyArray<LexicalSymbol> | undefined) {
return symbols !== this.symbols
? setTextRange(new OneOfSymbol(this.oneKeyword, this.ofKeyword, symbols), this.pos, this.end)
: this;
}
protected override get edgeCount() { return 3; }
protected override edgeName(offset: number): string | undefined {
switch (offset) {
case 0: return "oneKeyword";
case 1: return "ofKeyword";
case 2: return "symbols";
}
return undefined;
}
protected override edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined {
switch (offset) {
case 0: return this.oneKeyword;
case 1: return this.ofKeyword;
case 2: return this.symbols;
}
return undefined;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitOneOfSymbol(this); }
}
/** {@docCategory Nodes} */
export class InvalidSymbol extends LexicalSymbolBase<SyntaxKind.InvalidSymbol> {
public constructor() {
super(SyntaxKind.InvalidSymbol);
}
protected override accept(visitor: NodeVisitor) { return visitor.visitInvalidSymbol(this); }
}
/** {@docCategory Nodes} */
export type LexicalSymbol =
| PrimarySymbol
| Assertion
| PlaceholderSymbol
| UnicodeCharacterRange
| ButNotSymbol
| Prose
| OneOfSymbol
| InvalidSymbol
;
//
// Primary Symbols
//
/** {@docCategory Nodes} */
export abstract class PrimarySymbolBase<TKind extends PrimarySymbolKind> extends LexicalSymbolBase<TKind> {
}
//
// Optional Symbols
//
/** {@docCategory Nodes} */
export abstract class OptionalSymbolBase<TKind extends OptionalSymbolKind> extends PrimarySymbolBase<TKind> {
public readonly questionToken: Token<SyntaxKind.QuestionToken> | undefined;
public constructor(kind: TKind, questionToken: Token<SyntaxKind.QuestionToken> | undefined) {
super(kind);
this.questionToken = questionToken;
}
}
/**
* Represents a terminal token in the grammar.
* ```grammarkdown
* `yield` `*`?
* ```
* {@docCategory Nodes}
*/
export class Terminal extends OptionalSymbolBase<SyntaxKind.Terminal> {
public readonly literal: UnicodeCharacterLiteral | TerminalLiteral;
public constructor(literal: UnicodeCharacterLiteral | TerminalLiteral, questionToken: Token<SyntaxKind.QuestionToken> | undefined) {
super(SyntaxKind.Terminal, questionToken);
this.literal = literal;
}
public override get firstChild(): Node | undefined { return this.literal; }
public override get lastChild(): Node | undefined { return this.questionToken ?? this.literal; }
public override forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined {
return cbNode(this.literal)
|| (this.questionToken && cbNode(this.questionToken));
}
public override * children(): IterableIterator<Node> {
yield this.literal;
if (this.questionToken) yield this.questionToken;
}
public update(literal: UnicodeCharacterLiteral | TerminalLiteral, questionToken: Token<SyntaxKind.QuestionToken> | undefined) {
return literal !== this.literal || questionToken !== this.questionToken
? setTextRange(new Terminal(literal, questionToken), this.pos, this.end)
: this;
}
protected override get edgeCount() { return 2; }
protected override edgeName(offset: number): string | undefined {
switch (offset) {
case 0: return "literal";
case 1: return "questionToken";
}
}
protected override edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined {
switch (offset) {
case 0: return this.literal;
case 1: return this.questionToken;
}
}
protected override accept(visitor: NodeVisitor) { return visitor.visitTerminal(this); }
}
/**
* Represents a non-terminal reference to another Production.
* ```grammarkdown
* IdentifierReference[~Yield, ~Await]
* ```
* {@docCategory Nodes}
*/
export class Nonterminal extends OptionalSymbolBase<SyntaxKind.Nonterminal> {
public readonly name: Identifier;
public readonly argumentList: ArgumentList | undefined;
public constructor(name: Identifier, argumentList: ArgumentList | undefined, questionToken: Token<SyntaxKind.QuestionToken> | undefined) {
super(SyntaxKind.Nonterminal, questionToken);
this.name = name;
this.argumentList = argumentList;
}
public override get firstChild(): Node | undefined { return this.name; }
public override get lastChild(): Node | undefined { return this.questionToken || this.argumentList || this.name; }
public override forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined {
return cbNode(this.name)
|| (this.argumentList && cbNode(this.argumentList))
|| (this.questionToken && cbNode(this.questionToken));
}
public override * children(): IterableIterator<Node> {
yield this.name;
if (this.argumentList) yield this.argumentList;
if (this.questionToken) yield this.questionToken;
}
public update(name: Identifier, argumentList: ArgumentList | undefined) {
return name !== this.name || argumentList !== this.argumentList
? setTextRange(new Nonterminal(name, argumentList, this.questionToken), this.pos, this.end)
: this;
}
protected override get edgeCount() { return 3; }
protected override edgeName(offset: number): string | undefined {
switch (offset) {
case 0: return "name";
case 1: return "argumentList";
case 2: return "questionToken";
}
return undefined;
}
protected override edgeValue(offset: number): Node | ReadonlyArray<Node> | undefined {
switch (offset) {
case 0: return this.name;
case 1: return this.argumentList;
case 2: return this.questionToken;
}
return undefined;
}
protected override accept(visitor: NodeVisitor) { return visitor.visitNonterminal(this); }
}
/** {@docCategory Nodes} */
export type OptionalSymbol =
| Terminal
| Nonterminal
;
/** {@docCategory Nodes} */
export type PrimarySymbol =
| OptionalSymbol;
//
// Assertions
//
/** {@docCategory Nodes} */
export abstract class AssertionBase<TKind extends AssertionKind, TBracket extends SyntaxKind.OpenBracketToken | SyntaxKind.OpenBracketGreaterThanToken> extends LexicalSymbolBase<TKind> {
public readonly openBracketToken: Token<TBracket>;
public readonly closeBracketToken: Token<SyntaxKind.CloseBracketToken> | undefined;
public constructor(kind: TKind, openBracketToken: Token<TBracket>, closeBracketToken: Token<SyntaxKind.CloseBracketToken> | undefined) {
super(kind);
this.openBracketToken = openBracketToken;
this.closeBracketToken = closeBracketToken;
}
public override get firstChild(): Node | undefined { return this.openBracketToken; }
public abstract override get lastChild(): Node | undefined;
}
/**
* Represents the `empty` assertion, which matches exactly zero tokens.
* ```grammarkdown
* [empty]
* ```
* {@docCategory Nodes}
*/
export class EmptyAssertion extends AssertionBase<SyntaxKind.EmptyAssertion, SyntaxKind.OpenBracketToken> {
public readonly emptyKeyword: Token<SyntaxKind.EmptyKeyword>;
public constructor(openBracketToken: Token<SyntaxKind.OpenBracketToken>, emptyKeyword: Token<SyntaxKind.EmptyKeyword>, closeBracketToken: Token<SyntaxKind.CloseBracketToken> | undefined) {
super(SyntaxKind.EmptyAssertion, openBracketToken, closeBracketToken);
this.emptyKeyword = emptyKeyword;
}
public override get lastChild(): Node | undefined { return this.closeBracketToken || this.emptyKeyword; }