mirror of
https://gitee.com/openharmony/third_party_rust_nom
synced 2024-11-26 17:20:29 +00:00
Ensure all examples compile (#1604)
* docs: Remove unused example * docs: Ensure all examples compile Co-authored-by: Ed Page <eopage@gmail.com>
This commit is contained in:
parent
b66ff43eff
commit
9cff115667
10
Cargo.toml
10
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"
|
||||
|
@ -21,10 +21,12 @@ impl<I> ParseError<I> for CustomError<I> {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
@ -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<usize>,
|
||||
}
|
||||
|
||||
impl<'a, 'b:'a> JsonValue<'a, 'b> {
|
||||
impl<'a, 'b: 'a> JsonValue<'a, 'b> {
|
||||
pub fn new(input: &'a str, offset: &'b Cell<usize>) -> 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<f64> {
|
||||
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<impl Iterator<Item=JsonValue<'a, 'b>>> {
|
||||
pub fn array(&self) -> Option<impl Iterator<Item = JsonValue<'a, 'b>>> {
|
||||
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<impl Iterator<Item=(&'a str, JsonValue<'a, 'b>)>> {
|
||||
pub fn object(&self) -> Option<impl Iterator<Item = (&'a str, JsonValue<'a, 'b>)>> {
|
||||
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);
|
||||
}
|
||||
|
@ -1,17 +0,0 @@
|
||||
#[macro_use]
|
||||
extern crate nom;
|
||||
|
||||
use nom::{character::complete::digit0, number::complete::be_u32};
|
||||
|
||||
|
||||
named!(first<u32>, flat_map!(digit0, parse_to!(u32)));
|
||||
named!(second<u32>, call!(be_u32));
|
||||
|
||||
named!(parser<u32>, alt!(first | second));
|
||||
|
||||
fn main() {
|
||||
let data = b"1234;";
|
||||
|
||||
let res = parser(&data[..]);
|
||||
println!("res: {:?}", res);
|
||||
}
|
Loading…
Reference in New Issue
Block a user