From 84cff78b8609cbab47e647f3d6494393aac387b8 Mon Sep 17 00:00:00 2001 From: zzy <2450266535@qq.com> Date: Sun, 11 Jan 2026 14:20:54 +0800 Subject: [PATCH] =?UTF-8?q?feat(lexer):=20=E5=9C=A8=E6=8D=A2=E8=A1=8C?= =?UTF-8?q?=E7=AC=A6=E5=A4=84=E7=90=86=E6=97=B6=E6=B7=BB=E5=8A=A0=E6=B5=81?= =?UTF-8?q?=E5=9B=9E=E9=80=80=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在词法分析器中遇到换行符时,添加对 scc_probe_stream_back 的调用, 以确保流探针位置正确回退。同时修正了 probe_stream_t 接口定义中的 注释说明,明确 back 函数的作用是后退探针位置而非头指针位置。 修复了内存探针流实现中回退逻辑的边界条件判断, 现在能够正确处理探针位置的回退操作。 --- libs/lexer/src/lexer.c | 1 + runtime/scc_core/include/scc_core_stream.h | 6 +++--- runtime/scc_core/src/stream.c | 9 +++++---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/libs/lexer/src/lexer.c b/libs/lexer/src/lexer.c index 96f4ab7..a6c4c8f 100644 --- a/libs/lexer/src/lexer.c +++ b/libs/lexer/src/lexer.c @@ -331,6 +331,7 @@ void scc_lexer_get_token(scc_lexer_t *lexer, scc_lexer_tok_t *token) { break; case '\r': case '\n': + scc_probe_stream_back(stream); scc_lex_parse_skip_endline(stream, &lexer->pos); scc_probe_stream_sync(stream); token->type = SCC_TOK_BLANK; diff --git a/runtime/scc_core/include/scc_core_stream.h b/runtime/scc_core/include/scc_core_stream.h index ccf145d..31db64f 100644 --- a/runtime/scc_core/include/scc_core_stream.h +++ b/runtime/scc_core/include/scc_core_stream.h @@ -30,15 +30,15 @@ struct scc_probe_stream { /// @brief 移动探针位置并返回字符 int (*next)(scc_probe_stream_t *stream); + /// @brief 回退一个字符(单次后退,探针位置后退一步) + cbool (*back)(scc_probe_stream_t *stream); + /// @brief 移动头指针到探针位置 void (*sync)(scc_probe_stream_t *stream); /// @brief 重置探针位置到头指针位置 void (*reset)(scc_probe_stream_t *stream); - /// @brief 回退一个字符(单次后退,头指针后退一步) - cbool (*back)(scc_probe_stream_t *stream); - /// @brief 读取指定数量的字符到缓冲区 usize (*read_buf)(scc_probe_stream_t *stream, char *buffer, usize count); diff --git a/runtime/scc_core/src/stream.c b/runtime/scc_core/src/stream.c index 571895a..a2cd64d 100644 --- a/runtime/scc_core/src/stream.c +++ b/runtime/scc_core/src/stream.c @@ -59,12 +59,13 @@ static cbool mem_probe_stream_back(scc_probe_stream_t *_stream) { Assert(_stream != null); scc_mem_probe_stream_t *stream = (scc_mem_probe_stream_t *)_stream; - // 只能回退一个字符,且不能回退到探针位置之前 - if (stream->curr_pos == 0 || stream->curr_pos <= stream->probe_pos) { + // 只能回退一个字符 + if (stream->probe_pos == 0) + return false; + if (stream->curr_pos + 1 > stream->probe_pos) return false; - } - stream->curr_pos--; + stream->probe_pos--; return true; }