fix: 修复数字和标识符区分错误

- 新增 skipInvalidLexeme 方法,用于跳过无效的标识符和数字字符
- 修改错误处理逻辑,遇到词法错误时调用 skipInvalidLexeme 方法
This commit is contained in:
Luke 2025-07-16 23:16:12 +08:00
parent f23e15339c
commit 93a553ea93

View File

@ -87,7 +87,7 @@ public class LexerEngine {
errors.add(new LexicalError(
absPath, le.getLine(), le.getColumn(), le.getReason()
));
context.advance(); // 跳过问题字符
skipInvalidLexeme();
}
handled = true;
break;
@ -97,6 +97,20 @@ public class LexerEngine {
}
tokens.add(Token.eof(context.getLine()));
}
/**
* 跳过当前位置起连续的标识符 / 数字 / 下划线 / 字符
* <p>这样可以把诸如 {@code 1abc} 的残余 {@code abc}{@code _}
* {@code .999} 等一次性忽略避免后续被误识别为新的 token</p>
*/
private void skipInvalidLexeme() {
while (!context.isAtEnd()) {
char c = context.peek();
if (Character.isWhitespace(c)) break; // 空白 / 换行
if (!Character.isLetterOrDigit(c)
&& c != '_' && c != '.') break; // 符号分隔
context.advance(); // 否则继续吞掉
}
}
/**
* 目前包含三条规则: <br>