refactor(lexer): 重构词法分析器头文件结构并优化缓冲区管理

移除了旧的lexer_stream.c实现,引入新的环形缓冲区机制来替代原有的
动态数组缓冲区。更新了词法分析器的核心数据结构,修改了token获取
相关函数的实现以支持新的缓冲区管理方式。

BREAKING CHANGE: 移除了scc_lexer_stream_t相关的API,替换为基于
环形缓冲区的新接口scc_lexer_to_ring和相关函数。

feat(lexer_token): 添加词法分析结果内存泄漏警告注释

docs: 移除预处理器模块的测试文件和相关配置
This commit is contained in:
zzy
2026-02-16 21:21:23 +08:00
parent 0e7dec202a
commit b4929be6b8
72 changed files with 119 additions and 2474 deletions

View File

@@ -1,51 +0,0 @@
#include "scc_sstream.h"
#include <stdio.h>
int main(int argc, char **argv) {
const char *filename = (argc > 1) ? argv[1] : __FILE__; // 默认读取自身
scc_sstream_t stream;
scc_sstream_ring_t *ring;
// 初始化
if (scc_sstream_init(&stream, filename, 16) != 0) {
fprintf(stderr, "Failed to initialize stream for %s\n", filename);
return 1;
}
ring = scc_sstream_ref_ring(&stream);
Assert(ring != null);
printf("Reading file: %s\n", filename);
scc_sstream_char_t elem;
cbool ok;
int char_count = 0;
int line_count = 0;
// 循环读取所有字符
while (1) {
scc_ring_next_consume(*ring, elem, ok);
if (!ok)
break; // 文件结束或错误
char_count++;
if (elem.character == '\n')
line_count++;
// 打印前 200 个字符的位置信息(避免刷屏)
if (char_count <= 200) {
printf("char[%d]: '%c' (line %zu, col %zu)\n", char_count,
elem.character == '\n' ? ' '
: elem.character, // 换行符显示为空格
elem.pos.line, elem.pos.col);
}
}
printf("\nSummary:\n");
printf(" Total characters: %d\n", char_count);
printf(" Total lines: %d\n", line_count);
// 释放资源
scc_sstream_drop_ring(ring);
scc_sstream_drop(&stream);
return 0;
}

View File

@@ -66,7 +66,7 @@ static int sstream_scan_at(scc_sstream_t *stream, scc_pos_t scan_pos,
}
// 环形缓冲区填充回调(通过 userdata 获取流对象)
static cbool fill_func(scc_sstream_char_t *out, void *userdata) {
static int fill_func(scc_sstream_char_t *out, void *userdata) {
scc_sstream_t *stream = (scc_sstream_t *)userdata;
if (stream->fill_pos.offset >= stream->len)
return false; // 已到文件尾
@@ -82,6 +82,10 @@ static cbool fill_func(scc_sstream_char_t *out, void *userdata) {
int scc_sstream_init(scc_sstream_t *stream, const char *fname, int ring_size) {
Assert(stream != null && fname != null);
scc_file_t file = scc_fopen(fname, SCC_FILE_READ);
if (file == null) {
LOG_ERROR("Failed to open file: %s", fname);
return 0;
}
usize fsize = scc_fsize(file);
if (fsize == 0) {
LOG_WARN("file size is 0");
@@ -115,7 +119,7 @@ int scc_sstream_init_by_buffer(scc_sstream_t *stream, const char *buffer,
return 0;
}
scc_sstream_ring_t *scc_sstream_ref_ring(scc_sstream_t *stream) {
scc_sstream_ring_t *scc_sstream_to_ring(scc_sstream_t *stream) {
Assert(stream != null);
stream->used++;
return &stream->ring;