Parse blocks, statements, and control-flow constructs that operate on parsed
expressions.
Classes:
ControlFlowParserMixin
Bases: ParserMixinBase
Methods:
get_tok_precedence
get_tok_precedence() -> int
Source code in packages/arx/src/arx/parser/base.py
334
335
336
337
338
339
340 | def get_tok_precedence(self) -> int:
"""
title: Get the precedence of the pending binary operator token.
returns:
type: int
"""
raise NotImplementedError
|
parse_assert_stmt
Source code in packages/arx/src/arx/parser/control_flow.py
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 | def parse_assert_stmt(self) -> astx.AssertStmt:
"""
title: Parse one fatal assertion statement.
returns:
type: astx.AssertStmt
"""
assert_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat assert
condition = cast(astx.Expr, self.parse_expression())
message: astx.Expr | None = None
if self._is_operator(","):
self._consume_operator(",")
if self.tokens.cur_tok.kind in {
TokenKind.eof,
TokenKind.indent,
}:
raise ParserException(
"Expected string literal after ',' in assert statement."
)
message = cast(astx.Expr, self.parse_expression())
if not isinstance(message, astx.LiteralString):
raise ParserException(
"Assertion messages must be string literals."
)
return astx.AssertStmt(
condition=condition,
message=message,
loc=assert_loc,
)
|
parse_block
Source code in packages/arx/src/arx/parser/control_flow.py
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 | def parse_block(
self,
allow_docstring: bool = False,
declared_names: tuple[str, ...] = (),
declared_lists: tuple[str, ...] = (),
declared_tensors: dict[str, TensorBinding | None] | None = None,
) -> astx.Block:
"""
title: Parse a block of nodes.
parameters:
allow_docstring:
type: bool
declared_names:
type: tuple[str, Ellipsis]
declared_lists:
type: tuple[str, Ellipsis]
declared_tensors:
type: dict[str, TensorBinding | None] | None
returns:
type: astx.Block
"""
start_token = self.tokens.cur_tok
if start_token.kind != TokenKind.indent:
raise ParserException("Expected indentation to start a block.")
cur_indent = start_token.value
prev_indent = self.indent_level
if cur_indent <= prev_indent:
raise ParserException("There is no new block to be parsed.")
self.indent_level = cur_indent
self.tokens.get_next_token() # eat indentation
self._push_value_scope(
declared_names,
declared_lists,
declared_tensors,
)
block = astx.Block()
docstring_allowed_here = allow_docstring
try:
while True:
# Indentation tokens are line markers. Consume same-level
# markers (including comment/blank lines), stop on dedent,
# and reject unexpected over-indentation at this parsing
# level.
if self.tokens.cur_tok.kind == TokenKind.indent:
new_indent = self.tokens.cur_tok.value
if new_indent < cur_indent:
break
if new_indent > cur_indent:
raise ParserException("Indentation not allowed here.")
self.tokens.get_next_token()
continue
if self.tokens.cur_tok.kind == TokenKind.docstring:
if not docstring_allowed_here:
raise ParserException(
"Docstrings are only allowed as the first "
"statement inside a function body."
)
try:
validate_docstring(self.tokens.cur_tok.value)
except ValueError as err:
raise ParserException(
f"Invalid function docstring: {err}"
) from err
self.tokens.get_next_token()
docstring_allowed_here = False
else:
node = self.parse_expression()
block.nodes.append(node)
docstring_allowed_here = False
while self._is_operator(";"):
self.tokens.get_next_token()
next_kind: TokenKind = self.tokens.cur_tok.kind
if next_kind not in {
TokenKind.indent,
TokenKind.docstring,
}:
break
finally:
self._pop_value_scope()
self.indent_level = prev_indent
return block
|
parse_class_decl
Source code in packages/arx/src/arx/parser/base.py
429
430
431
432
433
434
435
436
437
438
439
440
441
442 | 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 packages/arx/src/arx/parser/base.py
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458 | 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 packages/arx/src/arx/parser/base.py
342
343
344
345
346
347
348 | def parse_expression(self) -> astx.AST:
"""
title: Parse one expression.
returns:
type: astx.AST
"""
raise NotImplementedError
|
parse_extern
parse_extern() -> FunctionPrototype
Source code in packages/arx/src/arx/parser/base.py
411
412
413
414
415
416
417 | def parse_extern(self) -> astx.FunctionPrototype:
"""
title: Parse one extern declaration.
returns:
type: astx.FunctionPrototype
"""
raise NotImplementedError
|
parse_for_count_stmt
Source code in packages/arx/src/arx/parser/control_flow.py
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 | def parse_for_count_stmt(
self, for_loc: SourceLocation
) -> astx.ForCountLoopStmt:
"""
title: Parse count-style for loop.
parameters:
for_loc:
type: SourceLocation
returns:
type: astx.ForCountLoopStmt
"""
initializer = self.parse_inline_var_declaration()
self._consume_operator(";")
declared_lists: tuple[str, ...] = ()
if isinstance(initializer.type_, astx.ListType):
declared_lists = (initializer.name,)
declared_tensors: dict[str, TensorBinding | None] = {}
if is_tensor_type(initializer.type_):
binding = binding_from_type(initializer.type_)
if binding is None:
raise ParserException(
"Tensor loop initializers require a static shape."
)
declared_tensors[initializer.name] = binding
self._push_value_scope(
(initializer.name,),
declared_lists,
declared_tensors,
)
try:
condition = self.parse_expression()
self._consume_operator(";")
update = self.parse_expression()
self._consume_operator(":")
body = self.parse_block()
finally:
self._pop_value_scope()
return astx.ForCountLoopStmt(
initializer,
cast(astx.Expr, condition),
cast(astx.Expr, update),
body,
loc=for_loc,
)
|
parse_for_stmt
Source code in packages/arx/src/arx/parser/control_flow.py
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 | def parse_for_stmt(self) -> astx.AST:
"""
title: Parse for-loop expressions.
returns:
type: astx.AST
"""
for_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat for
if self.tokens.cur_tok.kind == TokenKind.kw_var:
return self.parse_for_count_stmt(for_loc)
if self.tokens.cur_tok.kind != TokenKind.identifier:
raise ParserException("Parser: Expected identifier after for")
var_name = cast(str, self.tokens.cur_tok.value)
var_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat identifier
if self.tokens.cur_tok != Token(TokenKind.kw_in, "in"):
raise ParserException("Parser: Expected 'in' after loop variable.")
self.tokens.get_next_token() # eat in
if self._looks_like_removed_for_range_header():
raise ParserException(
"Colon range syntax was removed; use "
"'range(start, stop[, step])' after 'in'."
)
iterable = cast(astx.Expr, self.parse_expression())
return self._parse_for_iterable_stmt(
for_loc=for_loc,
loop_var_name=var_name,
loop_var_loc=var_loc,
iterable=iterable,
)
|
parse_function
Source code in packages/arx/src/arx/parser/base.py
396
397
398
399
400
401
402
403
404
405
406
407
408
409 | 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_if_stmt
parse_if_stmt() -> IfStmt
Source code in packages/arx/src/arx/parser/control_flow.py
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 | def parse_if_stmt(self) -> astx.IfStmt:
"""
title: Parse the `if` expression.
returns:
type: astx.IfStmt
"""
if_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat if
cond = self.parse_expression()
self._consume_operator(":")
then_block = self.parse_block()
if self.tokens.cur_tok.kind == TokenKind.indent:
self.tokens.get_next_token()
else_block = astx.Block()
if self.tokens.cur_tok.kind == TokenKind.kw_else:
self.tokens.get_next_token() # eat else
self._consume_operator(":")
else_block = self.parse_block()
return astx.IfStmt(
cast(astx.Expr, cond), then_block, else_block, loc=if_loc
)
|
parse_import_stmt
parse_import_stmt() -> ImportStmt | ImportFromStmt
Source code in packages/arx/src/arx/parser/base.py
419
420
421
422
423
424
425
426
427 | def parse_import_stmt(
self,
) -> astx.ImportStmt | astx.ImportFromStmt:
"""
title: Parse one import statement.
returns:
type: astx.ImportStmt | astx.ImportFromStmt
"""
raise NotImplementedError
|
parse_inline_var_declaration
parse_inline_var_declaration() -> InlineVariableDeclaration
Source code in packages/arx/src/arx/parser/control_flow.py
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 | def parse_inline_var_declaration(self) -> astx.InlineVariableDeclaration:
"""
title: Parse inline variable declaration used by count-style for loops.
returns:
type: astx.InlineVariableDeclaration
"""
if self.tokens.cur_tok.kind != TokenKind.kw_var:
raise ParserException("Parser: Expected 'var' in for initializer")
var_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat var
cur_kind: TokenKind = self.tokens.cur_tok.kind
if cur_kind != TokenKind.identifier:
raise ParserException("Parser: Expected identifier after var")
name = cast(str, self.tokens.cur_tok.value)
self.tokens.get_next_token() # eat identifier
if not self._is_operator(":"):
raise ParserException(
"Parser: Expected type annotation for inline variable "
f"'{name}'."
)
self._consume_operator(":")
var_type = self.parse_type(type_context=TypeUseContext.INLINE_VARIABLE)
self._consume_operator("=")
try:
value = coerce_expression(
cast(astx.Expr, self.parse_expression()),
var_type,
context=f"inline variable '{name}'",
)
except ValueError as err:
raise ParserException(str(err)) from err
return astx.InlineVariableDeclaration(
name=name,
type_=var_type,
value=value,
mutability=astx.MutabilityKind.mutable,
loc=var_loc,
)
|
parse_prototype
parse_prototype(expect_colon: bool) -> FunctionPrototype
Source code in packages/arx/src/arx/parser/base.py
518
519
520
521
522
523
524
525
526
527
528
529
530
531 | 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 packages/arx/src/arx/parser/control_flow.py
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488 | def parse_return_function(self) -> astx.FunctionReturn:
"""
title: Parse the return expression.
returns:
type: astx.FunctionReturn
"""
return_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat return
bare_return_terminators = {
TokenKind.indent,
TokenKind.eof,
TokenKind.kw_function,
TokenKind.kw_class,
TokenKind.kw_extern,
TokenKind.kw_import,
}
if (
self.tokens.cur_tok.kind in bare_return_terminators
or self._is_operator(";")
):
return astx.FunctionReturn(astx.LiteralNone(), loc=return_loc)
value = self.parse_expression()
return_type = self._current_return_type()
if return_type is not None:
try:
value = coerce_expression(
cast(astx.Expr, value),
return_type,
context="return value",
)
except ValueError as err:
raise ParserException(str(err)) from err
return astx.FunctionReturn(cast(astx.DataType, value), loc=return_loc)
|
parse_template_argument_list
parse_template_argument_list() -> tuple[DataType, ...]
Source code in packages/arx/src/arx/parser/base.py
460
461
462
463
464
465
466
467
468 | 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,
type_context: TypeUseContext = GENERAL,
) -> DataType
Source code in packages/arx/src/arx/parser/base.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 | def parse_type(
self,
*,
allow_template_vars: bool = True,
allow_union: bool = False,
type_context: TypeUseContext = TypeUseContext.GENERAL,
) -> astx.DataType:
"""
title: Parse one type annotation.
parameters:
allow_template_vars:
type: bool
allow_union:
type: bool
type_context:
type: TypeUseContext
returns:
type: astx.DataType
"""
del allow_template_vars, allow_union, type_context
raise NotImplementedError
|
parse_var_expr
parse_var_expr() -> VariableDeclaration
Source code in packages/arx/src/arx/parser/control_flow.py
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
415
416
417
418
419
420 | def parse_var_expr(self) -> astx.VariableDeclaration:
"""
title: Parse typed variable declarations.
returns:
type: astx.VariableDeclaration
"""
var_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat var
if self.tokens.cur_tok.kind != TokenKind.identifier:
raise ParserException("Parser: Expected identifier after var")
name = cast(str, self.tokens.cur_tok.value)
self.tokens.get_next_token() # eat identifier
if not self._is_operator(":"):
raise ParserException(
f"Parser: Expected type annotation for variable '{name}'."
)
self._consume_operator(":")
var_type = self.parse_type(type_context=TypeUseContext.VARIABLE)
value: astx.Expr | None = None
if self._is_operator("="):
self._consume_operator("=")
try:
value = coerce_expression(
cast(astx.Expr, self.parse_expression()),
var_type,
context=f"variable '{name}'",
)
except ValueError as err:
raise ParserException(str(err)) from err
if self.tokens.cur_tok == Token(TokenKind.kw_in, "in"):
raise ParserException(
"Legacy 'var ... in ...' syntax is not "
"supported in this parser."
)
if value is None:
value = self._default_value_for_type(var_type)
declaration = astx.VariableDeclaration(
name=name,
type_=var_type,
value=value,
mutability=astx.MutabilityKind.mutable,
loc=var_loc,
)
self._declare_value_name(name)
if is_tensor_type(var_type):
binding = binding_from_type(var_type)
if binding is None:
raise ParserException(
"Tensor declarations require a static shape."
)
self._declare_tensor_name(name, binding)
if isinstance(var_type, astx.ListType):
self._declare_list_name(name)
return declaration
|
parse_while_stmt
parse_while_stmt() -> WhileStmt
Source code in packages/arx/src/arx/parser/control_flow.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190 | def parse_while_stmt(self) -> astx.WhileStmt:
"""
title: Parse the `while` expression.
returns:
type: astx.WhileStmt
"""
while_loc = self.tokens.cur_tok.location
self.tokens.get_next_token() # eat while
condition = self.parse_expression()
self._consume_operator(":")
body = self.parse_block()
return astx.WhileStmt(cast(astx.Expr, condition), body, loc=while_loc)
|