1
1
use super :: Expression ;
2
- use nom:: types:: CompleteStr ;
3
- use nom:: { digit, ErrorKind , IResult } ;
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} ,
10
+ } ;
4
11
use std:: str:: { from_utf8, FromStr } ;
5
12
6
- named ! ( raw_ident< CompleteStr , String >,
7
- map! ( is_a! (
13
+ fn raw_ident ( i : & str ) -> IResult < & str , String > {
14
+ map ( is_a (
8
15
"abcdefghijklmnopqrstuvwxyz \
9
16
ABCDEFGHIJKLMNOPQRSTUVWXYZ \
10
17
0123456789 \
11
18
_-"
12
- ) , |s: CompleteStr | {
13
- s. to_string( )
14
- } )
15
- ) ;
16
-
17
- named ! ( integer<CompleteStr , isize >,
18
- map_res!(
19
- ws!( digit) ,
20
- |s: CompleteStr | {
21
- s. parse( )
22
- }
23
- )
24
- ) ;
25
-
26
- named ! ( ident<CompleteStr , Expression >, map!( raw_ident, Expression :: Identifier ) ) ;
27
-
28
- #[ allow( cyclomatic_complexity) ]
29
- fn postfix ( expr : Expression ) -> Box < Fn ( CompleteStr ) -> IResult < CompleteStr , Expression > > {
30
- Box :: new ( move |i : CompleteStr | {
31
- alt ! (
32
- i,
33
- do_parse!( tag!( "." ) >> id: raw_ident >> ( Expression :: Child ( Box :: new( expr. clone( ) ) , id) ) )
34
- | delimited!(
35
- char !( '[' ) ,
36
- do_parse!(
37
- negative: opt!( tag!( "-" ) ) >> num: integer
38
- >> ( Expression :: Subscript (
39
- Box :: new( expr. clone( ) ) ,
40
- num * ( if negative. is_none( ) { 1 } else { -1 } ) ,
41
- ) )
42
- ) ,
43
- char !( ']' )
44
- )
45
- )
46
- } )
19
+ ) , |s : & str | s. to_string ( ) ) ( i)
20
+ }
21
+
22
+ 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)
31
+ }
32
+
33
+ fn ident ( i : & str ) -> IResult < & str , Expression > {
34
+ map ( raw_ident, Expression :: Identifier ) ( i)
35
+ }
36
+
37
+ 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) ) ;
40
+
41
+ let subscript = map ( delimited ( char ( '[' ) , integer, char ( ']' ) ) , move |num| Expression :: Subscript ( Box :: new ( e2. clone ( ) ) , num) ) ;
42
+
43
+ alt ( (
44
+ child,
45
+ subscript
46
+ ) )
47
47
}
48
48
49
49
pub fn from_str ( input : & str ) -> Result < Expression , ErrorKind > {
50
- match ident ( CompleteStr ( input) ) {
50
+ match ident ( input) {
51
51
Ok ( ( mut rem, mut expr) ) => {
52
52
while !rem. is_empty ( ) {
53
53
match postfix ( expr) ( rem) {
@@ -58,7 +58,7 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
58
58
59
59
// Forward Incomplete and Error
60
60
result => {
61
- return result. map ( |( _, o) | o) . map_err ( |e| e . into_error_kind ( ) ) ;
61
+ return result. map ( |( _, o) | o) . map_err ( to_error_kind ) ;
62
62
}
63
63
}
64
64
}
@@ -67,10 +67,17 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
67
67
}
68
68
69
69
// Forward Incomplete and Error
70
- result => result. map ( |( _, o) | o) . map_err ( |e| e . into_error_kind ( ) ) ,
70
+ result => result. map ( |( _, o) | o) . map_err ( to_error_kind ) ,
71
71
}
72
72
}
73
73
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
+ }
79
+ }
80
+
74
81
#[ cfg( test) ]
75
82
mod test {
76
83
use super :: Expression :: * ;
0 commit comments