|
1 | 1 | use super::Expression;
|
2 | 2 | use nom::{
|
3 |
| - IResult, Err, |
4 |
| - error::ErrorKind, |
5 |
| - bytes::complete::{is_a, tag}, |
6 |
| - character::complete::{char, digit1, space0}, |
7 |
| - sequence::{delimited, pair, preceded}, |
8 |
| - branch::alt, |
9 |
| - combinator::{map, map_res, opt, recognize}, |
| 3 | + branch::alt, |
| 4 | + bytes::complete::{is_a, tag}, |
| 5 | + character::complete::{char, digit1, space0}, |
| 6 | + combinator::{map, map_res, opt, recognize}, |
| 7 | + error::ErrorKind, |
| 8 | + sequence::{delimited, pair, preceded}, |
| 9 | + Err, IResult, |
10 | 10 | };
|
11 | 11 | use std::str::{from_utf8, FromStr};
|
12 | 12 |
|
13 | 13 | fn raw_ident(i: &str) -> IResult<&str, String> {
|
14 |
| - map(is_a( |
15 |
| - "abcdefghijklmnopqrstuvwxyz \ |
| 14 | + map( |
| 15 | + is_a( |
| 16 | + "abcdefghijklmnopqrstuvwxyz \ |
16 | 17 | ABCDEFGHIJKLMNOPQRSTUVWXYZ \
|
17 | 18 | 0123456789 \
|
18 |
| - _-" |
19 |
| - ), |s:&str| s.to_string())(i) |
| 19 | + _-", |
| 20 | + ), |
| 21 | + |s: &str| s.to_string(), |
| 22 | + )(i) |
20 | 23 | }
|
21 | 24 |
|
22 | 25 | fn integer(i: &str) -> IResult<&str, isize> {
|
23 |
| - map_res( |
24 |
| - delimited( |
25 |
| - space0, |
26 |
| - recognize(pair(opt(tag("-")), digit1)), |
27 |
| - space0 |
28 |
| - ), |
29 |
| - FromStr::from_str |
30 |
| - )(i) |
| 26 | + map_res( |
| 27 | + delimited(space0, recognize(pair(opt(tag("-")), digit1)), space0), |
| 28 | + FromStr::from_str, |
| 29 | + )(i) |
31 | 30 | }
|
32 | 31 |
|
33 | 32 | fn ident(i: &str) -> IResult<&str, Expression> {
|
34 |
| - map(raw_ident, Expression::Identifier)(i) |
| 33 | + map(raw_ident, Expression::Identifier)(i) |
35 | 34 | }
|
36 | 35 |
|
37 | 36 | fn postfix<'a>(expr: Expression) -> impl Fn(&'a str) -> IResult<&'a str, Expression> {
|
38 |
| - let e2 = expr.clone(); |
39 |
| - let child = map(preceded(tag("."), raw_ident), move |id| Expression::Child(Box::new(expr.clone()), id)); |
| 37 | + let e2 = expr.clone(); |
| 38 | + let child = map(preceded(tag("."), raw_ident), move |id| { |
| 39 | + Expression::Child(Box::new(expr.clone()), id) |
| 40 | + }); |
40 | 41 |
|
41 |
| - let subscript = map(delimited(char('['), integer, char(']')), move |num| Expression::Subscript(Box::new(e2.clone()), num)); |
| 42 | + let subscript = map(delimited(char('['), integer, char(']')), move |num| { |
| 43 | + Expression::Subscript(Box::new(e2.clone()), num) |
| 44 | + }); |
42 | 45 |
|
43 |
| - alt(( |
44 |
| - child, |
45 |
| - subscript |
46 |
| - )) |
| 46 | + alt((child, subscript)) |
47 | 47 | }
|
48 | 48 |
|
49 | 49 | pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
|
@@ -72,10 +72,10 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
|
72 | 72 | }
|
73 | 73 |
|
74 | 74 | pub fn to_error_kind(e: Err<(&str, ErrorKind)>) -> ErrorKind {
|
75 |
| - match e { |
76 |
| - Err::Incomplete(_) => ErrorKind::Complete, |
77 |
| - Err::Failure((_, e)) | Err::Error((_, e)) => e, |
78 |
| - } |
| 75 | + match e { |
| 76 | + Err::Incomplete(_) => ErrorKind::Complete, |
| 77 | + Err::Failure((_, e)) | Err::Error((_, e)) => e, |
| 78 | + } |
79 | 79 | }
|
80 | 80 |
|
81 | 81 | #[cfg(test)]
|
|
0 commit comments