-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmyna.ts
1175 lines (997 loc) · 49.2 KB
/
myna.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
// Myna Parsing Library
// Copyright (c) 2016 Christopher Diggins
// Usage permitted under terms of MIT License
// A parsing combinator library for JavaScript/TypeScript based on the PEG formalism.
// For more information see http://www.github.com/cdiggins/myna-parser
export namespace Myna
{
// A parser error class
export class ParserError extends Error {
type = 'ParserError'
constructor(message?: string) {
super(message);
}
}
//====================================================================================
// Internal variables used by the Myna library
// A lookup table of all grammars registered with the Myna module
export const grammars = {}
// A lookup table of all named rules registered with the Myna module
export const allRules = {}
// A lookup table of parsing functions for each registered grammar
export const parsers = {}
//===========================================================================
// class ParseLocation
// Used to indicate the location of the parser to the user in a pleasant way.
export class ParseLocation
{
lineNum:number = 0;
colNum:number = 0;
lineStart:number = 0;
lineEnd:number = 0;
lineText:string;
pointerText:string;
constructor(
public input:string,
public index:number)
{
let r1 = 0;
let r2 = 1;
for (let i=0; i < this.index; ++i) {
if (this.input.charCodeAt(i) == 13) {
this.lineStart = i;
r1++;
}
if (this.input.charCodeAt(i) == 10) {
this.lineStart = i;
r2++;
}
}
for (this.lineEnd=this.index; this.lineEnd < this.input.length; ++this.lineEnd)
{
if (this.input.charCodeAt(this.lineEnd) == 13 || this.input.charCodeAt(this.lineEnd) == 10)
break;
}
this.lineNum = r1 > r2 ? r1 : r2;
this.colNum = this.index - this.lineStart;
this.lineText = this.input.substring(this.lineStart, this.lineEnd);
this.pointerText = Array(this.colNum).join(' ') + "^";
}
toString() : string {
return "Index " + this.index
+ ", Line " + this.lineNum
+ ", Column " + this.colNum
+ "\n" + this.lineText
+ "\n" + this.pointerText;
}
}
//===========================================================================
// class ParseState
// This stores the state of the parser and is passed to the parse and match functions.
export class ParseState
{
length:number = 0;
rules:AstRule[] = [];
constructor(
public input:string,
public index:number,
public nodes:AstNode[])
{
this.length = this.input.length;
}
// Returns an object that representation of the location.
get location() : ParseLocation {
return new ParseLocation(this.input, this.index);
}
}
//===============================================================
// RuleType union of Rule, string, and boolean
// For convenience this enables strings and boolean to be used interchangably with Rules in the combinators.
export type RuleType = Rule | string | boolean;
// Represents a node in the generated parse tree. These nodes are returned by the Rule.parse function. If a Rule
// has the "_createAstNode" field set to true (because you created the rule using the ".ast" property), then the
// generated node will also be added to the constructed parse tree.
export class AstNode
{
// The list of child nodes in the parse tree.
// This is not allocated unless used, to minimize memory consumption
children: AstNode[] = null;
// Constructs a new node associated with the given rule.
constructor(
public rule:Rule,
public input:string,
public start:number=0,
public end:number=-1)
{ }
// Returns the name of the rule associated with this node
get name() : string { return this.rule != null ? this.rule.name : "unnamed"; }
// Returns the name of the rule, preceded by the grammar name, associated with this node
get fullName() : string { return this.rule != null ? this.rule.fullName : "unnamed"; }
// Returns the parsed text associated with this node's start and end locations
get allText() : string { return this.input.slice(this.start, this.end); }
// Returns true if this node has no children
get isLeaf() : boolean { return this.children == null || this.children.length == 0; }
// Returns the first child with the given name, or null if no named child is found.
child(name:string) : AstNode {
if (this.children)
for (const c of this.children)
if (c.name == name) return c;
return null;
}
// The position of the first child, or the end position for the entire node if no children
get _firstChildStart() : number {
return this.isLeaf ? this.end : this.children[0].start;
}
// The end position of the last child, or the end position for the entire node if no children
get _lastChildEnd() : number {
return this.isLeaf ? this.end : this.children[0].end;
}
// Returns the text before the children, or if no children returns the entire text.
get beforeChildrenText() : string {
return this.input.slice(this.start, this._firstChildStart);
}
// Returns the text after the children, or if no children returns the empty string.
get afterChildrenText() : string {
return this.input.slice(this._lastChildEnd, this.end);
}
// Returns the text from the beginning of the first child to the end of the last child.
get allChildrenText() : string {
return this.input.slice(this._firstChildStart, this._lastChildEnd);
}
// Returns the AST as a string for debug and test purposes
toString() : string {
let contents = this.isLeaf
? this.allText
: this.children.map(c => c.toString()).join(" ");
return "(" + this.rule.name + ': ' + contents + ")";
}
}
//===============================================================
// class Rule
// A Rule is both a rule in the PEG grammar and a parser. The parse function takes
// a particular parse location (in either a string, or array of tokens) and will return
// the location of the end of the parse if successful or null if not successful.
export class Rule
{
// Identifies individual rule
name:string = "";
// Identifies the grammar that this rule belongs to
grammarName:string = "";
// Identifies types of rules. Rules can have "types" that are different than the class name
type:string = "";
// Used to provide access to the name of the class
className:string = "Rule"
// Indicates whether generated nodes should be added to the abstract syntax tree
_createAstNode:boolean = false;
// A parser function, computed in a rule's constructor. If successful returns either the original or a new
// ParseState object. If it fails it returns null.
parser : (ParseState)=>boolean = null;
// A lexer function, computed in a rule's constructor. The lexer may update the ParseState if successful.
// If it fails it is required that the lexer restore the ParseState index to the previous state.
// Lexers should only update the index.
lexer : (ParseState)=>boolean = null;
// Creates a node with the given children. This is used to manually build parts of the AST.
node(text: string = '', ...children): AstNode {
let r = new AstNode(this, text, 0, text.length);
r.children = children;
return r;
}
// Parses a string into an AST node
parse(s: string): AstNode {
return Myna.parse(this, s);
}
// Constructor
// Note: child-rules are exposed as a public field
constructor(
public rules:Rule[])
{ }
// Sets the name of the rule, and the grammar
// Warning: this modifies the rule, use "copy" first if you don't want to update the rule.
setName(grammarName:string, ruleName:string) : Rule {
this.grammarName = grammarName;
this.name = ruleName;
return this;
}
// Returns a default definition of the rule
get definition() : string {
return this.className + "(" + this.rules.map((r) => r.toString()).join(", ") + ")";
}
// Returns the name of the rule preceded by the grammar name and a "."
get fullName() : string {
return this.grammarName + "." + this.name
}
// Returns either the name of the rule, or it's definition
get nameOrDefinition() : string {
return this.name
? this.fullName
: this.definition;
}
// Returns a string representation of the rule
toString() : string {
return this.nameOrDefinition;
}
// Returns the first child rule
get firstChild() : Rule {
return this.rules[0];
}
// Sets the "type" associated with the rule.
// This is useful for tracking how a rule was created.
setType(type : string) : Rule {
this.type = type;
return this;
}
// Returns a copy of this rule with default values for all fields.
// Note: Every new rule class must override cloneImplemenation
cloneImplementation() : Rule {
throw new Error("Missing override for cloneImplementation");
}
// Returns a copy of this rule with all fields copied.
get copy() : Rule {
const r = this.cloneImplementation();
if (typeof(r) !== typeof(this))
throw new Error("Error in implementation of cloneImplementation: not returning object of correct type");
r.name = this.name;
r.grammarName = this.grammarName;
r._createAstNode = this._createAstNode;
return r;
}
// Returns true if any of the child rules are "ast rules" meaning they create nodes in the parse tree.
get hasAstChildRule() : boolean {
return this.rules.filter(r => r.createsAstNode).length > 0;
}
// Returns true if this rule when parsed successfully will create a node in the parse tree.
// Some rules will override this function.
get createsAstNode() : boolean {
return this._createAstNode;
}
// Returns true if this rule doesn't advance the input
get nonAdvancing() : boolean {
return false;
}
// Returns a string that describes the AST nodes created by this rule.
// Will throw an exception if this is not a valid AST rule (this.isAstRule != true)
astRuleDefn(inSeq:boolean=false, inChoice:boolean=false) : string {
const rules = this.rules.filter(r => r.createsAstNode);
if (!rules.length)
return this.name;
if (rules.length == 1) {
let result = rules[0].astRuleNameOrDefn(inSeq, inChoice);
if (this instanceof Quantified)
result += "[" + this.min + "," + this.max + "]";
return result;
}
if (this instanceof Sequence) {
const tmp = rules.map(r => r.astRuleNameOrDefn(true, false)).join(",");
if (inSeq) return tmp;
return "seq(" + tmp + ")";
}
if (this instanceof Choice) {
const tmp = rules.map(r => r.astRuleNameOrDefn(false, true)).join(",");
if (inChoice) return tmp;
return "choice(" + tmp + ")";
}
throw new Error("Internal error: not a valid AST rule");
}
// Returns a string that is either the name of the AST parse node, or a definition
// (schema) describing the makeup of the rules.
astRuleNameOrDefn(inSeq:boolean = false, inChoice:boolean = false) : string {
if (this._createAstNode)
return this.name;
return this.astRuleDefn(inSeq, inChoice);
}
//======================================================
// Extensions to support method/property chaining.
// This is also known as a fluent API syntax
get opt() : Rule { return opt(this); }
get zeroOrMore() : Rule { return zeroOrMore(this); }
get oneOrMore() : Rule { return oneOrMore(this); }
get at() : Rule { return at(this); }
get not() : Rule { return not(this); }
get advance() : Rule { return this.then(advance); }
get ws() : Rule { return this.then(ws); }
get all() : Rule { return this.then(all); }
get end() : Rule { return this.then(end); }
get assert() : Rule { return assert(this); }
get ast() : Rule { return new AstRule(this); }
then(r:RuleType) : Rule { return seq(this, r); }
thenAt(r:RuleType) : Rule { return this.then(at(r)); }
thenNot(r:RuleType) : Rule { return this.then(not(r)); }
or(r:RuleType) : Rule { return choice(this, r); }
until(r:RuleType) : Rule { return repeatWhileNot(this, r); }
untilPast(r:RuleType) : Rule { return repeatUntilPast(this, r); }
repeat(count:number) : Rule { return repeat(this, count); }
quantified(min:number, max:number) : Rule { return quantified(this, min, max); }
delimited(delimiter:RuleType) : Rule { return delimited(this, delimiter); }
unless(r:RuleType) : Rule { return unless(this, r); }
}
//===============================================================
// Rule derived classes
// These are the core Rule classes of Myna. Normally you would not use theses directly but use the factory methods
// If you fork this code, think twice before adding new classes here. Maybe you can implement your new Rule
// in terms of functions or other low-level rules. Then you can be happy knowing that the same code is being
// re-used and tested all the time.
// Creates a node in the AST tree
export class AstRule extends Rule
{
type = 'ast';
className = "AstRule";
constructor(public r:Rule) {
super([r]);
this._createAstNode = true;
this.parser = (p : ParseState) => {
const originalIndex = p.index;
const originalNodes = p.nodes;
p.nodes = [];
p.rules.push(this);
if (!r.parser(p)) {
p.rules.pop();
p.nodes = originalNodes;
p.index = originalIndex;
return false;
}
else {
p.rules.pop();
let node = new AstNode(this, p.input, originalIndex, p.index);
node.children = p.nodes;
p.nodes = originalNodes;
p.nodes.push(node);
return true;
}
}
this.lexer = r.lexer;
}
}
// Matches a series of rules in order. Succeeds only if all sub-rules succeed.
export class Sequence extends Rule
{
type = "seq";
className = "Sequence";
constructor(public rule1:Rule, public rule2:Rule) {
super([rule1, rule2]);
this.parser = (p : ParseState) => {
const originalCount = p.nodes.length;
const originalIndex = p.index;
if (rule1.parser(p) === false)
// The first parser will restore everything automatically
return false;
if (rule2.parser(p) === false) {
// Any created nodes need to be popped off the list
if (p.nodes.length !== originalCount)
p.nodes.splice(-1, p.nodes.length - originalCount);
// Assure that the parser is restored to its original position
p.index = originalIndex;
return false;
}
return true;
};
this.lexer = (p : ParseState) => {
const original = p.index;
if (rule1.lexer(p) === false)
return false;
if (rule2.lexer(p) === false) {
p.index = original;
return false;
}
return true;
}
// When none of the child rules create a node, we can use the lexer to parse
if (!this.createsAstNode)
this.parser = this.lexer;
}
get definition() : string {
let result = this.rules.map((r) => r.toString()).join(" ");
if (this.rules.length > 1)
result = "(" + result + ")";
return result;
}
get nonAdvancing() : boolean {
return this.rules.every(r => r.nonAdvancing);
}
get createsAstNode() : boolean {
return this._createAstNode || this.hasAstChildRule;
}
cloneImplementation() : Rule { return new Sequence(this.rule1, this.rule2); }
}
// Tries to match each rule in order until one succeeds. Succeeds if any of the sub-rules succeed.
export class Choice extends Rule
{
type = "choice";
className = "Choice";
constructor(public rule1:Rule, public rule2:Rule) {
super([rule1, rule2]);
this.parser = (p : ParseState) => {
return rule1.parser(p) || rule2.parser(p);
};
this.lexer = (p : ParseState) => {
return rule1.lexer(p) || rule2.lexer(p);
}
// When none of the child rules create a node, we can use the lexer to parse
if (!this.createsAstNode)
this.parser = this.lexer;
}
get definition() : string {
let result = this.rules.map((r) => r.toString()).join(" / ");
if (this.rules.length > 1)
result = "(" + result + ")";
return result;
}
get nonAdvancing() : boolean {
return this.rules.every(r => r.nonAdvancing);
}
get createsAstNode() : boolean {
return this._createAstNode || this.hasAstChildRule;
}
cloneImplementation() : Rule { return new Choice(this.rule1, this.rule2); }
}
// A generalization of several rules such as zeroOrMore (0+), oneOrMore (1+), opt(0 or 1),
// When matching with an unbounded upper limit set the maxium to -1
export class Quantified extends Rule
{
type = "quantified";
className = "Quantified";
constructor(rule:Rule, public min:number=0, public max:number=Infinity) {
super([rule]);
if (max === Infinity && rule.nonAdvancing)
throw new Error("Rule would create an infinite loop");
this.parser = (p : ParseState) => {
const originalCount = p.nodes.length;
const originalIndex = p.index;
for (let i=0; i < max; ++i) {
// If parsing the rule fails, we return the last result, or failed
// if the minimum number of matches is not met.
if (rule.parser(p) === false) {
if (i >= min)
return true;
// Any created nodes need to be popped off the list
if (p.nodes.length !== originalCount)
p.nodes.splice(-1, p.nodes.length - originalCount);
// Assure that the parser is restored to its original position
p.index = originalIndex;
return false;
}
// Check for progress, to assure we aren't hitting an infinite loop
debugAssert(max !== Infinity || p.index !== originalIndex, this);
}
return true;
};
this.lexer = (p : ParseState) => {
const originalIndex = p.index;
for (let i=0; i < max; ++i) {
if (rule.lexer(p) === false) {
if (i >= min)
return true;
p.index = originalIndex;
return false;
}
// Check for progress, to assure we aren't hitting an infinite loop
debugAssert(max !== Infinity || p.index !== originalIndex, this, "Infinite loop");
}
return true;
};
// When none of the child rules create a node, we can use the lexer to parse
if (!this.createsAstNode)
this.parser = this.lexer;
}
// Used for creating a human readable definition of the grammar.
get definition() : string {
if (this.min == 0 && this.max == 1)
return this.firstChild.toString() + "?";
if (this.min == 0 && this.max == Infinity)
return this.firstChild.toString() + "*";
if (this.min == 1 && this.max == Infinity)
return this.firstChild.toString() + "+";
return this.firstChild.toString() + "{" + this.min + "," + this.max + "}";
}
get createsAstNode() : boolean {
return this._createAstNode || this.hasAstChildRule;
}
cloneImplementation() : Rule { return new Quantified(this.firstChild, this.min, this.max); }
}
// Matches a child rule zero or once.
export class Optional extends Quantified
{
type = "optional";
className = "Optional";
constructor(rule:Rule) {
super(rule, 0, 1);
this.parser = (p : ParseState) => {
rule.parser(p);
return true;
};
this.lexer = (p : ParseState) => {
rule.lexer(p);
return true;
};
// When none of the child rules create a node, we can use the lexer to parse
if (!this.createsAstNode)
this.parser = this.lexer;
}
// Used for creating a human readable definition of the grammar.
get definition() : string {
return this.firstChild.toString() + "?";
}
cloneImplementation() : Rule { return new Optional(this.firstChild); }
}
// Advances the parser by one token unless at the end
export class Advance extends Rule
{
type = "advance";
className = "Advance";
constructor() {
super([]);
this.lexer = (p : ParseState) =>
p.index < p.length ? ++p.index >= 0 : false;
this.parser = this.lexer;
}
get definition() : string { return "<advance>"; }
cloneImplementation() : Rule { return new Advance(); }
}
// Advances the parser by one token if the predicate is true.
export class AdvanceIf extends Rule
{
type = "advanceIf";
className = "AdvanceIf";
constructor(condition:Rule) {
super([condition]);
this.lexer = (p : ParseState) => {
return condition.lexer(p) && p.index < p.length ? ++p.index !== 0 : false;
}
this.parser = this.lexer;
}
get definition() : string { return "advanceIf(" + this.firstChild.toString() + ")"; }
cloneImplementation() : Rule { return new AdvanceIf(this.firstChild); }
}
// Used to match a string in the input string, advances the token.
export class Text extends Rule
{
type = "text";
className = "Text";
constructor(public text:string) {
super([]);
const length = text.length;
const vals:number[] = [];
for (let i=0; i < length; ++i)
vals.push(text.charCodeAt(i));
this.lexer = (p : ParseState) => {
let index = p.index;
if (index + vals.length > p.input.length)
return false;
// TODO: consider pulling the sub-string out of the text.
for (let val of vals)
if (p.input.charCodeAt(index++) !== val)
return false;
p.index = index;
return true;
}
this.parser = this.lexer;
}
get definition() : string { return '"' + escapeChars(this.text) + '"' }
cloneImplementation() : Rule { return new Text(this.text); }
}
// Used to match a string in the input string ignoring case, advances the token.
export class AnyCaseText extends Rule
{
type = "anyCaseText";
className = "AnyCaseText";
constructor(public text:string) {
super([]);
text = text.toLowerCase();
this.text = text;
const length = text.length;
const vals:string[] = [];
for (let i=0; i < length; ++i)
vals.push(text[i]);
this.lexer = (p : ParseState) => {
let index = p.index;
if (index + vals.length > p.input.length)
return false;
for (let val of vals) {
if (p.input[index++].toLowerCase() !== val)
return false;
}
p.index = index;
return true;
}
this.parser = this.lexer;
}
get definition() : string { return 'AnyCase("' + escapeChars(this.text) + '")' }
cloneImplementation() : Rule { return new AnyCaseText(this.text); }
}
// Creates a rule that is defined from a function that generates the rule.
// This allows two rules to have a cyclic relation.
export class Delay extends Rule
{
type = "delay";
className = "Delay";
constructor(public fn:()=>Rule) {
super([]);
let tmpParser = null;
this.parser = (p : ParseState) => (tmpParser ? tmpParser : tmpParser = fn().parser)(p);
let tmpLexer = null;
this.lexer = (p : ParseState) => (tmpLexer ? tmpLexer : tmpLexer = fn().lexer)(p);
}
cloneImplementation() : Rule { return new Delay(this.fn); }
get definition() : string { return "<delay>"; }
// It is assumed that a delay function creates an AST node,
get createsAstNode() : boolean {
return true;
}
}
//====================================================================================================================
// Predicates don't advance the input
// Used to identify rules that do not advance the input
export class NonAdvancingRule extends Rule {
type = "charSet";
constructor(rules:Rule[]) {
super(rules);
}
get nonAdvancing() : boolean {
return true;
}
}
// Returns true if the current token is in the token set.
export class CharSet extends NonAdvancingRule {
type = "charSet";
className = "CharSet";
constructor(public chars:string) {
super([]);
let vals = [];
let length = chars.length;
for (let i=0; i < length; ++i)
vals[i] = chars.charCodeAt(i);
this.lexer = (p : ParseState) =>
// TODO: Try this instead, could be faster.
// chars.indexOf(p.input[p.index]) >= 0;
vals.indexOf(p.input.charCodeAt(p.index)) >= 0;
this.parser = this.lexer;
}
get definition() : string { return "[" + escapeChars(this.chars) + "]"};
cloneImplementation() : Rule { return new CharSet(this.chars); }
}
// Returns true if the current token is within a range of characters, otherwise returns false
export class CharRange extends NonAdvancingRule {
type = "charRange";
className = "CharRange";
constructor(public min:string, public max:string) {
super([]);
let minCode = min.charCodeAt(0);
let maxCode = max.charCodeAt(0);
this.lexer = (p : ParseState) => {
let code = p.input.charCodeAt(p.index);
return code >= minCode && code <= maxCode;
}
this.parser = this.lexer;
}
get definition() : string { return "[" + this.min + ".." + this.max + "]"};
cloneImplementation() : Rule { return new CharRange(this.min, this.max); }
}
// Returns true only if the child rule fails to match.
export class Not extends NonAdvancingRule
{
type = "not";
className = "Not";
constructor(rule:Rule) {
super([rule]);
const childLexer = rule.lexer;
this.lexer = (p : ParseState) => {
if (p.index >= p.length) return true;
let index = p.index;
if (childLexer(p) === false)
return true;
p.index = index;
return false;
}
this.parser = this.lexer;
}
cloneImplementation() : Rule { return new Not(this.firstChild); }
get definition() : string { return "!" + this.firstChild.toString(); }
}
// Returns true only if the child rule matches, but does not advance the parser
export class At extends NonAdvancingRule
{
type = "at";
className = "At";
constructor(rule:Rule) {
super([rule]);
const childLexer = rule.lexer;
this.lexer = (p : ParseState) => {
let index = p.index;
if (childLexer(p) === false)
return false;
p.index = index;
return true;
}
this.parser = this.lexer;
}
cloneImplementation() : Rule { return new At(this.firstChild); }
get definition() : string { return "&" + this.firstChild.toString(); }
}
// Uses a function to return true or not based on the behavior of the predicate rule
export class Predicate extends NonAdvancingRule
{
type = "predicate";
className = "Predicate";
constructor(public fn:(p:ParseState)=>boolean) {
super([]);
this.lexer = fn;
this.parser = this.lexer;
}
cloneImplementation() : Rule { return new Predicate(this.fn); }
get definition() : string { return "<predicate>"; }
}
//===============================================================
// Rule creation function
// Create a rule that matches the text
export function text(text:string) { return new Text(text); }
// Create a rule that matches the text
export function textAnyCase(text:string) { return new AnyCaseText(text); }
// Creates a rule that matches a series of rules in order, and succeeds if they all do
export function seq(...rules:RuleType[]) {
const rs = rules.map(RuleTypeToRule);
if (rs.length == 0) throw new Error("At least one rule is expected when calling `seq`");
if (rs.length == 1) return rs[0];
const rule1 = rs[0];
const rule2 = seq(...rs.slice(1));
if (rule1.nonAdvancing && rule2 instanceof Advance)
return new AdvanceIf(rule1);
else
return new Sequence(rule1, rule2);
}
// Creates a rule that tries to match each rule in order, and succeeds if at least one does
export function choice(...rules:RuleType[]) {
const rs = rules.map(RuleTypeToRule);
if (rs.length == 0) throw new Error("At least one rule is expected when calling `choice`");
if (rs.length == 1) return rs[0];
const rule1 = rs[0];
const rule2 = choice(...rs.slice(1));
if (rule1 instanceof AdvanceIf && rule2 instanceof AdvanceIf)
return new AdvanceIf(choice(rule1.firstChild, rule2.firstChild));
else
return new Choice(rule1, rule2);
}
// Enables Rules to be defined in terms of variables that are defined later on.
// This enables recursive rule definitions.
export function delay(fxn:()=>Rule) { return new Delay(fxn); }
// Parses successfully if the given rule does not match the input at the current location
export function not(rule:RuleType) { return new Not(RuleTypeToRule(rule)); };
// Returns true if the rule successfully matches, but does not advance the parser index.
export function at(rule:RuleType) { return new At(RuleTypeToRule(rule)); };
// Attempts to apply a rule between min and max number of times inclusive. If the maximum is set to Infinity,
// it will attempt to match as many times as it can, but throw an exception if the parser does not advance
export function quantified(rule:RuleType, min:number=0, max:number=Infinity) {
if (min === 0 && max === 1)
return new Optional(RuleTypeToRule(rule));
else
return new Quantified(RuleTypeToRule(rule), min, max);
}
// Attempts to apply the rule 0 or more times. Will always succeed unless the parser does not
// advance, in which case an exception is thrown.
export function zeroOrMore(rule:RuleType) { return quantified(rule).setType("zeroOrMore"); };
// Attempts to apply the rule 1 or more times. Will throw an exception if the parser does not advance.
export function oneOrMore(rule:RuleType) { return quantified(rule, 1).setType("oneOrMore"); }
// Attempts to match a rule 0 or 1 times. Always succeeds.
export function opt(rule:RuleType) { return quantified(rule, 0, 1).setType("optional"); }
// Attempts to apply a rule a precise number of times
export function repeat(rule:RuleType, count:number) { return quantified(rule, count, count).setType("repeat"); }
// Returns true if one of the characters are present. Does not advances the parser position.
export function atChar(chars:string) { return new CharSet(chars); }
// Returns true if none of the characters are present. Does not advances the parser position.
export function notAtChar(chars:string) { return atChar(chars).not; }
// Advances parser if one of the characters are present.
export function char(chars:string) { return atChar(chars).advance; }
// Advances parser if none of the characters are present.
export function notChar(chars:string) { return notAtChar(chars).advance; }
// Advances if one of the characters are present, or returns false
export function atRange(min:string, max:string) { return new CharRange(min, max); }
// Advances if one of the characters are present, or returns false
export function range(min:string, max:string) { return atRange(min, max).advance; }
// Returns true if on of the characters are not in the range, but does not advance the parser position
export function notRange(min:string, max:string) { return range(min, max).not; }
// Repeats a rule zero or more times, with a delimiter between each one.
export function delimited(rule:RuleType, delimiter:RuleType) { return opt(seq(rule, seq(delimiter, rule).zeroOrMore)).setType("delimitedList"); }
// Executes the rule, if the condition is not true
export function unless(rule:RuleType, condition:RuleType) { return seq(not(condition), rule).setType("unless"); }
// Repeats the rule while the condition is not true
export function repeatWhileNot(body:RuleType, condition:RuleType) { return unless(body, condition).zeroOrMore.setType("repeatWhileNot"); }
// Repeats the rule while the condition is not true, but must execute at least once
export function repeatOneOrMoreWhileNot(body:RuleType, condition:RuleType) { return not(condition).then(body).then(repeatWhileNot(body, condition)).setType("repeatOneOrMoreWhileNot"); }
// Repeats the rule until just after the condition is true once
export function repeatUntilPast(body:RuleType, condition:RuleType) { return repeatWhileNot(body, condition).then(condition).setType("repeatUntilPast"); }
// Repeats the rule until just after the condition is true once but must execute at least once
export function repeatOneOrMoreUntilPast(body:RuleType, condition:RuleType) { return not(condition).then(body).then(repeatUntilPast(body, condition)).setType("repeatOneOrMoreUntilPast"); }
// Advances the parse state while the rule is not true.
export function advanceWhileNot(rule:RuleType) { return repeatWhileNot(advance, rule).setType("advanceWhileNot"); }
// Advances the parse state while the rule is not true but must execute ast least once
export function advanceOneOrMoreWhileNot(rule:RuleType) { return repeatOneOrMoreWhileNot(advance, rule).setType("advanceOneOrMoreWhileNot"); }
// Advance the parser until just after the rule is executed
export function advanceUntilPast(rule:RuleType) { return repeatUntilPast(advance, rule).setType("advanceUntilPast"); }
// Advance the parser until just after the rule is executed, but must execute at least once
export function advanceOneOrMoreUntilPast(rule:RuleType) { return repeatOneOrMoreUntilPast(advance, rule).setType("advanceOneOrMoreUntilPast"); }
// Advances the parser unless the rule is true.
export function advanceUnless(rule:RuleType) { return advance.unless(rule).setType("advanceUnless"); }
// Parses successfully if the predictate passes
export function predicate(fn:(p:ParseState)=>boolean) { return new Predicate(fn); }
// Executes an action when arrived at and continues
export function action(fn:(p:ParseState)=>void) { return predicate((p)=> { fn(p); return true; }).setType("action"); }
// Logs a message as an action
export function log(msg:string = "") { return action(p=> { console.log(msg); }).setType("log"); }
// Throw a Error if reached
export function err(message) { return action(p => {
const e = new ParserError(message + '\n' + p.location.toString());
throw e;
}).setType("err"); }
// Asserts that the rule is executed
// This has to be embedded in a function because the rule might be in a circular definition.
export function assert(rule:RuleType) { return choice(rule, err("Expected: " + RuleTypeToRule(rule))); }
// If first part of a guarded sequence passes then each subsequent rule must pass as well
// otherwise an exception occurs. This helps create parsers that fail fast, and thus provide
// better feedback for badly formed input.
export function guardedSeq(condition:RuleType, ...rules:RuleType[]) {
return seq(condition, seq(...rules.map((r) => assert(r)))).setType("guardedSeq");
}
// Parses the given rule surrounded by double quotes
export function doubleQuoted(rule:RuleType) { return guardedSeq("\"", rule, assert("\"")).setType("doubleQuoted"); }
// Parses a double quoted string, taking into account special escape rules
export function doubleQuotedString(escape:RuleType) { return doubleQuoted(choice(escape, notChar('"').zeroOrMore)).setType("doubleQuotedString"); }
// Parses the given rule surrounded by single quotes
export function singleQuoted(rule:RuleType) { return guardedSeq("'", rule, assert("'")).setType("singleQuoted"); }
// Parses a singe quoted string, taking into account special escape rules
export function singleQuotedString(escape:RuleType) { return singleQuoted(choice(escape, notChar("'").zeroOrMore)).setType("singleQuotedString"); }
// Parses the given rule surrounded by parentheses, and consumes whitespace
export function parenthesized(rule:RuleType) { return guardedSeq("(", ws, rule, ws, ")").setType("parenthesized"); }
// Parses the given rule surrounded by curly braces, and consumes whitespace
export function braced(rule:RuleType) { return guardedSeq("{", ws, rule, ws, "}").setType("braced"); }
// Parses the given rule surrounded by square brackets, and consumes whitespace
export function bracketed(rule:RuleType) { return guardedSeq("[", ws, rule, ws, "]").setType("bracketed"); }
// Parses the given rule surrounded by angle brackets, and consumes whitespace
export function tagged(rule:RuleType) { return guardedSeq("<", ws, rule, ws, ">").setType("tagged"); }
// A complete identifier, with no other letters or numbers
export function keyword(text:string) { return seq(text, not(identifierNext)).setType("keyword"); }