l0_parser.py

l0_parser.py

Module: l0_parser

Source: compiler/stage1_py/l0_parser.py Language: Python

Symbols

Namespace l0_parser

Function l0_parser.token_len

1
int l0_parser.token_len(Token tok)

Calculate the display length of a token.

Parameters:

  • tok: The token to measure.

Returns: The length of the token’s text, accounting for literal quotes.

Class l0_parser::_ParseSyncException

Internal exception used for error recovery during parsing.

Class l0_parser::Parser

Recursive descent parser for L0.

Converts a flat list of tokens into a hierarchical AST. Implements error recovery via synchronization points.

Member Data l0_parser.Parser._RESERVED_BINARY_OPS

1
dict _RESERVED_BINARY_OPS

Member Data l0_parser.Parser.tokens

1
l0_parser.Parser::tokens

Member Data l0_parser.Parser.index

1
l0_parser.Parser::index

Member Data l0_parser.Parser.filename

1
l0_parser.Parser::filename

Member Data l0_parser.Parser.diagnostics

1
l0_parser.Parser::diagnostics

Member Data l0_parser.Parser.eof_aborted

1
l0_parser.Parser::eof_aborted

Member Data l0_parser.Parser._last_token

1
l0_parser.Parser::_last_token

Function l0_parser.Parser.__init__

1
None l0_parser.Parser.__init__(self, List[Token] tokens, Optional[str] filename=None, Optional[List[Diagnostic]] diagnostics=None)

Initialize the parser.

Parameters:

  • tokens: The tokens to parse.
  • filename: Optional source filename.
  • diagnostics: Optional list to collect diagnostics into.

    Function l0_parser.Parser.from_source

1
"Parser" l0_parser.Parser.from_source(cls, str source)

Create a parser from a source string.

Parameters:

  • source: The source code text.

Returns: A new Parser instance populated with tokens.

Function l0_parser.Parser.parse_module

1
Module l0_parser.Parser.parse_module(self, Optional[str] filename=None)

Parse a complete L0 module.

Parameters:

  • filename: Optional source filename for diagnostics.

Returns: A Module AST node.

Function l0_parser.Parser._error

1
None l0_parser.Parser._error(self, str message, Optional[Token] token=None)

Add an error diagnostic.

Parameters:

  • message: The error message.
  • token: Optional token where the error occurred. Defaults to peek().

    Function l0_parser.Parser._error_bail

1
NoReturn l0_parser.Parser._error_bail(self, str message, Optional[Token] token=None)

Report an error and raise a synchronization exception.

Parameters:

  • message: The error message.
  • token: Optional token where the error occurred.

    Function l0_parser.Parser._error_unexpected

1
NoReturn l0_parser.Parser._error_unexpected(self, str error_code, Token tok, str context)

Report an unexpected token error and raise a synchronization exception.

Parameters:

  • error_code: The diagnostic code (e.g., “[PAR-nnnn]”).
  • tok: The unexpected token.
  • context: Description of what was being parsed.

    Function l0_parser.Parser._peek

1
Token l0_parser.Parser._peek(self)

Return the current token without advancing.

Lexer-error wrappers are interpreted as logical tokens. A wrapper with recovery is exposed as that recovered token; a wrapper without recovery is skipped. Deferred diagnostics are emitted once per physical token.

Function l0_parser.Parser._emit_lexer_error

1
None l0_parser.Parser._emit_lexer_error(self, int index, Token tok)

Emit deferred lexer diagnostics for a physical token once.

Function l0_parser.Parser._logical_token_at_cursor

1
Token l0_parser.Parser._logical_token_at_cursor(self)

Return the logical current token, skipping unrecoverable wrappers.

Function l0_parser.Parser._skip_lexer_errors

1
None l0_parser.Parser._skip_lexer_errors(self)

Emit and step over unrecoverable LEXER_ERROR tokens at the cursor.

Function l0_parser.Parser._emit_remaining_lexer_errors

1
None l0_parser.Parser._emit_remaining_lexer_errors(self)

Emit deferred lexer diagnostics that parsing never reached.

Function l0_parser.Parser._last

1
Token l0_parser.Parser._last(self)

Return the previously advanced token.

Function l0_parser.Parser._at_end

1
bool l0_parser.Parser._at_end(self)

Check if at the end of the token stream.

Function l0_parser.Parser._advance

1
Token l0_parser.Parser._advance(self)

Advance the current index and return the token.

Function l0_parser.Parser._check

1
bool l0_parser.Parser._check(self, TokenKind kind)

Check if the current token is of the specified kind.

Function l0_parser.Parser._match

1
bool l0_parser.Parser._match(self, *TokenKind kinds)

Advance and return True if the current token matches any of the kinds.

Function l0_parser.Parser._expect

1
Token l0_parser.Parser._expect(self, TokenKind kind, str msg)

Advance if the current token matches kind, otherwise bail.

Parameters:

  • kind: The expected TokenKind.
  • msg: The error message if expectation fails.

Returns: The matched token.

Function l0_parser.Parser._expect_semicolon

1
None l0_parser.Parser._expect_semicolon(self, Optional[str] msg=None)

Expect and consume a semicolon, reporting an error if missing.

Function l0_parser.Parser._expect_variable_name

1
Token l0_parser.Parser._expect_variable_name(self, str msg)

Expect and consume a valid variable name (non-reserved identifier).

Function l0_parser.Parser._span_start

1
Span l0_parser.Parser._span_start(self)

Create a zero-length span starting at the current token.

Function l0_parser.Parser._extend_span

1
Span l0_parser.Parser._extend_span(self, Span start)

Create a new span extending from start to the end of the last token.

Function l0_parser.Parser._get_dotted_module_name

1
list[str] l0_parser.Parser._get_dotted_module_name(self, Token first)

Parse a dotted module name (e.g., a.b.c).

Function l0_parser.Parser._try_parse_qualified_name

1
Optional[tuple[list[str], Optional[list[str]], Token]] l0_parser.Parser._try_parse_qualified_name(self)

Try to parse a qualified name (e.g., mod::name).

Returns: A tuple (module_path, qualifier, name_token) if successful, else None.

Function l0_parser.Parser._sync_top_level

1
None l0_parser.Parser._sync_top_level(self)

Skip tokens until we find the start of a new top-level declaration or EOF.

Function l0_parser.Parser._parse_top_level_decl

1
TopLevelDecl l0_parser.Parser._parse_top_level_decl(self)

Parse a single top-level declaration.

Function l0_parser.Parser._parse_extern_func

1
FuncDecl l0_parser.Parser._parse_extern_func(self)

Parse an ‘extern’ function declaration.

Function l0_parser.Parser._parse_function

1
FuncDecl l0_parser.Parser._parse_function(self, bool is_extern)

Parse a function declaration or definition.

Function l0_parser.Parser._parse_struct

1
StructDecl l0_parser.Parser._parse_struct(self)

Parse a struct definition.

Function l0_parser.Parser._parse_enum

1
EnumDecl l0_parser.Parser._parse_enum(self)

Parse an enum definition.

Function l0_parser.Parser._parse_type_alias

1
TypeAliasDecl l0_parser.Parser._parse_type_alias(self)

Parse a type alias declaration.

Function l0_parser.Parser._parse_top_level_let

1
LetDecl l0_parser.Parser._parse_top_level_let(self)

Parse a top-level ‘let’ binding.

Function l0_parser.Parser._parse_type

1
TypeRef l0_parser.Parser._parse_type(self)

Parse a type reference including pointer and nullability suffixes.

Function l0_parser.Parser._parse_block

1
Block l0_parser.Parser._parse_block(self)

Parse a block of statements enclosed in braces.

Function l0_parser.Parser._sync_stmt

1
None l0_parser.Parser._sync_stmt(self)

Skip tokens until we reach a statement boundary or end of block.

Function l0_parser.Parser._parse_stmt

1
Stmt l0_parser.Parser._parse_stmt(self)

Parse a single statement.

Function l0_parser.Parser._parse_simple_stmt

1
Stmt l0_parser.Parser._parse_simple_stmt(self)

Parse a simple statement (let, break, return, assign, or expr).

Function l0_parser.Parser._parse_let_stmt

1
LetStmt l0_parser.Parser._parse_let_stmt(self)

Parse a local ‘let’ statement.

Function l0_parser.Parser._parse_if_stmt

1
IfStmt l0_parser.Parser._parse_if_stmt(self)

Parse an ‘if’ statement.

Function l0_parser.Parser._parse_while_stmt

1
WhileStmt l0_parser.Parser._parse_while_stmt(self)

Parse a ‘while’ loop statement.

Function l0_parser.Parser._parse_for_stmt

1
Stmt l0_parser.Parser._parse_for_stmt(self)

Parse a ‘for’ loop statement.

Function l0_parser.Parser._parse_for_update_stmt

1
Stmt l0_parser.Parser._parse_for_update_stmt(self)

Parse a statement permitted in a for-loop update clause.

Function l0_parser.Parser._parse_return_stmt

1
ReturnStmt l0_parser.Parser._parse_return_stmt(self)

Parse a ‘return’ statement.

Function l0_parser.Parser._parse_drop_stmt

1
DropStmt l0_parser.Parser._parse_drop_stmt(self)

Parse a ‘drop’ statement.

Function l0_parser.Parser._parse_match_stmt

1
MatchStmt l0_parser.Parser._parse_match_stmt(self)

Parse a ‘match’ statement.

Function l0_parser.Parser._parse_with_item

1
WithItem l0_parser.Parser._parse_with_item(self)

Parse a single item in a ‘with’ statement.

Function l0_parser.Parser._parse_with_stmt

1
WithStmt l0_parser.Parser._parse_with_stmt(self)

Parse a ‘with’ statement for resource management.

Function l0_parser.Parser._parse_case_stmt

1
CaseStmt l0_parser.Parser._parse_case_stmt(self)

Parse a ‘case’ statement (multi-way constant branch).

Function l0_parser.Parser._at_case_arm_start

1
bool l0_parser.Parser._at_case_arm_start(self)

Return True if the current token is a likely case-arm recovery boundary.

Function l0_parser.Parser._sync_case_invalid_arm

1
None l0_parser.Parser._sync_case_invalid_arm(self)

Skip a malformed or forbidden case arm without leaving the case.

Function l0_parser.Parser._parse_case_literal

1
Expr l0_parser.Parser._parse_case_literal(self)

Parse a literal value for a ‘case’ arm.

Function l0_parser.Parser._parse_pattern

1
Pattern l0_parser.Parser._parse_pattern(self)

Parse a pattern for ‘match’ arms.

Function l0_parser.Parser._parse_break_stmt

1
Stmt l0_parser.Parser._parse_break_stmt(self)

Parse a ‘break’ statement.

Function l0_parser.Parser._parse_continue_stmt

1
Stmt l0_parser.Parser._parse_continue_stmt(self)

Parse a ‘continue’ statement.

Function l0_parser.Parser._parse_expr

1
Expr l0_parser.Parser._parse_expr(self)

Parse an expression (entry point for expression parsing).

Function l0_parser.Parser._check_reserved_binary_op

1
None l0_parser.Parser._check_reserved_binary_op(self)

Raise a diagnostic if the next token is a reserved binary operator.

Function l0_parser.Parser._parse_or_expr

1
Expr l0_parser.Parser._parse_or_expr(self)

Parse a logical OR expression.

Function l0_parser.Parser._parse_and_expr

1
Expr l0_parser.Parser._parse_and_expr(self)

Parse a logical AND expression.

Function l0_parser.Parser._parse_equality_expr

1
Expr l0_parser.Parser._parse_equality_expr(self)

Parse an equality expression.

Function l0_parser.Parser._parse_rel_expr

1
Expr l0_parser.Parser._parse_rel_expr(self)

Parse a relational expression.

Function l0_parser.Parser._parse_add_expr

1
Expr l0_parser.Parser._parse_add_expr(self)

Parse an addition/subtraction expression.

Function l0_parser.Parser._parse_mul_expr

1
Expr l0_parser.Parser._parse_mul_expr(self)

Parse a multiplication/division/modulo expression.

Function l0_parser.Parser._parse_unary_expr

1
Expr l0_parser.Parser._parse_unary_expr(self)

Parse a unary expression.

Function l0_parser.Parser._parse_cast_expr

1
Expr l0_parser.Parser._parse_cast_expr(self)

Parse a type cast expression.

Function l0_parser.Parser._parse_postfix_expr

1
Expr l0_parser.Parser._parse_postfix_expr(self)

Parse a postfix expression (calls, indexing, field access, try).

Function l0_parser.Parser._parse_call_argument

1
Expr l0_parser.Parser._parse_call_argument(self)

Parse a function call argument, attempting to parse as TypeExpr first.

Function l0_parser.Parser._is_builtin_type_name

1
bool l0_parser.Parser._is_builtin_type_name(self)

Check if current token is a builtin type keyword.

Function l0_parser.Parser._lookahead_is_type_suffix

1
bool l0_parser.Parser._lookahead_is_type_suffix(self)

Check if there’s a ‘*’ or ‘?’ immediately after the current token.

Function l0_parser.Parser._is_unambiguous_type_start

1
bool l0_parser.Parser._is_unambiguous_type_start(self)

Check if the current position unambiguously starts a type reference.

Function l0_parser.Parser._parse_primary_expr

1
Expr l0_parser.Parser._parse_primary_expr(self)

Parse a primary expression (literals, identifiers, parenthesized expressions).