|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Lightweight token stream facade for recursive-descent parsers. More...
#include <Compiler_Parser_Utils.H>
Public Member Functions | |
| Compiler_Parser_Stream (Compiler_Lexer &token_source, Diagnostic_Engine *dx=nullptr) noexcept | |
| Builds one parser stream over an existing lexer. | |
| Compiler_Token | peek () |
| Returns the next token without consuming it. | |
| Compiler_Token | next () |
| Consumes and returns the next token. | |
| bool | check (const Compiler_Token_Kind kind) |
| Returns whether the next token has the requested kind. | |
| bool | match (const Compiler_Token_Kind kind) |
Consumes the next token if it matches kind. | |
| 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. | |
| template<typename Parse_Item > | |
| 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. | |
| void | synchronize_statement () |
| Skips tokens until a statement boundary suitable for recovery. | |
Private Member Functions | |
| void | emit_error (const Source_Span &span, const std::string &message, const std::string &code, const std::string ¬e="", const std::string &help="") const |
Private Attributes | |
| Compiler_Lexer * | lexer = nullptr |
| Diagnostic_Engine * | diagnostics = nullptr |
Lightweight token stream facade for recursive-descent parsers.
The stream wraps one Compiler_Lexer and exposes parser-oriented helpers: lookahead, match, expect, identifier expectations, separated-list parsing, and statement-oriented recovery.
Definition at line 128 of file Compiler_Parser_Utils.H.
|
inlinenoexcept |
Builds one parser stream over an existing lexer.
Definition at line 152 of file Compiler_Parser_Utils.H.
|
inline |
Returns whether the next token has the requested kind.
Definition at line 169 of file Compiler_Parser_Utils.H.
References Aleph::Compiler_Token::kind, and peek().
Referenced by match(), and parse_separated_list().
|
inlineprivate |
Definition at line 133 of file Compiler_Parser_Utils.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 expect(), and expect_identifier().
|
inline |
Consumes a token of kind kind or emits one diagnostic error.
Definition at line 184 of file Compiler_Parser_Utils.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::code(), emit_error(), Aleph::message(), next(), and peek().
Referenced by Aleph::Compiler_Parser::expect().
|
inline |
Consumes one identifier token or emits one diagnostic error.
Definition at line 199 of file Compiler_Parser_Utils.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::code(), emit_error(), Aleph::Identifier, next(), and peek().
Referenced by Aleph::Compiler_Parser::expect_identifier().
|
inline |
Consumes the next token if it matches kind.
Definition at line 175 of file Compiler_Parser_Utils.H.
References Aleph::blossom_maximum_cardinality_matching(), check(), and next().
Referenced by Aleph::Compiler_Parser::match(), and parse_separated_list().
|
inline |
Consumes and returns the next token.
Definition at line 163 of file Compiler_Parser_Utils.H.
References lexer, and Aleph::Compiler_Lexer::next().
Referenced by expect(), expect_identifier(), match(), Aleph::Compiler_Parser::next(), and synchronize_statement().
|
inline |
Parses one separated list until a terminator token is reached.
| separator | Separator token between items, such as Comma. |
| terminator | Closing token that ends the list. |
| parse_item | Callback that parses and records one item. |
| allow_trailing_separator | If true, a, b, ) is accepted. |
false if parse_item() reports failure; true otherwise. Definition at line 219 of file Compiler_Parser_Utils.H.
References Aleph::and, Aleph::blossom_maximum_cardinality_matching(), check(), match(), and separator().
Referenced by Aleph::Compiler_Parser::parse_call_suffix(), Aleph::Compiler_Parser::parse_enum_declaration(), Aleph::Compiler_Parser::parse_function(), Aleph::Compiler_Parser::parse_function_type(), and Aleph::Compiler_Parser::parse_parenthesized_type().
|
inline |
Returns the next token without consuming it.
Definition at line 157 of file Compiler_Parser_Utils.H.
References lexer, and Aleph::Compiler_Lexer::peek().
Referenced by check(), expect(), expect_identifier(), Aleph::Compiler_Parser::peek(), and synchronize_statement().
|
inline |
Skips tokens until a statement boundary suitable for recovery.
The method stops at EOF, }, the next top-level declaration keyword, or the next structural statement starter ({, let, return, if, while, break, continue). If a semicolon is encountered, it is consumed before returning so that callers can resume after the failed statement.
Definition at line 248 of file Compiler_Parser_Utils.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::compiler_is_top_level_declaration_start(), Aleph::Kw_Break, Aleph::Kw_Continue, Aleph::Kw_If, Aleph::Kw_Let, Aleph::Kw_Return, Aleph::Kw_While, Aleph::LBrace, next(), peek(), Aleph::RBrace, and Aleph::Semicolon.
Referenced by Aleph::Compiler_Parser::synchronize_statement().
|
private |
Definition at line 131 of file Compiler_Parser_Utils.H.
Referenced by emit_error().
|
private |
Definition at line 130 of file Compiler_Parser_Utils.H.