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() +
"_" +
81 if (ch ==
'/' or ch ==
'\\' or ch ==
' ')
84 temp_filename = (std::filesystem::temp_directory_path() / base).
string();
107TEST(PutCharInBufferTest, BasicAppend)
111 char* end = buffer + 10;
117 EXPECT_STREQ(buffer,
"Hi");
120TEST(PutCharInBufferTest, PointerAdvances)
124 char* end = buffer + 10;
126 EXPECT_EQ(ptr, buffer);
128 EXPECT_EQ(ptr, buffer + 1);
130 EXPECT_EQ(ptr, buffer + 2);
133TEST(PutCharInBufferTest, FillsToCapacity)
137 char* end = buffer + 5;
139 for (
int i = 0; i < 5; ++i)
151TEST(TokenScanningTest, InitSavesPosition)
162TEST(TokenScanningTest, CloseSavesToken)
164 char buffer[20] =
"hello";
165 char* ptr = buffer + 5;
166 char* end = buffer + 20;
173TEST(TokenScanningTest, CloseAddsNullTerminator)
175 char buffer[20] =
"test";
176 char* ptr = buffer + 4;
177 char* end = buffer + 20;
186 EXPECT_EQ(buffer[4],
'\0');
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);
251 EXPECT_EQ(input.peek(),
'h');
256 create_temp_file(
"\t\thello");
257 ifstream input(temp_filename);
261 EXPECT_EQ(input.peek(),
'h');
266 create_temp_file(
"\n\nhello");
267 ifstream input(temp_filename);
271 EXPECT_EQ(input.peek(),
'h');
276 create_temp_file(
" \t\n \t\nhello");
277 ifstream input(temp_filename);
281 EXPECT_EQ(input.peek(),
'h');
286 create_temp_file(
"hello");
287 ifstream input(temp_filename);
291 EXPECT_EQ(input.peek(),
'h');
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);
348 EXPECT_THROW(
load_number(input), std::domain_error);
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);
474 EXPECT_EQ(result,
"line1\nline2");
481TEST(CommandLineTest, SingleArgument)
483 const char* argv[] = {
"program"};
486 EXPECT_EQ(result,
" program");
489TEST(CommandLineTest, MultipleArguments)
491 const char* argv[] = {
"program",
"--input",
"file.txt",
"-v"};
494 EXPECT_EQ(result,
" program --input file.txt -v");
497TEST(CommandLineTest, EmptyArguments)
501 EXPECT_EQ(result,
"");
508TEST(ResetParseStateTest, ResetsLineNumbers)
523TEST(ResetParseStateTest, ClearsToken)
541 create_temp_file(
"count 10\nname \"test file\"\nsize 42\n");
542 ifstream input(temp_filename);
546 EXPECT_EQ(key1,
"count");
551 EXPECT_EQ(key2,
"name");
552 EXPECT_EQ(val2,
"test file");
556 EXPECT_EQ(key3,
"size");
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);
629 EXPECT_EQ(result, 0);
634 create_temp_file(
"\"hello@world#test!\" ");
635 ifstream input(temp_filename);
637 EXPECT_EQ(
load_string(input),
"hello@world#test!");
642 create_temp_file(
"\"path\\to\\file\" ");
643 ifstream input(temp_filename);
651 string longstr(400,
'X');
652 create_temp_file(
"\"" + longstr +
"\" ");
653 ifstream input(temp_filename);
662TEST(BufferSizeTest, HasReasonableValue)
672TEST(SourceLocationTest, DefaultConstruction)
676 EXPECT_EQ(loc.
line, 1);
680TEST(SourceLocationTest, FullConstruction)
683 EXPECT_EQ(loc.
filename,
"test.cpp");
684 EXPECT_EQ(loc.
line, 42);
685 EXPECT_EQ(loc.
column, 15);
688TEST(SourceLocationTest, ToStringWithFilename)
691 EXPECT_EQ(loc.
to_string(),
"test.cpp:10:5");
694TEST(SourceLocationTest, ToStringWithoutFilename)
700TEST(SourceLocationTest, CurrentFactory)
707 EXPECT_EQ(loc.filename,
"file.txt");
708 EXPECT_EQ(loc.line, 100);
709 EXPECT_EQ(loc.column, 50);
718TEST(ParseErrorTest, WithLocation)
723 string msg = err.what();
724 EXPECT_NE(msg.find(
"test.cpp"), string::npos);
725 EXPECT_NE(msg.find(
"5"), string::npos);
726 EXPECT_NE(msg.find(
"unexpected token"), string::npos);
729TEST(ParseErrorTest, LocationAccessor)
744 create_temp_file(
"ABC");
745 ifstream input(temp_filename);
749 EXPECT_EQ(input.get(),
'A');
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)
771 EXPECT_EQ(input.get(),
' ');
772 EXPECT_EQ(input.get(),
'w');
781 create_temp_file(
"// this is a comment\nhello");
782 ifstream input(temp_filename);
790 EXPECT_EQ(input.peek(),
'h');
796 create_temp_file(
"/* comment */hello");
797 ifstream input(temp_filename);
805 EXPECT_EQ(input.peek(),
'h');
810 create_temp_file(
"/* line 1\n line 2\n line 3 */hello");
811 ifstream input(temp_filename);
819 EXPECT_EQ(input.peek(),
'h');
825 create_temp_file(
" // comment\n /* block */ hello");
826 ifstream input(temp_filename);
830 EXPECT_EQ(input.peek(),
'h');
835 create_temp_file(
" # python style comment\nhello");
836 ifstream input(temp_filename);
840 EXPECT_EQ(input.peek(),
'h');
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);
991TEST(KeywordTest, IsKeywordTrue)
993 vector<string> keywords = {
"if",
"while",
"for",
"return"};
1002 vector<string> keywords = {
"if",
"while",
"for",
"return"};
1005 EXPECT_FALSE(
is_keyword(
"unless", keywords));
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);
1034 EXPECT_NO_THROW(
expect(input,
"function"));
1039 create_temp_file(
" procedure foo");
1040 ifstream input(temp_filename);
1047 create_temp_file(
" ; next");
1048 ifstream input(temp_filename);
1053 EXPECT_EQ(input.peek(),
'n');
1058 create_temp_file(
" , next");
1059 ifstream input(temp_filename);
1061 EXPECT_FALSE(
try_char(input,
';'));
1064 EXPECT_EQ(input.peek(),
',');
1071TEST(EscapeTest, ProcessBasicEscapes)
1082TEST(EscapeTest, ProcessUnknownEscape)
1095 create_temp_file(
"\"hello\\nworld\" ");
1096 ifstream input(temp_filename);
1099 EXPECT_EQ(result,
"hello\nworld");
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);
1117 EXPECT_EQ(result,
"he said \"hello\"");
1122 create_temp_file(
"\"path\\\\to\\\\file\" ");
1123 ifstream input(temp_filename);
1126 EXPECT_EQ(result,
"path\\to\\file");
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);
1188 Token tok(TokenType::IDENTIFIER,
"myVar", loc);
1190 EXPECT_EQ(tok.
type, TokenType::IDENTIFIER);
1191 EXPECT_EQ(tok.
value,
"myVar");
1200 EXPECT_TRUE(eof.
is_eof());
1201 EXPECT_FALSE(
id.is_eof());
1210 create_temp_file(
"line1\nline2\nline3");
1213 EXPECT_EQ(contents,
"line1\nline2\nline3");
1218 create_temp_file(
"line1\nline2\nline3");
1221 ASSERT_EQ(lines.
size(), 3u);
1228TEST(FileUtilsTest, LoadFileContentsNotFound)
1240 ASSERT_EQ(parts.
size(), 4u);
1248TEST(StringUtilsTest, SplitStringEmpty)
1252 EXPECT_EQ(parts.
size(), 0u);
1257 EXPECT_EQ(
trim(
" hello "),
"hello");
1258 EXPECT_EQ(
trim(
"hello"),
"hello");
1259 EXPECT_EQ(
trim(
" hello"),
"hello");
1260 EXPECT_EQ(
trim(
"hello "),
"hello");
1265 EXPECT_EQ(
trim(
"\t\thello\t\t"),
"hello");
1270 EXPECT_EQ(
trim(
""),
"");
1271 EXPECT_EQ(
trim(
" "),
"");
1281 create_temp_file(
"x = 42 + 3.14;");
1282 ifstream input(temp_filename);
1290 EXPECT_EQ(intVal, 42);
1295 EXPECT_NEAR(dblVal, 3.14, 0.01);
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).
size_t size() const noexcept
Return the number of items of the list, in constant time.
Exception class for parsing errors with location information.
const SourceLocation & location() const
Get the source location of the error.
static long long process_id() noexcept
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.
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.
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.
bool is_eof() const
Check if this is an end-of-file token.
std::string value
String value/content.
SourceLocation location
Where token appeared.
TokenType type
Type of the token.