-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
42 lines (42 loc) · 1.07 KB
/
parser.js
File metadata and controls
42 lines (42 loc) · 1.07 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
import tokenize, { TokenType } from "./transpiler";
export default class Parser {
tokens = [];
not_EOF() {
return this.tokens[0].type != TokenType.EOF;
}
at() {
return this.tokens[0];
}
eat() {
const prev = this.tokens.shift();
return prev;
}
produceAST(sourceCode) {
this.tokens = tokenize(sourceCode);
const program = {
kind: 'Program',
body: []
};
// parse until end of file
while (this.not_EOF()) {
program.body.push(this.parse_statement());
}
return program;
}
// parse_statement
parse_statement() {
return this.parse_expression();
}
// parse_expression
parse_expression() {
return this.parse_primaryExpression();
}
// parse_primaryExpression
parse_primaryExpression() {
const currentTokenType = this.at().type;
switch (currentTokenType) {
case TokenType.Identifier:
return { kind: 'Identifier', symbol: this.at().value };
}
}
}