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; }