-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.js
414 lines (349 loc) · 9.81 KB
/
Parser.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
const Token = require('../lexer/Token');
const Lexer = require('../lexer');
const AST = require('../ast');
/**
* Parser implementation for a language.
* Converts stream of tokens into AST.
*
* @class
* @since 1.0.0
*/
class Parser {
/**
* Creates new parser instance.
* It accepts as an input source code of a program.
* In result, it will parse it and return an AST of specified program.
* As a dependency, it uses the lexer which returns stream of tokens.
*
* @param {String} input Source code of a program
* @example
* const parser = new Parser('2 + 5');
*/
constructor(input) {
this.lexer = new Lexer(input);
this.currentToken = this.lexer.getNextToken();
}
/**
* Consumes one specified token type.
* In case, token type is not equal to the current one, it throws an error.
* When you are consuming a token you are not expecting, it means broken syntax structure.
*
* @param {String} tokenType Token type from {@link Token} dictionary
* @returns {Parser} Returns current parser instance
* @example
* const parser = new Parser('2 + 5'); // currentToken = INTEGER
*
* parser
* .eat(Token.INTEGER) // currentToken = PLUS
* .eat(Token.PLUS) // currentToken = INTEGER
* .eat(Token.PLUS) // throws an error, because currentToken = INTEGER
*/
eat(tokenType) {
if (this.currentToken.is(tokenType)) {
this.currentToken = this.lexer.getNextToken();
} else {
Parser.error(`You provided unexpected token type "${tokenType}" while current token is ${this.currentToken}`);
}
return this;
}
/**
* empty:
*
* @returns {NoOperation}
*/
empty() {
return AST.NoOperation.create();
}
/**
* variable: IDENTIFIER
*
* @returns {Variable}
*/
variable() {
const node = AST.Variable.create(this.currentToken, this.currentToken.getValue());
this.eat(Token.IDENTIFIER);
return node;
}
/**
* factor: PLUS factor
* | MINUS factor
* | INTEGER_LITERAL
* | REAL_LITERAL
* | LEFT_PARENTHESIS expr RIGHT_PARENTHESIS
* | variable
*
* @returns {Node}
*/
factor() {
const token = this.currentToken;
if (token.is(Token.PLUS)) {
this.eat(Token.PLUS);
return AST.UnaryOperator.create(token, this.factor());
} else if (token.is(Token.MINUS)) {
this.eat(Token.MINUS);
return AST.UnaryOperator.create(token, this.factor());
} else if (token.is(Token.INTEGER_LITERAL)) {
this.eat(Token.INTEGER_LITERAL);
return AST.Number.create(token);
} else if (token.is(Token.REAL_LITERAL)) {
this.eat(Token.REAL_LITERAL);
return AST.Number.create(token);
} else if (token.is(Token.LEFT_PARENTHESIS)) {
this.eat(Token.LEFT_PARENTHESIS);
const node = this.expr();
this.eat(Token.RIGHT_PARENTHESIS);
return node;
}
return this.variable();
}
/**
* term: factor ASTERISK term
* | factor SLASH term
* | factor REAL_DIV term
* | factor
*
* @returns {Node}
*/
term() {
let node = this.factor();
while ([Token.ASTERISK, Token.SLASH, Token.INTEGER_DIV].some(type => this.currentToken.is(type))) {
const token = this.currentToken;
if (token.is(Token.ASTERISK)) {
this.eat(Token.ASTERISK);
} else if (token.is(Token.SLASH)) {
this.eat(Token.SLASH);
} else {
this.eat(Token.INTEGER_DIV);
}
node = AST.BinaryOperator.create(node, token, this.factor());
}
return node;
}
/**
* expr: term PLUS expr
* | term MINUS expr
* | term
*
* @returns {Node}
*/
expr() {
let node = this.term();
while ([Token.PLUS, Token.MINUS].some(type => this.currentToken.is(type))) {
const token = this.currentToken;
if (token.is(Token.PLUS)) {
this.eat(Token.PLUS);
} else {
this.eat(Token.MINUS);
}
node = AST.BinaryOperator.create(node, token, this.term());
}
return node;
}
/**
* assignmentStatement: variable ASSIGN expr
*
* @returns {Assign}
*/
assignmentStatement() {
const variable = this.variable();
const token = this.currentToken;
this.eat(Token.ASSIGN);
const expression = this.expr();
return AST.Assign.create(variable, token, expression);
}
/**
* compoundStatement: BEGIN statementList END
*
* @returns {Compound}
*/
compoundStatement() {
this.eat(Token.BEGIN);
const nodes = this.statementList();
this.eat(Token.END);
const root = AST.Compound.create();
nodes.forEach(node => root.append(node));
return root;
}
/**
* statement: compoundStatement
* | assignmentStatement
* | empty
*
* @returns {Node}
*/
statement() {
let node;
if (this.currentToken.is(Token.BEGIN)) {
node = this.compoundStatement();
} else if (this.currentToken.is(Token.IDENTIFIER)) {
node = this.assignmentStatement();
} else {
node = this.empty();
}
return node;
}
/**
* statementList: statement
* | statement SEMICOLON statementList
*
* @returns {Array<Node>}
*/
statementList() {
const node = this.statement();
const nodes = [node];
while (this.currentToken.is(Token.SEMICOLON)) {
this.eat(Token.SEMICOLON);
nodes.push(this.statement());
}
if (this.currentToken.is(Token.IDENTIFIER)) {
Parser.error(`Unexpected identifier in "statementList" production: ${this.currentToken}`);
}
return nodes;
}
/**
* typeSpec: INTEGER_TYPE
* | REAL_TYPE
*
* @returns {Type}
*/
typeSpec() {
const token = this.currentToken;
if (token.is(Token.INTEGER_TYPE)) {
this.eat(Token.INTEGER_TYPE);
} else {
this.eat(Token.REAL_TYPE);
}
return AST.Type.create(token);
}
/**
* variableDeclaration: IDENTIFIER (COMMA IDENTIFIER)* COLON typeSpec
*
* @returns {Array<VarDecl>}
*/
variableDeclaration() {
const varNodes = [AST.Variable.create(this.currentToken, this.currentToken.getValue())];
this.eat(Token.IDENTIFIER);
while (this.currentToken.is(Token.COMMA)) {
this.eat(Token.COMMA);
varNodes.push(AST.Variable.create(this.currentToken, this.currentToken.getValue()));
this.eat(Token.IDENTIFIER);
}
this.eat(Token.COLON);
const typeNode = this.typeSpec();
return varNodes.map(node => AST.VarDecl.create(node, typeNode));
}
/**
* formalParameters: ID (COMMA ID)* COLON type_spec
*
* @returns {Array<Param>}
*/
formalParameters() {
const varNodes = [AST.Variable.create(this.currentToken, this.currentToken.getValue())];
this.eat(Token.IDENTIFIER);
while (this.currentToken.is(Token.COMMA)) {
this.eat(Token.COMMA);
varNodes.push(AST.Variable.create(this.currentToken, this.currentToken.getValue()));
this.eat(Token.IDENTIFIER);
}
this.eat(Token.COLON);
const typeNode = this.typeSpec();
return varNodes.map(varNode => AST.Param.create(varNode, typeNode));
}
/**
* formalParameterList: formalParameters
* | formalParameters SEMICOLON formalParameterList
*
* @returns {Array<Param>}
*/
formalParameterList() {
let params = this.formalParameters();
while (this.currentToken.is(Token.SEMICOLON)) {
this.eat(Token.SEMICOLON);
params = params.concat(this.formalParameters());
}
return params;
}
/**
* declarations: VAR (variableDeclaration SEMICOLON)+
* | (PROCEDURE ID (LPAREN formalParameterList RPAREN)? SEMICOLON block SEMICOLON)*
* | empty
*
* @returns {Array}
*/
declarations() {
let declarations = [];
let params = [];
if (this.currentToken.is(Token.VAR)) {
this.eat(Token.VAR);
while (this.currentToken.is(Token.IDENTIFIER)) {
const varDecl = this.variableDeclaration();
declarations = declarations.concat(varDecl);
this.eat(Token.SEMICOLON);
}
}
while (this.currentToken.is(Token.PROCEDURE)) {
this.eat(Token.PROCEDURE);
const procedureName = this.currentToken.getValue();
this.eat(Token.IDENTIFIER);
if (this.currentToken.is(Token.LEFT_PARENTHESIS)) {
this.eat(Token.LEFT_PARENTHESIS);
params = this.formalParameterList();
this.eat(Token.RIGHT_PARENTHESIS);
}
this.eat(Token.SEMICOLON);
const blockNode = this.block();
this.eat(Token.SEMICOLON);
const procedureNode = AST.ProcedureDecl.create(procedureName, params, blockNode);
declarations.push(procedureNode);
}
return declarations;
}
/**
* block: declarations compoundStatement
*
* @returns {Block}
*/
block() {
const declarations = this.declarations();
const compoundStatement = this.compoundStatement();
return AST.Block.create(declarations, compoundStatement);
}
/**
* program: PROGRAM variable SEMICOLON block DOT
*
* @returns {Node}
*/
program() {
this.eat(Token.PROGRAM);
const variableNode = this.variable();
const programName = variableNode.getName();
this.eat(Token.SEMICOLON);
const blockNode = this.block();
const programNode = AST.Program.create(programName, blockNode);
this.eat(Token.DOT);
return programNode;
}
/**
* Parses an input source program and returns an AST.
* It uses all the grammar rules above to parse tokens and build AST from it.
*
* @returns {Node}
* @example
* const parser = new Parser('BEGIN END.');
*
* parser.parse(); // return an object that represents an AST of source program
*/
parse() {
return this.program();
}
/**
* Static helper for notifying about an error, during parsing.
*
* @static
* @param {String} msg Error message
*/
static error(msg) {
throw new Error(`[Parser]\n${msg}`);
}
}
module.exports = Parser;