Skip to content

Commit 4e51181

Browse files
committed
Disallow identifiers starting with underscores
Modified lexer logic to reject identifiers starting with an underscore as they are not supported by the default parser. Updated tests to reflect the new validation and ensure proper error handling.
1 parent 0c9810f commit 4e51181

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/core/scanner/lexer.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ impl IdentifierRecognizer {
918918
impl TokenRecognizer for IdentifierRecognizer {
919919
fn can_handle(&self, input: &dyn CharacterStream) -> bool {
920920
if let Some(ch) = input.current() {
921-
ch.is_alphabetic() || ch == '_'
921+
ch.is_alphabetic()
922922
} else {
923923
false
924924
}
@@ -944,6 +944,19 @@ impl TokenRecognizer for IdentifierRecognizer {
944944
span,
945945
));
946946
}
947+
948+
if identifier.is_empty() && ch == '_' {
949+
let pos = input.position().to_symbol_location();
950+
let span = SymbolSpan {
951+
start: pos.clone(),
952+
end: pos,
953+
};
954+
return Err(LexError::new(
955+
"Default parser does not support identifiers starting with underscore".to_owned(),
956+
span,
957+
));
958+
}
959+
947960
if ch.is_alphanumeric() || ch == '_' {
948961
identifier.push(ch);
949962
input.advance();
@@ -1575,8 +1588,6 @@ mod tests {
15751588
"with123numbers",
15761589
TokenType::Identifier("with123numbers".to_string()),
15771590
),
1578-
("_", TokenType::Identifier("_".to_string())),
1579-
("__private", TokenType::Identifier("__private".to_string())),
15801591
("true", TokenType::Literal("true".to_string())),
15811592
("false", TokenType::Literal("false".to_string())),
15821593
];
@@ -1586,6 +1597,14 @@ mod tests {
15861597
let token = lexer.next_token().unwrap().unwrap();
15871598
assert_eq!(*token.r#type(), expected, "Failed for input: {input}");
15881599
}
1600+
1601+
let test_cases = vec!["_", "__private"];
1602+
1603+
for input in test_cases {
1604+
let mut lexer = Lexer::default_for_input(input);
1605+
let token = lexer.next_token();
1606+
assert!(token.is_err());
1607+
}
15891608
}
15901609

15911610
#[test]

0 commit comments

Comments
 (0)