-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathparser.js
More file actions
312 lines (262 loc) · 6.73 KB
/
parser.js
File metadata and controls
312 lines (262 loc) · 6.73 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
"use strict";
let util = require('./util');
let sprintf = require('util').format;
let lexer = require('./lexer');
let equalKeywords = ['object', 'array', 'string', 'number', 'boolean', 'true', 'false'];
function ParserException(message) {
this.name = 'ParserException';
this.message = message;
}
ParserException.prototype = Object.create(Error.prototype);
ParserException.prototype.constructor = ParserException;
module.exports = (input) => {
// Pass the input to the lexer to get all the tokens. Lexer errors will get thrown from within.
let tokens = lexer(input);
// Call the starting production. The return value will be the final AST.
// Productions sort of recursively call other productions and returning
// their final trees which get added up to the final result.
// We have to pass around the iterator interface tho.
let ast = Z({
next: () => {
return tokens.next().value;
},
backup: tokens.backup,
});
return ast;
};
function throwException(token, message) {
throw new ParserException(sprintf('%s (line: %d, char: %d)', message, token.line, token.char));
}
// Z => S
// Entry point to any program
function Z(tokens) {
let node = {
type: 'program',
children: [],
};
// Loop over all valid S productions.
do {
node.children.push(S(tokens));
// Need to check for EOF here so we don't try to keep going.
let token = tokens.next();
if (util.isEOF(token)) {
break;
}
// If the last token was not the EOF, then we need to
// backup because it is the start of another S production.
tokens.backup();
} while (true);
return node;
}
// S => test t { T }
function S(tokens) {
let token = tokens.next();
if (!util.isKeyword(token, 'test')) {
throwException(token, 'Expected keyword `test`');
}
token = tokens.next();
if (!util.isStr(token)) {
throwException(token, 'Expected string');
}
// Found the start of a test block.
let node = {
type: 'testStmt',
left: {
type: 'strlit',
literal: token.literal,
},
right: null,
};
token = tokens.next();
if (!util.isOpeningBrace(token)) {
throwException(token, 'Expected token `{`');
}
node.right = {
type: 'blockStmt',
children: [],
}
// Loop over all valid T productions.
do {
node.right.children.push(T(tokens));
// Check if we have a closing brace to end the test block.
if (util.isClosingBrace(tokens.next())) {
break;
}
// If the last token was not a closing brace, then we need to
// backup because it is the start of another T production.
tokens.backup();
} while (true);
return node;
}
// T => fields F { T }
// T => each F { T }
// T => A should BCE
function T(tokens) {
let token = tokens.next();
// Check if we are starting a 'fields' or 'each' block.
// We have a separate function for these.
if (util.isKeyword(token, 'fields')) {
return TBlock('fields', tokens);
} else if (util.isKeyword(token, 'each')) {
return TBlock('each', tokens);
}
// Found the start of a should statement
let node = {
type: 'shouldStmt',
left: null,
right: null,
};
// See if we have an optional identifier before our 'should' keyword.
let a = A(token);
if (a) {
node.left = a;
token = tokens.next();
}
let shouldArgsNode = {
type: 'args',
children: [],
};
if (!util.isKeyword(token, 'should')) {
if (a) {
throwException(token, 'Expected to find keyword `should` after a field name. If field name is more than one word, try enclosing in quotes.');
}
throwException(token, 'Expected keyword `should` or closing `}`');
}
// Check if we have an optional 'not' operator.
let b = B(tokens);
if (b) {
shouldArgsNode.children.push(b);
}
// Get the last bits of the should statement.
shouldArgsNode.children.push(C(tokens));
shouldArgsNode.children.push(E(tokens));
node.right = shouldArgsNode;
node.line = token.line;
return node;
}
// T => fields F { T }
// T => each F { T }
function TBlock(blockType, tokens) {
let node = {
type: blockType + 'Stmt',
left: F(tokens), // Check if we have an optional identifier
right: null,
};
let token = tokens.next();
if (!util.isOpeningBrace(token)) {
throwException(token, 'Expected token `{`');
}
node.right = {
type: 'blockStmt',
children: [],
}
// Loop over all valid T productions.
// We recursively check for more T productions here as there can be
// multiple nested fields and each blocks containing should statements.
do {
node.right.children.push(T(tokens));
// Check if we have a closing brace to end the test block.
if (util.isClosingBrace(tokens.next())) {
break;
}
// If the last token was not a closing brace, then we need to
// backup because it is the start of another T production.
tokens.backup();
} while (true);
return node;
}
// A => size | i | t | e
function A(token) {
if (util.isKeyword(token, 'size')) {
return {
type: "kwd",
literal: "size",
};
} else if (util.isIdentifier(token)) {
return {
type: "id",
literal: token.literal,
};
} else if (util.isStr(token)) {
return {
type: "strlit",
literal: token.literal,
};
} else {
return null;
}
}
// B => not | e
function B(tokens) {
let token = tokens.next();
if (util.isKeyword(token, 'not')) {
return {
type: "kwd",
literal: "not",
};
}
// If we did not find the optional not operator, then we need to
// backup, because it is the next piece of the should statement.
tokens.backup();
return null;
}
// C => be | equal
function C(tokens) {
let token = tokens.next();
if (util.isKeyword(token, 'be')) {
return {
type: "kwd",
literal: "be",
};
}
if (util.isKeyword(token, 'equal')) {
return {
type: "kwd",
literal: "equal",
};
}
throwException(token, 'Expected either keyword `be` or `equal` to follow a `should` or a `not`.');
}
// E => object | array | string | number | boolean | true | false | t
function E(tokens) {
let token = tokens.next();
if (util.isStr(token)) {
return {
type: 'strlit',
literal: token.literal,
}
}
if (util.isNumber(token)) {
return {
type: 'numlit',
literal: token.literal,
}
}
if (token.type === 'kwd' && equalKeywords.indexOf(token.literal) !== -1) {
return {
type: "kwd",
literal: token.literal,
}
}
throwException(token, 'Expected to find either a string literal, a number literal, or a value type at the end of a should statement. If the comparison value is more than one word, try enclosing it in quotes.');
}
// F => i | t | e
function F(tokens) {
let token = tokens.next();
if (util.isStr(token)) {
return {
type: "strlit",
literal: token.literal,
};
} else if (util.isIdentifier(token)) {
return {
type: "id",
literal: token.literal,
};
} else {
// If we did not find an optional identifier here, then we need to
// backup, because it is the next piece of the T production.
tokens.backup();
return null;
}
}