diff --git a/Cargo.toml b/Cargo.toml index 37066f63b..d97e38e1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -104,11 +104,21 @@ name = "reborrow_fold" name = "fnmut" required-features = ["alloc"] +[[example]] +name = "custom_error" +required-features = ["alloc"] +path = "examples/custom_error.rs" + [[example]] name = "json" required-features = ["alloc"] path = "examples/json.rs" +[[example]] +name = "json_iterator" +required-features = ["alloc"] +path = "examples/json_iterator.rs" + [[example]] name = "iterator" path = "examples/iterator.rs" diff --git a/examples/custom_error.rs b/examples/custom_error.rs index 976b032e8..c1fb2d88d 100644 --- a/examples/custom_error.rs +++ b/examples/custom_error.rs @@ -21,10 +21,12 @@ impl ParseError for CustomError { } } -fn parse(input: &str) -> IResult<&str, &str, CustomError<&str>> { +pub fn parse(_input: &str) -> IResult<&str, &str, CustomError<&str>> { Err(Error(CustomError::MyError)) } +fn main() {} + #[cfg(test)] mod tests { use super::parse; diff --git a/examples/json_iterator.rs b/examples/json_iterator.rs index f0853c8da..ebc026346 100644 --- a/examples/json_iterator.rs +++ b/examples/json_iterator.rs @@ -4,23 +4,17 @@ use nom::{ branch::alt, bytes::complete::{escaped, tag, take_while}, character::complete::{alphanumeric1 as alphanumeric, char, one_of}, - combinator::{map, opt, cut}, - error::{context, ErrorKind, ParseError}, - error::{VerboseError, VerboseErrorKind}, - multi::separated_list, + combinator::{cut, map}, + error::{context, ParseError}, + multi::separated_list0, number::complete::double, - sequence::{delimited, preceded, separated_pair, terminated}, - Err, IResult, Offset, + sequence::{preceded, separated_pair, terminated}, + IResult, }; use std::collections::HashMap; -use std::str; use std::cell::Cell; - -struct Cursor<'a> { - inner: &'a str, - offset: usize, -} +use std::str; #[derive(Clone, Debug)] pub struct JsonValue<'a, 'b> { @@ -28,7 +22,7 @@ pub struct JsonValue<'a, 'b> { pub offset: &'b Cell, } -impl<'a, 'b:'a> JsonValue<'a, 'b> { +impl<'a, 'b: 'a> JsonValue<'a, 'b> { pub fn new(input: &'a str, offset: &'b Cell) -> JsonValue<'a, 'b> { JsonValue { input, offset } } @@ -49,7 +43,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> { self.offset(i); println!("-> {}", s); Some(s) - }, + } _ => None, } } @@ -61,27 +55,27 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> { self.offset(i); println!("-> {}", o); Some(o) - }, + } _ => None, } } pub fn number(&self) -> Option { println!("number()"); - match double::<_,()>(self.data()) { + match double::<_, ()>(self.data()) { Ok((i, o)) => { self.offset(i); println!("-> {}", o); Some(o) - }, + } _ => None, } } - pub fn array(&self) -> Option>> { + pub fn array(&self) -> Option>> { println!("array()"); - match tag::<_,_,()>("[")(self.data()) { + match tag::<_, _, ()>("[")(self.data()) { Err(_) => None, Ok((i, _)) => { println!("["); @@ -92,7 +86,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> { let v = self.clone(); - Some(std::iter::from_fn(move|| { + Some(std::iter::from_fn(move || { if done { return None; } @@ -103,30 +97,29 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> { match value(v.data()) { Ok((i, _)) => { v.offset(i); - }, - Err(_) => {}, + } + Err(_) => {} } } - - match tag::<_,_,()>("]")(v.data()) { + match tag::<_, _, ()>("]")(v.data()) { Ok((i, _)) => { println!("]"); v.offset(i); done = true; return None; - }, + } Err(_) => {} }; if first { first = false; } else { - match tag::<_,_,()>(",")(v.data()) { + match tag::<_, _, ()>(",")(v.data()) { Ok((i, _)) => { - println!(","); + println!(","); v.offset(i); - }, + } Err(_) => { done = true; return None; @@ -137,15 +130,14 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> { println!("-> {}", v.data()); previous = v.offset.get(); Some(v.clone()) - })) } } } - pub fn object(&self) -> Option)>> { + pub fn object(&self) -> Option)>> { println!("object()"); - match tag::<_,_,()>("{")(self.data()) { + match tag::<_, _, ()>("{")(self.data()) { Err(_) => None, Ok((i, _)) => { self.offset(i); @@ -158,7 +150,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> { let v = self.clone(); - Some(std::iter::from_fn(move|| { + Some(std::iter::from_fn(move || { if done { return None; } @@ -169,29 +161,29 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> { match value(v.data()) { Ok((i, _)) => { v.offset(i); - }, - Err(_) => {}, + } + Err(_) => {} } } - match tag::<_,_,()>("}")(v.data()) { + match tag::<_, _, ()>("}")(v.data()) { Ok((i, _)) => { println!("}}"); v.offset(i); done = true; return None; - }, + } Err(_) => {} }; if first { first = false; } else { - match tag::<_,_,()>(",")(v.data()) { + match tag::<_, _, ()>(",")(v.data()) { Ok((i, _)) => { println!(","); v.offset(i); - }, + } Err(_) => { done = true; return None; @@ -203,7 +195,7 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> { Ok((i, key)) => { v.offset(i); - match tag::<_,_,()>(":")(v.data()) { + match tag::<_, _, ()>(":")(v.data()) { Err(_) => None, Ok((i, _)) => { v.offset(i); @@ -214,10 +206,9 @@ impl<'a, 'b:'a> JsonValue<'a, 'b> { Some((key, v.clone())) } } - }, + } _ => None, } - })) } } @@ -235,47 +226,44 @@ fn parse_str<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str } fn string<'a>(i: &'a str) -> IResult<&'a str, &'a str> { - context("string", - preceded( - char('\"'), - cut(terminated( - parse_str, - char('\"') - ))))(i) + context( + "string", + preceded(char('\"'), cut(terminated(parse_str, char('\"')))), + )(i) } fn boolean<'a>(input: &'a str) -> IResult<&'a str, bool> { - alt(( - map(tag("false"), |_| false), - map(tag("true"), |_| true) - ))(input) + alt((map(tag("false"), |_| false), map(tag("true"), |_| true)))(input) } fn array<'a>(i: &'a str) -> IResult<&'a str, ()> { context( "array", - preceded(char('['), - cut(terminated( - map(separated_list(preceded(sp, char(',')), value), |_| ()), - preceded(sp, char(']')))) - ))(i) + preceded( + char('['), + cut(terminated( + map(separated_list0(preceded(sp, char(',')), value), |_| ()), + preceded(sp, char(']')), + )), + ), + )(i) } fn key_value<'a>(i: &'a str) -> IResult<&'a str, (&'a str, ())> { -separated_pair(preceded(sp, string), cut(preceded(sp, char(':'))), value)(i) + separated_pair(preceded(sp, string), cut(preceded(sp, char(':'))), value)(i) } fn hash<'a>(i: &'a str) -> IResult<&'a str, ()> { context( "map", - preceded(char('{'), - cut(terminated( - map( - separated_list(preceded(sp, char(',')), key_value), - |_| ()), - preceded(sp, char('}')), - )) - ))(i) + preceded( + char('{'), + cut(terminated( + map(separated_list0(preceded(sp, char(',')), key_value), |_| ()), + preceded(sp, char('}')), + )), + ), + )(i) } fn value<'a>(i: &'a str) -> IResult<&'a str, ()> { @@ -314,13 +302,17 @@ fn main() { let parser = JsonValue::new(data, &offset); if let Some(o) = parser.object() { - let s: HashMap<&str, &str> = o.filter(|(k,_)| *k == "users" ) - .filter_map(|(_, v)| v.object()).flatten() - .filter_map(|(user, v)| v.object().map(|o| (user, o))) - .map(|(user, o)| { - o.filter(|(k,_)| *k == "city" ) - .filter_map(move |(_, v)| v.string().map(|s| (user, s))) - }).flatten().collect(); + let s: HashMap<&str, &str> = o + .filter(|(k, _)| *k == "users") + .filter_map(|(_, v)| v.object()) + .flatten() + .filter_map(|(user, v)| v.object().map(|o| (user, o))) + .map(|(user, o)| { + o.filter(|(k, _)| *k == "city") + .filter_map(move |(_, v)| v.string().map(|s| (user, s))) + }) + .flatten() + .collect(); println!("res = {:?}", s); } diff --git a/examples/macro.rs b/examples/macro.rs deleted file mode 100644 index a5ecdfa17..000000000 --- a/examples/macro.rs +++ /dev/null @@ -1,17 +0,0 @@ -#[macro_use] -extern crate nom; - -use nom::{character::complete::digit0, number::complete::be_u32}; - - -named!(first, flat_map!(digit0, parse_to!(u32))); -named!(second, call!(be_u32)); - -named!(parser, alt!(first | second)); - -fn main() { - let data = b"1234;"; - - let res = parser(&data[..]); - println!("res: {:?}", res); -}