feat(core): 重构词法分析器流接口并迁移至 core 库
将 lexer_stream 抽象为 core_stream,统一运行时核心组件的输入流模型。 移除了旧的 `lexer_stream.h` 定义,并将其功能完整迁移至 `core_stream.h` 中。 更新了内存流实现以适配新的 core_stream 接口,并修复部分资源释放问题。 同时调整日志模块包含方式,增强模块间解耦能力。 此变更影响词法分析器对输入流的操作方式,所有涉及 stream 的类型与函数均已替换为 core 前缀版本。 测试用例同步更新并验证通过。
This commit is contained in:
@@ -72,11 +72,11 @@ static inline int keyword_cmp(const char* name, int len) {
|
||||
return -1; // Not a keyword.
|
||||
}
|
||||
|
||||
void lexer_init(smcc_lexer_t* lexer, lexer_stream_t* stream) {
|
||||
void lexer_init(smcc_lexer_t* lexer, core_stream_t* stream) {
|
||||
lexer->stream = stream;
|
||||
lexer->pos = (lexer_loc_t) {
|
||||
.name = stream->name,
|
||||
.name_len = stream->name_len,
|
||||
.name = cstring_as_cstr(&stream->name),
|
||||
.name_len = cstring_len(&stream->name),
|
||||
.line = 1,
|
||||
.column = 1,
|
||||
.offset = 0,
|
||||
@@ -91,14 +91,14 @@ void lexer_init(smcc_lexer_t* lexer, lexer_stream_t* stream) {
|
||||
#define set_err_token(token) ((token)->type = TOKEN_UNKNOWN)
|
||||
|
||||
static void skip_newline(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
lexer_stream_t* stream = lexer->stream;
|
||||
core_stream_t* stream = lexer->stream;
|
||||
token->type = TOKEN_LINE_COMMENT;
|
||||
|
||||
// 循环直到遇到换行符或文件结束
|
||||
while (1) {
|
||||
int ch = stream_next_char(stream);
|
||||
|
||||
if (ch == lexer_stream_eof) {
|
||||
if (ch == core_stream_eof) {
|
||||
// 到达文件末尾,直接返回
|
||||
return;
|
||||
}
|
||||
@@ -114,7 +114,7 @@ static void skip_newline(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
}
|
||||
|
||||
static void skip_block_comment(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
lexer_stream_t* stream = lexer->stream;
|
||||
core_stream_t* stream = lexer->stream;
|
||||
token->type = TOKEN_BLOCK_COMMENT;
|
||||
int ch;
|
||||
|
||||
@@ -131,7 +131,7 @@ static void skip_block_comment(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
ch = stream_next_char(stream);
|
||||
lexer_next_pos(lexer);
|
||||
|
||||
if (ch == lexer_stream_eof) {
|
||||
if (ch == core_stream_eof) {
|
||||
// 未闭合的块注释
|
||||
LEX_WARN("Unterminated block comment");
|
||||
return;
|
||||
@@ -183,11 +183,11 @@ static inline int got_slash(int peek) {
|
||||
static void parse_char(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
token->loc = lexer->pos;
|
||||
token->type = TOKEN_CHAR_LITERAL;
|
||||
lexer_stream_t *stream = lexer->stream;
|
||||
core_stream_t *stream = lexer->stream;
|
||||
stream_reset_char(stream);
|
||||
int ch = stream_peek_char(stream);
|
||||
|
||||
if (ch == lexer_stream_eof) {
|
||||
if (ch == core_stream_eof) {
|
||||
LEX_WARN("Unexpected EOF at begin");
|
||||
goto ERR;
|
||||
} else if (ch != '\'') {
|
||||
@@ -200,7 +200,7 @@ static void parse_char(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
ch = stream_next_char(stream);
|
||||
lexer_next_pos(lexer);
|
||||
|
||||
if (ch == lexer_stream_eof) {
|
||||
if (ch == core_stream_eof) {
|
||||
LEX_WARN("Unexpected EOF at middle");
|
||||
goto ERR;
|
||||
} else if (ch == '\\') {
|
||||
@@ -229,11 +229,11 @@ ERR:
|
||||
static void parse_string(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
token->loc = lexer->pos;
|
||||
token->type = TOKEN_STRING_LITERAL;
|
||||
lexer_stream_t *stream = lexer->stream;
|
||||
core_stream_t *stream = lexer->stream;
|
||||
stream_reset_char(stream);
|
||||
int ch = stream_peek_char(stream);
|
||||
|
||||
if (ch == lexer_stream_eof) {
|
||||
if (ch == core_stream_eof) {
|
||||
LEX_WARN("Unexpected EOF at begin");
|
||||
goto ERR;
|
||||
} else if (ch != '"') {
|
||||
@@ -248,7 +248,7 @@ static void parse_string(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
while (1) {
|
||||
ch = stream_peek_char(stream);
|
||||
|
||||
if (ch == lexer_stream_eof) {
|
||||
if (ch == core_stream_eof) {
|
||||
LEX_ERROR("Unexpected EOF at string literal");
|
||||
break;
|
||||
} else if (ch == '\n') {
|
||||
@@ -285,11 +285,11 @@ ERR:
|
||||
|
||||
static void parse_number(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
token->loc = lexer->pos;
|
||||
lexer_stream_t *stream = lexer->stream;
|
||||
core_stream_t *stream = lexer->stream;
|
||||
stream_reset_char(stream);
|
||||
int ch = stream_peek_char(stream);
|
||||
int base = 0;
|
||||
if (ch == lexer_stream_eof) {
|
||||
if (ch == core_stream_eof) {
|
||||
LEX_WARN("Unexpected EOF at begin");
|
||||
goto ERR;
|
||||
} else if (ch == '0') {
|
||||
@@ -325,7 +325,7 @@ static void parse_number(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
while (1) {
|
||||
ch = stream_peek_char(stream);
|
||||
|
||||
if (ch == lexer_stream_eof) {
|
||||
if (ch == core_stream_eof) {
|
||||
break;
|
||||
} else if (ch >= 'a' && ch <= 'z') {
|
||||
tmp = ch - 'a' + 10;
|
||||
@@ -356,11 +356,11 @@ ERR:
|
||||
|
||||
static void parse_line(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
token->loc = lexer->pos;
|
||||
lexer_stream_t *stream = lexer->stream;
|
||||
core_stream_t *stream = lexer->stream;
|
||||
stream_reset_char(stream);
|
||||
int ch = stream_peek_char(stream);
|
||||
|
||||
if (ch == lexer_stream_eof) {
|
||||
if (ch == core_stream_eof) {
|
||||
LEX_WARN("Unexpected EOF at begin");
|
||||
goto ERR;
|
||||
} else if (ch != '#') {
|
||||
@@ -418,7 +418,7 @@ ERR:
|
||||
void lexer_get_token(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
token->loc = lexer->pos;
|
||||
token->type = TOKEN_UNKNOWN;
|
||||
lexer_stream_t *stream = lexer->stream;
|
||||
core_stream_t *stream = lexer->stream;
|
||||
|
||||
stream_reset_char(stream);
|
||||
token_type_t type = TOKEN_UNKNOWN;
|
||||
@@ -556,7 +556,7 @@ void lexer_get_token(smcc_lexer_t* lexer, lexer_tok_t* token) {
|
||||
token->type = TOKEN_BLANK;
|
||||
goto END;
|
||||
case '\0':
|
||||
case lexer_stream_eof:
|
||||
case core_stream_eof:
|
||||
// EOF
|
||||
type = TOKEN_EOF;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user