From 8db5d3e5bd7c490a03752a3b3e79109c3eb2ea48 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 17 Dec 2024 14:57:31 -0600 Subject: [PATCH] refactor(path): Top-down organize the parser --- src/path/parser.rs | 74 +++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/path/parser.rs b/src/path/parser.rs index fbb8ba24..8fd02db3 100644 --- a/src/path/parser.rs +++ b/src/path/parser.rs @@ -12,42 +12,6 @@ use nom::{ use crate::path::Expression; -fn raw_ident(i: &str) -> IResult<&str, String> { - map( - is_a( - "abcdefghijklmnopqrstuvwxyz \ - ABCDEFGHIJKLMNOPQRSTUVWXYZ \ - 0123456789 \ - _-", - ), - ToString::to_string, - )(i) -} - -fn integer(i: &str) -> IResult<&str, isize> { - map_res( - delimited(space0, recognize(pair(opt(tag("-")), digit1)), space0), - FromStr::from_str, - )(i) -} - -fn ident(i: &str) -> IResult<&str, Expression> { - map(raw_ident, Expression::Identifier)(i) -} - -fn postfix<'a>(expr: Expression) -> impl FnMut(&'a str) -> IResult<&'a str, Expression> { - let e2 = expr.clone(); - let child = map(preceded(tag("."), raw_ident), move |id| { - Expression::Child(Box::new(expr.clone()), id) - }); - - let subscript = map(delimited(char('['), integer, char(']')), move |num| { - Expression::Subscript(Box::new(e2.clone()), num) - }); - - alt((child, subscript)) -} - pub(crate) fn from_str(input: &str) -> Result { match ident(input) { Ok((mut rem, mut expr)) => { @@ -73,7 +37,43 @@ pub(crate) fn from_str(input: &str) -> Result { } } -pub(crate) fn to_error_kind(e: Err>) -> ErrorKind { +fn ident(i: &str) -> IResult<&str, Expression> { + map(raw_ident, Expression::Identifier)(i) +} + +fn postfix<'a>(expr: Expression) -> impl FnMut(&'a str) -> IResult<&'a str, Expression> { + let e2 = expr.clone(); + let child = map(preceded(tag("."), raw_ident), move |id| { + Expression::Child(Box::new(expr.clone()), id) + }); + + let subscript = map(delimited(char('['), integer, char(']')), move |num| { + Expression::Subscript(Box::new(e2.clone()), num) + }); + + alt((child, subscript)) +} + +fn raw_ident(i: &str) -> IResult<&str, String> { + map( + is_a( + "abcdefghijklmnopqrstuvwxyz \ + ABCDEFGHIJKLMNOPQRSTUVWXYZ \ + 0123456789 \ + _-", + ), + ToString::to_string, + )(i) +} + +fn integer(i: &str) -> IResult<&str, isize> { + map_res( + delimited(space0, recognize(pair(opt(tag("-")), digit1)), space0), + FromStr::from_str, + )(i) +} + +fn to_error_kind(e: Err>) -> ErrorKind { match e { Err::Incomplete(_) => ErrorKind::Complete, Err::Failure(e) | Err::Error(e) => e.code,