206 if (start_addr >= end_addr)
207 AH_ERROR(
"String too long (buffer overflow in parse_utils)");
209 *start_addr++ =
static_cast<char>(c);
286 const int c = input_stream.get();
322 input_stream.unget();
359 char *start_addr = buffer;
375 if (not std::isdigit(c))
377 input_stream.unget();
379 return std::strtol(buffer,
nullptr, 10);
386 while (std::isdigit(c))
396 return std::strtol(buffer,
nullptr, 10);
402 catch (
const std::out_of_range &)
406 return std::strtol(buffer,
nullptr, 10);
442 char *start_addr = buffer;
462 if (c ==
'\"' and quoted)
466 if (not quoted
and (std::isblank(c) or c ==
'\n' or c == EOF))
560 for (
int i = 0; i < argc; ++i)
633 std::ostringstream oss;
690#define ah_parse_error() \
691 Aleph::ExceptionBuilder<Aleph::ParseError>() <<= \
692 std::ostringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
699#define ah_parse_error_if(C) \
700 while ((C) ? true : false) AH_ERROR_UNLIKELY \
701 Aleph::ExceptionBuilder<Aleph::ParseError>() <<= \
702 std::ostringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
709#define ah_parse_error_unless(C) \
710 while (not (C)) AH_ERROR_UNLIKELY \
711 Aleph::ExceptionBuilder<Aleph::ParseError>() <<= \
712 std::ostringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
746 return input_stream.peek();
763 sp.
pos = input_stream.tellg();
782 input_stream.seekg(pos.
pos);
810 while (not input_stream.eof())
812 if (
const int c = input_stream.get(); c ==
'\n')
838 const std::string &open =
841 const std::string &close =
846 size_t match_pos = 0;
848 while (not input_stream.eof())
850 if (
const int c = input_stream.get(); c ==
'\n')
860 if (c == close[match_pos])
863 if (match_pos == close.size())
867 match_pos = (c == close[0]) ? 1 : 0;
890 while (not input_stream.eof())
892 if (
const int c =
peek_char(input_stream); std::isspace(c))
905 else if (
next ==
'*')
914 input_stream.unget();
970 if (c ==
'-' or c ==
'+')
976 bool has_digits =
false;
979 while (std::isdigit(c))
992 while (std::isdigit(c))
1003 if (c ==
'e' or c ==
'E')
1008 if (c ==
'+' or c ==
'-')
1016 while (std::isdigit(c))
1026 input_stream.unget();
1032 return std::strtod(buffer,
nullptr);
1070 while (std::isxdigit(c))
1076 input_stream.unget();
1081 return std::strtol(buffer,
nullptr, 16);
1114 ah_parse_error_if(c ==
'x' or c ==
'X') <<
"Expected octal number, got hexadecimal";
1116 while (c >=
'0' and c <=
'7')
1122 input_stream.unget();
1131 return std::strtol(buffer,
nullptr, 8);
1168 while (c ==
'0' or c ==
'1')
1174 input_stream.unget();
1179 return std::strtol(buffer,
nullptr, 2);
1214 <<
"Identifier must start with letter or underscore";
1222 if (std::isalnum(c) or c ==
'_')
1228 input_stream.unget();
1251inline bool is_keyword(
const std::string &s,
const std::vector<std::string> &keywords)
1253 return std::find(keywords.begin(), keywords.end(), s) != keywords.end();
1283 <<
"Expected '" <<
expected <<
"' but found '" <<
static_cast<char>(c) <<
"'";
1328inline bool try_char(std::ifstream &input_stream,
char ch)
1389 return static_cast<char>(c);
1443 const size_t len = ptr - buffer;
1445 return std::string(buffer, len);
1481 char result =
static_cast<char>(c);
1525 return "IDENTIFIER";
1537 return "PUNCTUATION";
1593 std::ifstream
file(filename);
1596 std::ostringstream ss;
1611 std::ifstream
file(filename);
1617 while (std::getline(
file, line))
1633 std::istringstream iss(str);
1636 while (std::getline(iss, token, delimiter))
1735#define PRINT_ERROR(...) \
1736 ((std::cout << input_file_name << "(" << Aleph::previous_line_number << "," \
1737 << Aleph::previous_col_number << "): " << '\n'), \
1738 (std::cout << "Last token: " << Aleph::token_instance << '\n'), AH_ERROR(__VA_ARGS__))
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
String manipulation utilities.
#define AH_ERROR(...)
Print an error message (always enabled).
Core header for the Aleph-w library.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Exception class for parsing errors with location information.
ParseError(const std::string &message, const SourceLocation &location)
Construct a parse error with message and location.
ParseError(const std::string &message)
Construct a parse error with message only.
const SourceLocation & location() const
Get the source location of the error.
Minimal std::expected-style result type for C++20.
Main namespace for Aleph-w library functions.
void restore_position(std::ifstream &input_stream, const StreamPosition &pos)
Restore a previously marked position.
int current_line_number
Current line number in the input stream.
char load_char_literal(std::ifstream &input_stream)
Load a character literal.
bool is_keyword(const std::string &s, const std::vector< std::string > &keywords)
Check if a string is in a list of keywords.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
void skip_block_comment(std::ifstream &input_stream, const std::string &open="/" "*", const std::string &close="*" "/")
Skip a block comment (C-style)
std::string load_file_contents(const std::string &filename)
Load entire file contents into a string.
void init_token_scanning()
Initialize token scanning by recording current position.
void close_token_scanning(const char *buffer, char *&start_addr, const char *end_addr)
Finalize token scanning by null-terminating and saving the token.
Container< std::string > split_string(const std::string &s, const std::string &delim)
Split a std::string by a set of delimiter characters.
int read_char_from_stream(std::ifstream &input_stream)
Read a single character from an input stream with position tracking.
void expect_char(std::ifstream &input_stream, char expected)
Expect and consume a specific character.
void expect(std::ifstream &input_stream, const std::string &expected)
Expect and consume a specific string/keyword.
void print_parse_error_and_exit(const std::string &str)
Print a parse error message and terminate the program.
StreamPosition mark_position(std::ifstream &input_stream)
Mark the current position for potential backtracking.
double load_double(std::ifstream &input_stream)
Load a floating-point number from the input stream.
and
Check uniqueness with explicit hash + equality functors.
int current_col_number
Current column number in the input stream.
int previous_col_number
Column number at the start of the current token.
std::string trim(const std::string &s)
Return a trimmed copy of a std::string (leading + trailing whitespace removed).
std::string token_instance
The most recently scanned token.
void put_char_in_buffer(char *&start_addr, const char *end_addr, int c)
Append a character to a buffer with bounds checking.
bool try_char(std::ifstream &input_stream, char ch)
Try to match a character without throwing.
char process_escape(const int c)
Process an escape sequence.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
long load_binary_number(std::ifstream &input_stream)
Load a binary number from the input stream.
std::string command_line_to_string(int argc, char *argv[])
Convert command line arguments to a single string.
std::string load_identifier(std::ifstream &input_stream)
Load an identifier from the input stream.
void reset_parse_state()
Reset the parsing state to initial values.
int previous_line_number
Line number at the start of the current token.
void skip_white_spaces(std::ifstream &input_stream)
Skip whitespace characters in the input stream.
std::string token_type_to_string(TokenType type)
Convert TokenType to string for debugging.
DynList< std::string > load_file_lines(const std::string &filename)
Load file as a list of lines.
long load_octal_number(std::ifstream &input_stream)
Load an octal number from the input stream.
void next()
Advance all underlying iterators (bounds-checked).
std::string load_escaped_string(std::ifstream &input_stream)
Load a string with escape sequence processing.
constexpr size_t Buffer_Size
Default buffer size for token parsing.
TokenType
Enumeration of basic token types.
@ END_OF_FILE
End of input.
@ INTEGER
Integer literal.
@ UNKNOWN
Unknown/invalid token.
@ IDENTIFIER
Variable/function name.
@ KEYWORD
Reserved keyword.
@ PUNCTUATION
Punctuation (;, {, }, etc.)
@ OPERATOR
Operator (+, -, etc.)
@ FLOAT
Floating-point literal.
@ COMMENT
Comment (if preserving)
std::string load_string(std::ifstream &input_stream)
Load a string from the input stream.
void skip_whitespace_and_comments(std::ifstream &input_stream)
Skip whitespace and comments (C/C++ style)
int peek_char(std::ifstream &input_stream)
Peek at the next character without consuming it.
long load_hex_number(std::ifstream &input_stream)
Load a hexadecimal number from the input stream.
void skip_line_comment(std::ifstream &input_stream)
Skip a line comment (// style or # style)
void print_parse_warning(const std::string &str)
Print a parse warning message.
long load_number(std::ifstream &input_stream)
Load an integer number from the input stream.
#define ah_parse_error_if(C)
Throws a ParseError if condition holds.
#define ah_parse_error_unless(C)
Throws a ParseError if condition does NOT hold.
#define ah_parse_error()
Throws a ParseError unconditionally.
Represents a location in source code.
int column
Column number (1-based)
SourceLocation(std::string file, const int ln, const int col)
Construct with all fields.
SourceLocation()=default
Default constructor.
int line
Line number (1-based)
std::string filename
Name of the source file.
std::string to_string() const
Convert to human-readable string.
static SourceLocation previous(const std::string &file="")
Construct from the previous (token start) global parsing state.
static SourceLocation current(const std::string &file="")
Construct from the current global parsing state.
Structure to save stream position for backtracking.
int column
Column number at this position.
int line
Line number at this position.
std::streampos pos
Stream position.
Structure representing a lexical token.
Token(TokenType t, std::string v, SourceLocation loc)
Construct with all fields.
bool is_eof() const
Check if this is an end-of-file token.
std::string value
String value/content.
Token()=default
Default constructor.
std::string to_string() const
String representation for debugging.
SourceLocation location
Where token appeared.
TokenType type
Type of the token.