|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Token generator for a single source file. More...
#include <Compiler_Lexer.H>
Public Member Functions | |
| Compiler_Lexer (const Source_Manager &sm, const Source_File_Id id, Diagnostic_Engine *dx=nullptr, const Compiler_Lexer_Options &opts={}) | |
| Initializes a lexer for a registered source file. | |
| Source_File_Id | source_file_id () const noexcept |
| Returns the file ID currently being analyzed. | |
| Source_Offset | current_offset () const noexcept |
| Returns the current byte offset in the file. | |
| void | reset (const Source_Offset offset=0) |
| Repositions the reading cursor. | |
| const Compiler_Token & | peek () |
| Peeks at the next token without advancing the cursor. | |
| Compiler_Token | next () |
| Consumes and returns the next token from the stream. | |
| bool | eof () |
| Indicates whether the end of the file has been reached. | |
Private Member Functions | |
| bool | at_end () const noexcept |
| Returns true if the cursor has reached the end of the input. | |
| char | char_at (const Source_Offset off) const noexcept |
| Safe character access at a given offset. | |
| char | current_char () const noexcept |
| Returns the character at the current cursor position. | |
| char | next_char () const noexcept |
| Returns the character following the current cursor position. | |
| Source_Span | make_span (const Source_Offset begin, const Source_Offset end) const |
| Creates a source span for the given range in the current file. | |
| Compiler_Token | make_token (const Compiler_Token_Kind kind, const Source_Offset begin, const Source_Offset end) const |
| Creates a token with the given kind and range. | |
| Compiler_Token | make_eof_token () const |
| Creates an End_Of_File token at the current position. | |
| void | emit_error (const Source_Span &span, const std::string &message, const std::string &code, const std::string ¬e="", const std::string &help="") const |
| Emits a diagnostic error through the engine. | |
| void | skip_whitespace () |
| Consumes whitespace characters. | |
| Compiler_Token | lex_line_comment () |
| Lexes a single-line comment. | |
| Compiler_Token | lex_block_comment () |
| Lexes a multi-line block comment. | |
| Compiler_Token | lex_identifier_or_keyword () |
| Lexes an identifier or classifies it as a keyword. | |
| Compiler_Token | lex_number () |
| Lexes a numeric literal. | |
| Compiler_Token | lex_string () |
| Lexes a string literal. | |
| Compiler_Token | lex_char_literal () |
| Lexes a character literal. | |
| Compiler_Token | lex_punctuation_or_operator () |
| Lexes punctuation or operator symbols. | |
| Compiler_Token | lex_token () |
| Dispatches the next token from the input stream. | |
Static Private Member Functions | |
| static bool | is_ident_start (const unsigned char ch) noexcept |
| Checks if a character can start an identifier. | |
| static bool | is_ident_continue (const unsigned char ch) noexcept |
| Checks if a character can continue an identifier. | |
Private Attributes | |
| const Source_Manager * | sources_ = nullptr |
| Source_File_Id | file_id_ = 0 |
| const std::string * | input_ = nullptr |
| Diagnostic_Engine * | diagnostics_ = nullptr |
| Compiler_Lexer_Options | options_ |
| Source_Offset | cursor_ = 0 |
| bool | has_lookahead_ = false |
| Compiler_Token | lookahead_ |
Token generator for a single source file.
Maintains the reading state (cursor) and provides a "consumption" interface for logical symbols.
Definition at line 89 of file Compiler_Lexer.H.
|
inline |
Initializes a lexer for a registered source file.
| sm | Source manager containing the file text. |
| id | Identifier of the file to tokenize. |
| dx | Diagnostic engine to report errors. If null, errors are only indicated via Invalid tokens. |
| opts | Trivia handling options (comments, etc.). |
| std::out_of_range | If id is not valid in sm. |
Definition at line 534 of file Compiler_Lexer.H.
|
inlineprivatenoexcept |
Returns true if the cursor has reached the end of the input.
Definition at line 113 of file Compiler_Lexer.H.
References cursor_, and input_.
Referenced by lex_block_comment(), lex_char_literal(), lex_identifier_or_keyword(), lex_line_comment(), lex_number(), lex_string(), lex_token(), and skip_whitespace().
|
inlineprivatenoexcept |
Safe character access at a given offset.
Definition at line 119 of file Compiler_Lexer.H.
References Aleph::blossom_maximum_cardinality_matching(), and input_.
Referenced by current_char(), and next_char().
|
inlineprivatenoexcept |
Returns the character at the current cursor position.
Definition at line 125 of file Compiler_Lexer.H.
References char_at(), and cursor_.
Referenced by lex_block_comment(), lex_char_literal(), lex_identifier_or_keyword(), lex_line_comment(), lex_number(), lex_punctuation_or_operator(), lex_string(), lex_token(), and skip_whitespace().
|
inlinenoexcept |
Returns the current byte offset in the file.
Useful for saving recovery states or for telemetry.
Definition at line 560 of file Compiler_Lexer.H.
References cursor_.
Referenced by TEST().
|
inlineprivate |
Emits a diagnostic error through the engine.
Definition at line 159 of file Compiler_Lexer.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 lex_block_comment(), lex_char_literal(), lex_punctuation_or_operator(), and lex_string().
|
inline |
Indicates whether the end of the file has been reached.
true if the next call to next() would return End_Of_File. Definition at line 628 of file Compiler_Lexer.H.
References Aleph::Compiler_Token::is_eof(), and peek().
|
inlinestaticprivatenoexcept |
Checks if a character can continue an identifier.
Definition at line 107 of file Compiler_Lexer.H.
References Aleph::blossom_maximum_cardinality_matching().
Referenced by lex_identifier_or_keyword().
|
inlinestaticprivatenoexcept |
Checks if a character can start an identifier.
Definition at line 101 of file Compiler_Lexer.H.
References Aleph::blossom_maximum_cardinality_matching().
Referenced by lex_token().
|
inlineprivate |
Lexes a multi-line block comment.
Definition at line 194 of file Compiler_Lexer.H.
References Aleph::and, at_end(), Aleph::Block_Comment, Aleph::blossom_maximum_cardinality_matching(), current_char(), cursor_, emit_error(), Aleph::Invalid, make_span(), make_token(), next_char(), Aleph::Source_Manager::slice(), and sources_.
Referenced by lex_token().
|
inlineprivate |
Lexes a character literal.
Definition at line 293 of file Compiler_Lexer.H.
References Aleph::and, at_end(), Aleph::blossom_maximum_cardinality_matching(), Aleph::Char_Literal, current_char(), cursor_, emit_error(), Aleph::Invalid, make_span(), make_token(), Aleph::Source_Manager::slice(), and sources_.
Referenced by lex_token().
|
inlineprivate |
Lexes an identifier or classifies it as a keyword.
Definition at line 215 of file Compiler_Lexer.H.
References Aleph::and, at_end(), Aleph::blossom_maximum_cardinality_matching(), Aleph::classify_compiler_keyword(), current_char(), cursor_, is_ident_continue(), make_span(), Aleph::Source_Manager::slice(), and sources_.
Referenced by lex_token().
|
inlineprivate |
Lexes a single-line comment.
Definition at line 184 of file Compiler_Lexer.H.
References Aleph::and, at_end(), Aleph::blossom_maximum_cardinality_matching(), current_char(), cursor_, Aleph::Line_Comment, and make_token().
Referenced by lex_token().
|
inlineprivate |
Lexes a numeric literal.
Definition at line 228 of file Compiler_Lexer.H.
References Aleph::and, at_end(), Aleph::blossom_maximum_cardinality_matching(), current_char(), cursor_, Aleph::Integer_Literal, make_token(), and next_char().
Referenced by lex_token().
|
inlineprivate |
Lexes punctuation or operator symbols.
Definition at line 341 of file Compiler_Lexer.H.
References Aleph::Amp, Aleph::and, Aleph::AndAnd, Aleph::Arrow, Aleph::Assign, Aleph::Bang, Aleph::blossom_maximum_cardinality_matching(), Aleph::Caret, Aleph::Colon, Aleph::Comma, current_char(), cursor_, Aleph::Dot, Aleph::Double_Colon, emit_error(), Aleph::EqEq, Aleph::Greater, Aleph::GreaterEq, Aleph::Invalid, Aleph::LBrace, Aleph::LBracket, Aleph::Less, Aleph::LessEq, Aleph::LParen, make_span(), make_token(), Aleph::Minus, Aleph::MinusEq, Aleph::MinusMinus, next_char(), Aleph::NotEq, Aleph::OrOr, Aleph::Percent, Aleph::PercentEq, Aleph::Pipe, Aleph::Plus, Aleph::PlusEq, Aleph::PlusPlus, Aleph::Question, Aleph::RBrace, Aleph::RBracket, Aleph::RParen, Aleph::Semicolon, Aleph::Slash, Aleph::SlashEq, Aleph::Source_Manager::slice(), sources_, Aleph::Star, Aleph::StarEq, and Aleph::Tilde.
Referenced by lex_token().
|
inlineprivate |
Lexes a string literal.
Definition at line 254 of file Compiler_Lexer.H.
References at_end(), Aleph::blossom_maximum_cardinality_matching(), current_char(), cursor_, emit_error(), Aleph::Invalid, make_span(), make_token(), Aleph::Source_Manager::slice(), sources_, and Aleph::String_Literal.
Referenced by lex_token().
|
inlineprivate |
Dispatches the next token from the input stream.
Definition at line 485 of file Compiler_Lexer.H.
References Aleph::Compiler_Lexer_Options::allow_block_comments, Aleph::and, at_end(), Aleph::blossom_maximum_cardinality_matching(), current_char(), is_ident_start(), Aleph::Compiler_Lexer_Options::keep_comments, lex_block_comment(), lex_char_literal(), lex_identifier_or_keyword(), lex_line_comment(), lex_number(), lex_punctuation_or_operator(), lex_string(), make_eof_token(), next_char(), options_, and skip_whitespace().
|
inlineprivate |
Creates an End_Of_File token at the current position.
Definition at line 152 of file Compiler_Lexer.H.
References Aleph::blossom_maximum_cardinality_matching(), cursor_, Aleph::End_Of_File, and make_span().
Referenced by lex_token().
|
inlineprivate |
Creates a source span for the given range in the current file.
Definition at line 137 of file Compiler_Lexer.H.
References file_id_, sources_, and Aleph::Source_Manager::span().
Referenced by lex_block_comment(), lex_char_literal(), lex_identifier_or_keyword(), lex_punctuation_or_operator(), lex_string(), make_eof_token(), and make_token().
|
inlineprivate |
Creates a token with the given kind and range.
Definition at line 143 of file Compiler_Lexer.H.
References Aleph::blossom_maximum_cardinality_matching(), make_span(), Aleph::Source_Manager::slice(), and sources_.
Referenced by lex_block_comment(), lex_char_literal(), lex_line_comment(), lex_number(), lex_punctuation_or_operator(), and lex_string().
|
inline |
Consumes and returns the next token from the stream.
Advances the internal cursor. If there is a lexical error, returns an Invalid token but continues advancing to attempt recovery.
Definition at line 612 of file Compiler_Lexer.H.
References has_lookahead_, lex_token(), and lookahead_.
Referenced by main(), Aleph::Compiler_Parser_Stream::next(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlineprivatenoexcept |
Returns the character following the current cursor position.
Definition at line 131 of file Compiler_Lexer.H.
References char_at(), and cursor_.
Referenced by lex_block_comment(), lex_number(), lex_punctuation_or_operator(), and lex_token().
|
inline |
Peeks at the next token without advancing the cursor.
Allows the parser to make syntactic decisions based on the next symbol without consuming it permanently. Successive calls return the same token.
Definition at line 593 of file Compiler_Lexer.H.
References Aleph::blossom_maximum_cardinality_matching(), has_lookahead_, lex_token(), and lookahead_.
Referenced by eof(), Aleph::Compiler_Parser_Stream::peek(), and TEST().
|
inline |
Repositions the reading cursor.
Discards any pre-read tokens (lookahead) and restarts the analysis from the indicated position.
| offset | New byte offset (default 0, the beginning). |
| std::out_of_range | If the offset exceeds the file length. |
Definition at line 575 of file Compiler_Lexer.H.
References ah_out_of_range_error_unless, Aleph::blossom_maximum_cardinality_matching(), cursor_, has_lookahead_, lookahead_, offset, and Aleph::size().
Referenced by TEST().
|
inlineprivate |
Consumes whitespace characters.
Definition at line 177 of file Compiler_Lexer.H.
References Aleph::and, at_end(), Aleph::blossom_maximum_cardinality_matching(), current_char(), and cursor_.
Referenced by lex_token().
|
inlinenoexcept |
Returns the file ID currently being analyzed.
Definition at line 547 of file Compiler_Lexer.H.
References file_id_.
Referenced by TEST().
|
private |
Definition at line 96 of file Compiler_Lexer.H.
Referenced by at_end(), current_char(), current_offset(), lex_block_comment(), lex_char_literal(), lex_identifier_or_keyword(), lex_line_comment(), lex_number(), lex_punctuation_or_operator(), lex_string(), make_eof_token(), next_char(), reset(), and skip_whitespace().
|
private |
Definition at line 94 of file Compiler_Lexer.H.
Referenced by emit_error().
|
private |
Definition at line 92 of file Compiler_Lexer.H.
Referenced by make_span(), and source_file_id().
Definition at line 97 of file Compiler_Lexer.H.
|
private |
Definition at line 93 of file Compiler_Lexer.H.
|
private |
Definition at line 98 of file Compiler_Lexer.H.
|
private |
Definition at line 95 of file Compiler_Lexer.H.
Referenced by lex_token().
|
private |
Definition at line 91 of file Compiler_Lexer.H.
Referenced by lex_block_comment(), lex_char_literal(), lex_identifier_or_keyword(), lex_punctuation_or_operator(), lex_string(), make_span(), and make_token().