Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Compiler_Parser Class Reference

Recursive-descent parser that produces an AST in Compiler_Ast_Context. More...

#include <Compiler_Parser.H>

Collaboration diagram for Aleph::Compiler_Parser:
[legend]

Public Member Functions

 Compiler_Parser (Compiler_Ast_Context &ctx, const Source_Manager &sm, const Source_File_Id file_id, Diagnostic_Engine *dx=nullptr, const Compiler_Parser_Options &opts={})
 Constructs a parser for one registered source file.
 
Compiler_Module * parse_module ()
 Parses a full module until EOF.
 
Compiler_Function_Decl * parse_function ()
 Parses a top-level function declaration.
 
Compiler_Stmt * parse_statement ()
 Parses a single statement.
 

Private Member Functions

Compiler_Token peek ()
 Peeks at the next token without consuming it.
 
Compiler_Token next ()
 Consumes and returns the next token.
 
bool match (const Compiler_Token_Kind kind)
 Consumes the next token if it matches kind.
 
void emit_error (const Source_Span &span, const std::string &message, const std::string &code, const std::string &note="", const std::string &help="") const
 Internal helper to report a diagnostic error.
 
Compiler_Invalid_Expr * invalid_expr (const Source_Span &span={}) const
 Creates an invalid expression node in the current arena.
 
Compiler_Invalid_Stmt * invalid_stmt (const Source_Span &span={}) const
 Creates an invalid statement node in the current arena.
 
Compiler_Invalid_Type_Expr * invalid_type_expr (const Source_Span &span={}) const
 Creates an invalid type-expression node in the current arena.
 
Compiler_Invalid_Import_Decl * invalid_import_decl (const Source_Span &span={}) const
 Creates an invalid import-declaration node in the current arena.
 
Compiler_Invalid_Type_Decl * invalid_type_decl (const Source_Span &span={}) const
 Creates an invalid type-declaration node in the current arena.
 
Compiler_Token expect (const Compiler_Token_Kind kind, const std::string &message, const std::string &code)
 Consumes a token of kind kind or emits an error.
 
Compiler_Token expect_identifier (const std::string &context, const std::string &code="PAR002")
 Consumes an identifier token or emits an error.
 
void synchronize_statement ()
 Error recovery helper: skips tokens until a statement boundary.
 
Source_Span parse_statement_terminator (const Source_Span &anchor)
 Ensures a statement is terminated by a semicolon or reports an error.
 
Compiler_Grouping_Expr * parse_grouping_expr ()
 Parses a parenthesized expression: ( expression ).
 
Compiler_Expr * parse_primary ()
 Parses primary expressions: literals, identifiers, and groupings.
 
Compiler_Expr * parse_call_suffix (Compiler_Expr *callee)
 Parses function call argument list suffix: ( args? ).
 
Compiler_Expr * parse_postfix ()
 Parses postfix expressions such as calls.
 
Compiler_Expr * parse_unary ()
 Parses prefix unary expressions: !, -, +, ~.
 
Compiler_Expr * parse_expression (const int min_precedence=1)
 Parses binary expressions using precedence-climbing.
 
Compiler_Type_Expr * parse_named_type ()
 Parses a named type such as Int or T.
 
Compiler_Type_Expr * parse_parenthesized_type ()
 Parses parenthesized tuple-type syntax.
 
Compiler_Type_Expr * parse_function_type ()
 Parses a function type such as fn(Int) -> Bool.
 
Compiler_Type_Expr * parse_type_primary ()
 Parses one primary type-expression form.
 
Compiler_Type_Expr * parse_type_expression ()
 Parses a type expression used by explicit annotations.
 
Compiler_Import_Decl * parse_import_declaration ()
 Parses one top-level import declaration: import string ;.
 
Compiler_Type_Decl * parse_type_alias_declaration ()
 Parses one top-level type alias: type ident = type ;.
 
Compiler_Type_Decl * parse_struct_declaration ()
 Parses one top-level struct declaration.
 
Compiler_Type_Decl * parse_enum_declaration ()
 Parses one top-level unit-variant enum declaration.
 
Compiler_Block_Stmt * parse_block_statement ()
 Parses a block statement: { statements* }.
 
Compiler_Stmt * parse_let_statement ()
 Parses a variable declaration: let ident (: type)? (= expr)? ;.
 
Compiler_Stmt * parse_return_statement ()
 Parses a return statement: return expr? ;.
 
Compiler_Stmt * parse_break_statement ()
 Parses a break statement: break;.
 
Compiler_Stmt * parse_continue_statement ()
 Parses a continue statement: continue;.
 
Compiler_Stmt * parse_if_statement ()
 Parses an if statement: if expr stmt (else stmt)?.
 
Compiler_Stmt * parse_while_statement ()
 Parses a while loop: while expr stmt.
 
Compiler_Stmt * parse_expression_statement ()
 Parses an expression statement: expr ;.
 

Private Attributes

Compiler_Ast_Context * ast = nullptr
 AST owner.
 
Diagnostic_Engine * diagnostics = nullptr
 diagnostic sink.
 
Compiler_Lexer lexer
 Token source.
 
Compiler_Parser_Stream tokens
 Reusable parser-oriented token facade.
 
Compiler_Parser_Options options
 Parser behavior configuration.
 
std::string source_name
 User-facing source name being parsed.
 

Detailed Description

Recursive-descent parser that produces an AST in Compiler_Ast_Context.

Implements a hand-written parser for a C-like language. It uses precedence-climbing for expressions and recursive descent for statements and declarations, including top-level import, fn, type, struct, and enum.

Note
Thread-safety: Not thread-safe.

Definition at line 75 of file Compiler_Parser.H.

Constructor & Destructor Documentation

◆ Compiler_Parser()

Aleph::Compiler_Parser::Compiler_Parser ( Compiler_Ast_Context &  ctx,
const Source_Manager &  sm,
const Source_File_Id  file_id,
Diagnostic_Engine *  dx = nullptr,
const Compiler_Parser_Options &  opts = {} 
)
inline

Constructs a parser for one registered source file.

Parameters
ctxAST context that will own the produced nodes.
smSource manager owning the file text.
file_idRegistered source file id to parse.
dxOptional diagnostic sink shared with the lexer.
optsParser options for top-level grammar handling.
Note
Thread-safety: Not thread-safe.
Complexity: O(1).

Definition at line 647 of file Compiler_Parser.H.

Member Function Documentation

◆ emit_error()

void Aleph::Compiler_Parser::emit_error ( const Source_Span &  span,
const std::string &  message,
const std::string &  code,
const std::string &  note = "",
const std::string &  help = "" 
) const
inlineprivate

◆ expect()

Compiler_Token Aleph::Compiler_Parser::expect ( const Compiler_Token_Kind  kind,
const std::string &  message,
const std::string &  code 
)
inlineprivate

◆ expect_identifier()

Compiler_Token Aleph::Compiler_Parser::expect_identifier ( const std::string &  context,
const std::string &  code = "PAR002" 
)
inlineprivate

◆ invalid_expr()

Compiler_Invalid_Expr * Aleph::Compiler_Parser::invalid_expr ( const Source_Span &  span = {}) const
inlineprivate

Creates an invalid expression node in the current arena.

Definition at line 123 of file Compiler_Parser.H.

Referenced by parse_primary().

◆ invalid_import_decl()

Compiler_Invalid_Import_Decl * Aleph::Compiler_Parser::invalid_import_decl ( const Source_Span &  span = {}) const
inlineprivate

Creates an invalid import-declaration node in the current arena.

Definition at line 141 of file Compiler_Parser.H.

Referenced by parse_import_declaration().

◆ invalid_stmt()

Compiler_Invalid_Stmt * Aleph::Compiler_Parser::invalid_stmt ( const Source_Span &  span = {}) const
inlineprivate

Creates an invalid statement node in the current arena.

Definition at line 129 of file Compiler_Parser.H.

Referenced by parse_statement().

◆ invalid_type_decl()

Compiler_Invalid_Type_Decl * Aleph::Compiler_Parser::invalid_type_decl ( const Source_Span &  span = {}) const
inlineprivate

Creates an invalid type-declaration node in the current arena.

Definition at line 147 of file Compiler_Parser.H.

◆ invalid_type_expr()

Compiler_Invalid_Type_Expr * Aleph::Compiler_Parser::invalid_type_expr ( const Source_Span &  span = {}) const
inlineprivate

Creates an invalid type-expression node in the current arena.

Definition at line 135 of file Compiler_Parser.H.

Referenced by parse_named_type(), and parse_type_primary().

◆ match()

bool Aleph::Compiler_Parser::match ( const Compiler_Token_Kind  kind)
inlineprivate

Consumes the next token if it matches kind.

Returns
true if matched and consumed.

Definition at line 99 of file Compiler_Parser.H.

References Aleph::Compiler_Parser_Stream::match(), and tokens.

Referenced by parse_let_statement().

◆ next()

◆ parse_block_statement()

Compiler_Block_Stmt * Aleph::Compiler_Parser::parse_block_statement ( )
inlineprivate

◆ parse_break_statement()

Compiler_Stmt * Aleph::Compiler_Parser::parse_break_statement ( )
inlineprivate

Parses a break statement: break;.

Definition at line 586 of file Compiler_Parser.H.

References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Compiler_Ast_Context::make(), next(), and parse_statement_terminator().

Referenced by parse_statement().

◆ parse_call_suffix()

◆ parse_continue_statement()

Compiler_Stmt * Aleph::Compiler_Parser::parse_continue_statement ( )
inlineprivate

Parses a continue statement: continue;.

Definition at line 594 of file Compiler_Parser.H.

References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Compiler_Ast_Context::make(), next(), and parse_statement_terminator().

Referenced by parse_statement().

◆ parse_enum_declaration()

Compiler_Type_Decl * Aleph::Compiler_Parser::parse_enum_declaration ( )
inlineprivate

◆ parse_expression()

◆ parse_expression_statement()

Compiler_Stmt * Aleph::Compiler_Parser::parse_expression_statement ( )
inlineprivate

Parses an expression statement: expr ;.

Definition at line 629 of file Compiler_Parser.H.

References ast, Aleph::Compiler_Ast_Context::make(), parse_expression(), and parse_statement_terminator().

Referenced by parse_statement().

◆ parse_function()

Compiler_Function_Decl * Aleph::Compiler_Parser::parse_function ( )
inline

Parses a top-level function declaration.

Syntax: fn ident ( params? ) (-> type)? block.

Returns
Function declaration node. Recovery still returns a node on error.
Note
Thread-safety: Not thread-safe.
Complexity: O(N) where N is source length.

Definition at line 745 of file Compiler_Parser.H.

References Aleph::Arrow, ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Colon, Aleph::Comma, Aleph::compiler_merge_spans(), expect(), expect_identifier(), Aleph::Compiler_Token::kind, Aleph::Kw_Fn, Aleph::LParen, Aleph::Compiler_Ast_Context::make(), next(), parse_block_statement(), Aleph::Compiler_Parser_Stream::parse_separated_list(), parse_type_expression(), peek(), Aleph::RParen, Aleph::Compiler_Token::span, and tokens.

Referenced by parse_module().

◆ parse_function_type()

◆ parse_grouping_expr()

Compiler_Grouping_Expr * Aleph::Compiler_Parser::parse_grouping_expr ( )
inlineprivate

Parses a parenthesized expression: ( expression ).

Definition at line 192 of file Compiler_Parser.H.

References ast, Aleph::blossom_maximum_cardinality_matching(), expect(), Aleph::Compiler_Ast_Context::make(), next(), parse_expression(), and Aleph::RParen.

Referenced by parse_primary().

◆ parse_if_statement()

Compiler_Stmt * Aleph::Compiler_Parser::parse_if_statement ( )
inlineprivate

◆ parse_import_declaration()

Compiler_Import_Decl * Aleph::Compiler_Parser::parse_import_declaration ( )
inlineprivate

◆ parse_let_statement()

Compiler_Stmt * Aleph::Compiler_Parser::parse_let_statement ( )
inlineprivate

◆ parse_module()

Compiler_Module * Aleph::Compiler_Parser::parse_module ( )
inline

Parses a full module until EOF.

Processes top-level imports, type declarations, functions, and optional top-level statements depending on options.

Returns
Arena-backed module node containing imports, type declarations, functions, and statements.
Note
Thread-safety: Not thread-safe.
Complexity: O(N) where N is source length.

Definition at line 666 of file Compiler_Parser.H.

References Aleph::Compiler_Parser_Options::allow_top_level_statements, Aleph::blossom_maximum_cardinality_matching(), emit_error(), Aleph::Kw_Enum, Aleph::Kw_Fn, Aleph::Kw_Import, Aleph::Kw_Struct, Aleph::Kw_Type, options, parse_enum_declaration(), parse_function(), parse_import_declaration(), parse_statement(), parse_struct_declaration(), parse_type_alias_declaration(), and peek().

◆ parse_named_type()

Compiler_Type_Expr * Aleph::Compiler_Parser::parse_named_type ( )
inlineprivate

◆ parse_parenthesized_type()

Compiler_Type_Expr * Aleph::Compiler_Parser::parse_parenthesized_type ( )
inlineprivate

Parses parenthesized tuple-type syntax.

A single member such as (Int) is normalized to Int, while two or more members produce one tuple type-expression node.

Definition at line 349 of file Compiler_Parser.H.

References Aleph::and, ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Comma, Aleph::compiler_merge_spans(), expect(), Aleph::LParen, Aleph::Compiler_Ast_Context::make(), Aleph::Compiler_Tuple_Type_Expr::members, Aleph::Compiler_Parser_Stream::parse_separated_list(), parse_type_expression(), peek(), Aleph::RParen, and tokens.

Referenced by parse_type_primary().

◆ parse_postfix()

Compiler_Expr * Aleph::Compiler_Parser::parse_postfix ( )
inlineprivate

Parses postfix expressions such as calls.

Definition at line 282 of file Compiler_Parser.H.

References Aleph::LParen, parse_call_suffix(), parse_primary(), and peek().

Referenced by parse_unary().

◆ parse_primary()

◆ parse_return_statement()

Compiler_Stmt * Aleph::Compiler_Parser::parse_return_statement ( )
inlineprivate

◆ parse_statement()

Compiler_Stmt * Aleph::Compiler_Parser::parse_statement ( )
inline

◆ parse_statement_terminator()

◆ parse_struct_declaration()

Compiler_Type_Decl * Aleph::Compiler_Parser::parse_struct_declaration ( )
inlineprivate

◆ parse_type_alias_declaration()

Compiler_Type_Decl * Aleph::Compiler_Parser::parse_type_alias_declaration ( )
inlineprivate

◆ parse_type_expression()

Compiler_Type_Expr * Aleph::Compiler_Parser::parse_type_expression ( )
inlineprivate

Parses a type expression used by explicit annotations.

Definition at line 433 of file Compiler_Parser.H.

References parse_type_primary().

Referenced by parse_function(), parse_function_type(), parse_let_statement(), parse_parenthesized_type(), parse_struct_declaration(), and parse_type_alias_declaration().

◆ parse_type_primary()

Compiler_Type_Expr * Aleph::Compiler_Parser::parse_type_primary ( )
inlineprivate

◆ parse_unary()

Compiler_Expr * Aleph::Compiler_Parser::parse_unary ( )
inlineprivate

◆ parse_while_statement()

Compiler_Stmt * Aleph::Compiler_Parser::parse_while_statement ( )
inlineprivate

Parses a while loop: while expr stmt.

Definition at line 620 of file Compiler_Parser.H.

References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Compiler_Ast_Context::make(), next(), parse_expression(), and parse_statement().

Referenced by parse_statement().

◆ peek()

◆ synchronize_statement()

void Aleph::Compiler_Parser::synchronize_statement ( )
inlineprivate

Error recovery helper: skips tokens until a statement boundary.

Definition at line 169 of file Compiler_Parser.H.

References Aleph::Compiler_Parser_Stream::synchronize_statement(), and tokens.

Referenced by parse_statement(), and parse_statement_terminator().

Member Data Documentation

◆ ast

◆ diagnostics

Diagnostic_Engine* Aleph::Compiler_Parser::diagnostics = nullptr
private

diagnostic sink.

Definition at line 78 of file Compiler_Parser.H.

Referenced by emit_error().

◆ lexer

Compiler_Lexer Aleph::Compiler_Parser::lexer
private

Token source.

Definition at line 79 of file Compiler_Parser.H.

◆ options

Compiler_Parser_Options Aleph::Compiler_Parser::options
private

Parser behavior configuration.

Definition at line 81 of file Compiler_Parser.H.

Referenced by parse_module().

◆ source_name

std::string Aleph::Compiler_Parser::source_name
private

User-facing source name being parsed.

Definition at line 82 of file Compiler_Parser.H.

◆ tokens

Compiler_Parser_Stream Aleph::Compiler_Parser::tokens
private

The documentation for this class was generated from the following file: