/
pexprs-assertChoicesHaveUniformArity.js
70 lines (60 loc) · 2.44 KB
/
pexprs-assertChoicesHaveUniformArity.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
import {abstract} from './common.js';
import * as errors from './errors.js';
import * as pexprs from './pexprs-main.js';
// --------------------------------------------------------------------
// Operations
// --------------------------------------------------------------------
pexprs.PExpr.prototype.assertChoicesHaveUniformArity = abstract(
'assertChoicesHaveUniformArity',
);
pexprs.any.assertChoicesHaveUniformArity =
pexprs.end.assertChoicesHaveUniformArity =
pexprs.Terminal.prototype.assertChoicesHaveUniformArity =
pexprs.Range.prototype.assertChoicesHaveUniformArity =
pexprs.Param.prototype.assertChoicesHaveUniformArity =
pexprs.Lex.prototype.assertChoicesHaveUniformArity =
pexprs.UnicodeChar.prototype.assertChoicesHaveUniformArity =
function(ruleName) {
// no-op
};
pexprs.Alt.prototype.assertChoicesHaveUniformArity = function(ruleName) {
if (this.terms.length === 0) {
return;
}
const arity = this.terms[0].getArity();
for (let idx = 0; idx < this.terms.length; idx++) {
const term = this.terms[idx];
term.assertChoicesHaveUniformArity();
const otherArity = term.getArity();
if (arity !== otherArity) {
throw errors.inconsistentArity(ruleName, arity, otherArity, term);
}
}
};
pexprs.Extend.prototype.assertChoicesHaveUniformArity = function(ruleName) {
// Extend is a special case of Alt that's guaranteed to have exactly two
// cases: [extensions, origBody].
const actualArity = this.terms[0].getArity();
const expectedArity = this.terms[1].getArity();
if (actualArity !== expectedArity) {
throw errors.inconsistentArity(ruleName, expectedArity, actualArity, this.terms[0]);
}
};
pexprs.Seq.prototype.assertChoicesHaveUniformArity = function(ruleName) {
for (let idx = 0; idx < this.factors.length; idx++) {
this.factors[idx].assertChoicesHaveUniformArity(ruleName);
}
};
pexprs.Iter.prototype.assertChoicesHaveUniformArity = function(ruleName) {
this.expr.assertChoicesHaveUniformArity(ruleName);
};
pexprs.Not.prototype.assertChoicesHaveUniformArity = function(ruleName) {
// no-op (not required b/c the nested expr doesn't show up in the CST)
};
pexprs.Lookahead.prototype.assertChoicesHaveUniformArity = function(ruleName) {
this.expr.assertChoicesHaveUniformArity(ruleName);
};
pexprs.Apply.prototype.assertChoicesHaveUniformArity = function(ruleName) {
// The arities of the parameter expressions is required to be 1 by
// `assertAllApplicationsAreValid()`.
};