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

Lightweight token stream facade for recursive-descent parsers. More...

#include <Compiler_Parser_Utils.H>

Collaboration diagram for Aleph::Compiler_Parser_Stream:
[legend]

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 &note="", 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 &note="", const std::string &help="") const
 

Private Attributes

Compiler_Lexer * lexer = nullptr
 
Diagnostic_Engine * diagnostics = nullptr
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Compiler_Parser_Stream()

Aleph::Compiler_Parser_Stream::Compiler_Parser_Stream ( Compiler_Lexer &  token_source,
Diagnostic_Engine *  dx = nullptr 
)
inlinenoexcept

Builds one parser stream over an existing lexer.

Definition at line 152 of file Compiler_Parser_Utils.H.

Member Function Documentation

◆ check()

bool Aleph::Compiler_Parser_Stream::check ( const Compiler_Token_Kind  kind)
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().

◆ emit_error()

void Aleph::Compiler_Parser_Stream::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_Stream::expect ( const Compiler_Token_Kind  kind,
const std::string &  message,
const std::string &  code,
const std::string &  note = "",
const std::string &  help = "" 
)
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().

◆ expect_identifier()

Compiler_Token Aleph::Compiler_Parser_Stream::expect_identifier ( const std::string_view  context,
const std::string &  code = "PAR002" 
)
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().

◆ match()

bool Aleph::Compiler_Parser_Stream::match ( const Compiler_Token_Kind  kind)
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().

◆ next()

Compiler_Token Aleph::Compiler_Parser_Stream::next ( )
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().

◆ parse_separated_list()

template<typename Parse_Item >
bool Aleph::Compiler_Parser_Stream::parse_separated_list ( const Compiler_Token_Kind  separator,
const Compiler_Token_Kind  terminator,
Parse_Item &&  parse_item,
const bool  allow_trailing_separator = false 
)
inline

Parses one separated list until a terminator token is reached.

Parameters
separatorSeparator token between items, such as Comma.
terminatorClosing token that ends the list.
parse_itemCallback that parses and records one item.
allow_trailing_separatorIf true, a, b, ) is accepted.
Returns
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().

◆ peek()

Compiler_Token Aleph::Compiler_Parser_Stream::peek ( )
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().

◆ synchronize_statement()

void Aleph::Compiler_Parser_Stream::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().

Member Data Documentation

◆ diagnostics

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

Definition at line 131 of file Compiler_Parser_Utils.H.

Referenced by emit_error().

◆ lexer

Compiler_Lexer* Aleph::Compiler_Parser_Stream::lexer = nullptr
private

Definition at line 130 of file Compiler_Parser_Utils.H.

Referenced by next(), and peek().


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