-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_parser.py
281 lines (229 loc) · 10.4 KB
/
_parser.py
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
# A parser is a software component that takes input data (frequently text) and builds
# a data structure – often some kind of parse tree, abstract syntax tree or other
# hierarchical structure – giving a structural representation of the input, checking for
# correct syntax in the process. […] The parser is often preceded by a separate lexical
# analyser, which creates tokens from the sequence of input characters
import enum
import logging
import types
import typing
import typing_extensions
import _abstract_syntax_tree as _ast
import _lexer
import _token
log = logging.getLogger(__name__)
trace_log = logging.getLogger('tracelogs')
# log.setLevel(logging.CRITICAL + 1)
trace_log.setLevel(logging.CRITICAL + 1)
class Tracer:
def __init__(self):
self.indent_level = 0
def trace(self, func):
def wrapper(parser, *args, **kwargs):
trace_log.debug(' ' * self.indent_level + f'BEGIN {func.__name__}')
self.indent_level += 1
result = func(parser, *args, **kwargs)
self.indent_level -= 1
trace_log.debug(' ' * self.indent_level + f'END {func.__name__}')
return result
return wrapper
def reset(self):
self.indent_level = 0
class Precedence(enum.IntEnum):
LOWEST = 1
# ==
EQUALS = 2
LESSGREATER = 3
SUM = 4
PRODUCT = 5
PREFIX = 6
CALL = 7
precendence_map = types.MappingProxyType(
{
_token.TokenType.EQ: Precedence.EQUALS,
_token.TokenType.NOT_EQ: Precedence.EQUALS,
_token.TokenType.LT: Precedence.LESSGREATER,
_token.TokenType.GT: Precedence.LESSGREATER,
_token.TokenType.PLUS: Precedence.SUM,
_token.TokenType.MINUS: Precedence.SUM,
_token.TokenType.SLASH: Precedence.PRODUCT,
_token.TokenType.ASTERISK: Precedence.PRODUCT,
}
)
class Parser:
tracer = Tracer()
def __init__(self, lexer: _lexer.Lexer):
self.lexer = lexer
self._current_token = None
self._peek_token = None
self.next_token()
self.next_token()
self.tracer.reset()
self._register_infix_parse_functions()
self._register_prefix_parse_functions()
def _register_prefix_parse_functions(self):
self.prefix_parse_functions = types.MappingProxyType(
{
_token.TokenType.IDENT: self._parse_identifier,
_token.TokenType.INT: self._parse_integer_literal,
_token.TokenType.BANG: self._parse_prefix_expression,
_token.TokenType.MINUS: self._parse_prefix_expression,
_token.TokenType.TRUE: self._parse_boolean,
_token.TokenType.FALSE: self._parse_boolean,
_token.TokenType.LPAREN: self._parse_group_expression,
_token.TokenType.IF: self._parse_if_expression,
_token.TokenType.FUNCTION: self._parse_function_literal,
_token.TokenType.STRING: self._parse_string_literal,
_token.TokenType.LBRACKET: self._parse_array_literal,
_token.TokenType.LBRACE: self._parse_hash_literal,
}
)
def _register_infix_parse_functions(self):
self.infix_parse_functions = types.MappingProxyType(
{
_token.TokenType.PLUS: self._parse_infix_expression,
_token.TokenType.MINUS: self._parse_infix_expression,
_token.TokenType.SLASH: self._parse_infix_expression,
_token.TokenType.ASTERISK: self._parse_infix_expression,
_token.TokenType.EQ: self._parse_infix_expression,
_token.TokenType.NOT_EQ: self._parse_infix_expression,
_token.TokenType.LT: self._parse_infix_expression,
_token.TokenType.GT: self._parse_infix_expression,
_token.TokenType.LPAREN: self._parse_call_expression,
_token.TokenType.LBRACKET: self._parse_index_expression,
}
)
@tracer.trace
def _parse_if_expression(self) -> _ast.IfExpression:
current_token = self._current_token
if not self._expect_peek(_token.TokenType.LPAREN):
return
self.next_token()
condition = self._parse_expression(Precedence.LOWEST)
if not self._expect_peek(_token.TokenType.RPAREN) or not self._expect_peek(_token.TokenType.LBRACE):
return
consequence = self._parse_block_statement()
alternative = None
if self._peek_token.token_type == _token.TokenType.ELSE:
self.next_token()
if not self._expect_peek(_token.TokenType.LBRACE):
return
alternative = self._parse_block_statement()
return _ast.IfExpression(token=current_token, condition=condition, consequence=consequence, alternative=alternative)
@tracer.trace
def _parse_block_statement(self) -> _ast.BlockStatement:
current_token = self._current_token
statements = []
self.next_token()
while not self._current_token.token_type == _token.TokenType.RBRACE and not self._current_token.token_type == _token.TokenType.EOF:
statement = self._parse_statement()
if statement:
statements.append(statement)
self.next_token()
return _ast.BlockStatement(token=current_token, statements=statements)
def _parse_function_literal(self): ...
def _parse_string_literal(self): ...
def _parse_array_literal(self): ...
def _parse_hash_literal(self): ...
def _parse_call_expression(sefl): ...
def _parse_index_expression(self): ...
@tracer.trace
def _parse_group_expression(self) -> _ast.Expression | None:
self.next_token()
exp = self._parse_expression(Precedence.LOWEST)
if not self._peek_token.token_type == _token.TokenType.RPAREN:
return None
return exp
@tracer.trace
def _parse_boolean(self) -> _ast.Boolean:
return _ast.Boolean(token=self._current_token, value=self._current_token.token_type == _token.TokenType.TRUE)
@tracer.trace
def _parse_integer_literal(self) -> _ast.IntegerLiteral:
return _ast.IntegerLiteral(token=self._current_token, value=int(self._current_token.literal))
@tracer.trace
def _parse_prefix_expression(self) -> _ast.PrefixExpression:
current_token = self._current_token
return _ast.PrefixExpression(current_token, operator=current_token.literal, right=self.next_token()._parse_expression(Precedence.PREFIX))
@tracer.trace
def _parse_infix_expression(self, left: _ast.Expression):
current_token = self._current_token
precedence = precendence_map.get(self._current_token.token_type) or Precedence.LOWEST
return _ast.InfixExpression(
token=current_token,
operator=current_token.literal,
left=left,
right=self.next_token()._parse_expression(precedence),
)
def next_token(self) -> typing_extensions.Self:
self._current_token = self._peek_token
self._peek_token = self.lexer.next_token()
return self
def parse_program(self) -> typing.Optional[_ast.Program]:
log.debug(f'source code - {self.lexer.source_code}')
trace_log.debug(f'source code - {self.lexer.source_code}')
program = _ast.Program([])
while self._current_token.token_type not in (_token.TokenType.EOF, _token.TokenType.ILLEGAL):
statement = self._parse_statement()
if statement:
program.statements.append(statement)
self.next_token()
return program
@tracer.trace
def _parse_statement(self) -> typing.Optional[_ast.Statement]:
log.debug(f'(parse statement) - {self._current_token=}')
match self._current_token.token_type:
case _token.TokenType.LET:
return self._parse_let_statement()
case _token.TokenType.RETURN:
return self._parse_return_statement()
case _:
return self._parse_expression_statement()
@tracer.trace
def _parse_expression(self, precendence: Precedence) -> _ast.Expression:
prefix = self.prefix_parse_functions.get(self._current_token.token_type)
log.debug(f'(parse expression) - {self._current_token=}')
if not prefix:
return None
left_exp = prefix()
while self._peek_token.token_type != _token.TokenType.SEMICOLON and precendence < precendence_map.get(self._peek_token.token_type, Precedence.LOWEST):
infix = self.infix_parse_functions.get(self._peek_token.token_type)
log.debug(f'(parse expression) - {self._peek_token=}')
if not infix:
return left_exp
self.next_token()
left_exp = infix(left_exp)
return left_exp
@tracer.trace
def _parse_identifier(self) -> _ast.Expression:
return _ast.Identifier(self._current_token, value=self._current_token.literal)
@tracer.trace
def _parse_expression_statement(self) -> _ast.Expression:
log.debug(f'(parse expression statement) - {self._current_token=}')
statement = _ast.ExpressionStatement(token=self._current_token, expression=self._parse_expression(Precedence.LOWEST))
if self._peek_token.token_type == _token.TokenType.SEMICOLON:
self.next_token()
return statement
@tracer.trace
def _parse_return_statement(self) -> _ast.ReturnStatement:
token = self._current_token
return_value = self.next_token()._parse_expression_statement()
if self._peek_token.token_type == _token.TokenType.SEMICOLON:
self.next_token()
return _ast.ReturnStatement(token, return_value)
def _expect_peek(self, token_type: _token.TokenType) -> bool | None:
if self._peek_token.token_type == token_type:
self.next_token()
return True
else:
raise SyntaxError(f'Unexpected {token_type}')
@tracer.trace
def _parse_let_statement(self) -> typing.Optional[_ast.LetStatement]:
token = self._current_token
if not self._expect_peek(_token.TokenType.IDENT):
return None
identifier = _ast.Identifier(token=self._current_token, value=self._current_token.literal)
# skip the expression for now
while self._current_token.token_type != _token.TokenType.SEMICOLON:
self.next_token()
statement = _ast.LetStatement(token=token, name=identifier, value=None)
return statement