mirror of
https://gitee.com/openharmony/third_party_rust_nom
synced 2024-11-26 17:20:29 +00:00
Remove Clone trait bound from fold parsers
Update documentation
This commit is contained in:
parent
5a65658a95
commit
18cc1d18b9
@ -145,7 +145,7 @@ where
|
||||
// Our parser function– parses a single string fragment
|
||||
parse_fragment,
|
||||
// Our init value, an empty string
|
||||
String::new(),
|
||||
String::new,
|
||||
// Our folding function. For each fragment, append the fragment to the
|
||||
// string.
|
||||
|mut string, fragment| {
|
||||
|
@ -596,7 +596,7 @@ where
|
||||
/// the results using a given function and initial value.
|
||||
/// # Arguments
|
||||
/// * `f` The parser to apply.
|
||||
/// * `init` The initial value.
|
||||
/// * `init` A function returning the initial value.
|
||||
/// * `g` The function that combines a result of `f` with
|
||||
/// the current accumulator.
|
||||
/// ```rust
|
||||
@ -608,7 +608,7 @@ where
|
||||
/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
|
||||
/// fold_many0(
|
||||
/// tag("abc"),
|
||||
/// Vec::new(),
|
||||
/// Vec::new,
|
||||
/// |mut acc: Vec<_>, item| {
|
||||
/// acc.push(item);
|
||||
/// acc
|
||||
@ -621,20 +621,20 @@ where
|
||||
/// assert_eq!(parser("123123"), Ok(("123123", vec![])));
|
||||
/// assert_eq!(parser(""), Ok(("", vec![])));
|
||||
/// ```
|
||||
pub fn fold_many0<I, O, E, F, G, R>(
|
||||
pub fn fold_many0<I, O, E, F, G, H, R>(
|
||||
mut f: F,
|
||||
init: R,
|
||||
mut init: H,
|
||||
mut g: G,
|
||||
) -> impl FnMut(I) -> IResult<I, R, E>
|
||||
where
|
||||
I: Clone + PartialEq,
|
||||
F: Parser<I, O, E>,
|
||||
G: FnMut(R, O) -> R,
|
||||
H: FnMut() -> R,
|
||||
E: ParseError<I>,
|
||||
R: Clone,
|
||||
{
|
||||
move |i: I| {
|
||||
let mut res = init.clone();
|
||||
let mut res = init();
|
||||
let mut input = i;
|
||||
|
||||
loop {
|
||||
@ -666,7 +666,7 @@ where
|
||||
/// once.
|
||||
/// # Arguments
|
||||
/// * `f` The parser to apply.
|
||||
/// * `init` The initial value.
|
||||
/// * `init` A function returning the initial value.
|
||||
/// * `g` The function that combines a result of `f` with
|
||||
/// the current accumulator.
|
||||
/// ```rust
|
||||
@ -678,7 +678,7 @@ where
|
||||
/// fn parser(s: &str) -> IResult<&str, Vec<&str>> {
|
||||
/// fold_many1(
|
||||
/// tag("abc"),
|
||||
/// Vec::new(),
|
||||
/// Vec::new,
|
||||
/// |mut acc: Vec<_>, item| {
|
||||
/// acc.push(item);
|
||||
/// acc
|
||||
@ -691,21 +691,21 @@ where
|
||||
/// assert_eq!(parser("123123"), Err(Err::Error(Error::new("123123", ErrorKind::Many1))));
|
||||
/// assert_eq!(parser(""), Err(Err::Error(Error::new("", ErrorKind::Many1))));
|
||||
/// ```
|
||||
pub fn fold_many1<I, O, E, F, G, R>(
|
||||
pub fn fold_many1<I, O, E, F, G, H, R>(
|
||||
mut f: F,
|
||||
init: R,
|
||||
mut init: H,
|
||||
mut g: G,
|
||||
) -> impl FnMut(I) -> IResult<I, R, E>
|
||||
where
|
||||
I: Clone + PartialEq,
|
||||
F: Parser<I, O, E>,
|
||||
G: FnMut(R, O) -> R,
|
||||
H: FnMut() -> R,
|
||||
E: ParseError<I>,
|
||||
R: Clone,
|
||||
{
|
||||
move |i: I| {
|
||||
let _i = i.clone();
|
||||
let init = init.clone();
|
||||
let init = init();
|
||||
match f.parse(_i) {
|
||||
Err(Err::Error(_)) => Err(Err::Error(E::from_error_kind(i, ErrorKind::Many1))),
|
||||
Err(e) => Err(e),
|
||||
@ -745,7 +745,7 @@ where
|
||||
/// * `m` The minimum number of iterations.
|
||||
/// * `n` The maximum number of iterations.
|
||||
/// * `f` The parser to apply.
|
||||
/// * `init` The initial value.
|
||||
/// * `init` A function returning the initial value.
|
||||
/// * `g` The function that combines a result of `f` with
|
||||
/// the current accumulator.
|
||||
/// ```rust
|
||||
@ -759,7 +759,7 @@ where
|
||||
/// 0,
|
||||
/// 2,
|
||||
/// tag("abc"),
|
||||
/// Vec::new(),
|
||||
/// Vec::new,
|
||||
/// |mut acc: Vec<_>, item| {
|
||||
/// acc.push(item);
|
||||
/// acc
|
||||
@ -773,22 +773,22 @@ where
|
||||
/// assert_eq!(parser(""), Ok(("", vec![])));
|
||||
/// assert_eq!(parser("abcabcabc"), Ok(("abc", vec!["abc", "abc"])));
|
||||
/// ```
|
||||
pub fn fold_many_m_n<I, O, E, F, G, R>(
|
||||
pub fn fold_many_m_n<I, O, E, F, G, H, R>(
|
||||
min: usize,
|
||||
max: usize,
|
||||
mut parse: F,
|
||||
init: R,
|
||||
mut init: H,
|
||||
mut fold: G,
|
||||
) -> impl FnMut(I) -> IResult<I, R, E>
|
||||
where
|
||||
I: Clone + PartialEq,
|
||||
F: Parser<I, O, E>,
|
||||
G: FnMut(R, O) -> R,
|
||||
H: FnMut() -> R,
|
||||
E: ParseError<I>,
|
||||
R: Clone,
|
||||
{
|
||||
move |mut input: I| {
|
||||
let mut acc = init.clone();
|
||||
let mut acc = init();
|
||||
for count in 0..max {
|
||||
match parse.parse(input.clone()) {
|
||||
Ok((tail, value)) => {
|
||||
|
@ -422,10 +422,10 @@ fn fold_many0_test() {
|
||||
acc
|
||||
}
|
||||
fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
||||
fold_many0(tag("abcd"), Vec::new(), fold_into_vec)(i)
|
||||
fold_many0(tag("abcd"), Vec::new, fold_into_vec)(i)
|
||||
}
|
||||
fn multi_empty(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
||||
fold_many0(tag(""), Vec::new(), fold_into_vec)(i)
|
||||
fold_many0(tag(""), Vec::new, fold_into_vec)(i)
|
||||
}
|
||||
|
||||
assert_eq!(multi(&b"abcdef"[..]), Ok((&b"ef"[..], vec![&b"abcd"[..]])));
|
||||
@ -454,7 +454,7 @@ fn fold_many1_test() {
|
||||
acc
|
||||
}
|
||||
fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
||||
fold_many1(tag("abcd"), Vec::new(), fold_into_vec)(i)
|
||||
fold_many1(tag("abcd"), Vec::new, fold_into_vec)(i)
|
||||
}
|
||||
|
||||
let a = &b"abcdef"[..];
|
||||
@ -481,7 +481,7 @@ fn fold_many_m_n_test() {
|
||||
acc
|
||||
}
|
||||
fn multi(i: &[u8]) -> IResult<&[u8], Vec<&[u8]>> {
|
||||
fold_many_m_n(2, 4, tag("Abcd"), Vec::new(), fold_into_vec)(i)
|
||||
fold_many_m_n(2, 4, tag("Abcd"), Vec::new, fold_into_vec)(i)
|
||||
}
|
||||
|
||||
let a = &b"Abcdef"[..];
|
||||
|
@ -37,7 +37,7 @@ fn term(i: &str) -> IResult<&str, i64> {
|
||||
|
||||
fold_many0(
|
||||
pair(alt((char('*'), char('/'))), factor),
|
||||
init,
|
||||
move || init,
|
||||
|acc, (op, val): (char, i64)| {
|
||||
if op == '*' {
|
||||
acc * val
|
||||
@ -53,7 +53,7 @@ fn expr(i: &str) -> IResult<&str, i64> {
|
||||
|
||||
fold_many0(
|
||||
pair(alt((char('+'), char('-'))), term),
|
||||
init,
|
||||
move || init,
|
||||
|acc, (op, val): (char, i64)| {
|
||||
if op == '+' {
|
||||
acc + val
|
||||
|
@ -82,7 +82,7 @@ fn character(input: &str) -> IResult<&str, char> {
|
||||
fn string(input: &str) -> IResult<&str, String> {
|
||||
delimited(
|
||||
char('"'),
|
||||
fold_many0(character, String::new(), |mut string, c| {
|
||||
fold_many0(character, String::new, |mut string, c| {
|
||||
string.push(c);
|
||||
string
|
||||
}),
|
||||
|
@ -23,7 +23,7 @@ fn atom<'a>(_tomb: &'a mut ()) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], Stri
|
||||
fn list<'a>(i: &'a [u8], tomb: &'a mut ()) -> IResult<&'a [u8], String> {
|
||||
delimited(
|
||||
char('('),
|
||||
fold_many0(atom(tomb), String::new(), |acc: String, next: String| {
|
||||
fold_many0(atom(tomb), String::new, |acc: String, next: String| {
|
||||
acc + next.as_str()
|
||||
}),
|
||||
char(')'),
|
||||
|
Loading…
Reference in New Issue
Block a user