fix: handle path expressions that start with an underscore
@@ -13,6 +13,10 @@ fn peek<I>(chars: &mut Peekable<I>) -> Option<(usize, char)> where I: Iterator<I
chars.peek().map(|tup| (tup.0.clone(), tup.1.clone()))
}
+fn is_identifier_char(ch: char) -> bool {
+ ch.is_alphabetic() || ch.is_numeric() || ch == '_' || ch == '-'
+}
+
// identifier -> a-zA-Z0-9+
fn identifier<I>(ch: char, chars: &mut Peekable<I>, tokens: &mut Vec<PathToken>, path: &String)-> Result<(), String>
where I: Iterator<Item = (usize, char)> {
@@ -21,7 +25,7 @@ fn identifier<I>(ch: char, chars: &mut Peekable<I>, tokens: &mut Vec<PathToken>,
let mut next_char = peek(chars);
while next_char.is_some() {
let ch = next_char.unwrap();
- if ch.1.is_alphabetic() || ch.1.is_numeric() || ch.1 == '_' || ch.1 == '-' {
+ if is_identifier_char(ch.1) {
chars.next();
id.push(ch.1);
} else if ch.1 == '.' || ch.1 == '\'' || ch.1 == '[' {
@@ -45,7 +49,7 @@ fn path_identifier<I>(chars: &mut Peekable<I>, tokens: &mut Vec<PathToken>, path
tokens.push(PathToken::Star);
Ok(())
},
- c if c.is_alphabetic() || c.is_numeric() => {
+ c if is_identifier_char(c) => {
identifier(c, chars, tokens, path)?;
Ok(())
},
@@ -259,6 +263,9 @@ mod tests {
be_ok().value(vec![PathToken::Root, PathToken::Field(s!("user_id")),
PathToken::Field(s!("user-id"))])
);
+ expect!(parse_path_exp(s!("$._id"))).to(
+ be_ok().value(vec![PathToken::Root, PathToken::Field(s!("_id"))])
+ );
}
#[test]
