Skip to content

expressions

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 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_array_expr

parse_array_expr() -> Literal
Source code in packages/arx/src/arx/parser/expressions.py
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
282
283
284
285
def parse_array_expr(self) -> astx.Literal:
    """
    title: Parse list and tensor literals.
    returns:
      type: astx.Literal
    """
    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 and tensor literals currently support only "
                    "literal elements."
                )
            elements.append(elem)

            if self._is_operator("]"):
                break

            self._consume_operator(",")

    self._consume_operator("]")
    if any(
        isinstance(element, (astx.LiteralList, astx.LiteralTuple))
        for element in elements
    ):
        return astx.LiteralTuple(tuple(elements))
    return astx.LiteralList(elements)

parse_assert_stmt

parse_assert_stmt() -> AssertStmt
Source code in packages/arx/src/arx/parser/base.py
502
503
504
505
506
507
508
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 packages/arx/src/arx/parser/expressions.py
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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, ...] = (),
    declared_lists: tuple[str, ...] = (),
    declared_tensors: dict[str, TensorBinding | None]
    | None = None,
) -> Block
Source code in packages/arx/src/arx/parser/base.py
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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 one 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
    """
    del allow_docstring, declared_names, declared_lists, declared_tensors
    raise NotImplementedError

parse_bool_expr

parse_bool_expr() -> LiteralBoolean
Source code in packages/arx/src/arx/parser/expressions.py
224
225
226
227
228
229
230
231
232
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 packages/arx/src/arx/parser/expressions.py
214
215
216
217
218
219
220
221
222
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 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

parse_declaration_prefixes(
    *, body_indent: int | None = None
) -> ParsedDeclarationPrefixes
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/expressions.py
175
176
177
178
179
180
181
182
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 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_float_expr

parse_float_expr() -> LiteralFloat32
Source code in packages/arx/src/arx/parser/expressions.py
194
195
196
197
198
199
200
201
202
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

parse_for_stmt() -> AST
Source code in packages/arx/src/arx/parser/base.py
486
487
488
489
490
491
492
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 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_identifier_expr

parse_identifier_expr() -> AST
Source code in packages/arx/src/arx/parser/expressions.py
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
343
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
370
371
372
373
374
375
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("("):
        identifier = astx.Identifier(id_name, loc=id_loc)
        binding = self._lookup_tensor_binding(id_name)
        if binding is not None:
            attach_binding(identifier, binding)
        return identifier

    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(
            type_context=TypeUseContext.EXPRESSION
        )
        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 packages/arx/src/arx/parser/base.py
470
471
472
473
474
475
476
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 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_int_expr

parse_int_expr() -> LiteralInt32
Source code in packages/arx/src/arx/parser/expressions.py
184
185
186
187
188
189
190
191
192
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_none_expr

parse_none_expr() -> LiteralNone
Source code in packages/arx/src/arx/parser/expressions.py
234
235
236
237
238
239
240
241
242
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 packages/arx/src/arx/parser/expressions.py
244
245
246
247
248
249
250
251
252
253
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

parse_postfix() -> AST
Source code in packages/arx/src/arx/parser/expressions.py
 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
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("[") or self._is_operator("."):
        if self._is_operator("["):
            expr = self.parse_subscript_expr(expr)
            continue

        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(")")
            if (
                member_name == "append"
                and isinstance(expr, astx.Identifier)
                and self._is_list_name(expr.name)
            ):
                if template_args is not None:
                    raise ParserException(
                        "List append does not accept template arguments."
                    )
                if len(args) != 1:
                    raise ParserException(
                        "List append expects exactly one argument."
                    )
                expr = astx.ListAppend(expr, args[0])
                continue
            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

parse_primary() -> AST
Source code in packages/arx/src/arx/parser/expressions.py
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
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_array_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 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/base.py
510
511
512
513
514
515
516
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 packages/arx/src/arx/parser/expressions.py
204
205
206
207
208
209
210
211
212
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_subscript_expr

parse_subscript_expr(base: AST) -> AST
Source code in packages/arx/src/arx/parser/expressions.py
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
def parse_subscript_expr(self, base: astx.AST) -> astx.AST:
    """
    title: Parse one postfix subscript or tensor index expression.
    parameters:
      base:
        type: astx.AST
    returns:
      type: astx.AST
    """
    subscript_loc = self.tokens.cur_tok.location
    self._consume_operator("[")

    if self._is_operator("]"):
        raise ParserException("Expected one index inside '[' and ']'.")

    indices: list[astx.Expr] = []
    while True:
        indices.append(cast(astx.Expr, self.parse_expression()))
        if self._is_operator("]"):
            break
        self._consume_operator(",")

    self._consume_operator("]")

    tensor_base = self._coerce_tensor_base(base)
    if tensor_base is None:
        if len(indices) != 1:
            raise ParserException(
                "Multidimensional indexing is only supported for "
                "tensor values."
            )
        return astx.SubscriptExpr(
            cast(astx.Expr, base),
            indices[0],
            loc=subscript_loc,
        )

    self._validate_tensor_indices(tensor_base, indices)
    return astx.TensorIndex(tensor_base, indices)

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_unary

parse_unary() -> AST
Source code in packages/arx/src/arx/parser/expressions.py
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
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 packages/arx/src/arx/parser/base.py
494
495
496
497
498
499
500
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 packages/arx/src/arx/parser/base.py
478
479
480
481
482
483
484
def parse_while_stmt(self) -> astx.WhileStmt:
    """
    title: Parse one while expression.
    returns:
      type: astx.WhileStmt
    """
    raise NotImplementedError