|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Recursive-descent parser that produces an AST in Compiler_Ast_Context.
More...
#include <Compiler_Parser.H>
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 ¬e="", 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. | |
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.
Definition at line 75 of file Compiler_Parser.H.
|
inline |
Constructs a parser for one registered source file.
| ctx | AST context that will own the produced nodes. |
| sm | Source manager owning the file text. |
| file_id | Registered source file id to parse. |
| dx | Optional diagnostic sink shared with the lexer. |
| opts | Parser options for top-level grammar handling. |
Definition at line 647 of file Compiler_Parser.H.
|
inlineprivate |
Internal helper to report a diagnostic error.
Definition at line 105 of file Compiler_Parser.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Diagnostic_Builder::code(), Aleph::code(), diagnostics, Aleph::Diagnostic_Builder::emit(), Aleph::Diagnostic_Engine::error(), Aleph::Diagnostic_Builder::help(), Aleph::message(), and Aleph::Diagnostic_Builder::note().
Referenced by parse_module(), parse_named_type(), parse_primary(), parse_statement(), parse_statement_terminator(), and parse_type_primary().
|
inlineprivate |
Consumes a token of kind kind or emits an error.
Definition at line 155 of file Compiler_Parser.H.
References Aleph::code(), Aleph::Compiler_Parser_Stream::expect(), Aleph::message(), and tokens.
Referenced by parse_block_statement(), parse_call_suffix(), parse_enum_declaration(), parse_function(), parse_function_type(), parse_grouping_expr(), parse_import_declaration(), parse_parenthesized_type(), parse_struct_declaration(), and parse_type_alias_declaration().
|
inlineprivate |
Consumes an identifier token or emits an error.
Definition at line 163 of file Compiler_Parser.H.
References Aleph::code(), Aleph::Compiler_Parser_Stream::expect_identifier(), and tokens.
Referenced by parse_enum_declaration(), parse_function(), parse_let_statement(), parse_struct_declaration(), and parse_type_alias_declaration().
|
inlineprivate |
Creates an invalid expression node in the current arena.
Definition at line 123 of file Compiler_Parser.H.
Referenced by parse_primary().
|
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().
|
inlineprivate |
Creates an invalid statement node in the current arena.
Definition at line 129 of file Compiler_Parser.H.
Referenced by parse_statement().
|
inlineprivate |
Creates an invalid type-declaration node in the current arena.
Definition at line 147 of file Compiler_Parser.H.
|
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().
|
inlineprivate |
Consumes the next token if it matches kind.
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().
|
inlineprivate |
Consumes and returns the next token.
Definition at line 91 of file Compiler_Parser.H.
References Aleph::Compiler_Parser_Stream::next(), and tokens.
Referenced by parse_break_statement(), parse_continue_statement(), parse_enum_declaration(), parse_expression(), parse_function(), parse_grouping_expr(), parse_if_statement(), parse_import_declaration(), parse_let_statement(), parse_named_type(), parse_primary(), parse_return_statement(), parse_statement(), parse_statement_terminator(), parse_struct_declaration(), parse_type_alias_declaration(), parse_type_primary(), parse_unary(), and parse_while_statement().
|
inlineprivate |
Parses a block statement: { statements* }.
Definition at line 531 of file Compiler_Parser.H.
References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::compiler_merge_spans(), expect(), Aleph::LBrace, Aleph::Compiler_Ast_Context::make(), parse_statement(), peek(), Aleph::RBrace, and Aleph::Compiler_SSA_Block::span.
Referenced by parse_function(), and parse_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().
|
inlineprivate |
Parses function call argument list suffix: ( args? ).
Definition at line 260 of file Compiler_Parser.H.
References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Comma, Aleph::compiler_merge_spans(), expect(), Aleph::LParen, Aleph::Compiler_Ast_Context::make(), parse_expression(), Aleph::Compiler_Parser_Stream::parse_separated_list(), Aleph::RParen, Aleph::Compiler_Ast_Node::span, and tokens.
Referenced by parse_postfix().
|
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().
|
inlineprivate |
Parses one top-level unit-variant enum declaration.
Definition at line 503 of file Compiler_Parser.H.
References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Comma, Aleph::compiler_merge_spans(), expect(), expect_identifier(), Aleph::LBrace, Aleph::Compiler_Ast_Context::make(), next(), Aleph::Compiler_Parser_Stream::parse_separated_list(), Aleph::RBrace, and tokens.
Referenced by parse_module().
|
inlineprivate |
Parses binary expressions using precedence-climbing.
Definition at line 306 of file Compiler_Parser.H.
References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::compiler_binary_associativity(), Aleph::compiler_binary_precedence(), Aleph::Compiler_Ast_Context::make(), next(), parse_expression(), parse_unary(), peek(), and Aleph::Right.
Referenced by parse_call_suffix(), parse_expression(), parse_expression_statement(), parse_grouping_expr(), parse_if_statement(), parse_let_statement(), parse_return_statement(), and parse_while_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().
|
inline |
Parses a top-level function declaration.
Syntax: fn ident ( params? ) (-> type)? block.
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().
|
inlineprivate |
Parses a function type such as fn(Int) -> Bool.
Definition at line 382 of file Compiler_Parser.H.
References Aleph::Arrow, ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Comma, Aleph::compiler_merge_spans(), expect(), Aleph::Kw_Fn, Aleph::LParen, Aleph::Compiler_Ast_Context::make(), Aleph::Compiler_Parser_Stream::parse_separated_list(), parse_type_expression(), Aleph::RParen, and tokens.
Referenced by parse_type_primary().
|
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().
|
inlineprivate |
Parses an if statement: if expr stmt (else stmt)?.
Definition at line 602 of file Compiler_Parser.H.
References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Kw_Else, Aleph::Compiler_Ast_Context::make(), next(), parse_expression(), parse_statement(), peek(), and Aleph::Compiler_Token::span.
Referenced by parse_statement().
|
inlineprivate |
Parses one top-level import declaration: import string ;.
Definition at line 439 of file Compiler_Parser.H.
References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::compiler_merge_spans(), Aleph::compiler_unquote_string_token(), expect(), invalid_import_decl(), Aleph::Compiler_Ast_Context::make(), next(), parse_statement_terminator(), peek(), and Aleph::String_Literal.
Referenced by parse_module().
|
inlineprivate |
Parses a variable declaration: let ident (: type)? (= expr)? ;.
Definition at line 551 of file Compiler_Parser.H.
References Aleph::Assign, ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Colon, expect_identifier(), Aleph::init, Aleph::Compiler_Ast_Context::make(), match(), next(), parse_expression(), parse_statement_terminator(), parse_type_expression(), peek(), and Aleph::Compiler_Token::span.
Referenced by parse_statement().
|
inline |
Parses a full module until EOF.
Processes top-level imports, type declarations, functions, and optional top-level statements depending on options.
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().
|
inlineprivate |
Parses a named type such as Int or T.
Definition at line 329 of file Compiler_Parser.H.
References ast, Aleph::blossom_maximum_cardinality_matching(), emit_error(), Aleph::Identifier, invalid_type_expr(), Aleph::Compiler_Ast_Context::make(), next(), and peek().
Referenced by parse_type_primary().
|
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().
|
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().
|
inlineprivate |
Parses primary expressions: literals, identifiers, and groupings.
Definition at line 202 of file Compiler_Parser.H.
References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Bool_Literal, Aleph::Char_Literal, emit_error(), Aleph::Identifier, Aleph::Integer_Literal, invalid_expr(), Aleph::Kw_False, Aleph::Kw_True, Aleph::LParen, Aleph::Compiler_Ast_Context::make(), next(), parse_grouping_expr(), peek(), and Aleph::String_Literal.
Referenced by parse_postfix().
|
inlineprivate |
Parses a return statement: return expr? ;.
Definition at line 573 of file Compiler_Parser.H.
References Aleph::and, ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Compiler_Ast_Context::make(), next(), parse_expression(), parse_statement_terminator(), peek(), Aleph::RBrace, Aleph::Semicolon, and value.
Referenced by parse_statement().
|
inline |
Parses a single statement.
Dispatches to specific statement parsers based on the next token.
Compiler_Invalid_Stmt. Definition at line 793 of file Compiler_Parser.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::compiler_is_expression_start(), emit_error(), invalid_stmt(), Aleph::Kw_Break, Aleph::Kw_Continue, Aleph::Kw_If, Aleph::Kw_Let, Aleph::Kw_Return, Aleph::Kw_While, Aleph::LBrace, next(), parse_block_statement(), parse_break_statement(), parse_continue_statement(), parse_expression_statement(), parse_if_statement(), parse_let_statement(), parse_return_statement(), parse_while_statement(), peek(), and synchronize_statement().
Referenced by parse_block_statement(), parse_if_statement(), parse_module(), and parse_while_statement().
|
inlineprivate |
Ensures a statement is terminated by a semicolon or reports an error.
Definition at line 175 of file Compiler_Parser.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::compiler_is_statement_start(), emit_error(), next(), peek(), Aleph::RBrace, Aleph::Semicolon, Aleph::Compiler_Token::span, and synchronize_statement().
Referenced by parse_break_statement(), parse_continue_statement(), parse_expression_statement(), parse_import_declaration(), parse_let_statement(), parse_return_statement(), and parse_type_alias_declaration().
|
inlineprivate |
Parses one top-level struct declaration.
Definition at line 469 of file Compiler_Parser.H.
References ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::Colon, Aleph::compiler_merge_spans(), expect(), expect_identifier(), Aleph::LBrace, Aleph::Compiler_Ast_Context::make(), next(), parse_type_expression(), peek(), Aleph::RBrace, and Aleph::Semicolon.
Referenced by parse_module().
|
inlineprivate |
Parses one top-level type alias: type ident = type ;.
Definition at line 457 of file Compiler_Parser.H.
References Aleph::Assign, ast, Aleph::blossom_maximum_cardinality_matching(), Aleph::equal(), expect(), expect_identifier(), Aleph::Compiler_Ast_Context::make(), next(), parse_statement_terminator(), and parse_type_expression().
Referenced by parse_module().
|
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().
|
inlineprivate |
Parses one primary type-expression form.
Definition at line 414 of file Compiler_Parser.H.
References Aleph::blossom_maximum_cardinality_matching(), emit_error(), Aleph::Identifier, invalid_type_expr(), Aleph::Compiler_Token::kind, Aleph::Kw_Fn, Aleph::LParen, next(), parse_function_type(), parse_named_type(), parse_parenthesized_type(), and peek().
Referenced by parse_type_expression().
|
inlineprivate |
Parses prefix unary expressions: !, -, +, ~.
Definition at line 291 of file Compiler_Parser.H.
References ast, Aleph::Bang, Aleph::blossom_maximum_cardinality_matching(), Aleph::Compiler_Ast_Context::make(), Aleph::Minus, next(), parse_postfix(), parse_unary(), peek(), Aleph::Plus, and Aleph::Tilde.
Referenced by parse_expression(), and parse_unary().
|
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().
|
inlineprivate |
Peeks at the next token without consuming it.
Definition at line 85 of file Compiler_Parser.H.
References Aleph::Compiler_Parser_Stream::peek(), and tokens.
Referenced by parse_block_statement(), parse_expression(), parse_function(), parse_if_statement(), parse_import_declaration(), parse_let_statement(), parse_module(), parse_named_type(), parse_parenthesized_type(), parse_postfix(), parse_primary(), parse_return_statement(), parse_statement(), parse_statement_terminator(), parse_struct_declaration(), parse_type_primary(), and parse_unary().
|
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().
|
private |
AST owner.
Definition at line 77 of file Compiler_Parser.H.
Referenced by parse_block_statement(), parse_break_statement(), parse_call_suffix(), parse_continue_statement(), parse_enum_declaration(), parse_expression(), parse_expression_statement(), parse_function(), parse_function_type(), parse_grouping_expr(), parse_if_statement(), parse_import_declaration(), parse_let_statement(), parse_named_type(), parse_parenthesized_type(), parse_primary(), parse_return_statement(), parse_struct_declaration(), parse_type_alias_declaration(), parse_unary(), and parse_while_statement().
|
private |
|
private |
Token source.
Definition at line 79 of file Compiler_Parser.H.
|
private |
Parser behavior configuration.
Definition at line 81 of file Compiler_Parser.H.
Referenced by parse_module().
|
private |
User-facing source name being parsed.
Definition at line 82 of file Compiler_Parser.H.
|
private |
Reusable parser-oriented token facade.
Definition at line 80 of file Compiler_Parser.H.
Referenced by expect(), expect_identifier(), match(), next(), parse_call_suffix(), parse_enum_declaration(), parse_function(), parse_function_type(), parse_parenthesized_type(), peek(), and synchronize_statement().