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

Commit 880f8dc

Browse files
committed
优化注释处理逻辑,确保在字符串和反引号字符串内不处理注释,提升解析器的准确性和鲁棒性。
1 parent cd20623 commit 880f8dc

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

src/parser/lexer.rs

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ pub fn remove_comments(source: &str) -> String {
1212
let mut in_backtick_string = false; // 标记是否在反引号字符串内
1313
let mut escape = false; // 标记是否是转义字符
1414
let mut i = 0;
15-
15+
1616
let chars: Vec<char> = source.chars().collect();
17-
17+
1818
while i < chars.len() {
1919
// 处理双引号字符串
2020
if in_string {
@@ -31,14 +31,14 @@ pub fn remove_comments(source: &str) -> String {
3131
}
3232
i += 1;
3333
continue;
34-
} else if chars[i] == '"' && !in_backtick_string {
35-
// 字符串开始
34+
} else if chars[i] == '"' && !in_backtick_string && !in_single_line_comment && multi_line_comment_depth == 0 {
35+
// 字符串开始(只有在非注释状态下才处理)
3636
in_string = true;
3737
result.push(chars[i]);
3838
i += 1;
3939
continue;
4040
}
41-
41+
4242
// 处理反引号字符串
4343
if in_backtick_string {
4444
result.push(chars[i]);
@@ -48,44 +48,62 @@ pub fn remove_comments(source: &str) -> String {
4848
}
4949
i += 1;
5050
continue;
51-
} else if chars[i] == '`' {
52-
// 反引号字符串开始
51+
} else if chars[i] == '`' && !in_single_line_comment && multi_line_comment_depth == 0 {
52+
// 反引号字符串开始(只有在非注释状态下才处理)
5353
in_backtick_string = true;
5454
result.push(chars[i]);
5555
i += 1;
5656
continue;
5757
}
58-
59-
// 处理注释
60-
if i + 1 < chars.len() && chars[i] == '/' && chars[i + 1] == '/' && multi_line_comment_depth == 0 {
61-
// 单行注释开始(仅当不在多行注释中时)
62-
in_single_line_comment = true;
63-
i += 2;
64-
} else if i + 1 < chars.len() && chars[i] == '/' && chars[i + 1] == '!' && !in_single_line_comment {
65-
// 多行注释开始
66-
multi_line_comment_depth += 1;
67-
i += 2;
68-
} else if in_single_line_comment && chars[i] == '\n' {
69-
// 单行注释结束
70-
in_single_line_comment = false;
71-
result.push(chars[i]);
72-
i += 1;
73-
} else if i + 1 < chars.len() && chars[i] == '!' && chars[i + 1] == '/' && !in_single_line_comment {
74-
// 多行注释结束
75-
if multi_line_comment_depth > 0 {
76-
multi_line_comment_depth -= 1;
58+
59+
// 处理注释(只有在非字符串状态下才处理)
60+
if !in_string && !in_backtick_string {
61+
if i + 1 < chars.len() && chars[i] == '/' && chars[i + 1] == '/' && multi_line_comment_depth == 0 {
62+
// 单行注释开始(仅当不在多行注释中时)
63+
in_single_line_comment = true;
64+
i += 2;
65+
continue;
66+
} else if i + 1 < chars.len() && chars[i] == '/' && chars[i + 1] == '!' && !in_single_line_comment {
67+
// 多行注释开始 /!
68+
multi_line_comment_depth += 1;
69+
i += 2;
70+
continue;
71+
} else if i + 1 < chars.len() && chars[i] == '/' && chars[i + 1] == '*' && !in_single_line_comment {
72+
// 多行注释开始 /* (作为 /! 的别名)
73+
multi_line_comment_depth += 1;
74+
i += 2;
75+
continue;
76+
} else if in_single_line_comment && chars[i] == '\n' {
77+
// 单行注释结束
78+
in_single_line_comment = false;
79+
result.push(chars[i]);
80+
i += 1;
81+
continue;
82+
} else if i + 1 < chars.len() && chars[i] == '!' && chars[i + 1] == '/' && !in_single_line_comment {
83+
// 多行注释结束 !/
84+
if multi_line_comment_depth > 0 {
85+
multi_line_comment_depth -= 1;
86+
}
87+
i += 2;
88+
continue;
89+
} else if i + 1 < chars.len() && chars[i] == '*' && chars[i + 1] == '/' && !in_single_line_comment {
90+
// 多行注释结束 */ (作为 !/ 的别名)
91+
if multi_line_comment_depth > 0 {
92+
multi_line_comment_depth -= 1;
93+
}
94+
i += 2;
95+
continue;
7796
}
78-
i += 2;
79-
} else if !in_single_line_comment && multi_line_comment_depth == 0 {
97+
}
98+
99+
if !in_single_line_comment && multi_line_comment_depth == 0 {
80100
// 非注释内容
81101
result.push(chars[i]);
82-
i += 1;
83-
} else {
84-
// 在注释内,跳过
85-
i += 1;
86102
}
103+
// 在注释内,跳过(不添加到result)
104+
i += 1;
87105
}
88-
106+
89107
result
90108
}
91109

0 commit comments

Comments
 (0)