Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit fd32578

Browse files
committed
在词法分析器中增强数字解析,支持科学计数法的处理,提升了对数字格式的识别能力。
1 parent 32255e4 commit fd32578

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/parser/lexer.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,17 @@ pub fn tokenize(source: &str, debug: bool) -> Vec<String> {
257257
continue;
258258
}
259259

260-
// 检查数字
261-
if c.is_digit(10) || (c == '.' && i + 1 < chars.len() && chars[i + 1].is_digit(10)) {
260+
// 检查数字(包括科学计数法)
261+
if c.is_digit(10) || (c == '.' && i + 1 < chars.len() && chars[i + 1].is_digit(10)) || (c == 'e' || c == 'E') {
262262
let mut number = String::new();
263263
let mut has_dot = c == '.';
264-
264+
265265
if has_dot {
266266
number.push('.');
267267
i += 1;
268268
}
269-
269+
270+
// 解析整数部分和小数部分
270271
while i < chars.len() && (chars[i].is_digit(10) || (chars[i] == '.' && !has_dot)) {
271272
if chars[i] == '.' {
272273
// 检查是否是范围操作符
@@ -278,6 +279,25 @@ pub fn tokenize(source: &str, debug: bool) -> Vec<String> {
278279
number.push(chars[i]);
279280
i += 1;
280281
}
282+
283+
// 检查科学计数法 (e 或 E)
284+
if i < chars.len() && (chars[i] == 'e' || chars[i] == 'E') {
285+
number.push(chars[i]);
286+
i += 1;
287+
288+
// 检查可选的正负号
289+
if i < chars.len() && (chars[i] == '+' || chars[i] == '-') {
290+
number.push(chars[i]);
291+
i += 1;
292+
}
293+
294+
// 解析指数部分
295+
while i < chars.len() && chars[i].is_digit(10) {
296+
number.push(chars[i]);
297+
i += 1;
298+
}
299+
}
300+
281301
tokens.push(number);
282302
continue;
283303
}

0 commit comments

Comments
 (0)