Parse primary, postfix, unary, and binary expressions plus literal and call
syntax.
Classes:
ExpressionParserMixin
Bases: ParserMixinBase
Methods:
get_tok_precedence
get_tok_precedence() -> int
Source code in src/arx/parser/base.py
225
226
227
228
229
230
231 | def get_tok_precedence(self) -> int:
"""
title: Get the precedence of the pending binary operator token.
returns:
type: int
"""
raise NotImplementedError
|
parse_assert_stmt
parse_assert_stmt() -> AssertStmt
Source code in src/arx/parser/base.py
384
385
386
387
388
389
390 | def parse_assert_stmt(self) -> astx.AssertStmt:
"""
title: Parse one fatal assertion statement.
returns:
type: astx.AssertStmt
"""
raise NotImplementedError
|
parse_bin_op_rhs
parse_bin_op_rhs(expr_prec: int, lhs: AST) -> AST
Source code in src/arx/parser/expressions.py
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 | def parse_bin_op_rhs(self, expr_prec: int, lhs: astx.AST) -> astx.AST:
"""
title: Parse a binary expression rhs.
parameters:
expr_prec:
type: int
lhs:
type: astx.AST
returns:
type: astx.AST
"""
while True:
cur_prec = self.get_tok_precedence()
if cur_prec < expr_prec:
return lhs
bin_op = cast(str, self.tokens.cur_tok.value)
bin_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat operator
rhs = self.parse_unary()
next_prec = self.get_tok_precedence()
if cur_prec < next_prec:
rhs = self.parse_bin_op_rhs(cur_prec + 1, rhs)
lhs = astx.BinaryOp(
bin_op,
cast(astx.DataType, lhs),
cast(astx.DataType, rhs),
loc=bin_loc,
)
|
parse_block
parse_block(
allow_docstring: bool = False,
declared_names: tuple[str, ...] = (),
) -> Block
Source code in src/arx/parser/base.py
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 | def parse_block(
self,
allow_docstring: bool = False,
declared_names: tuple[str, ...] = (),
) -> astx.Block:
"""
title: Parse one block of nodes.
parameters:
allow_docstring:
type: bool
declared_names:
type: tuple[str, Ellipsis]
returns:
type: astx.Block
"""
del allow_docstring, declared_names
raise NotImplementedError
|
parse_bool_expr
parse_bool_expr() -> LiteralBoolean
Source code in src/arx/parser/expressions.py
202
203
204
205
206
207
208
209
210 | def parse_bool_expr(self) -> astx.LiteralBoolean:
"""
title: Parse the bool expression.
returns:
type: astx.LiteralBoolean
"""
result = astx.LiteralBoolean(self.tokens.cur_tok.value)
self.tokens.get_next_token()
return result
|
parse_char_expr
parse_char_expr() -> LiteralUTF8Char
Source code in src/arx/parser/expressions.py
192
193
194
195
196
197
198
199
200 | def parse_char_expr(self) -> astx.LiteralUTF8Char:
"""
title: Parse the char expression.
returns:
type: astx.LiteralUTF8Char
"""
result = astx.LiteralUTF8Char(self.tokens.cur_tok.value)
self.tokens.get_next_token()
return result
|
parse_class_decl
parse_class_decl(
annotations: ParsedAnnotation | None = None,
) -> ClassDefStmt
Source code in src/arx/parser/base.py
311
312
313
314
315
316
317
318
319
320
321
322
323
324 | def parse_class_decl(
self,
annotations: ParsedAnnotation | None = None,
) -> astx.ClassDefStmt:
"""
title: Parse one class declaration.
parameters:
annotations:
type: ParsedAnnotation | None
returns:
type: astx.ClassDefStmt
"""
del annotations
raise NotImplementedError
|
parse_declaration_prefixes
Source code in src/arx/parser/base.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340 | def parse_declaration_prefixes(
self,
*,
body_indent: int | None = None,
) -> ParsedDeclarationPrefixes:
"""
title: Parse declaration prefixes before one declaration.
parameters:
body_indent:
type: int | None
returns:
type: ParsedDeclarationPrefixes
"""
del body_indent
raise NotImplementedError
|
parse_expression
parse_expression() -> AST
Source code in src/arx/parser/expressions.py
153
154
155
156
157
158
159
160 | def parse_expression(self) -> astx.AST:
"""
title: Parse an expression.
returns:
type: astx.AST
"""
lhs = self.parse_unary()
return self.parse_bin_op_rhs(0, lhs)
|
parse_extern
parse_extern() -> FunctionPrototype
Source code in src/arx/parser/base.py
293
294
295
296
297
298
299 | def parse_extern(self) -> astx.FunctionPrototype:
"""
title: Parse one extern declaration.
returns:
type: astx.FunctionPrototype
"""
raise NotImplementedError
|
parse_float_expr
parse_float_expr() -> LiteralFloat32
Source code in src/arx/parser/expressions.py
172
173
174
175
176
177
178
179
180 | def parse_float_expr(self) -> astx.LiteralFloat32:
"""
title: Parse the float expression.
returns:
type: astx.LiteralFloat32
"""
result = astx.LiteralFloat32(self.tokens.cur_tok.value)
self.tokens.get_next_token()
return result
|
parse_for_stmt
Source code in src/arx/parser/base.py
368
369
370
371
372
373
374 | def parse_for_stmt(self) -> astx.AST:
"""
title: Parse one for-loop expression.
returns:
type: astx.AST
"""
raise NotImplementedError
|
parse_function
parse_function(
template_params: tuple[TemplateParam, ...] = (),
) -> FunctionDef
Source code in src/arx/parser/base.py
278
279
280
281
282
283
284
285
286
287
288
289
290
291 | def parse_function(
self,
template_params: tuple[astx.TemplateParam, ...] = (),
) -> astx.FunctionDef:
"""
title: Parse one function definition.
parameters:
template_params:
type: tuple[astx.TemplateParam, Ellipsis]
returns:
type: astx.FunctionDef
"""
del template_params
raise NotImplementedError
|
parse_identifier_expr
parse_identifier_expr() -> AST
Source code in src/arx/parser/expressions.py
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 | def parse_identifier_expr(self) -> astx.AST:
"""
title: Parse the identifier expression.
returns:
type: astx.AST
"""
id_name = cast(str, self.tokens.cur_tok.value)
id_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat identifier
template_args = self._parse_template_args_for_call()
if not self._is_operator("("):
return astx.Identifier(id_name, loc=id_loc)
self._consume_operator("(")
if id_name == builtins.BUILTIN_CAST:
if template_args is not None:
raise ParserException(
f"Builtin '{id_name}' does not accept template arguments."
)
value_expr = self.parse_expression()
self._consume_operator(",")
target_type = self.parse_type()
self._consume_operator(")")
return builtins.build_cast(
cast(astx.DataType, value_expr), target_type
)
if id_name == builtins.BUILTIN_PRINT:
if template_args is not None:
raise ParserException(
f"Builtin '{id_name}' does not accept template arguments."
)
message = self.parse_expression()
self._consume_operator(")")
return builtins.build_print(cast(astx.Expr, message))
if id_name in {"datetime", "timestamp"}:
if template_args is not None:
raise ParserException(
f"Builtin '{id_name}' does not accept template arguments."
)
arg = self.parse_expression()
self._consume_operator(")")
if not isinstance(arg, astx.LiteralString):
raise ParserException(
f"Builtin '{id_name}' expects a string literal argument."
)
if id_name == "datetime":
return astx.LiteralDateTime(arg.value, loc=id_loc)
return astx.LiteralTimestamp(arg.value, loc=id_loc)
args: list[astx.DataType] = []
if not self._is_operator(")"):
while True:
args.append(cast(astx.DataType, self.parse_expression()))
if self._is_operator(")"):
break
self._consume_operator(",")
self._consume_operator(")")
if id_name in self.known_class_names and not self._name_is_shadowed(
id_name
):
if template_args is not None:
raise ParserException(
"class construction does not accept template arguments"
)
if args:
raise ParserException(
"class construction does not accept arguments"
)
return astx.ClassConstruct(id_name)
call = astx.FunctionCall(id_name, args, loc=id_loc)
if template_args is not None:
astx.set_template_args(call, template_args)
return call
|
parse_if_stmt
parse_if_stmt() -> IfStmt
Source code in src/arx/parser/base.py
352
353
354
355
356
357
358 | def parse_if_stmt(self) -> astx.IfStmt:
"""
title: Parse one if expression.
returns:
type: astx.IfStmt
"""
raise NotImplementedError
|
parse_import_stmt
parse_import_stmt() -> ImportStmt | ImportFromStmt
Source code in src/arx/parser/base.py
301
302
303
304
305
306
307
308
309 | def parse_import_stmt(
self,
) -> astx.ImportStmt | astx.ImportFromStmt:
"""
title: Parse one import statement.
returns:
type: astx.ImportStmt | astx.ImportFromStmt
"""
raise NotImplementedError
|
parse_int_expr
parse_int_expr() -> LiteralInt32
Source code in src/arx/parser/expressions.py
162
163
164
165
166
167
168
169
170 | def parse_int_expr(self) -> astx.LiteralInt32:
"""
title: Parse the integer expression.
returns:
type: astx.LiteralInt32
"""
result = astx.LiteralInt32(self.tokens.cur_tok.value)
self.tokens.get_next_token()
return result
|
parse_list_expr
parse_list_expr() -> LiteralList
Source code in src/arx/parser/expressions.py
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 | def parse_list_expr(self) -> astx.LiteralList:
"""
title: Parse list literals.
returns:
type: astx.LiteralList
"""
self._consume_operator("[")
elements: list[astx.Literal] = []
if not self._is_operator("]"):
while True:
elem = self.parse_expression()
if not isinstance(elem, astx.Literal):
raise ParserException(
"List literals currently support only literal "
"elements."
)
elements.append(elem)
if self._is_operator("]"):
break
self._consume_operator(",")
self._consume_operator("]")
return astx.LiteralList(elements)
|
parse_none_expr
parse_none_expr() -> LiteralNone
Source code in src/arx/parser/expressions.py
212
213
214
215
216
217
218
219
220 | def parse_none_expr(self) -> astx.LiteralNone:
"""
title: Parse the none expression.
returns:
type: astx.LiteralNone
"""
result = astx.LiteralNone()
self.tokens.get_next_token()
return result
|
parse_paren_expr
parse_paren_expr() -> AST
Source code in src/arx/parser/expressions.py
222
223
224
225
226
227
228
229
230
231 | def parse_paren_expr(self) -> astx.AST:
"""
title: Parse the parenthesis expression.
returns:
type: astx.AST
"""
self._consume_operator("(")
expr = self.parse_expression()
self._consume_operator(")")
return expr
|
parse_postfix
Source code in src/arx/parser/expressions.py
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 | def parse_postfix(self) -> astx.AST:
"""
title: Parse postfix member access and method calls.
returns:
type: astx.AST
"""
expr = self.parse_primary()
while self._is_operator("."):
self._consume_operator(".")
if self.tokens.cur_tok.kind != TokenKind.identifier:
raise ParserException(
"Parser: Expected member name after '.'."
)
member_name = cast(str, self.tokens.cur_tok.value)
self.tokens.get_next_token()
template_args = self._parse_template_args_for_call()
if self._is_operator("("):
self._consume_operator("(")
args: list[astx.DataType] = []
if not self._is_operator(")"):
while True:
args.append(
cast(astx.DataType, self.parse_expression())
)
if self._is_operator(")"):
break
self._consume_operator(",")
self._consume_operator(")")
class_name = self._class_name_from_expr(expr)
if class_name is not None:
expr = astx.StaticMethodCall(
class_name,
member_name,
args,
)
else:
expr = astx.MethodCall(
expr,
member_name,
args,
)
if template_args is not None:
astx.set_template_args(expr, template_args)
continue
class_name = self._class_name_from_expr(expr)
if class_name is not None:
expr = astx.StaticFieldAccess(class_name, member_name)
else:
expr = astx.FieldAccess(expr, member_name)
return expr
|
parse_primary
Source code in src/arx/parser/expressions.py
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 | def parse_primary(self) -> astx.AST:
"""
title: Parse the primary expression.
returns:
type: astx.AST
"""
if self._is_operator("@"):
raise ParserException(
"Declaration prefixes are only allowed before declarations."
)
if self.tokens.cur_tok.kind == TokenKind.kw_class:
raise ParserException(
"Class declarations are only allowed at module scope."
)
if self.tokens.cur_tok.kind == TokenKind.kw_import:
raise ParserException(
"Import statements are only allowed at module scope."
)
if self.tokens.cur_tok.kind == TokenKind.identifier:
return self.parse_identifier_expr()
if self.tokens.cur_tok.kind == TokenKind.int_literal:
return self.parse_int_expr()
if self.tokens.cur_tok.kind == TokenKind.float_literal:
return self.parse_float_expr()
if self.tokens.cur_tok.kind == TokenKind.string_literal:
return self.parse_string_expr()
if self.tokens.cur_tok.kind == TokenKind.char_literal:
return self.parse_char_expr()
if self.tokens.cur_tok.kind == TokenKind.bool_literal:
return self.parse_bool_expr()
if self.tokens.cur_tok.kind == TokenKind.none_literal:
return self.parse_none_expr()
if self._is_operator("("):
return self.parse_paren_expr()
if self._is_operator("["):
return self.parse_list_expr()
if self.tokens.cur_tok.kind == TokenKind.kw_if:
return self.parse_if_stmt()
if self.tokens.cur_tok.kind == TokenKind.kw_while:
return self.parse_while_stmt()
if self.tokens.cur_tok.kind == TokenKind.kw_for:
return self.parse_for_stmt()
if self.tokens.cur_tok.kind == TokenKind.kw_var:
return self.parse_var_expr()
if self.tokens.cur_tok.kind == TokenKind.kw_assert:
return self.parse_assert_stmt()
if self._is_operator(";"):
self.tokens.get_next_token()
return self.parse_primary()
if self.tokens.cur_tok.kind == TokenKind.kw_return:
return self.parse_return_function()
if self.tokens.cur_tok.kind == TokenKind.indent:
return self.parse_block()
if self.tokens.cur_tok.kind == TokenKind.docstring:
raise ParserException(
"Docstrings are only allowed at module start or as the "
"first statement inside a function body."
)
msg = (
"Parser: Unknown token when expecting an expression: "
f"'{self.tokens.cur_tok.get_name()}'."
)
if self.tokens.cur_tok.kind != TokenKind.eof:
self.tokens.get_next_token()
raise ParserException(msg)
|
parse_prototype
parse_prototype(expect_colon: bool) -> FunctionPrototype
Source code in src/arx/parser/base.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413 | def parse_prototype(
self,
expect_colon: bool,
) -> astx.FunctionPrototype:
"""
title: Parse one function or extern prototype.
parameters:
expect_colon:
type: bool
returns:
type: astx.FunctionPrototype
"""
del expect_colon
raise NotImplementedError
|
parse_return_function
parse_return_function() -> FunctionReturn
Source code in src/arx/parser/base.py
392
393
394
395
396
397
398 | def parse_return_function(self) -> astx.FunctionReturn:
"""
title: Parse one return expression.
returns:
type: astx.FunctionReturn
"""
raise NotImplementedError
|
parse_string_expr
parse_string_expr() -> LiteralString
Source code in src/arx/parser/expressions.py
182
183
184
185
186
187
188
189
190 | def parse_string_expr(self) -> astx.LiteralString:
"""
title: Parse the string expression.
returns:
type: astx.LiteralString
"""
result = astx.LiteralString(self.tokens.cur_tok.value)
self.tokens.get_next_token()
return result
|
parse_template_argument_list
parse_template_argument_list() -> tuple[DataType, ...]
Source code in src/arx/parser/base.py
342
343
344
345
346
347
348
349
350 | def parse_template_argument_list(
self,
) -> tuple[astx.DataType, ...]:
"""
title: Parse one explicit template-argument list.
returns:
type: tuple[astx.DataType, Ellipsis]
"""
raise NotImplementedError
|
parse_type
parse_type(
*,
allow_template_vars: bool = True,
allow_union: bool = False,
) -> DataType
Source code in src/arx/parser/base.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 | def parse_type(
self,
*,
allow_template_vars: bool = True,
allow_union: bool = False,
) -> astx.DataType:
"""
title: Parse one type annotation.
parameters:
allow_template_vars:
type: bool
allow_union:
type: bool
returns:
type: astx.DataType
"""
del allow_template_vars, allow_union
raise NotImplementedError
|
parse_unary
Source code in src/arx/parser/expressions.py
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 | def parse_unary(self) -> astx.AST:
"""
title: Parse a unary expression.
returns:
type: astx.AST
"""
if self._is_operator("@"):
raise ParserException(
"Declaration prefixes are only allowed before declarations."
)
if (
self.tokens.cur_tok.kind != TokenKind.operator
or self.tokens.cur_tok.value in ("(", "[", ",", ":", ")", "]", ";")
):
return self.parse_postfix()
op_code = cast(str, self.tokens.cur_tok.value)
self.tokens.get_next_token()
operand = self.parse_unary()
unary = astx.UnaryOp(op_code, cast(astx.DataType, operand))
unary.type_ = cast(
astx.ExprType,
getattr(operand, "type_", astx.ExprType()),
)
return unary
|
parse_var_expr
parse_var_expr() -> VariableDeclaration
Source code in src/arx/parser/base.py
376
377
378
379
380
381
382 | def parse_var_expr(self) -> astx.VariableDeclaration:
"""
title: Parse one typed variable declaration.
returns:
type: astx.VariableDeclaration
"""
raise NotImplementedError
|
parse_while_stmt
parse_while_stmt() -> WhileStmt
Source code in src/arx/parser/base.py
360
361
362
363
364
365
366 | def parse_while_stmt(self) -> astx.WhileStmt:
"""
title: Parse one while expression.
returns:
type: astx.WhileStmt
"""
raise NotImplementedError
|