refactor(lex_parser): 移除旧的词法解析器实现并更新依赖

移除了 libs/lex_parser 目录下的所有头文件和源文件,包括:
- lex_parser.h 和 lex_parser.c 核心解析功能
- 所有测试文件(test_char.c, test_identifier.c, test_number.c,
  test_skip_block_comment.c, test_skip_line.c, test_string.c)

更新了 lexer 模块的依赖配置,将 lex_parser 替换为 sstream,
同时更新了 lexer.h 中的相关包含头文件和数据结构定义,
简化了 scc_lexer_t 结构体的字段。
This commit is contained in:
zzy
2026-02-16 16:56:40 +08:00
parent 088050c903
commit 0e7dec202a
30 changed files with 1840 additions and 1979 deletions

View File

@@ -0,0 +1,326 @@
#include <scc_core.h>
#include <scc_core_ring.h>
#include <stdio.h>
#include <string.h>
#include <utest/acutest.h>
// 为测试定义简单的 token 类型(包含动态字符串)
typedef struct {
int id;
char *data;
} test_token_t;
// 定义环形缓冲区类型别名(方便使用)
typedef SCC_RING(char) char_ring_t;
typedef SCC_RING(test_token_t) token_ring_t;
/* ------------------- 字符流测试辅助 ------------------ */
static const char *test_chars = "abcdefghijklmnopqrstuvwxyz";
static size_t char_index = 0;
cbool char_fill(char *out, void *userdata) {
(void)userdata;
if (char_index < strlen(test_chars)) {
*out = test_chars[char_index++];
return true;
}
return false;
}
void reset_char_fill(void) { char_index = 0; }
/* ------------------- token 流测试辅助 ------------------ */
static int token_id = 0;
cbool token_fill(test_token_t *out, void *userdata) {
(void)userdata;
if (token_id < 10) { // 只产生 10 个 token
out->id = token_id;
out->data = (char *)scc_malloc(20);
if (!out->data)
return false;
snprintf_(out->data, 20, "token%d", token_id);
token_id++;
return true;
}
return false;
}
void reset_token_fill(void) { token_id = 0; }
void free_token(test_token_t *tok) {
if (tok->data) {
scc_free(tok->data);
tok->data = NULL;
}
}
/* ==================== 字符环形缓冲区测试 ==================== */
void test_char_ring_basic(void) {
reset_char_fill();
char_ring_t ring;
scc_ring_init(ring, 4, char_fill, 0);
char c;
cbool ok;
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'a');
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'b');
// peek
scc_ring_peek(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'c');
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'c');
// back
scc_ring_back(ring, ok);
TEST_CHECK(ok == true);
scc_ring_peek(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'c');
// consume & reset
scc_ring_consume(ring);
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'c');
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'd');
scc_ring_reset(ring);
scc_ring_peek(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'c');
scc_ring_back(ring, ok);
TEST_CHECK(ok == false); // 不能低于 head
scc_ring_free(ring);
}
void test_char_ring_full(void) {
reset_char_fill();
char_ring_t ring;
scc_ring_init(ring, 3, char_fill, 0);
char c;
cbool ok;
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // a
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // b
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // c
// 缓冲区满peek 应失败
scc_ring_peek(ring, c, ok);
TEST_CHECK(ok == false);
scc_ring_consume(ring); // 释放已读空间
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // d
TEST_CHECK(c == 'd');
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // e
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // f
scc_ring_peek(ring, c, ok);
TEST_CHECK(ok == false); // 再次满
scc_ring_free(ring);
}
void test_char_ring_eof(void) {
reset_char_fill();
char_ring_t ring;
scc_ring_init(ring, 32, char_fill, 0);
char c;
cbool ok;
for (int i = 0; i < 26; i++) {
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == test_chars[i]);
}
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == false);
scc_ring_peek(ring, c, ok);
TEST_CHECK(ok == false);
scc_ring_free(ring);
}
void test_char_ring_back_boundary(void) {
reset_char_fill();
char_ring_t ring;
scc_ring_init(ring, 4, char_fill, 0);
char c;
cbool ok;
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // a
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // b
scc_ring_back(ring, ok);
TEST_CHECK(ok == true);
scc_ring_back(ring, ok);
TEST_CHECK(ok == true);
scc_ring_back(ring, ok);
TEST_CHECK(ok == false); // 已到 head
scc_ring_peek(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'a');
scc_ring_free(ring);
}
void test_char_ring_consume_reset(void) {
reset_char_fill();
char_ring_t ring;
scc_ring_init(ring, 5, char_fill, 0);
char c;
cbool ok;
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // a
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // b
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // c
scc_ring_back(ring, ok);
TEST_CHECK(ok == true);
scc_ring_back(ring, ok);
TEST_CHECK(ok == true);
scc_ring_back(ring, ok);
TEST_CHECK(ok == true); // 此时 probe 指向 a
scc_ring_consume(ring); // head 移至 a
scc_ring_reset(ring); // probe 也移至 a
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == 'a'); // 应该返回 a
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // b
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true); // c
scc_ring_free(ring);
}
void test_char_ring_wrap(void) {
reset_char_fill();
char_ring_t ring;
scc_ring_init(ring, 3, char_fill, 0);
char c;
cbool ok;
for (int i = 0; i < 26; i++) {
scc_ring_next(ring, c, ok);
TEST_CHECK(ok == true);
TEST_CHECK(c == test_chars[i]);
scc_ring_consume(ring); // 立即消费,保持缓冲区几乎为空
}
scc_ring_peek(ring, c, ok);
TEST_CHECK(ok == false); // 无数据
scc_ring_free(ring);
}
/* ==================== token 环形缓冲区测试 ==================== */
void test_token_ring_basic(void) {
reset_token_fill();
token_ring_t ring;
scc_ring_init(ring, 3, token_fill, 0);
test_token_t tok;
cbool ok;
scc_ring_next(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 0);
free_token(&tok);
scc_ring_next(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 1);
free_token(&tok);
scc_ring_peek(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 2); // peek 不应消费
scc_ring_next(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 2);
scc_ring_back(ring, ok);
TEST_CHECK(ok == true);
scc_ring_peek(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 2);
scc_ring_next(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 2);
free_token(&tok);
scc_ring_consume(ring);
// 消费剩余 token
while (1) {
scc_ring_next(ring, tok, ok);
if (!ok)
break;
free_token(&tok);
}
scc_ring_free(ring);
}
void test_token_ring_full(void) {
reset_token_fill();
token_ring_t ring;
scc_ring_init(ring, 2, token_fill, 0);
test_token_t tok;
cbool ok;
scc_ring_next(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 0);
free_token(&tok);
scc_ring_next(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 1);
free_token(&tok);
scc_ring_peek(ring, tok, ok);
TEST_CHECK(ok == false); // 缓冲区满
scc_ring_consume(ring);
scc_ring_next(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 2);
free_token(&tok);
scc_ring_next(ring, tok, ok);
TEST_CHECK(ok == true);
TEST_CHECK(tok.id == 3);
free_token(&tok);
scc_ring_peek(ring, tok, ok);
TEST_CHECK(ok == false); // 再次满
scc_ring_free(ring);
}
/* ==================== 测试列表 ==================== */
TEST_LIST = {{"test_char_ring_basic", test_char_ring_basic},
{"test_char_ring_full", test_char_ring_full},
{"test_char_ring_eof", test_char_ring_eof},
{"test_char_ring_back_boundary", test_char_ring_back_boundary},
{"test_char_ring_consume_reset", test_char_ring_consume_reset},
{"test_char_ring_wrap", test_char_ring_wrap},
{"test_token_ring_basic", test_token_ring_basic},
{"test_token_ring_full", test_token_ring_full},
{NULL, NULL}};