-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathcode-unit.js
More file actions
209 lines (175 loc) · 4.38 KB
/
Copy pathcode-unit.js
File metadata and controls
209 lines (175 loc) · 4.38 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
/**
* The MIT License (MIT)
* Copyright (c) 2015-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
*/
import vm from 'vm';
const SANDBOX = Object.assign({},global, {
/**
* Matched text of the tokenizer.
*/
yytext: '',
/**
* Length of the matched text.
*/
yyleng: 0,
/**
* Extra global object lex rules, and other handlers
* may use to track any needed state across the handlers.
*/
yy: {},
/**
* Stores different parse callbacks.
*/
yyparse: {
onParseBegin: () => {},
onParseEnd: () => {},
},
/**
* Creates location object.
*/
yyloc(start, end) {
// Epsilon doesn't produce location.
if (!start || !end) {
return start || end;
}
return {
startOffset: start.startOffset,
endOffset: end.endOffset,
startLine: start.startLine,
endLine: end.endLine,
startColumn: start.startColumn,
endColumn: end.endColumn,
};
},
/**
* Result value of production handlers, used as $$.
*/
__: null,
/**
* Result node location object.
*/
__loc: null,
/**
* To require modules.
*/
require,
});
/**
* Execution context.
*/
const context = new vm.createContext(SANDBOX);
/**
* Evaluation unit for different handlers (of lex rules, productions, etc),
* which shares the same global space via sandbox.
*/
const CodeUnit = {
/**
* Sets value to bindings.
*/
setBindings(bindings) {
Object.assign(SANDBOX, bindings);
},
/**
* Evaluates the code. If `shouldRewrite` is `true` (default),
* rewrites $1, $2, $$, etc. to _1, _2, __, etc.
*/
eval(code, shouldRewrite = true) {
if (shouldRewrite) {
code = this._rewriteParamsInCode(code);
}
return vm.runInContext(code, context);
},
/**
* Creates parameters string for a semantic action.
*
* Consists of: positioned arguments, named arguments,
* and location data.
*
* Example of using the arguments in a handler:
*
* $1, $2, $expr, $term, @1, @2
*
* Created parameters: _1, _2, _expr, _term, _1loc, _2loc
*/
createProductionParamsArray({production, captureLocations}) {
if (production.isEpsilon()) {
return [];
}
const symbols = production.getRHS().map(symbol => symbol.getSymbol());
// $1, $2, ...
let semanticValues = [];
// @1, @2, ...
let locations = captureLocations ? [] : null;
for (var i = 0; i < symbols.length; i++) {
const semanticValue = `_${i + 1}`;
semanticValues.push(semanticValue);
if (captureLocations) {
locations.push(`${semanticValue}loc`);
}
}
const params = captureLocations
? semanticValues.concat(locations)
: semanticValues;
return params;
},
/**
* See `createProductionParamsArray`.
*/
createProductionParams({production, captureLocations}) {
return this.createProductionParamsArray({
production,
captureLocations,
}).join(', ');
},
/**
* Creates default location prologue to semantic action. Begin is
* taken from first symbol on RHS, the end -- from the last one.
*
* @$.startOffset = @1.startOffset
* @$.endOffset = @1.endOffset
* ...
*/
createLocationPrologue(production) {
if (production.isEpsilon()) {
return '__loc = null;';
}
const start = 1;
const end = production.getRHS().length;
return `__loc = yyloc(_${start}loc, _${end}loc);`;
},
/**
* Creates a handler for a production. Attaches default
* location prologue (user code can override it).
*/
createProductionHandler({production, captureLocations}) {
const params = this.createProductionParams({production, captureLocations});
const locationPrologue = captureLocations
? this.createLocationPrologue(production)
: '';
const action = production.getRawSemanticAction();
return this.createHandler(params, locationPrologue + action);
},
/**
* Rewrites $1, @1, $name to _1, _1loc, _name
*/
_rewriteParamsInCode(code) {
return code
.replace(/\$(\d+)/g, '_$1')
.replace(/@(\d+)/g, '_$1loc')
.replace(/\$\$/g, '__')
.replace(/@\$/g, '__loc');
},
/**
* Creates a handler function with code, and parameters.
*/
createHandler(parameters, code) {
return this.eval(`(function(${parameters}) { ${code} })`);
},
/**
* Returns evaluation sandbox.
*/
getSandbox() {
return SANDBOX;
},
};
export default CodeUnit;