-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathparser-state.js
335 lines (296 loc) · 9.8 KB
/
parser-state.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
import { TOP, TNUMBER, TSTRING, TPAREN, TBRACKET, TCOMMA, TNAME, TSEMICOLON, TEOF } from './token';
import { Instruction, INUMBER, IVAR, IVARNAME, IFUNCALL, IFUNDEF, IEXPR, IMEMBER, IENDSTATEMENT, IARRAY, ternaryInstruction, binaryInstruction, unaryInstruction } from './instruction';
import contains from './contains';
export function ParserState(parser, tokenStream, options) {
this.parser = parser;
this.tokens = tokenStream;
this.current = null;
this.nextToken = null;
this.next();
this.savedCurrent = null;
this.savedNextToken = null;
this.allowMemberAccess = options.allowMemberAccess !== false;
}
ParserState.prototype.next = function () {
this.current = this.nextToken;
return (this.nextToken = this.tokens.next());
};
ParserState.prototype.tokenMatches = function (token, value) {
if (typeof value === 'undefined') {
return true;
} else if (Array.isArray(value)) {
return contains(value, token.value);
} else if (typeof value === 'function') {
return value(token);
} else {
return token.value === value;
}
};
ParserState.prototype.save = function () {
this.savedCurrent = this.current;
this.savedNextToken = this.nextToken;
this.tokens.save();
};
ParserState.prototype.restore = function () {
this.tokens.restore();
this.current = this.savedCurrent;
this.nextToken = this.savedNextToken;
};
ParserState.prototype.accept = function (type, value) {
if (this.nextToken.type === type && this.tokenMatches(this.nextToken, value)) {
this.next();
return true;
}
return false;
};
ParserState.prototype.expect = function (type, value) {
if (!this.accept(type, value)) {
var coords = this.tokens.getCoordinates();
throw new Error('parse error [' + coords.line + ':' + coords.column + ']: Expected ' + (value || type));
}
};
ParserState.prototype.parseAtom = function (instr) {
var unaryOps = this.tokens.unaryOps;
function isPrefixOperator(token) {
return token.value in unaryOps;
}
if (this.accept(TNAME) || this.accept(TOP, isPrefixOperator)) {
instr.push(new Instruction(IVAR, this.current.value));
} else if (this.accept(TNUMBER)) {
instr.push(new Instruction(INUMBER, this.current.value));
} else if (this.accept(TSTRING)) {
instr.push(new Instruction(INUMBER, this.current.value));
} else if (this.accept(TPAREN, '(')) {
this.parseExpression(instr);
this.expect(TPAREN, ')');
} else if (this.accept(TBRACKET, '[')) {
if (this.accept(TBRACKET, ']')) {
instr.push(new Instruction(IARRAY, 0));
} else {
var argCount = this.parseArrayList(instr);
instr.push(new Instruction(IARRAY, argCount));
}
} else {
throw new Error('unexpected ' + this.nextToken);
}
};
ParserState.prototype.parseExpression = function (instr) {
var exprInstr = [];
if (this.parseUntilEndStatement(instr, exprInstr)) {
return;
}
this.parseVariableAssignmentExpression(exprInstr);
if (this.parseUntilEndStatement(instr, exprInstr)) {
return;
}
this.pushExpression(instr, exprInstr);
};
ParserState.prototype.pushExpression = function (instr, exprInstr) {
for (var i = 0, len = exprInstr.length; i < len; i++) {
instr.push(exprInstr[i]);
}
};
ParserState.prototype.parseUntilEndStatement = function (instr, exprInstr) {
if (!this.accept(TSEMICOLON)) return false;
if (this.nextToken && this.nextToken.type !== TEOF && !(this.nextToken.type === TPAREN && this.nextToken.value === ')')) {
exprInstr.push(new Instruction(IENDSTATEMENT));
}
if (this.nextToken.type !== TEOF) {
this.parseExpression(exprInstr);
}
instr.push(new Instruction(IEXPR, exprInstr));
return true;
};
ParserState.prototype.parseArrayList = function (instr) {
var argCount = 0;
while (!this.accept(TBRACKET, ']')) {
this.parseExpression(instr);
++argCount;
while (this.accept(TCOMMA)) {
this.parseExpression(instr);
++argCount;
}
}
return argCount;
};
ParserState.prototype.parseVariableAssignmentExpression = function (instr) {
this.parseConditionalExpression(instr);
while (this.accept(TOP, '=')) {
var varName = instr.pop();
var varValue = [];
var lastInstrIndex = instr.length - 1;
if (varName.type === IFUNCALL) {
if (!this.tokens.isOperatorEnabled('()=')) {
throw new Error('function definition is not permitted');
}
for (var i = 0, len = varName.value + 1; i < len; i++) {
var index = lastInstrIndex - i;
if (instr[index].type === IVAR) {
instr[index] = new Instruction(IVARNAME, instr[index].value);
}
}
this.parseVariableAssignmentExpression(varValue);
instr.push(new Instruction(IEXPR, varValue));
instr.push(new Instruction(IFUNDEF, varName.value));
continue;
}
if (varName.type !== IVAR && varName.type !== IMEMBER) {
throw new Error('expected variable for assignment');
}
this.parseVariableAssignmentExpression(varValue);
instr.push(new Instruction(IVARNAME, varName.value));
instr.push(new Instruction(IEXPR, varValue));
instr.push(binaryInstruction('='));
}
};
ParserState.prototype.parseConditionalExpression = function (instr) {
this.parseOrExpression(instr);
while (this.accept(TOP, '?')) {
var trueBranch = [];
var falseBranch = [];
this.parseConditionalExpression(trueBranch);
this.expect(TOP, ':');
this.parseConditionalExpression(falseBranch);
instr.push(new Instruction(IEXPR, trueBranch));
instr.push(new Instruction(IEXPR, falseBranch));
instr.push(ternaryInstruction('?'));
}
};
ParserState.prototype.parseOrExpression = function (instr) {
this.parseAndExpression(instr);
while (this.accept(TOP, 'or')) {
var falseBranch = [];
this.parseAndExpression(falseBranch);
instr.push(new Instruction(IEXPR, falseBranch));
instr.push(binaryInstruction('or'));
}
};
ParserState.prototype.parseAndExpression = function (instr) {
this.parseComparison(instr);
while (this.accept(TOP, 'and')) {
var trueBranch = [];
this.parseComparison(trueBranch);
instr.push(new Instruction(IEXPR, trueBranch));
instr.push(binaryInstruction('and'));
}
};
var COMPARISON_OPERATORS = ['==', '!=', '<', '<=', '>=', '>', 'in'];
ParserState.prototype.parseComparison = function (instr) {
this.parseAddSub(instr);
while (this.accept(TOP, COMPARISON_OPERATORS)) {
var op = this.current;
this.parseAddSub(instr);
instr.push(binaryInstruction(op.value));
}
};
var ADD_SUB_OPERATORS = ['+', '-', '||'];
ParserState.prototype.parseAddSub = function (instr) {
this.parseTerm(instr);
while (this.accept(TOP, ADD_SUB_OPERATORS)) {
var op = this.current;
this.parseTerm(instr);
instr.push(binaryInstruction(op.value));
}
};
var TERM_OPERATORS = ['*', '/', '%'];
ParserState.prototype.parseTerm = function (instr) {
this.parseFactor(instr);
while (this.accept(TOP, TERM_OPERATORS)) {
var op = this.current;
this.parseFactor(instr);
instr.push(binaryInstruction(op.value));
}
};
ParserState.prototype.parseFactor = function (instr) {
var unaryOps = this.tokens.unaryOps;
function isPrefixOperator(token) {
return token.value in unaryOps;
}
this.save();
if (this.accept(TOP, isPrefixOperator)) {
if (this.current.value !== '-' && this.current.value !== '+') {
if (this.nextToken.type === TPAREN && this.nextToken.value === '(') {
this.restore();
this.parseExponential(instr);
return;
} else if (this.nextToken.type === TSEMICOLON || this.nextToken.type === TCOMMA || this.nextToken.type === TEOF || (this.nextToken.type === TPAREN && this.nextToken.value === ')')) {
this.restore();
this.parseAtom(instr);
return;
}
}
var op = this.current;
this.parseFactor(instr);
instr.push(unaryInstruction(op.value));
} else {
this.parseExponential(instr);
}
};
ParserState.prototype.parseExponential = function (instr) {
this.parsePostfixExpression(instr);
while (this.accept(TOP, '^')) {
this.parseFactor(instr);
instr.push(binaryInstruction('^'));
}
};
ParserState.prototype.parsePostfixExpression = function (instr) {
this.parseFunctionCall(instr);
while (this.accept(TOP, '!')) {
instr.push(unaryInstruction('!'));
}
};
ParserState.prototype.parseFunctionCall = function (instr) {
var unaryOps = this.tokens.unaryOps;
function isPrefixOperator(token) {
return token.value in unaryOps;
}
if (this.accept(TOP, isPrefixOperator)) {
var op = this.current;
this.parseAtom(instr);
instr.push(unaryInstruction(op.value));
} else {
this.parseMemberExpression(instr);
while (this.accept(TPAREN, '(')) {
if (this.accept(TPAREN, ')')) {
instr.push(new Instruction(IFUNCALL, 0));
} else {
var argCount = this.parseArgumentList(instr);
instr.push(new Instruction(IFUNCALL, argCount));
}
}
}
};
ParserState.prototype.parseArgumentList = function (instr) {
var argCount = 0;
while (!this.accept(TPAREN, ')')) {
this.parseExpression(instr);
++argCount;
while (this.accept(TCOMMA)) {
this.parseExpression(instr);
++argCount;
}
}
return argCount;
};
ParserState.prototype.parseMemberExpression = function (instr) {
this.parseAtom(instr);
while (this.accept(TOP, '.') || this.accept(TBRACKET, '[')) {
var op = this.current;
if (op.value === '.') {
if (!this.allowMemberAccess) {
throw new Error('unexpected ".", member access is not permitted');
}
this.expect(TNAME);
instr.push(new Instruction(IMEMBER, this.current.value));
} else if (op.value === '[') {
if (!this.tokens.isOperatorEnabled('[')) {
throw new Error('unexpected "[]", arrays are disabled');
}
this.parseExpression(instr);
this.expect(TBRACKET, ']');
instr.push(binaryInstruction('['));
} else {
throw new Error('unexpected symbol: ' + op.value);
}
}
};