38#include <gtest/gtest.h>
45using namespace testing;
87 char* end = buffer + 10;
100 char* end = buffer + 10;
113 char* end = buffer + 5;
115 for (
int i = 0; i < 5; ++i)
140 char buffer[20] =
"hello";
141 char* ptr = buffer + 5;
142 char* end = buffer + 20;
151 char buffer[20] =
"test";
152 char* ptr = buffer + 4;
153 char* end = buffer + 20;
172 create_temp_file(
"ABC");
173 ifstream
input(temp_filename);
182 create_temp_file(
"ABC");
183 ifstream
input(temp_filename);
194 create_temp_file(
"A\nB");
195 ifstream
input(temp_filename);
207 create_temp_file(
"");
208 ifstream
input(temp_filename);
222 create_temp_file(
" hello");
223 ifstream
input(temp_filename);
232 create_temp_file(
"\t\thello");
233 ifstream
input(temp_filename);
242 create_temp_file(
"\n\nhello");
243 ifstream
input(temp_filename);
252 create_temp_file(
" \t\n \t\nhello");
253 ifstream
input(temp_filename);
262 create_temp_file(
"hello");
263 ifstream
input(temp_filename);
276 create_temp_file(
"42 ");
277 ifstream
input(temp_filename);
284 create_temp_file(
"-123 ");
285 ifstream
input(temp_filename);
292 create_temp_file(
"0 ");
293 ifstream
input(temp_filename);
300 create_temp_file(
" 100 ");
301 ifstream
input(temp_filename);
308 create_temp_file(
"10 20 30 ");
309 ifstream
input(temp_filename);
320 create_temp_file(
"42");
321 ifstream
input(temp_filename);
330 create_temp_file(
"42\n");
331 ifstream
input(temp_filename);
338 create_temp_file(
"1234567890 ");
339 ifstream
input(temp_filename);
346 create_temp_file(
"-987654321 ");
347 ifstream
input(temp_filename);
354 create_temp_file(
"999 ");
355 ifstream
input(temp_filename);
368 create_temp_file(
"hello ");
369 ifstream
input(temp_filename);
376 create_temp_file(
"\"hello world\" ");
377 ifstream
input(temp_filename);
384 create_temp_file(
"\"hello world\" ");
385 ifstream
input(temp_filename);
392 create_temp_file(
" hello ");
393 ifstream
input(temp_filename);
400 create_temp_file(
"hello world test ");
401 ifstream
input(temp_filename);
410 create_temp_file(
"hello \"world test\" foo ");
411 ifstream
input(temp_filename);
420 create_temp_file(
"\"\" ");
421 ifstream
input(temp_filename);
428 create_temp_file(
"test ");
429 ifstream
input(temp_filename);
438 create_temp_file(
"abc123 ");
439 ifstream
input(temp_filename);
446 create_temp_file(
"\"line1\nline2\" ");
447 ifstream
input(temp_filename);
459 const char*
argv[] = {
"program"};
467 const char*
argv[] = {
"program",
"--input",
"file.txt",
"-v"};
470 EXPECT_EQ(result,
" program --input file.txt -v");
517 create_temp_file(
"count 10\nname \"test file\"\nsize 42\n");
518 ifstream
input(temp_filename);
538 create_temp_file(
" 123 \"hello world\" -456 test ");
539 ifstream
input(temp_filename);
549 create_temp_file(
"line1\nline2\nline3\n");
550 ifstream
input(temp_filename);
570 create_temp_file(
"abc def\nghi jkl\n");
571 ifstream
input(temp_filename);
600 create_temp_file(
"- 5 ");
601 ifstream
input(temp_filename);
610 create_temp_file(
"\"hello@world#test!\" ");
611 ifstream
input(temp_filename);
618 create_temp_file(
"\"path\\to\\file\" ");
619 ifstream
input(temp_filename);
628 create_temp_file(
"\"" +
longstr +
"\" ");
629 ifstream
input(temp_filename);
699 string msg =
err.what();
700 EXPECT_NE(msg.find(
"test.cpp"), string::npos);
702 EXPECT_NE(msg.find(
"unexpected token"), string::npos);
720 create_temp_file(
"ABC");
721 ifstream
input(temp_filename);
731 create_temp_file(
"hello world");
732 ifstream
input(temp_filename);
735 for (
int i = 0; i < 5; ++i)
741 for (
int i = 0; i < 6; ++i)
757 create_temp_file(
"// this is a comment\nhello");
758 ifstream
input(temp_filename);
772 create_temp_file(
"/* comment */hello");
773 ifstream
input(temp_filename);
786 create_temp_file(
"/* line 1\n line 2\n line 3 */hello");
787 ifstream
input(temp_filename);
801 create_temp_file(
" // comment\n /* block */ hello");
802 ifstream
input(temp_filename);
811 create_temp_file(
" # python style comment\nhello");
812 ifstream
input(temp_filename);
825 create_temp_file(
"3.14159 ");
826 ifstream
input(temp_filename);
833 create_temp_file(
"-2.5 ");
834 ifstream
input(temp_filename);
841 create_temp_file(
"1.5e10 ");
842 ifstream
input(temp_filename);
849 create_temp_file(
"2.5e-3 ");
850 ifstream
input(temp_filename);
857 create_temp_file(
".5 ");
858 ifstream
input(temp_filename);
865 create_temp_file(
"5. ");
866 ifstream
input(temp_filename);
873 create_temp_file(
"0xFF ");
874 ifstream
input(temp_filename);
881 create_temp_file(
"0x1a2b ");
882 ifstream
input(temp_filename);
889 create_temp_file(
"0755 ");
890 ifstream
input(temp_filename);
897 create_temp_file(
"0 ");
898 ifstream
input(temp_filename);
905 create_temp_file(
"0b1010 ");
906 ifstream
input(temp_filename);
913 create_temp_file(
"0B11110000 ");
914 ifstream
input(temp_filename);
925 create_temp_file(
"myVariable ");
926 ifstream
input(temp_filename);
933 create_temp_file(
"_private_var ");
934 ifstream
input(temp_filename);
941 create_temp_file(
"var123 ");
942 ifstream
input(temp_filename);
949 create_temp_file(
"camelCaseIdentifier ");
950 ifstream
input(temp_filename);
957 create_temp_file(
"123invalid ");
958 ifstream
input(temp_filename);
991 create_temp_file(
" ( hello");
992 ifstream
input(temp_filename);
999 create_temp_file(
" [ hello");
1000 ifstream
input(temp_filename);
1007 create_temp_file(
" function foo");
1008 ifstream
input(temp_filename);
1015 create_temp_file(
" procedure foo");
1016 ifstream
input(temp_filename);
1023 create_temp_file(
" ; next");
1024 ifstream
input(temp_filename);
1034 create_temp_file(
" , next");
1035 ifstream
input(temp_filename);
1071 create_temp_file(
"\"hello\\nworld\" ");
1072 ifstream
input(temp_filename);
1080 create_temp_file(
"\"line1\\tline2\\r\\nline3\" ");
1081 ifstream
input(temp_filename);
1084 EXPECT_EQ(result,
"line1\tline2\r\nline3");
1089 create_temp_file(
"\"he said \\\"hello\\\"\" ");
1090 ifstream
input(temp_filename);
1098 create_temp_file(
"\"path\\\\to\\\\file\" ");
1099 ifstream
input(temp_filename);
1107 create_temp_file(
"\"no closing quote");
1108 ifstream
input(temp_filename);
1119 create_temp_file(
"'a' ");
1120 ifstream
input(temp_filename);
1127 create_temp_file(
"'\\n' ");
1128 ifstream
input(temp_filename);
1135 create_temp_file(
"'\\t' ");
1136 ifstream
input(temp_filename);
1143 create_temp_file(
"'\\'' ");
1144 ifstream
input(temp_filename);
1164 Token tok(TokenType::IDENTIFIER,
"myVar", loc);
1186 create_temp_file(
"line1\nline2\nline3");
1194 create_temp_file(
"line1\nline2\nline3");
1255 create_temp_file(
"x = 42 + 3.14;");
1256 ifstream
input(temp_filename);
1277 create_temp_file(
"// comment\nx = 10 ; /* another */ y = 20 ;");
1278 ifstream
input(temp_filename);
size_t size() const noexcept
Count the number of elements of the list.
Exception class for parsing errors with location information.
void create_temp_file(const string &content)
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 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.
StreamPosition mark_position(std::ifstream &input_stream)
Mark the current position for potential backtracking.
std::vector< std::string > load_file_lines(const std::string &filename)
Load file as a vector of lines.
double load_double(std::ifstream &input_stream)
Load a floating-point number from the input stream.
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.
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
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.
long load_octal_number(std::ifstream &input_stream)
Load an octal number from the input stream.
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.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
void skip_line_comment(std::ifstream &input_stream)
Skip a line comment (// style or # style)
long load_number(std::ifstream &input_stream)
Load an integer number from the input stream.
Comprehensive parsing utilities for text processing and compiler construction.
TEST_F(ParseUtilsTest, ReadCharBasic)
Represents a location in source code.
int column
Column number (1-based)
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 current(const std::string &file="")
Construct from the current global parsing state.
Structure to save stream position for backtracking.
Structure representing a lexical token.