-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathSemantics.js
More file actions
679 lines (588 loc) · 23.7 KB
/
Copy pathSemantics.js
File metadata and controls
679 lines (588 loc) · 23.7 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
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
import {InputStream} from './InputStream.js';
import {IterationNode} from './nodes.js';
import {MatchResult} from './MatchResult.js';
import * as common from './common.js';
import * as errors from './errors.js';
import * as util from './util.js';
// --------------------------------------------------------------------
// Private stuff
// --------------------------------------------------------------------
const globalActionStack = [];
const hasOwnProperty = (x, prop) => Object.prototype.hasOwnProperty.call(x, prop);
// ----------------- Wrappers -----------------
// Wrappers decorate CST nodes with all of the functionality (i.e., operations and attributes)
// provided by a Semantics (see below). `Wrapper` is the abstract superclass of all wrappers. A
// `Wrapper` must have `_node` and `_semantics` instance variables, which refer to the CST node and
// Semantics (resp.) for which it was created, and a `_childWrappers` instance variable which is
// used to cache the wrapper instances that are created for its child nodes. Setting these instance
// variables is the responsibility of the constructor of each Semantics-specific subclass of
// `Wrapper`.
class Wrapper {
constructor(node, sourceInterval, baseInterval) {
this._node = node;
this.source = sourceInterval;
// The interval that the childOffsets of `node` are relative to. It should be the source
// of the closest Nonterminal node.
this._baseInterval = baseInterval;
if (node.isNonterminal()) {
common.assert(sourceInterval === baseInterval);
}
this._childWrappers = [];
}
_forgetMemoizedResultFor(attributeName) {
// Remove the memoized attribute from the cstNode and all its children.
this._node[this._semantics.attributeKeys[attributeName]] = undefined;
this.children.forEach(child => {
child._forgetMemoizedResultFor(attributeName);
});
}
// Returns the wrapper of the specified child node. Child wrappers are created lazily and
// cached in the parent wrapper's `_childWrappers` instance variable.
child(idx) {
if (!(0 <= idx && idx < this._node.numChildren())) {
// TODO: Consider throwing an exception here.
return undefined;
}
let childWrapper = this._childWrappers[idx];
if (!childWrapper) {
const childNode = this._node.childAt(idx);
const offset = this._node.childOffsets[idx];
const source = this._baseInterval.subInterval(offset, childNode.matchLength);
const base = childNode.isNonterminal() ? source : this._baseInterval;
childWrapper = this._childWrappers[idx] = this._semantics.wrap(childNode, source, base);
}
return childWrapper;
}
// Returns an array containing the wrappers of all of the children of the node associated
// with this wrapper.
_children() {
// Force the creation of all child wrappers
for (let idx = 0; idx < this._node.numChildren(); idx++) {
this.child(idx);
}
return this._childWrappers;
}
// Returns `true` if the CST node associated with this wrapper corresponds to an iteration
// expression, i.e., a Kleene-*, Kleene-+, or an optional. Returns `false` otherwise.
isIteration() {
return this._node.isIteration();
}
// Returns `true` if the CST node associated with this wrapper is a terminal node, `false`
// otherwise.
isTerminal() {
return this._node.isTerminal();
}
// Returns `true` if the CST node associated with this wrapper is a nonterminal node, `false`
// otherwise.
isNonterminal() {
return this._node.isNonterminal();
}
// Returns `true` if the CST node associated with this wrapper is a nonterminal node
// corresponding to a syntactic rule, `false` otherwise.
isSyntactic() {
return this.isNonterminal() && this._node.isSyntactic();
}
// Returns `true` if the CST node associated with this wrapper is a nonterminal node
// corresponding to a lexical rule, `false` otherwise.
isLexical() {
return this.isNonterminal() && this._node.isLexical();
}
// Returns `true` if the CST node associated with this wrapper is an iterator node
// having either one or no child (? operator), `false` otherwise.
// Otherwise, throws an exception.
isOptional() {
return this._node.isOptional();
}
// Create a new _iter wrapper in the same semantics as this wrapper.
iteration(optChildWrappers) {
const childWrappers = optChildWrappers || [];
const childNodes = childWrappers.map(c => c._node);
const iter = new IterationNode(childNodes, [], -1, false);
const wrapper = this._semantics.wrap(iter, null, null);
wrapper._childWrappers = childWrappers;
return wrapper;
}
// Returns an array containing the children of this CST node.
get children() {
return this._children();
}
// Returns the name of grammar rule that created this CST node.
get ctorName() {
return this._node.ctorName;
}
// Returns the number of children of this CST node.
get numChildren() {
return this._node.numChildren();
}
// Returns the contents of the input stream consumed by this CST node.
get sourceString() {
return this.source.contents;
}
}
// ----------------- Semantics -----------------
// A Semantics is a container for a family of Operations and Attributes for a given grammar.
// Semantics enable modularity (different clients of a grammar can create their set of operations
// and attributes in isolation) and extensibility even when operations and attributes are mutually-
// recursive. This constructor should not be called directly except from
// `Semantics.createSemantics`. The normal ways to create a Semantics, given a grammar 'g', are
// `g.createSemantics()` and `g.extendSemantics(parentSemantics)`.
export class Semantics {
constructor(grammar, superSemantics) {
const self = this;
this.grammar = grammar;
this.checkedActionDicts = false;
// Constructor for wrapper instances, which are passed as the arguments to the semantic actions
// of an operation or attribute. Operations and attributes require double dispatch: the semantic
// action is chosen based on both the node's type and the semantics. Wrappers ensure that
// the `execute` method is called with the correct (most specific) semantics object as an
// argument.
this.Wrapper = class extends (superSemantics ? superSemantics.Wrapper : Wrapper) {
constructor(node, sourceInterval, baseInterval) {
super(node, sourceInterval, baseInterval);
self.checkActionDictsIfHaventAlready();
this._semantics = self;
}
toString() {
return '[semantics wrapper for ' + self.grammar.name + ']';
}
};
this.super = superSemantics;
if (superSemantics) {
if (!(grammar.equals(this.super.grammar) || grammar._inheritsFrom(this.super.grammar))) {
throw new Error(
"Cannot extend a semantics for grammar '" +
this.super.grammar.name +
"' for use with grammar '" +
grammar.name +
"' (not a sub-grammar)"
);
}
this.operations = Object.create(this.super.operations);
this.attributes = Object.create(this.super.attributes);
this.attributeKeys = Object.create(null);
// Assign unique symbols for each of the attributes inherited from the super-semantics so that
// they are memoized independently.
for (const attributeName in this.attributes) {
Object.defineProperty(this.attributeKeys, attributeName, {
value: util.uniqueId(attributeName),
});
}
} else {
this.operations = Object.create(null);
this.attributes = Object.create(null);
this.attributeKeys = Object.create(null);
}
}
toString() {
return '[semantics for ' + this.grammar.name + ']';
}
checkActionDictsIfHaventAlready() {
if (!this.checkedActionDicts) {
this.checkActionDicts();
this.checkedActionDicts = true;
}
}
// Checks that the action dictionaries for all operations and attributes in this semantics,
// including the ones that were inherited from the super-semantics, agree with the grammar.
// Throws an exception if one or more of them doesn't.
checkActionDicts() {
let name;
for (name in this.operations) {
this.operations[name].checkActionDict(this.grammar);
}
for (name in this.attributes) {
this.attributes[name].checkActionDict(this.grammar);
}
}
toRecipe(semanticsOnly) {
function hasSuperSemantics(s) {
return s.super !== Semantics.BuiltInSemantics._getSemantics();
}
let str = '(function(g) {\n';
if (hasSuperSemantics(this)) {
str += ' var semantics = ' + this.super.toRecipe(true) + '(g';
const superSemanticsGrammar = this.super.grammar;
let relatedGrammar = this.grammar;
while (relatedGrammar !== superSemanticsGrammar) {
str += '.superGrammar';
relatedGrammar = relatedGrammar.superGrammar;
}
str += ');\n';
str += ' return g.extendSemantics(semantics)';
} else {
str += ' return g.createSemantics()';
}
['Operation', 'Attribute'].forEach(type => {
const semanticOperations = this[type.toLowerCase() + 's'];
Object.keys(semanticOperations).forEach(name => {
const {actionDict, formals, builtInDefault} = semanticOperations[name];
let signature = name;
if (formals.length > 0) {
signature += '(' + formals.join(', ') + ')';
}
let method;
if (hasSuperSemantics(this) && this.super[type.toLowerCase() + 's'][name]) {
method = 'extend' + type;
} else {
method = 'add' + type;
}
str += '\n .' + method + '(' + JSON.stringify(signature) + ', {';
const srcArray = [];
Object.keys(actionDict).forEach(actionName => {
if (actionDict[actionName] !== builtInDefault) {
let source = actionDict[actionName].toString().trim();
// Convert method shorthand to plain old function syntax.
// https://github.com/ohmjs/ohm/issues/263
source = source.replace(/^.*\(/, 'function(');
srcArray.push('\n ' + JSON.stringify(actionName) + ': ' + source);
}
});
str += srcArray.join(',') + '\n })';
});
});
str += ';\n })';
if (!semanticsOnly) {
str =
'(function() {\n' +
' var grammar = this.fromRecipe(' +
this.grammar.toRecipe() +
');\n' +
' var semantics = ' +
str +
'(grammar);\n' +
' return semantics;\n' +
'});\n';
}
return str;
}
addOperationOrAttribute(type, signature, actionDict) {
const typePlural = type + 's';
const parsedNameAndFormalArgs = parseSignature(signature, type);
const {name} = parsedNameAndFormalArgs;
const {formals} = parsedNameAndFormalArgs;
// TODO: check that there are no duplicate formal arguments
this.assertNewName(name, type);
// Create the action dictionary for this operation / attribute that contains a `_default` action
// which defines the default behavior of iteration, terminal, and non-terminal nodes...
const builtInDefault = newDefaultAction(type, name, doIt);
const realActionDict = {_default: builtInDefault};
// ... and add in the actions supplied by the programmer, which may override some or all of the
// default ones.
Object.keys(actionDict).forEach(name => {
realActionDict[name] = actionDict[name];
});
const entry =
type === 'operation'
? new Operation(name, formals, realActionDict, builtInDefault)
: new Attribute(name, realActionDict, builtInDefault);
// The following check is not strictly necessary (it will happen later anyway) but it's better
// to catch errors early.
entry.checkActionDict(this.grammar);
this[typePlural][name] = entry;
function doIt(...args) {
// Dispatch to most specific version of this operation / attribute -- it may have been
// overridden by a sub-semantics.
const thisThing = this._semantics[typePlural][name];
// Check that the caller passed the correct number of arguments.
if (args.length !== thisThing.formals.length) {
throw new Error(
'Invalid number of arguments passed to ' +
name +
' ' +
type +
' (expected ' +
thisThing.formals.length +
', got ' +
arguments.length +
')'
);
}
// Create an "arguments object" from the arguments that were passed to this
// operation / attribute.
const argsObj = Object.create(null);
for (let idx = 0; idx < args.length; idx += 1) {
const formal = thisThing.formals[idx];
argsObj[formal] = args[idx];
}
const oldArgs = this.args;
this.args = argsObj;
const ans = thisThing.execute(this._semantics, this);
this.args = oldArgs;
return ans;
}
if (type === 'operation') {
this.Wrapper.prototype[name] = doIt;
this.Wrapper.prototype[name].toString = function () {
return '[' + name + ' operation]';
};
} else {
Object.defineProperty(this.Wrapper.prototype, name, {
get: doIt,
configurable: true, // So the property can be deleted.
});
Object.defineProperty(this.attributeKeys, name, {
value: util.uniqueId(name),
});
}
}
extendOperationOrAttribute(type, name, actionDict) {
const typePlural = type + 's';
// Make sure that `name` really is just a name, i.e., that it doesn't also contain formals.
parseSignature(name, 'attribute');
if (!(this.super && name in this.super[typePlural])) {
throw new Error(
'Cannot extend ' +
type +
" '" +
name +
"': did not inherit an " +
type +
' with that name'
);
}
if (hasOwnProperty(this[typePlural], name)) {
throw new Error('Cannot extend ' + type + " '" + name + "' again");
}
// Create a new operation / attribute whose actionDict delegates to the super operation /
// attribute's actionDict, and which has all the keys from `inheritedActionDict`.
const inheritedFormals = this[typePlural][name].formals;
const inheritedActionDict = this[typePlural][name].actionDict;
const newActionDict = Object.create(inheritedActionDict);
Object.keys(actionDict).forEach(name => {
newActionDict[name] = actionDict[name];
});
this[typePlural][name] =
type === 'operation'
? new Operation(name, inheritedFormals, newActionDict)
: new Attribute(name, newActionDict);
// The following check is not strictly necessary (it will happen later anyway) but it's better
// to catch errors early.
this[typePlural][name].checkActionDict(this.grammar);
}
assertNewName(name, type) {
if (hasOwnProperty(Wrapper.prototype, name)) {
throw new Error('Cannot add ' + type + " '" + name + "': that's a reserved name");
}
if (name in this.operations) {
throw new Error(
'Cannot add ' + type + " '" + name + "': an operation with that name already exists"
);
}
if (name in this.attributes) {
throw new Error(
'Cannot add ' + type + " '" + name + "': an attribute with that name already exists"
);
}
}
// Returns a wrapper for the given CST `node` in this semantics.
// If `node` is already a wrapper, returns `node` itself. // TODO: why is this needed?
wrap(node, source, optBaseInterval) {
const baseInterval = optBaseInterval || source;
return node instanceof this.Wrapper ? node : new this.Wrapper(node, source, baseInterval);
}
}
function parseSignature(signature, type) {
if (!Semantics.prototypeGrammar) {
// The Operations and Attributes grammar won't be available while Ohm is loading,
// but we can get away the following simplification b/c none of the operations
// that are used while loading take arguments.
common.assert(signature.indexOf('(') === -1);
return {
name: signature,
formals: [],
};
}
const r = Semantics.prototypeGrammar.match(
signature,
type === 'operation' ? 'OperationSignature' : 'AttributeSignature'
);
if (r.failed()) {
throw new Error(r.message);
}
return Semantics.prototypeGrammarSemantics(r).parse();
}
function newDefaultAction(type, name, doIt) {
return function (...children) {
const thisThing = this._semantics.operations[name] || this._semantics.attributes[name];
const args = thisThing.formals.map(formal => this.args[formal]);
if (!this.isIteration() && children.length === 1) {
// This CST node corresponds to a non-terminal in the grammar (e.g., AddExpr). The fact that
// we got here means that this action dictionary doesn't have an action for this particular
// non-terminal or a generic `_nonterminal` action.
// As a convenience, if this node only has one child, we just return the result of applying
// this operation / attribute to the child node.
return doIt.apply(children[0], args);
} else {
// Otherwise, we throw an exception to let the programmer know that we don't know what
// to do with this node.
throw errors.missingSemanticAction(this.ctorName, name, type, globalActionStack);
}
};
}
// Creates a new Semantics instance for `grammar`, inheriting operations and attributes from
// `optSuperSemantics`, if it is specified. Returns a function that acts as a proxy for the new
// Semantics instance. When that function is invoked with a CST node as an argument, it returns
// a wrapper for that node which gives access to the operations and attributes provided by this
// semantics.
Semantics.createSemantics = function (grammar, optSuperSemantics) {
const s = new Semantics(
grammar,
optSuperSemantics !== undefined
? optSuperSemantics
: Semantics.BuiltInSemantics._getSemantics()
);
// To enable clients to invoke a semantics like a function, return a function that acts as a proxy
// for `s`, which is the real `Semantics` instance.
const proxy = function ASemantics(matchResult) {
if (!(matchResult instanceof MatchResult)) {
throw new TypeError(
'Semantics expected a MatchResult, but got ' +
common.unexpectedObjToString(matchResult)
);
}
if (matchResult.failed()) {
throw new TypeError('cannot apply Semantics to ' + matchResult.toString());
}
const cst = matchResult._cst;
if (cst.grammar !== grammar) {
throw new Error(
"Cannot use a MatchResult from grammar '" +
cst.grammar.name +
"' with a semantics for '" +
grammar.name +
"'"
);
}
const inputStream = new InputStream(matchResult.input);
return s.wrap(cst, inputStream.interval(matchResult._cstOffset, matchResult.input.length));
};
// Forward public methods from the proxy to the semantics instance.
proxy.addOperation = function (signature, actionDict) {
s.addOperationOrAttribute('operation', signature, actionDict);
return proxy;
};
proxy.extendOperation = function (name, actionDict) {
s.extendOperationOrAttribute('operation', name, actionDict);
return proxy;
};
proxy.addAttribute = function (name, actionDict) {
s.addOperationOrAttribute('attribute', name, actionDict);
return proxy;
};
proxy.extendAttribute = function (name, actionDict) {
s.extendOperationOrAttribute('attribute', name, actionDict);
return proxy;
};
proxy._getActionDict = function (operationOrAttributeName) {
const action =
s.operations[operationOrAttributeName] || s.attributes[operationOrAttributeName];
if (!action) {
throw new Error(
'"' +
operationOrAttributeName +
'" is not a valid operation or attribute ' +
'name in this semantics for "' +
grammar.name +
'"'
);
}
return action.actionDict;
};
proxy._remove = function (operationOrAttributeName) {
let semantic;
if (operationOrAttributeName in s.operations) {
semantic = s.operations[operationOrAttributeName];
s.operations[operationOrAttributeName] = undefined;
} else if (operationOrAttributeName in s.attributes) {
semantic = s.attributes[operationOrAttributeName];
s.attributes[operationOrAttributeName] = undefined;
}
s.Wrapper.prototype[operationOrAttributeName] = undefined;
return semantic;
};
proxy.getOperationNames = function () {
return Object.keys(s.operations);
};
proxy.getAttributeNames = function () {
return Object.keys(s.attributes);
};
proxy.getGrammar = function () {
return s.grammar;
};
proxy.toRecipe = function (semanticsOnly) {
return s.toRecipe(semanticsOnly);
};
// Make the proxy's toString() work.
proxy.toString = s.toString.bind(s);
// Returns the semantics for the proxy.
proxy._getSemantics = function () {
return s;
};
return proxy;
};
// ----------------- Operation -----------------
// An Operation represents a function to be applied to a concrete syntax tree (CST) -- it's very
// similar to a Visitor (http://en.wikipedia.org/wiki/Visitor_pattern). An operation is executed by
// recursively walking the CST, and at each node, invoking the matching semantic action from
// `actionDict`. See `Operation.prototype.execute` for details of how a CST node's matching semantic
// action is found.
class Operation {
constructor(name, formals, actionDict, builtInDefault) {
this.name = name;
this.formals = formals;
this.actionDict = actionDict;
this.builtInDefault = builtInDefault;
}
checkActionDict(grammar) {
grammar._checkTopDownActionDict(this.typeName, this.name, this.actionDict);
}
// Execute this operation on the CST node associated with `nodeWrapper` in the context of the
// given Semantics instance.
execute(semantics, nodeWrapper) {
try {
// Look for a semantic action whose name matches the node's constructor name, which is either
// the name of a rule in the grammar, or '_terminal' (for a terminal node), or '_iter' (for an
// iteration node).
const {ctorName} = nodeWrapper._node;
let actionFn = this.actionDict[ctorName];
if (actionFn) {
globalActionStack.push([this, ctorName]);
return actionFn.apply(nodeWrapper, nodeWrapper._children());
}
// The action dictionary does not contain a semantic action for this specific type of node.
// If this is a nonterminal node and the programmer has provided a `_nonterminal` semantic
// action, we invoke it:
if (nodeWrapper.isNonterminal()) {
actionFn = this.actionDict._nonterminal;
if (actionFn) {
globalActionStack.push([this, '_nonterminal', ctorName]);
return actionFn.apply(nodeWrapper, nodeWrapper._children());
}
}
// Otherwise, we invoke the '_default' semantic action.
globalActionStack.push([this, 'default action', ctorName]);
return this.actionDict._default.apply(nodeWrapper, nodeWrapper._children());
} finally {
globalActionStack.pop();
}
}
}
Operation.prototype.typeName = 'operation';
// ----------------- Attribute -----------------
// Attributes are Operations whose results are memoized. This means that, for any given semantics,
// the semantic action for a CST node will be invoked no more than once.
class Attribute extends Operation {
constructor(name, actionDict, builtInDefault) {
super(name, [], actionDict, builtInDefault);
}
execute(semantics, nodeWrapper) {
const node = nodeWrapper._node;
const key = semantics.attributeKeys[this.name];
if (!hasOwnProperty(node, key)) {
// The following is a super-send -- isn't JS beautiful? :/
node[key] = Operation.prototype.execute.call(this, semantics, nodeWrapper);
}
return node[key];
}
}
Attribute.prototype.typeName = 'attribute';