-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
pexprs-main.js
188 lines (148 loc) · 3.76 KB
/
pexprs-main.js
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
import {UnicodeCategories} from './UnicodeCategories.js';
import * as common from './common.js';
// --------------------------------------------------------------------
// Private stuff
// --------------------------------------------------------------------
// General stuff
export class PExpr {
constructor() {
if (this.constructor === PExpr) {
throw new Error("PExpr cannot be instantiated -- it's abstract");
}
}
// Set the `source` property to the interval containing the source for this expression.
withSource(interval) {
if (interval) {
this.source = interval.trimmed();
}
return this;
}
}
// Any
export const any = Object.create(PExpr.prototype);
// End
export const end = Object.create(PExpr.prototype);
// Terminals
export class Terminal extends PExpr {
constructor(obj) {
super();
this.obj = obj;
}
}
// Ranges
export class Range extends PExpr {
constructor(from, to) {
super();
this.from = from;
this.to = to;
// If either `from` or `to` is made up of multiple code units, then
// the range should consume a full code point, not a single code unit.
this.matchCodePoint = from.length > 1 || to.length > 1;
}
}
// Parameters
export class Param extends PExpr {
constructor(index) {
super();
this.index = index;
}
}
// Alternation
export class Alt extends PExpr {
constructor(terms) {
super();
this.terms = terms;
}
}
// Extend is an implementation detail of rule extension
export class Extend extends Alt {
constructor(superGrammar, name, body) {
const origBody = superGrammar.rules[name].body;
super([body, origBody]);
this.superGrammar = superGrammar;
this.name = name;
this.body = body;
}
}
// Splice is an implementation detail of rule overriding with the `...` operator.
export class Splice extends Alt {
constructor(superGrammar, ruleName, beforeTerms, afterTerms) {
const origBody = superGrammar.rules[ruleName].body;
super([...beforeTerms, origBody, ...afterTerms]);
this.superGrammar = superGrammar;
this.ruleName = ruleName;
this.expansionPos = beforeTerms.length;
}
}
// Sequences
export class Seq extends PExpr {
constructor(factors) {
super();
this.factors = factors;
}
}
// Iterators and optionals
export class Iter extends PExpr {
constructor(expr) {
super();
this.expr = expr;
}
}
export class Star extends Iter {}
export class Plus extends Iter {}
export class Opt extends Iter {}
Star.prototype.operator = '*';
Plus.prototype.operator = '+';
Opt.prototype.operator = '?';
Star.prototype.minNumMatches = 0;
Plus.prototype.minNumMatches = 1;
Opt.prototype.minNumMatches = 0;
Star.prototype.maxNumMatches = Number.POSITIVE_INFINITY;
Plus.prototype.maxNumMatches = Number.POSITIVE_INFINITY;
Opt.prototype.maxNumMatches = 1;
// Predicates
export class Not extends PExpr {
constructor(expr) {
super();
this.expr = expr;
}
}
export class Lookahead extends PExpr {
constructor(expr) {
super();
this.expr = expr;
}
}
// "Lexification"
export class Lex extends PExpr {
constructor(expr) {
super();
this.expr = expr;
}
}
// Rule application
export class Apply extends PExpr {
constructor(ruleName, args = []) {
super();
this.ruleName = ruleName;
this.args = args;
}
isSyntactic() {
return common.isSyntactic(this.ruleName);
}
// This method just caches the result of `this.toString()` in a non-enumerable property.
toMemoKey() {
if (!this._memoKey) {
Object.defineProperty(this, '_memoKey', {value: this.toString()});
}
return this._memoKey;
}
}
// Unicode character
export class UnicodeChar extends PExpr {
constructor(category) {
super();
this.category = category;
this.pattern = UnicodeCategories[category];
}
}