-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
169 lines (138 loc) · 4.73 KB
/
parser.js
File metadata and controls
169 lines (138 loc) · 4.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
// ------- Parser --------
import { Token, TokenType } from "./main.js";
import { PrintStmt, ReturnStmt, Stmt } from "./stuff.js";
import { panic } from "./utils.js";
const Errors = {
EXPECT_INT_ERR: "Expected type annotation for main function",
LEFT_PAREN_ERR: "Expected '('",
RIGHT_PAREN_ERR: "Expected ')'",
LEFT_BRACE_ERR: "Expected '{'",
RIGHT_BRACE_ERR: "Expected '}'",
PRINTF_ERR: 'Expected "printf" statement',
SEMI_COLON_ERR: "Expected ';'",
STRING_ERR: "Expected string literal",
RETURN_ERR: "Expected return statement",
RETURN_CODE_ERR: "Expected return code",
NO_MORE_TOKENS_ERR: "No more tokens to parse!",
EOF_ERR: "Expected EOF token at the end",
};
/**
* @typedef { { token_type: number, error_message: string } } Pattern
*/
export class Parser {
/** @param { Token[] } tokens */
/** @param { { true_newline: boolean } | undefined } options */
constructor(tokens, options) {
this.tokens = tokens;
this.index = 0;
this.back_index = this.tokens.length - 1;
/**
* @type { Stmt[] }
*/
this.statements = [];
this.options = options ?? {
// By default use '\\n'. Useful for transpiling to rust
true_newline: false,
};
}
is_empty() {
return this.tokens.length == 0;
}
is_not_empty() {
return this.tokens.length > 0;
}
peek() {
if (this.is_empty()) {
panic(Errors.NO_MORE_TOKENS_ERR);
}
return this.tokens[0];
}
peek_back() {
return this.tokens[this.tokens.length - 1];
}
pop_front() {
if (this.is_empty()) {
return null;
}
return this.tokens.shift();
}
pop_back() {
if (this.is_empty()) {
return null;
}
return this.tokens.pop();
}
parse() {
// start main
this.expect(TokenType.INT_TYPE, Errors.EXPECT_INT_ERR);
const fn_name = this.expect(TokenType.IDENTIFIER).value;
if (fn_name !== "main") {
panic(`Invalid function name: "${fn_name}". Expected "main"`);
}
this.expect(TokenType.LEFT_PAREN, Errors.LEFT_PAREN_ERR);
this.expect(TokenType.RIGHT_PAREN, Errors.RIGHT_PAREN_ERR);
this.expect(TokenType.LEFT_BRACE, Errors.LEFT_BRACE_ERR);
this.expect_back(TokenType.EOF, Errors.EOF_ERR);
this.expect_back(TokenType.RIGHT_BRACE, Errors.RIGHT_BRACE_ERR);
// end main
while (this.is_not_empty()) {
this.statements.push(this.get_next_stmt());
}
return this.statements;
}
/**
* @param {number} expected_type
* @param {string} error_message
* @returns { Token }
*/
expect(expected_type, error_message) {
if (this.is_empty()) {
panic(error_message);
} else if (this.peek().token_type !== expected_type) {
panic(`${error_message} at ${this.peek().loc.display()}`);
}
return this.pop_front();
}
/**
* @param { number } expected_type
* @param { string } error_message
* @returns { Token }
*/
expect_back(expected_type, error_message) {
if (this.is_empty()) {
panic(error_message);
} else if (this.peek_back().token_type !== expected_type) {
panic(`${error_message} at ${this.peek().loc.display()}`);
}
return this.pop_back();
}
get_next_stmt() {
/**
* @type { Token }
*/
const token = this.peek();
if (token.token_type === TokenType.PRINTF) {
this.expect(TokenType.PRINTF, Errors.PRINTF_ERR); // skip print
this.expect(TokenType.LEFT_PAREN, Errors.LEFT_PAREN_ERR); // skip '('
let value = this.expect(TokenType.STRING, Errors.STRING_ERR).value; // get the string!
this.expect(TokenType.RIGHT_PAREN, Errors.RIGHT_PAREN_ERR); // skip ')'
this.expect(TokenType.SEMICOLON, Errors.SEMI_COLON_ERR);
if (this.options.true_newline) {
const regex = /\\n/gim;
value = value.replace(regex, "\n");
}
return new PrintStmt(value);
} else if (token.token_type === TokenType.RETURN) {
// console.log("current:", this.peek()?.display());
this.expect(TokenType.RETURN, Errors.RETURN_ERR); // skip return
const value = this.expect(
TokenType.NUMBER,
Errors.RETURN_CODE_ERR
).value; // skip number
this.expect(TokenType.SEMICOLON, Errors.SEMI_COLON_ERR);
return new ReturnStmt(value);
} else {
panic(`Unexpected '${token.value}' ${token.display()}`);
}
}
}