56#ifndef COMPILER_LEXER_H
57#define COMPILER_LEXER_H
103 return std::isalpha(
ch)
or ch ==
'_';
109 return std::isalnum(
ch)
or ch ==
'_';
161 const std::string &
code,
162 const std::string ¬e =
"",
163 const std::string &help =
"")
const
169 if (
not note.empty())
171 if (
not help.empty())
210 emit_error(
sp,
"unterminated block comment",
"LEX004",
"block comments must end with '*/'");
235 if (std::isdigit(
ch))
241 if (
ch ==
'_' and std::isdigit(
static_cast<unsigned char>(
next_char())))
277 if (
ch ==
'\n' or ch ==
'\r')
285 "unterminated string literal",
287 "string literal started here",
288 "close the string with a double quote");
301 emit_error(
sp,
"unterminated character literal",
"LEX003",
"character literal started here");
311 emit_error(
sp,
"unterminated escape sequence in character literal",
"LEX003");
334 "malformed character literal",
336 "character literals must contain exactly one character or escape");
348 if (a ==
':' and b ==
':')
353 if (a ==
'-' and b ==
'>')
358 if (a ==
'=' and b ==
'=')
363 if (a ==
'!' and b ==
'=')
368 if (a ==
'<' and b ==
'=')
373 if (a ==
'>' and b ==
'=')
378 if (a ==
'&' and b ==
'&')
383 if (a ==
'|' and b ==
'|')
388 if (a ==
'+' and b ==
'=')
393 if (a ==
'-' and b ==
'=')
398 if (a ==
'*' and b ==
'=')
403 if (a ==
'/' and b ==
'=')
408 if (a ==
'%' and b ==
'=')
413 if (a ==
'+' and b ==
'+')
418 if (a ==
'-' and b ==
'-')
478 emit_error(
sp, std::string(
"unexpected character '") + a +
"'",
"LEX001");
513 if (std::isdigit(
ch))
578 <<
"Compiler_Lexer::reset(): invalid offset " <<
offset;
Token model and parser-facing metadata for compiler front-ends.
Plain-text diagnostic engine for compiler-style tooling.
#define ah_out_of_range_error_unless(C)
Throws std::out_of_range if condition does NOT hold.
Token generator for a single source file.
Compiler_Token lex_identifier_or_keyword()
Lexes an identifier or classifies it as a keyword.
Source_Offset current_offset() const noexcept
Returns the current byte offset in the file.
Compiler_Lexer_Options options_
static bool is_ident_start(const unsigned char ch) noexcept
Checks if a character can start an identifier.
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 lex_punctuation_or_operator()
Lexes punctuation or operator symbols.
Compiler_Token lex_char_literal()
Lexes a character literal.
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.
Compiler_Token lex_number()
Lexes a numeric literal.
const Source_Manager * sources_
Compiler_Token lex_line_comment()
Lexes a single-line comment.
Compiler_Token make_eof_token() const
Creates an End_Of_File token at the current position.
char current_char() const noexcept
Returns the character at the current cursor position.
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.
void skip_whitespace()
Consumes whitespace characters.
Compiler_Token next()
Consumes and returns the next token from the stream.
bool eof()
Indicates whether the end of the file has been reached.
char char_at(const Source_Offset off) const noexcept
Safe character access at a given offset.
const Compiler_Token & peek()
Peeks at the next token without advancing the cursor.
Compiler_Token lex_token()
Dispatches the next token from the input stream.
Source_File_Id source_file_id() const noexcept
Returns the file ID currently being analyzed.
Diagnostic_Engine * diagnostics_
void reset(const Source_Offset offset=0)
Repositions the reading cursor.
bool at_end() const noexcept
Returns true if the cursor has reached the end of the input.
Compiler_Token lex_block_comment()
Lexes a multi-line block comment.
static bool is_ident_continue(const unsigned char ch) noexcept
Checks if a character can continue an identifier.
const std::string * input_
Compiler_Token lex_string()
Lexes a string literal.
Compiler_Token lookahead_
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.
char next_char() const noexcept
Returns the character following the current cursor position.
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.
std::string slice(const Source_Span &span) const
Returns the exact text covered by span.
Source_Span span(const Source_File_Id id, const Source_Offset begin, const Source_Offset end) const
Creates a validated span in file id.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Main namespace for Aleph-w library functions.
Compiler_Token_Kind classify_compiler_keyword(std::string_view lexeme) noexcept
Classifies a lexeme as a keyword or Identifier.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
size_t size(Node *root) noexcept
and
Check uniqueness with explicit hash + equality functors.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
Compiler_Token_Kind
Token kinds supported by the compiler MVP.
@ Line_Comment
Single-line comment (// ...).
@ Integer_Literal
Integer constant.
@ End_Of_File
End of the source stream.
@ Invalid
Sentinel for lexer errors or uninitialized tokens.
@ Char_Literal
Single character constant.
@ Block_Comment
Multi-line comment (/* ... *‍/).
@ String_Literal
String constant (with escape sequences).
Configuration options for lexer behavior.
bool allow_block_comments
If false, /* is treated as / followed by *.
bool keep_comments
If true, comments are returned as tokens.
Lexical token representation with its value and location.
bool is_eof() const noexcept
Returns whether the token marks the end of the file.
Half-open byte range inside a source file.