Skip to content

core

Hold parser state, scope bookkeeping, token helpers, and module-level orchestration for the concern-grouped parser mixins.

Classes:

ParserCore

ParserCore(tokens: TokenList = TokenList([]))

Bases: ParserMixinBase

Methods:

Source code in src/arx/parser/core.py
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
def __init__(self, tokens: TokenList = TokenList([])) -> None:
    """
    title: Instantiate the Parser object.
    parameters:
      tokens:
        type: TokenList
    """
    self.bin_op_precedence = {
        "=": 2,
        "||": 5,
        "or": 5,
        "&&": 6,
        "and": 6,
        "==": 10,
        "!=": 10,
        "<": 10,
        ">": 10,
        "<=": 10,
        ">=": 10,
        "+": 20,
        "-": 20,
        "*": 40,
        "/": 40,
    }
    self.indent_level = 0
    self.known_class_names = set()
    self.template_type_scopes = []
    self.value_scopes = [set()]
    self.tokens = tokens

clean

clean() -> None
Source code in src/arx/parser/core.py
77
78
79
80
81
82
83
84
85
def clean(self) -> None:
    """
    title: Reset the Parser static variables.
    """
    self.indent_level = 0
    self.known_class_names = set()
    self.template_type_scopes = []
    self.value_scopes = [set()]
    self.tokens = TokenList([])

get_tok_precedence

get_tok_precedence() -> int
Source code in src/arx/parser/core.py
471
472
473
474
475
476
477
def get_tok_precedence(self) -> int:
    """
    title: Get the precedence of the pending binary operator token.
    returns:
      type: int
    """
    return self.bin_op_precedence.get(self.tokens.cur_tok.value, -1)

parse

parse(
    tokens: TokenList, module_name: str = "main"
) -> Module
Source code in src/arx/parser/core.py
 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
def parse(
    self, tokens: TokenList, module_name: str = "main"
) -> astx.Module:
    """
    title: Parse the input code.
    parameters:
      tokens:
        type: TokenList
      module_name:
        type: str
    returns:
      type: astx.Module
    """
    self.clean()
    self.known_class_names = self._collect_class_names(tokens)
    self.tokens = tokens

    tree: astx.Module = astx.Module(module_name)
    self.tokens.get_next_token()

    if self.tokens.cur_tok.kind == TokenKind.not_initialized:
        self.tokens.get_next_token()

    allow_module_docstring = True
    while True:
        if self.tokens.cur_tok.kind == TokenKind.eof:
            break
        if self.tokens.cur_tok.kind == TokenKind.docstring:
            if (
                allow_module_docstring
                and self.tokens.cur_tok.location.line == 0
                and self.tokens.cur_tok.location.col == 1
            ):
                try:
                    validate_docstring(self.tokens.cur_tok.value)
                except ValueError as err:
                    raise ParserException(
                        f"Invalid module docstring: {err}"
                    ) from err
                self.tokens.get_next_token()
                allow_module_docstring = False
                continue
            raise ParserException(
                "Module docstrings are only allowed as the first "
                "statement starting at line 1, column 1."
            )

        if self._is_operator(";"):
            self.tokens.get_next_token()
            allow_module_docstring = False
            continue

        if self._is_operator("@"):
            prefixes = self.parse_declaration_prefixes()
            if self.tokens.cur_tok.kind == TokenKind.kw_class:
                if prefixes.template_params:
                    raise ParserException(
                        "template parameter blocks are only allowed "
                        "before functions or methods"
                    )
                tree.nodes.append(
                    self.parse_class_decl(prefixes.modifiers)
                )
                allow_module_docstring = False
                continue

            if prefixes.modifiers is not None:
                raise ParserException(
                    "annotation must be followed by a declaration"
                )
            if prefixes.template_params:
                if self.tokens.cur_tok.kind != TokenKind.kw_function:
                    raise ParserException(
                        "template parameter blocks are only allowed "
                        "before functions or methods"
                    )
                tree.nodes.append(
                    self.parse_function(prefixes.template_params)
                )
                allow_module_docstring = False
                continue

            raise ParserException(
                "annotation must be followed by a declaration"
            )

        if self.tokens.cur_tok.kind == TokenKind.kw_import:
            tree.nodes.append(self.parse_import_stmt())
            allow_module_docstring = False
            continue

        if self.tokens.cur_tok.kind == TokenKind.kw_function:
            tree.nodes.append(self.parse_function())
            allow_module_docstring = False
            continue

        if self.tokens.cur_tok.kind == TokenKind.kw_extern:
            tree.nodes.append(self.parse_extern())
            allow_module_docstring = False
            continue

        if self.tokens.cur_tok.kind == TokenKind.kw_class:
            tree.nodes.append(self.parse_class_decl())
            allow_module_docstring = False
            continue

        tree.nodes.append(self.parse_expression())
        allow_module_docstring = False

    return tree

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

parse_declaration_prefixes(
    *, body_indent: int | None = None
) -> ParsedDeclarationPrefixes
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/base.py
233
234
235
236
237
238
239
def parse_expression(self) -> astx.AST:
    """
    title: Parse one expression.
    returns:
      type: astx.AST
    """
    raise NotImplementedError

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_for_stmt

parse_for_stmt() -> AST
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_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_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_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_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