Skip to content

Commit 8db5d3e

Browse files
committed
refactor(path): Top-down organize the parser
1 parent 2153159 commit 8db5d3e

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

src/path/parser.rs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,6 @@ use nom::{
1212

1313
use crate::path::Expression;
1414

15-
fn raw_ident(i: &str) -> IResult<&str, String> {
16-
map(
17-
is_a(
18-
"abcdefghijklmnopqrstuvwxyz \
19-
ABCDEFGHIJKLMNOPQRSTUVWXYZ \
20-
0123456789 \
21-
_-",
22-
),
23-
ToString::to_string,
24-
)(i)
25-
}
26-
27-
fn integer(i: &str) -> IResult<&str, isize> {
28-
map_res(
29-
delimited(space0, recognize(pair(opt(tag("-")), digit1)), space0),
30-
FromStr::from_str,
31-
)(i)
32-
}
33-
34-
fn ident(i: &str) -> IResult<&str, Expression> {
35-
map(raw_ident, Expression::Identifier)(i)
36-
}
37-
38-
fn postfix<'a>(expr: Expression) -> impl FnMut(&'a str) -> IResult<&'a str, Expression> {
39-
let e2 = expr.clone();
40-
let child = map(preceded(tag("."), raw_ident), move |id| {
41-
Expression::Child(Box::new(expr.clone()), id)
42-
});
43-
44-
let subscript = map(delimited(char('['), integer, char(']')), move |num| {
45-
Expression::Subscript(Box::new(e2.clone()), num)
46-
});
47-
48-
alt((child, subscript))
49-
}
50-
5115
pub(crate) fn from_str(input: &str) -> Result<Expression, ErrorKind> {
5216
match ident(input) {
5317
Ok((mut rem, mut expr)) => {
@@ -73,7 +37,43 @@ pub(crate) fn from_str(input: &str) -> Result<Expression, ErrorKind> {
7337
}
7438
}
7539

76-
pub(crate) fn to_error_kind(e: Err<nom::error::Error<&str>>) -> ErrorKind {
40+
fn ident(i: &str) -> IResult<&str, Expression> {
41+
map(raw_ident, Expression::Identifier)(i)
42+
}
43+
44+
fn postfix<'a>(expr: Expression) -> impl FnMut(&'a str) -> IResult<&'a str, Expression> {
45+
let e2 = expr.clone();
46+
let child = map(preceded(tag("."), raw_ident), move |id| {
47+
Expression::Child(Box::new(expr.clone()), id)
48+
});
49+
50+
let subscript = map(delimited(char('['), integer, char(']')), move |num| {
51+
Expression::Subscript(Box::new(e2.clone()), num)
52+
});
53+
54+
alt((child, subscript))
55+
}
56+
57+
fn raw_ident(i: &str) -> IResult<&str, String> {
58+
map(
59+
is_a(
60+
"abcdefghijklmnopqrstuvwxyz \
61+
ABCDEFGHIJKLMNOPQRSTUVWXYZ \
62+
0123456789 \
63+
_-",
64+
),
65+
ToString::to_string,
66+
)(i)
67+
}
68+
69+
fn integer(i: &str) -> IResult<&str, isize> {
70+
map_res(
71+
delimited(space0, recognize(pair(opt(tag("-")), digit1)), space0),
72+
FromStr::from_str,
73+
)(i)
74+
}
75+
76+
fn to_error_kind(e: Err<nom::error::Error<&str>>) -> ErrorKind {
7777
match e {
7878
Err::Incomplete(_) => ErrorKind::Complete,
7979
Err::Failure(e) | Err::Error(e) => e.code,

0 commit comments

Comments
 (0)