38#include <gtest/gtest.h>
52using namespace testing;
67 return static_cast<long long>(
_getpid());
69 return static_cast<long long>(
getpid());
76 const auto *
test_info = UnitTest::GetInstance()->current_test_info();
77 string base = string(
"parse_utils_test_") +
test_info->test_suite_name() +
"_" +
84 temp_filename = (std::filesystem::temp_directory_path() / base).
string();
111 char* end = buffer + 10;
124 char* end = buffer + 10;
137 char* end = buffer + 5;
139 for (
int i = 0; i < 5; ++i)
164 char buffer[20] =
"hello";
165 char* ptr = buffer + 5;
166 char* end = buffer + 20;
175 char buffer[20] =
"test";
176 char* ptr = buffer + 4;
177 char* end = buffer + 20;
196 create_temp_file(
"ABC");
197 ifstream
input(temp_filename);
206 create_temp_file(
"ABC");
207 ifstream
input(temp_filename);
218 create_temp_file(
"A\nB");
219 ifstream
input(temp_filename);
231 create_temp_file(
"");
232 ifstream
input(temp_filename);
246 create_temp_file(
" hello");
247 ifstream
input(temp_filename);
256 create_temp_file(
"\t\thello");
257 ifstream
input(temp_filename);
266 create_temp_file(
"\n\nhello");
267 ifstream
input(temp_filename);
276 create_temp_file(
" \t\n \t\nhello");
277 ifstream
input(temp_filename);
286 create_temp_file(
"hello");
287 ifstream
input(temp_filename);
300 create_temp_file(
"42 ");
301 ifstream
input(temp_filename);
308 create_temp_file(
"-123 ");
309 ifstream
input(temp_filename);
316 create_temp_file(
"0 ");
317 ifstream
input(temp_filename);
324 create_temp_file(
" 100 ");
325 ifstream
input(temp_filename);
332 create_temp_file(
"10 20 30 ");
333 ifstream
input(temp_filename);
344 create_temp_file(
"42");
345 ifstream
input(temp_filename);
354 create_temp_file(
"42\n");
355 ifstream
input(temp_filename);
362 create_temp_file(
"1234567890 ");
363 ifstream
input(temp_filename);
370 create_temp_file(
"-987654321 ");
371 ifstream
input(temp_filename);
378 create_temp_file(
"999 ");
379 ifstream
input(temp_filename);
392 create_temp_file(
"hello ");
393 ifstream
input(temp_filename);
400 create_temp_file(
"\"hello world\" ");
401 ifstream
input(temp_filename);
408 create_temp_file(
"\"hello world\" ");
409 ifstream
input(temp_filename);
416 create_temp_file(
" hello ");
417 ifstream
input(temp_filename);
424 create_temp_file(
"hello world test ");
425 ifstream
input(temp_filename);
434 create_temp_file(
"hello \"world test\" foo ");
435 ifstream
input(temp_filename);
444 create_temp_file(
"\"\" ");
445 ifstream
input(temp_filename);
452 create_temp_file(
"test ");
453 ifstream
input(temp_filename);
462 create_temp_file(
"abc123 ");
463 ifstream
input(temp_filename);
470 create_temp_file(
"\"line1\nline2\" ");
471 ifstream
input(temp_filename);
483 const char*
argv[] = {
"program"};
491 const char*
argv[] = {
"program",
"--input",
"file.txt",
"-v"};
494 EXPECT_EQ(result,
" program --input file.txt -v");
541 create_temp_file(
"count 10\nname \"test file\"\nsize 42\n");
542 ifstream
input(temp_filename);
562 create_temp_file(
" 123 \"hello world\" -456 test ");
563 ifstream
input(temp_filename);
573 create_temp_file(
"line1\nline2\nline3\n");
574 ifstream
input(temp_filename);
594 create_temp_file(
"abc def\nghi jkl\n");
595 ifstream
input(temp_filename);
624 create_temp_file(
"- 5 ");
625 ifstream
input(temp_filename);
634 create_temp_file(
"\"hello@world#test!\" ");
635 ifstream
input(temp_filename);
642 create_temp_file(
"\"path\\to\\file\" ");
643 ifstream
input(temp_filename);
652 create_temp_file(
"\"" +
longstr +
"\" ");
653 ifstream
input(temp_filename);
723 string msg =
err.what();
724 EXPECT_NE(msg.find(
"test.cpp"), string::npos);
726 EXPECT_NE(msg.find(
"unexpected token"), string::npos);
744 create_temp_file(
"ABC");
745 ifstream
input(temp_filename);
755 create_temp_file(
"hello world");
756 ifstream
input(temp_filename);
759 for (
int i = 0; i < 5; ++i)
765 for (
int i = 0; i < 6; ++i)
781 create_temp_file(
"// this is a comment\nhello");
782 ifstream
input(temp_filename);
796 create_temp_file(
"/* comment */hello");
797 ifstream
input(temp_filename);
810 create_temp_file(
"/* line 1\n line 2\n line 3 */hello");
811 ifstream
input(temp_filename);
825 create_temp_file(
" // comment\n /* block */ hello");
826 ifstream
input(temp_filename);
835 create_temp_file(
" # python style comment\nhello");
836 ifstream
input(temp_filename);
849 create_temp_file(
"3.14159 ");
850 ifstream
input(temp_filename);
857 create_temp_file(
"-2.5 ");
858 ifstream
input(temp_filename);
865 create_temp_file(
"1.5e10 ");
866 ifstream
input(temp_filename);
873 create_temp_file(
"2.5e-3 ");
874 ifstream
input(temp_filename);
881 create_temp_file(
".5 ");
882 ifstream
input(temp_filename);
889 create_temp_file(
"5. ");
890 ifstream
input(temp_filename);
897 create_temp_file(
"0xFF ");
898 ifstream
input(temp_filename);
905 create_temp_file(
"0x1a2b ");
906 ifstream
input(temp_filename);
913 create_temp_file(
"0755 ");
914 ifstream
input(temp_filename);
921 create_temp_file(
"0 ");
922 ifstream
input(temp_filename);
929 create_temp_file(
"0b1010 ");
930 ifstream
input(temp_filename);
937 create_temp_file(
"0B11110000 ");
938 ifstream
input(temp_filename);
949 create_temp_file(
"myVariable ");
950 ifstream
input(temp_filename);
957 create_temp_file(
"_private_var ");
958 ifstream
input(temp_filename);
965 create_temp_file(
"var123 ");
966 ifstream
input(temp_filename);
973 create_temp_file(
"camelCaseIdentifier ");
974 ifstream
input(temp_filename);
981 create_temp_file(
"123invalid ");
982 ifstream
input(temp_filename);
1015 create_temp_file(
" ( hello");
1016 ifstream
input(temp_filename);
1023 create_temp_file(
" [ hello");
1024 ifstream
input(temp_filename);
1031 create_temp_file(
" function foo");
1032 ifstream
input(temp_filename);
1039 create_temp_file(
" procedure foo");
1040 ifstream
input(temp_filename);
1047 create_temp_file(
" ; next");
1048 ifstream
input(temp_filename);
1058 create_temp_file(
" , next");
1059 ifstream
input(temp_filename);
1095 create_temp_file(
"\"hello\\nworld\" ");
1096 ifstream
input(temp_filename);
1104 create_temp_file(
"\"line1\\tline2\\r\\nline3\" ");
1105 ifstream
input(temp_filename);
1108 EXPECT_EQ(result,
"line1\tline2\r\nline3");
1113 create_temp_file(
"\"he said \\\"hello\\\"\" ");
1114 ifstream
input(temp_filename);
1122 create_temp_file(
"\"path\\\\to\\\\file\" ");
1123 ifstream
input(temp_filename);
1131 create_temp_file(
"\"no closing quote");
1132 ifstream
input(temp_filename);
1143 create_temp_file(
"'a' ");
1144 ifstream
input(temp_filename);
1151 create_temp_file(
"'\\n' ");
1152 ifstream
input(temp_filename);
1159 create_temp_file(
"'\\t' ");
1160 ifstream
input(temp_filename);
1167 create_temp_file(
"'\\'' ");
1168 ifstream
input(temp_filename);
1210 create_temp_file(
"line1\nline2\nline3");
1218 create_temp_file(
"line1\nline2\nline3");
1281 create_temp_file(
"x = 42 + 3.14;");
1282 ifstream
input(temp_filename);
1303 create_temp_file(
"// comment\nx = 10 ; /* another */ y = 20 ;");
1304 ifstream
input(temp_filename);
size_t size_t int32_t * out
Iterator on the items of list.
T & get_curr() const
Return the current item.
Doubly-linked list (defined in tpl_dynList.H).
Exception class for parsing errors with location information.
static long long process_id() noexcept
void create_temp_file(const string &content)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
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.
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.
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.
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.
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.
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.
bool is_eof() const
Check if this is an end-of-file token.