Skip to content

imports

Parse import statements and grouped import syntax while reusing the shared parser core helpers.

Classes:

ImportParserMixin

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

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_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_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/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_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_grouped_import_names

parse_grouped_import_names() -> list[AliasExpr]
Source code in packages/arx/src/arx/parser/imports.py
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
def parse_grouped_import_names(self) -> list[astx.AliasExpr]:
    """
    title: Parse grouped named imports.
    returns:
      type: list[astx.AliasExpr]
    """
    self._consume_operator("(")
    self._skip_import_layout()

    if self._is_operator(")"):
        raise ParserException("empty grouped imports are not allowed")

    names: list[astx.AliasExpr] = []
    while True:
        if self._is_identifier_value("as"):
            raise ParserException(
                "alias requires an import target before 'as'"
            )
        if self.tokens.cur_tok.kind != TokenKind.identifier:
            raise ParserException(
                "Expected imported name in grouped import list."
            )

        name = cast(str, self.tokens.cur_tok.value)
        name_loc = self.tokens.cur_tok.location
        self.tokens.get_next_token()
        alias_name = self.parse_import_alias()
        names.append(astx.AliasExpr(name, asname=alias_name, loc=name_loc))

        self._skip_import_layout()
        if self._is_operator(")"):
            break

        self._consume_operator(",")
        self._skip_import_layout()
        if self._is_operator(")"):
            break

    self._consume_operator(")")
    return names

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_alias

parse_import_alias() -> str
Source code in packages/arx/src/arx/parser/imports.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def parse_import_alias(self) -> str:
    """
    title: Parse one optional import alias.
    returns:
      type: str
    """
    if not self._is_identifier_value("as"):
        return ""

    self._consume_identifier_value("as")
    if self.tokens.cur_tok.kind != TokenKind.identifier:
        raise ParserException("Expected alias name after 'as'.")

    alias_name = cast(str, self.tokens.cur_tok.value)
    self.tokens.get_next_token()
    return alias_name

parse_import_from_module_path

parse_import_from_module_path() -> tuple[int, str]
Source code in packages/arx/src/arx/parser/imports.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
def parse_import_from_module_path(self) -> tuple[int, str]:
    """
    title: Parse one absolute or relative module path for from-imports.
    returns:
      type: tuple[int, str]
    """
    level = 0
    while self._is_operator("."):
        self._consume_operator(".")
        level += 1

    if level > 0 and self.tokens.cur_tok.kind != TokenKind.identifier:
        raise ParserException(
            "Relative imports require a module path after leading '.'."
        )

    return level, self.parse_module_path()

parse_import_stmt

parse_import_stmt() -> ImportStmt | ImportFromStmt
Source code in packages/arx/src/arx/parser/imports.py
 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
def parse_import_stmt(self) -> astx.ImportStmt | astx.ImportFromStmt:
    """
    title: Parse one import statement.
    returns:
      type: astx.ImportStmt | astx.ImportFromStmt
    """
    import_loc = self.tokens.cur_tok.location
    self.tokens.get_next_token()  # eat import

    if self._is_operator("("):
        names = self.parse_grouped_import_names()
        if not self._is_identifier_value("from"):
            raise ParserException(
                "Grouped imports require 'from <module.path>'."
            )
        self._consume_identifier_value("from")
        level, module_path = self.parse_import_from_module_path()
        self._reject_public_builtin_import(module_path, level)
        return astx.ImportFromStmt(
            names=names,
            module=module_path,
            level=level,
            loc=import_loc,
        )

    if self._is_identifier_value("from"):
        raise ParserException(
            "Expected module path or imported name after 'import'."
        )

    if self._is_identifier_value("as"):
        raise ParserException(
            "alias requires an import target before 'as'"
        )

    if self.tokens.cur_tok.kind != TokenKind.identifier:
        raise ParserException(
            "Expected module path or imported name after 'import'."
        )

    target_name = cast(str, self.tokens.cur_tok.value)
    target_loc = self.tokens.cur_tok.location
    self.tokens.get_next_token()

    if self._is_operator("."):
        module_path = self.parse_module_path(prefix=target_name)
        alias_name = self.parse_import_alias()
        if self._is_identifier_value("from"):
            raise ParserException("Module imports do not use 'from'.")
        self._reject_public_builtin_import(module_path, level=0)
        return astx.ImportStmt(
            [
                astx.AliasExpr(
                    module_path,
                    asname=alias_name,
                    loc=target_loc,
                )
            ],
            loc=import_loc,
        )

    alias_name = self.parse_import_alias()
    if self._is_identifier_value("from"):
        self._consume_identifier_value("from")
        level, module_path = self.parse_import_from_module_path()
        self._reject_public_builtin_import(module_path, level)
        return astx.ImportFromStmt(
            names=[
                astx.AliasExpr(
                    target_name,
                    asname=alias_name,
                    loc=target_loc,
                )
            ],
            module=module_path,
            level=level,
            loc=import_loc,
        )

    if self._is_operator(","):
        raise ParserException("Grouped imports require parentheses.")

    if self._is_operator("("):
        raise ParserException(
            "Parentheses are only supported for grouped named imports."
        )

    self._reject_public_builtin_import(target_name, level=0)
    return astx.ImportStmt(
        [astx.AliasExpr(target_name, asname=alias_name, loc=target_loc)],
        loc=import_loc,
    )

parse_module_path

parse_module_path(prefix: str | None = None) -> str
Source code in packages/arx/src/arx/parser/imports.py
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
def parse_module_path(self, prefix: str | None = None) -> str:
    """
    title: Parse one dotted module path.
    parameters:
      prefix:
        type: str | None
    returns:
      type: str
    """
    parts: list[str] = []
    if prefix is not None:
        parts.append(prefix)
    else:
        if self.tokens.cur_tok.kind != TokenKind.identifier:
            raise ParserException("Expected module path.")
        parts.append(cast(str, self.tokens.cur_tok.value))
        self.tokens.get_next_token()

    while self._is_operator("."):
        self._consume_operator(".")
        if self.tokens.cur_tok.kind != TokenKind.identifier:
            raise ParserException(
                "Expected identifier after '.' in module path."
            )
        parts.append(cast(str, self.tokens.cur_tok.value))
        self.tokens.get_next_token()

    return ".".join(parts)

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_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/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