新增词法解析器库 `smcc_lex_parser`,包含基础的词法规则解析功能: - 支持字符、字符串、数字、标识符的解析 - 支持跳过注释、空白符、行尾等辅助函数 - 提供对应的单元测试用例,覆盖各类合法与非法输入情况 该模块依赖 `libcore`,并被 `smcc_lex` 模块引用以支持更上层的词法分析逻辑。
55 lines
2.2 KiB
C
55 lines
2.2 KiB
C
// test_identifier.c
|
|
#include <lex_parser.h>
|
|
#include <utest/acutest.h>
|
|
|
|
cbool check_identifier(const char *str, const char *expect, cstring_t *output) {
|
|
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);
|
|
|
|
cbool ret = lex_parse_identifier(stream, &pos, output);
|
|
if (ret && expect) {
|
|
return strcmp(output->data, expect) == 0;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
#define CHECK_IDENTIFIER_VALID(str, expect) \
|
|
do { \
|
|
cstring_t _output = cstring_new(); \
|
|
cbool ret = check_identifier(str, expect, &_output); \
|
|
TEST_CHECK(ret == true); \
|
|
TEST_CHECK(strcmp(_output.data, expect) == 0); \
|
|
cstring_free(&_output); \
|
|
} while (0)
|
|
|
|
#define CHECK_IDENTIFIER_INVALID(str) \
|
|
do { \
|
|
cstring_t _output = cstring_new(); \
|
|
cbool ret = check_identifier(str, NULL, &_output); \
|
|
TEST_CHECK(ret == false); \
|
|
cstring_free(&_output); \
|
|
} while (0)
|
|
|
|
void test_valid_identifier(void) {
|
|
TEST_CASE("valid identifiers");
|
|
CHECK_IDENTIFIER_VALID("variable", "variable");
|
|
CHECK_IDENTIFIER_VALID("my_var", "my_var");
|
|
CHECK_IDENTIFIER_VALID("_private", "_private");
|
|
CHECK_IDENTIFIER_VALID("Var123", "Var123");
|
|
CHECK_IDENTIFIER_VALID("a", "a");
|
|
}
|
|
|
|
void test_invalid_identifier(void) {
|
|
TEST_CASE("invalid identifiers");
|
|
CHECK_IDENTIFIER_INVALID("");
|
|
CHECK_IDENTIFIER_INVALID("123var");
|
|
}
|
|
|
|
TEST_LIST = {
|
|
{"test_valid_identifier", test_valid_identifier},
|
|
{"test_invalid_identifier", test_invalid_identifier},
|
|
{NULL, NULL},
|
|
}; |