diff options
Diffstat (limited to 'src/path/parser.rs')
-rw-r--r-- | src/path/parser.rs | 62 |
1 files changed, 34 insertions, 28 deletions
diff --git a/src/path/parser.rs b/src/path/parser.rs index 7629193..1ecd6e7 100644 --- a/src/path/parser.rs +++ b/src/path/parser.rs @@ -1,40 +1,37 @@ -use nom::*; +use nom::{ErrorKind, digit, IResult}; +use nom::types::CompleteStr; use std::str::{FromStr, from_utf8}; use super::Expression; -named!(ident_<String>, - map!( - map_res!(is_a!( - "abcdefghijklmnopqrstuvwxyz \ - ABCDEFGHIJKLMNOPQRSTUVWXYZ \ - 0123456789 \ - _-" - ), from_utf8), - |s: &str| { - s.to_string() - } - ) +named!(raw_ident<CompleteStr, String>, + map!(is_a!( + "abcdefghijklmnopqrstuvwxyz \ + ABCDEFGHIJKLMNOPQRSTUVWXYZ \ + 0123456789 \ + _-" + ), |s: CompleteStr| { + s.to_string() + }) ); -named!(integer <isize>, +named!(integer<CompleteStr, isize>, map_res!( - map_res!( - ws!(digit), - from_utf8 - ), - FromStr::from_str + ws!(digit), + |s: CompleteStr| { + s.parse() + } ) ); -named!(ident<Expression>, map!(ident_, Expression::Identifier)); +named!(ident<CompleteStr, Expression>, map!(raw_ident, Expression::Identifier)); #[allow(cyclomatic_complexity)] -fn postfix(expr: Expression) -> Box<Fn(&[u8]) -> IResult<&[u8], Expression>> { - Box::new(move |i: &[u8]| { +fn postfix(expr: Expression) -> Box<Fn(CompleteStr) -> IResult<CompleteStr, Expression>> { + Box::new(move |i: CompleteStr| { alt!(i, do_parse!( tag!(".") >> - id: ident_ >> + id: raw_ident >> (Expression::Child(Box::new(expr.clone()), id)) ) | delimited!( @@ -54,18 +51,18 @@ fn postfix(expr: Expression) -> Box<Fn(&[u8]) -> IResult<&[u8], Expression>> { } pub fn from_str(input: &str) -> Result<Expression, ErrorKind> { - match ident(input.as_bytes()) { - IResult::Done(mut rem, mut expr) => { + match ident(CompleteStr(input)) { + Ok((mut rem, mut expr)) => { while !rem.is_empty() { match postfix(expr)(rem) { - IResult::Done(rem_, expr_) => { + Ok((rem_, expr_)) => { rem = rem_; expr = expr_; } // Forward Incomplete and Error result => { - return result.to_result().map_err(|e| e.into_error_kind()); + return result.map(|(_,o)| o).map_err(|e| e.into_error_kind()); } } } @@ -74,7 +71,7 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> { } // Forward Incomplete and Error - result => result.to_result().map_err(|e| e.into_error_kind()), + result => result.map(|(_,o)| o).map_err(|e| e.into_error_kind()), } } @@ -101,6 +98,15 @@ mod test { let expected = Child(Box::new(Identifier("abcd".into())), "efgh".into()); assert_eq!(parsed, expected); + + let parsed: Expression = from_str("abcd.efgh.ijkl").unwrap(); + let expected = Child(Box::new( + Child(Box::new( + Identifier("abcd".into()) + ), "efgh".into()) + ), "ijkl".into()); + + assert_eq!(parsed, expected); } #[test] |