新增词法解析器库 `smcc_lex_parser`,包含基础的词法规则解析功能: - 支持字符、字符串、数字、标识符的解析 - 支持跳过注释、空白符、行尾等辅助函数 - 提供对应的单元测试用例,覆盖各类合法与非法输入情况 该模块依赖 `libcore`,并被 `smcc_lex` 模块引用以支持更上层的词法分析逻辑。
49 lines
1.3 KiB
C
49 lines
1.3 KiB
C
// test_skip_line.c
|
|
#include <lex_parser.h>
|
|
#include <utest/acutest.h>
|
|
|
|
void check_skip_line(const char *str, const char *expect_remaining) {
|
|
log_set_level(&__default_logger_root, 0);
|
|
core_pos_t pos = core_pos_init();
|
|
core_mem_stream_t mem_stream;
|
|
core_stream_t *stream =
|
|
core_mem_stream_init(&mem_stream, str, smcc_strlen(str), false);
|
|
|
|
lex_parse_skip_line(stream, &pos);
|
|
|
|
// Check remaining content
|
|
char buffer[256] = {0};
|
|
int i = 0;
|
|
int ch;
|
|
while ((ch = core_stream_next_char(stream)) != core_stream_eof && i < 255) {
|
|
buffer[i++] = (char)ch;
|
|
}
|
|
|
|
if (expect_remaining) {
|
|
TEST_CHECK(strcmp(buffer, expect_remaining) == 0);
|
|
}
|
|
}
|
|
|
|
void test_simple_line_comment(void) {
|
|
TEST_CASE("simple line comments");
|
|
check_skip_line("// comment\n", "");
|
|
check_skip_line("// comment\nint x;", "int x;");
|
|
}
|
|
|
|
void test_crlf_line_comment(void) {
|
|
TEST_CASE("CRLF line comments");
|
|
check_skip_line("// comment\r\n", "");
|
|
check_skip_line("// comment\r\nint x;", "int x;");
|
|
}
|
|
|
|
void test_eof_line_comment(void) {
|
|
TEST_CASE("EOF line comments");
|
|
check_skip_line("// comment", "");
|
|
}
|
|
|
|
TEST_LIST = {
|
|
{"test_simple_line_comment", test_simple_line_comment},
|
|
{"test_crlf_line_comment", test_crlf_line_comment},
|
|
{"test_eof_line_comment", test_eof_line_comment},
|
|
{NULL, NULL},
|
|
}; |