/
Grammar.js
379 lines (339 loc) · 11.6 KB
/
Grammar.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
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
import {Matcher} from './Matcher.js';
import {Semantics} from './Semantics.js';
import * as common from './common.js';
import * as errors from './errors.js';
import * as pexprs from './pexprs.js';
// --------------------------------------------------------------------
// Private stuff
// --------------------------------------------------------------------
const SPECIAL_ACTION_NAMES = ['_iter', '_terminal', '_nonterminal', '_default'];
function getSortedRuleValues(grammar) {
return Object.keys(grammar.rules)
.sort()
.map(name => grammar.rules[name]);
}
// Until ES2019, JSON was not a valid subset of JavaScript because U+2028 (line separator)
// and U+2029 (paragraph separator) are allowed in JSON string literals, but not in JS.
// This function properly encodes those two characters so that the resulting string is
// represents both valid JSON, and valid JavaScript (for ES2018 and below).
// See https://v8.dev/features/subsume-json for more details.
const jsonToJS = str => str.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
let ohmGrammar;
let buildGrammar;
export class Grammar {
constructor(name, superGrammar, rules, optDefaultStartRule) {
this.name = name;
this.superGrammar = superGrammar;
this.rules = rules;
if (optDefaultStartRule) {
if (!(optDefaultStartRule in rules)) {
throw new Error(
"Invalid start rule: '" +
optDefaultStartRule +
"' is not a rule in grammar '" +
name +
"'",
);
}
this.defaultStartRule = optDefaultStartRule;
}
this._matchStateInitializer = undefined;
this.supportsIncrementalParsing = true;
}
matcher() {
return new Matcher(this);
}
// Return true if the grammar is a built-in grammar, otherwise false.
// NOTE: This might give an unexpected result if called before BuiltInRules is defined!
isBuiltIn() {
return this === Grammar.ProtoBuiltInRules || this === Grammar.BuiltInRules;
}
equals(g) {
if (this === g) {
return true;
}
// Do the cheapest comparisons first.
if (
g == null ||
this.name !== g.name ||
this.defaultStartRule !== g.defaultStartRule ||
!(this.superGrammar === g.superGrammar || this.superGrammar.equals(g.superGrammar))
) {
return false;
}
const myRules = getSortedRuleValues(this);
const otherRules = getSortedRuleValues(g);
return (
myRules.length === otherRules.length &&
myRules.every((rule, i) => {
return (
rule.description === otherRules[i].description &&
rule.formals.join(',') === otherRules[i].formals.join(',') &&
rule.body.toString() === otherRules[i].body.toString()
);
})
);
}
match(input, optStartApplication) {
const m = this.matcher();
m.replaceInputRange(0, 0, input);
return m.match(optStartApplication);
}
trace(input, optStartApplication) {
const m = this.matcher();
m.replaceInputRange(0, 0, input);
return m.trace(optStartApplication);
}
createSemantics() {
return Semantics.createSemantics(this);
}
extendSemantics(superSemantics) {
return Semantics.createSemantics(this, superSemantics._getSemantics());
}
// Check that every key in `actionDict` corresponds to a semantic action, and that it maps to
// a function of the correct arity. If not, throw an exception.
_checkTopDownActionDict(what, name, actionDict) {
const problems = [];
// eslint-disable-next-line guard-for-in
for (const k in actionDict) {
const v = actionDict[k];
const isSpecialAction = SPECIAL_ACTION_NAMES.includes(k);
if (!isSpecialAction && !(k in this.rules)) {
problems.push(`'${k}' is not a valid semantic action for '${this.name}'`);
continue;
}
if (typeof v !== 'function') {
problems.push(`'${k}' must be a function in an action dictionary for '${this.name}'`);
continue;
}
const actual = v.length;
const expected = this._topDownActionArity(k);
if (actual !== expected) {
let details;
if (k === '_iter' || k === '_nonterminal') {
details =
`it should use a rest parameter, e.g. \`${k}(...children) {}\`. ` +
'NOTE: this is new in Ohm v16 — see https://ohmjs.org/d/ati for details.';
} else {
details = `expected ${expected}, got ${actual}`;
}
problems.push(`Semantic action '${k}' has the wrong arity: ${details}`);
}
}
if (problems.length > 0) {
const prettyProblems = problems.map(problem => '- ' + problem);
const error = new Error(
[
`Found errors in the action dictionary of the '${name}' ${what}:`,
...prettyProblems,
].join('\n'),
);
error.problems = problems;
throw error;
}
}
// Return the expected arity for a semantic action named `actionName`, which
// is either a rule name or a special action name like '_nonterminal'.
_topDownActionArity(actionName) {
// All special actions have an expected arity of 0, though all but _terminal
// are expected to use the rest parameter syntax (e.g. `_iter(...children)`).
// This is considered to have arity 0, i.e. `((...args) => {}).length` is 0.
return SPECIAL_ACTION_NAMES.includes(actionName) ?
0 :
this.rules[actionName].body.getArity();
}
_inheritsFrom(grammar) {
let g = this.superGrammar;
while (g) {
if (g.equals(grammar, true)) {
return true;
}
g = g.superGrammar;
}
return false;
}
toRecipe(superGrammarExpr = undefined) {
const metaInfo = {};
// Include the grammar source if it is available.
if (this.source) {
metaInfo.source = this.source.contents;
}
let startRule = null;
if (this.defaultStartRule) {
startRule = this.defaultStartRule;
}
const rules = {};
Object.keys(this.rules).forEach(ruleName => {
const ruleInfo = this.rules[ruleName];
const {body} = ruleInfo;
const isDefinition = !this.superGrammar || !this.superGrammar.rules[ruleName];
let operation;
if (isDefinition) {
operation = 'define';
} else {
operation = body instanceof pexprs.Extend ? 'extend' : 'override';
}
const metaInfo = {};
if (ruleInfo.source && this.source) {
const adjusted = ruleInfo.source.relativeTo(this.source);
metaInfo.sourceInterval = [adjusted.startIdx, adjusted.endIdx];
}
const description = isDefinition ? ruleInfo.description : null;
const bodyRecipe = body.outputRecipe(ruleInfo.formals, this.source);
rules[ruleName] = [
operation, // "define"/"extend"/"override"
metaInfo,
description,
ruleInfo.formals,
bodyRecipe,
];
});
// If the caller provided an expression to use for the supergrammar, use that.
// Otherwise, if the supergrammar is a user grammar, use its recipe inline.
let superGrammarOutput = 'null';
if (superGrammarExpr) {
superGrammarOutput = superGrammarExpr;
} else if (this.superGrammar && !this.superGrammar.isBuiltIn()) {
superGrammarOutput = this.superGrammar.toRecipe();
}
const recipeElements = [
...['grammar', metaInfo, this.name].map(JSON.stringify),
superGrammarOutput,
...[startRule, rules].map(JSON.stringify),
];
return jsonToJS(`[${recipeElements.join(',')}]`);
}
// TODO: Come up with better names for these methods.
// TODO: Write the analog of these methods for inherited attributes.
toOperationActionDictionaryTemplate() {
return this._toOperationOrAttributeActionDictionaryTemplate();
}
toAttributeActionDictionaryTemplate() {
return this._toOperationOrAttributeActionDictionaryTemplate();
}
_toOperationOrAttributeActionDictionaryTemplate() {
// TODO: add the super-grammar's templates at the right place, e.g., a case for AddExpr_plus
// should appear next to other cases of AddExpr.
const sb = new common.StringBuffer();
sb.append('{');
let first = true;
// eslint-disable-next-line guard-for-in
for (const ruleName in this.rules) {
const {body} = this.rules[ruleName];
if (first) {
first = false;
} else {
sb.append(',');
}
sb.append('\n');
sb.append(' ');
this.addSemanticActionTemplate(ruleName, body, sb);
}
sb.append('\n}');
return sb.contents();
}
addSemanticActionTemplate(ruleName, body, sb) {
sb.append(ruleName);
sb.append(': function(');
const arity = this._topDownActionArity(ruleName);
sb.append(common.repeat('_', arity).join(', '));
sb.append(') {\n');
sb.append(' }');
}
// Parse a string which expresses a rule application in this grammar, and return the
// resulting Apply node.
parseApplication(str) {
let app;
if (str.indexOf('<') === -1) {
// simple application
app = new pexprs.Apply(str);
} else {
// parameterized application
const cst = ohmGrammar.match(str, 'Base_application');
app = buildGrammar(cst, {});
}
// Ensure that the application is valid.
if (!(app.ruleName in this.rules)) {
throw errors.undeclaredRule(app.ruleName, this.name);
}
const {formals} = this.rules[app.ruleName];
if (formals.length !== app.args.length) {
const {source} = this.rules[app.ruleName];
throw errors.wrongNumberOfParameters(
app.ruleName,
formals.length,
app.args.length,
source,
);
}
return app;
}
_setUpMatchState(state) {
if (this._matchStateInitializer) {
this._matchStateInitializer(state);
}
}
}
// The following grammar contains a few rules that couldn't be written in "userland".
// At the bottom of src/main.js, we create a sub-grammar of this grammar that's called
// `BuiltInRules`. That grammar contains several convenience rules, e.g., `letter` and
// `digit`, and is implicitly the super-grammar of any grammar whose super-grammar
// isn't specified.
Grammar.ProtoBuiltInRules = new Grammar(
'ProtoBuiltInRules', // name
undefined, // supergrammar
{
any: {
body: pexprs.any,
formals: [],
description: 'any character',
primitive: true,
},
end: {
body: pexprs.end,
formals: [],
description: 'end of input',
primitive: true,
},
caseInsensitive: {
body: new pexprs.CaseInsensitiveTerminal(new pexprs.Param(0)),
formals: ['str'],
primitive: true,
},
lower: {
body: new pexprs.UnicodeChar('Ll'),
formals: [],
description: 'a lowercase letter',
primitive: true,
},
upper: {
body: new pexprs.UnicodeChar('Lu'),
formals: [],
description: 'an uppercase letter',
primitive: true,
},
// Union of Lt (titlecase), Lm (modifier), and Lo (other), i.e. any letter not in Ll or Lu.
unicodeLtmo: {
body: new pexprs.UnicodeChar('Ltmo'),
formals: [],
description: 'a Unicode character in Lt, Lm, or Lo',
primitive: true,
},
// These rules are not truly primitive (they could be written in userland) but are defined
// here for bootstrapping purposes.
spaces: {
body: new pexprs.Star(new pexprs.Apply('space')),
formals: [],
},
space: {
body: new pexprs.Range('\x00', ' '),
formals: [],
description: 'a space',
},
},
);
// This method is called from main.js once Ohm has loaded.
Grammar.initApplicationParser = function(grammar, builderFn) {
ohmGrammar = grammar;
buildGrammar = builderFn;
};