feat(lexer): 在换行符处理时添加流回退功能
在词法分析器中遇到换行符时,添加对 scc_probe_stream_back 的调用, 以确保流探针位置正确回退。同时修正了 probe_stream_t 接口定义中的 注释说明,明确 back 函数的作用是后退探针位置而非头指针位置。 修复了内存探针流实现中回退逻辑的边界条件判断, 现在能够正确处理探针位置的回退操作。
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user