46#ifndef COMPILER_PARSER_H
47#define COMPILER_PARSER_H
107 const std::string &
code,
108 const std::string ¬e =
"",
109 const std::string &help =
"")
const
115 if (
not note.empty())
117 if (
not help.empty())
157 const std::string &
code)
209 const auto name =
next();
276 call->rparen_span =
rparen.span;
297 const auto op =
next();
312 const auto op =
peek();
340 const auto name =
next();
357 if (first !=
nullptr and rparen.span.is_valid())
444 "expected string literal after 'import'",
490 "expected ';' after struct field declaration",
492 decl->fields.append({field.lexeme, field.span,
colon.span, annotation, semi.span});
518 decl->variants.append({variant.lexeme, variant.span});
668 auto *
module = ast->make<Compiler_Module>();
669 module->source_name = source_name;
679 module->span = tok.span;
681 module->span = compiler_merge_spans(first_span, last_span);
691 module->functions.append(fn);
699 module->imports.append(decl);
707 module->type_declarations.append(decl);
715 module->type_declarations.append(decl);
723 module->type_declarations.append(decl);
729 emit_error(
tok.span,
"top-level statement is not allowed in this parser mode",
"PAR004");
732 module->statements.append(stmt);
754 kw.span, name.lexeme, name.span,
Source_Span(),
nullptr,
nullptr);
Arena-backed AST node model for the compiler-support MVP.
Reusable token-stream helpers for recursive-descent frontends.
size_t size_t int32_t value
Arena-backed ownership context for AST nodes.
T * make(Args &&...args)
Allocates and constructs an AST node of type T.
Token generator for a single source file.
Lightweight token stream facade for recursive-descent parsers.
bool parse_separated_list(const Compiler_Token_Kind separator, const Compiler_Token_Kind terminator, Parse_Item &&parse_item, const bool allow_trailing_separator=false)
Parses one separated list until a terminator token is reached.
bool match(const Compiler_Token_Kind kind)
Consumes the next token if it matches kind.
void synchronize_statement()
Skips tokens until a statement boundary suitable for recovery.
Compiler_Token peek()
Returns the next token without consuming it.
Compiler_Token expect(const Compiler_Token_Kind kind, const std::string &message, const std::string &code, const std::string ¬e="", const std::string &help="")
Consumes a token of kind kind or emits one diagnostic error.
Compiler_Token expect_identifier(const std::string_view context, const std::string &code="PAR002")
Consumes one identifier token or emits one diagnostic error.
Compiler_Token next()
Consumes and returns the next token.
Recursive-descent parser that produces an AST in Compiler_Ast_Context.
Compiler_Expr * parse_expression(const int min_precedence=1)
Parses binary expressions using precedence-climbing.
Compiler_Grouping_Expr * parse_grouping_expr()
Parses a parenthesized expression: ( expression ).
Compiler_Parser_Options options
Parser behavior configuration.
Compiler_Ast_Context * ast
AST owner.
Compiler_Module * parse_module()
Parses a full module until EOF.
Compiler_Invalid_Expr * invalid_expr(const Source_Span &span={}) const
Creates an invalid expression 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_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_Expr * parse_type_primary()
Parses one primary type-expression form.
Compiler_Invalid_Import_Decl * invalid_import_decl(const Source_Span &span={}) const
Creates an invalid import-declaration node in the current arena.
Compiler_Token peek()
Peeks at the next token without consuming it.
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_Type_Expr * parse_function_type()
Parses a function type such as fn(Int) -> Bool.
Compiler_Lexer lexer
Token source.
bool match(const Compiler_Token_Kind kind)
Consumes the next token if it matches kind.
Compiler_Expr * parse_primary()
Parses primary expressions: literals, identifiers, and groupings.
Compiler_Stmt * parse_continue_statement()
Parses a continue statement: continue;.
Compiler_Expr * parse_call_suffix(Compiler_Expr *callee)
Parses function call argument list suffix: ( args? ).
Compiler_Stmt * parse_let_statement()
Parses a variable declaration: let ident (: type)? (= expr)? ;.
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_Type_Expr * parse_named_type()
Parses a named type such as Int or T.
Compiler_Type_Expr * parse_type_expression()
Parses a type expression used by explicit annotations.
Compiler_Expr * parse_unary()
Parses prefix unary expressions: !, -, +, ~.
Compiler_Token expect_identifier(const std::string &context, const std::string &code="PAR002")
Consumes an identifier token or emits an error.
Compiler_Stmt * parse_if_statement()
Parses an if statement: if expr stmt (else stmt)?.
Compiler_Function_Decl * parse_function()
Parses a top-level function declaration.
Compiler_Stmt * parse_while_statement()
Parses a while loop: while expr stmt.
Compiler_Stmt * parse_return_statement()
Parses a return statement: return expr? ;.
Compiler_Stmt * parse_statement()
Parses a single statement.
Diagnostic_Engine * diagnostics
diagnostic sink.
Compiler_Type_Expr * parse_parenthesized_type()
Parses parenthesized tuple-type syntax.
Compiler_Type_Decl * parse_struct_declaration()
Parses one top-level struct declaration.
Compiler_Stmt * parse_expression_statement()
Parses an expression statement: expr ;.
Compiler_Stmt * parse_break_statement()
Parses a break statement: break;.
Compiler_Parser_Stream tokens
Reusable parser-oriented token facade.
Source_Span parse_statement_terminator(const Source_Span &anchor)
Ensures a statement is terminated by a semicolon or reports an error.
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_Expr * parse_postfix()
Parses postfix expressions such as calls.
Compiler_Block_Stmt * parse_block_statement()
Parses a block statement: { statements* }.
void synchronize_statement()
Error recovery helper: skips tokens until a statement boundary.
std::string source_name
User-facing source name being parsed.
Compiler_Type_Decl * parse_enum_declaration()
Parses one top-level unit-variant enum declaration.
Compiler_Invalid_Stmt * invalid_stmt(const Source_Span &span={}) const
Creates an invalid statement node in the current arena.
Compiler_Token next()
Consumes and returns the next token.
Compiler_Invalid_Type_Expr * invalid_type_expr(const Source_Span &span={}) const
Creates an invalid type-expression node in the current arena.
Diagnostic_Builder & code(const std::string &value)
Sets the stable diagnostic code.
size_t emit() const noexcept
Finalizes the builder and returns the diagnostic index.
Diagnostic_Builder & help(const std::string &msg)
Appends a help line.
Diagnostic_Builder & note(const std::string &msg)
Appends a note line.
Diagnostic accumulator and renderer.
Diagnostic_Builder error(const Source_Span &span, const std::string &msg)
Starts an error diagnostic.
Stores source files and resolves offsets into human-readable data.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
bool compiler_is_expression_start(const Compiler_Token_Kind kind) noexcept
Returns whether a token kind can start an expression in the MVP grammar.
@ Integer_Literal
Numeric integer constant.
@ Char_Literal
Character constant.
@ String_Literal
String constant.
@ Bool_Literal
Boolean constant (true/false).
Source_Span compiler_merge_spans(const Source_Span &lhs, const Source_Span &rhs) noexcept
Merges two spans from the same source file.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
int compiler_binary_precedence(Compiler_Token_Kind kind) noexcept
Returns binary precedence for an operator.
and
Check uniqueness with explicit hash + equality functors.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
@ Right
Right-associative: a = (b = c).
bool equal(Itor1 beg, const Itor1 &end, Itor2 cmpBeg, BinaryPredicate op=BinaryPredicate())
Test if two ranges are equal.
Compiler_Token_Kind
Token kinds supported by the compiler MVP.
@ Identifier
Named identifier (e.g., variable or function name).
@ Integer_Literal
Integer constant.
@ Char_Literal
Single character constant.
@ String_Literal
String constant (with escape sequences).
bool compiler_is_statement_start(const Compiler_Token_Kind kind) noexcept
Returns whether a token kind can start a statement in the MVP grammar.
std::string compiler_unquote_string_token(const Compiler_Token &token)
Removes surrounding double quotes from one string-literal token.
static std::atomic< bool > init
Compiler_Associativity compiler_binary_associativity(Compiler_Token_Kind kind) noexcept
Returns binary associativity for an operator.
Source_Span span
Source region covered by this node.
Node representing an infix binary operation.
Node representing a braced sequence of statements.
Node representing a loop break.
Node representing a function or method call.
Node representing a loop continuation.
Nominal top-level enum declaration with unit variants.
Node representing an expression evaluated as a statement.
Abstract base class for expression nodes.
Node representing a top-level function declaration.
Function type syntax such as fn(Int, T) -> Bool.
Node representing an expression explicitly wrapped in parentheses.
Node representing a named identifier reference.
Node representing a conditional branch.
Abstract base class for top-level import declarations.
Placeholder node for expressions that failed to parse.
Placeholder node for malformed import declarations.
Placeholder node for statements that failed to parse.
Placeholder node for malformed type declarations.
Placeholder node for type syntax that failed to parse.
Node representing a local variable binding.
Node representing a literal value.
Node representing a complete translation unit or module.
Named type syntax such as Int, Bool, or T.
Parser configuration knobs for the MVP grammar.
bool allow_top_level_statements
If false, only top-level declarations are accepted.
Node representing a return from the current function.
Source_Span span
Aggregate source span.
Top-level import declaration naming one other source unit.
Abstract base class for statement nodes.
Nominal top-level struct declaration.
Lexical token representation with its value and location.
Source_Span span
Precise location in the source code.
Compiler_Token_Kind kind
Logical category (e.g., Kw_If).
Tuple type syntax such as (Int, Bool).
DynArray< Compiler_Type_Expr * > members
Tuple element types in source order.
Transparent top-level type alias declaration.
Abstract base class for top-level type declarations.
Abstract base class for parsed type-annotation syntax.
Node representing a prefix unary operation.
Node representing a while-loop.
Half-open byte range inside a source file.