/
pexprs-outputRecipe.js
102 lines (84 loc) · 3.43 KB
/
pexprs-outputRecipe.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
'use strict';
// --------------------------------------------------------------------
// Imports
// --------------------------------------------------------------------
const common = require('./common');
const pexprs = require('./pexprs-main');
// --------------------------------------------------------------------
// Private stuff
// --------------------------------------------------------------------
function getMetaInfo(expr, grammarInterval) {
const metaInfo = {};
if (expr.source && grammarInterval) {
const adjusted = expr.source.relativeTo(grammarInterval);
metaInfo.sourceInterval = [adjusted.startIdx, adjusted.endIdx];
}
return metaInfo;
}
// --------------------------------------------------------------------
// Operations
// --------------------------------------------------------------------
pexprs.PExpr.prototype.outputRecipe = common.abstract('outputRecipe');
pexprs.any.outputRecipe = function(formals, grammarInterval) {
return ['any', getMetaInfo(this, grammarInterval)];
};
pexprs.end.outputRecipe = function(formals, grammarInterval) {
return ['end', getMetaInfo(this, grammarInterval)];
};
pexprs.Terminal.prototype.outputRecipe = function(formals, grammarInterval) {
return ['terminal', getMetaInfo(this, grammarInterval), this.obj];
};
pexprs.Range.prototype.outputRecipe = function(formals, grammarInterval) {
return ['range', getMetaInfo(this, grammarInterval), this.from, this.to];
};
pexprs.Param.prototype.outputRecipe = function(formals, grammarInterval) {
return ['param', getMetaInfo(this, grammarInterval), this.index];
};
pexprs.Alt.prototype.outputRecipe = function(formals, grammarInterval) {
return ['alt', getMetaInfo(this, grammarInterval)].concat(
this.terms.map(term => term.outputRecipe(formals, grammarInterval))
);
};
pexprs.Extend.prototype.outputRecipe = function(formals, grammarInterval) {
const extension = this.terms[0]; // [extension, original]
return extension.outputRecipe(formals, grammarInterval);
};
pexprs.Splice.prototype.outputRecipe = function(formals, grammarInterval) {
const beforeTerms = this.terms.slice(0, this.expansionPos);
const afterTerms = this.terms.slice(this.expansionPos + 1);
return [
'splice',
getMetaInfo(this, grammarInterval),
beforeTerms.map(term => term.outputRecipe(formals, grammarInterval)),
afterTerms.map(term => term.outputRecipe(formals, grammarInterval)),
];
};
pexprs.Seq.prototype.outputRecipe = function(formals, grammarInterval) {
return ['seq', getMetaInfo(this, grammarInterval)].concat(
this.factors.map(factor => factor.outputRecipe(formals, grammarInterval))
);
};
pexprs.Star.prototype.outputRecipe =
pexprs.Plus.prototype.outputRecipe =
pexprs.Opt.prototype.outputRecipe =
pexprs.Not.prototype.outputRecipe =
pexprs.Lookahead.prototype.outputRecipe =
pexprs.Lex.prototype.outputRecipe =
function(formals, grammarInterval) {
return [
this.constructor.name.toLowerCase(),
getMetaInfo(this, grammarInterval),
this.expr.outputRecipe(formals, grammarInterval),
];
};
pexprs.Apply.prototype.outputRecipe = function(formals, grammarInterval) {
return [
'app',
getMetaInfo(this, grammarInterval),
this.ruleName,
this.args.map(arg => arg.outputRecipe(formals, grammarInterval)),
];
};
pexprs.UnicodeChar.prototype.outputRecipe = function(formals, grammarInterval) {
return ['unicodeChar', getMetaInfo(this, grammarInterval), this.category];
};