Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support underscore in variable names #6

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions libs/bart_derive/src/parsbart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,36 @@ struct CloseScopeTag {
pub name: Name,
}

fn identifier(input: &str) -> IResult<&str, &str> {
let input_length = input.input_len();
if input_length == 0 {
return IResult::Incomplete(Needed::Unknown);
}

for (idx, item) in input.iter_indices() {
if !(item.is_alphanum() || item == '_') {
if idx == 0 {
return IResult::Error(error_position!(ErrorKind::AlphaNumeric, input))
} else {
return IResult::Done(input.slice(idx..), input.slice(0..idx))
}
}
}
IResult::Done(input.slice(input_length..), input)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is written with two-space indents. Mind converting to four-space to match the surrounding code?


named!(name(&str) -> Name,
chain!(
dots: many0!(tag!(".")) ~
name:
opt!(
recognize!(
pair!(
alphanumeric,
identifier,
many0!(
pair!(
tag!("."),
alphanumeric
identifier
)
)
)
Expand Down Expand Up @@ -208,4 +226,18 @@ mod test {
])
);
}

#[test]
fn it_accepts_underscore_in_identifiers() {
assert_eq!(
parse_str("Hello {{your_name}}!").unwrap(),
Sequence(vec![
Sequence(vec![
Literal("Hello ".to_owned()),
Interpolation(Name { dots: 0, name: Some("your_name".to_owned()) })
]),
Literal("!".to_owned())
])
);
}
}