Skip to content

Commit

Permalink
Merge pull request #626 from calder/ident-fix
Browse files Browse the repository at this point in the history
Allow leading underscores in idents
  • Loading branch information
zesterer committed Apr 28, 2024
2 parents ef9f20f + 8c8b82b commit 0f4ea06
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Char for char {
}

fn is_ident_start(&self) -> bool {
unicode_ident::is_xid_start(*self)
unicode_ident::is_xid_start(*self) || *self == '_'
}

fn is_ident_continue(&self) -> bool {
Expand Down Expand Up @@ -546,6 +546,26 @@ mod tests {
text::unicode::keyword(s).ignored()
}

fn test_ok<'a, P: Parser<'a, &'a str, &'a str>>(parser: P, input: &'a str) {
assert_eq!(
parser.parse(input),
ParseResult {
output: Some(input),
errs: vec![]
}
);
}

fn test_err<'a, P: Parser<'a, &'a str, &'a str>>(parser: P, input: &'a str) {
assert_eq!(
parser.parse(input),
ParseResult {
output: None,
errs: vec![EmptyErr::default()]
}
);
}

#[test]
fn keyword_good() {
make_ascii_kw_parser::<char, &str>("hello");
Expand All @@ -556,6 +576,21 @@ mod tests {
make_unicode_kw_parser::<char, &str>("你好");
}

#[test]
fn ident() {
let ident = text::ident::<&str, char, extra::Default>();
test_ok(ident, "foo");
test_ok(ident, "foo_bar");
test_ok(ident, "foo_");
test_ok(ident, "_foo");
test_ok(ident, "_");
test_ok(ident, "__");
test_ok(ident, "__init__");
test_err(ident, "");
test_err(ident, ".");
test_err(ident, "123");
}

#[test]
#[should_panic]
fn keyword_numeric() {
Expand Down

0 comments on commit 0f4ea06

Please sign in to comment.