Skip to content

Commit 4303914

Browse files
authored
Merge pull request #115 from Geal/master
port to nom 5
2 parents ad29f8f + 04e85ea commit 4303914

File tree

4 files changed

+52
-45
lines changed

4 files changed

+52
-45
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ini = ["rust-ini"]
2323
[dependencies]
2424
lazy_static = "1.0"
2525
serde = "1.0.8"
26-
nom = "4.0.0"
26+
nom = "5.0.0"
2727

2828
toml = { version = "0.5", optional = true }
2929
serde_json = { version = "1.0.2", optional = true }

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub enum ConfigError {
4141
NotFound(String),
4242

4343
/// Configuration path could not be parsed.
44-
PathParse(nom::ErrorKind),
44+
PathParse(nom::error::ErrorKind),
4545

4646
/// Configuration could not be parsed from file.
4747
FileParse {

src/path/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use error::*;
2-
use nom::ErrorKind;
2+
use nom::error::ErrorKind;
33
use std::collections::HashMap;
44
use std::str::FromStr;
55
use value::{Value, ValueKind};

src/path/parser.rs

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
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+
};
411
use std::str::{from_utf8, FromStr};
512

6-
named!(raw_ident<CompleteStr, String>,
7-
map!(is_a!(
13+
fn raw_ident(i: &str) -> IResult<&str, String> {
14+
map(is_a(
815
"abcdefghijklmnopqrstuvwxyz \
916
ABCDEFGHIJKLMNOPQRSTUVWXYZ \
1017
0123456789 \
1118
_-"
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+
))
4747
}
4848

4949
pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
50-
match ident(CompleteStr(input)) {
50+
match ident(input) {
5151
Ok((mut rem, mut expr)) => {
5252
while !rem.is_empty() {
5353
match postfix(expr)(rem) {
@@ -58,7 +58,7 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
5858

5959
// Forward Incomplete and Error
6060
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);
6262
}
6363
}
6464
}
@@ -67,10 +67,17 @@ pub fn from_str(input: &str) -> Result<Expression, ErrorKind> {
6767
}
6868

6969
// 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),
7171
}
7272
}
7373

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+
7481
#[cfg(test)]
7582
mod test {
7683
use super::Expression::*;

0 commit comments

Comments
 (0)