Skip to content

Commit

Permalink
refactor(path): Top-down organize the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 17, 2024
1 parent 2153159 commit 8db5d3e
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions src/path/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Expression, ErrorKind> {
match ident(input) {
Ok((mut rem, mut expr)) => {
Expand All @@ -73,7 +37,43 @@ pub(crate) fn from_str(input: &str) -> Result<Expression, ErrorKind> {
}
}

pub(crate) fn to_error_kind(e: Err<nom::error::Error<&str>>) -> 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<nom::error::Error<&str>>) -> ErrorKind {
match e {
Err::Incomplete(_) => ErrorKind::Complete,
Err::Failure(e) | Err::Error(e) => e.code,
Expand Down

0 comments on commit 8db5d3e

Please sign in to comment.