Skip to content

Expose the Either parser #1821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 18 additions & 66 deletions benchmarks/benches/json.rs
Original file line number Diff line number Diff line change
@@ -5,14 +5,13 @@
use nom::{
branch::alt,
bytes::{tag, take},
character::{anychar, char, multispace0, none_of},
combinator::{map, map_opt, map_res, value, verify},
character::{char, multispace0, none_of},
combinator::{map, map_opt, map_res, success, value, verify},
error::{Error, ErrorKind, FromExternalError, ParseError},
multi::{fold, separated_list0},
number::double,
number::recognize_float,
number::{double, recognize_float},
sequence::{delimited, preceded, separated_pair},
Check, Complete, Emit, IResult, Mode, OutputM, Parser,
Check, Complete, Either, Emit, IResult, OutputM, Parser,
};
use nom_language::error::VerboseError;

@@ -68,69 +67,22 @@
'a,
E: ParseError<&'a str> + FromExternalError<&'a str, ParseIntError> + FromExternalError<&'a str, ()>,
>() -> impl Parser<&'a str, Output = char, Error = E> {
Character { e: PhantomData }
/*let (input, c) = none_of("\"")(input)?;
if c == '\\' {
alt((
map_res(anychar, |c| {
Ok(match c {
'"' | '\\' | '/' => c,
'b' => '\x08',
'f' => '\x0C',
'n' => '\n',
'r' => '\r',
't' => '\t',
_ => return Err(()),
})
}),
preceded(char('u'), unicode_escape()),
))
.parse(input)
} else {
Ok((input, c))
}*/
}

struct Character<E> {
e: PhantomData<E>,
}

impl<'a, E> Parser<&'a str> for Character<E>
where
E: ParseError<&'a str>
+ FromExternalError<&'a str, ParseIntError>
+ FromExternalError<&'a str, ()>,
{
type Output = char;

type Error = E;

fn process<OM: nom::OutputMode>(
&mut self,
input: &'a str,
) -> nom::PResult<OM, &'a str, Self::Output, Self::Error> {
let (input, c): (&str, char) =
none_of("\"").process::<OutputM<Emit, OM::Error, OM::Incomplete>>(input)?;
if c == '\\' {
alt((
map_res(anychar, |c| {
Ok(match c {
'"' | '\\' | '/' => c,
'b' => '\x08',
'f' => '\x0C',
'n' => '\n',
'r' => '\r',
't' => '\t',
_ => return Err(()),
})
}),
preceded(char('u'), unicode_escape()),
))
.process::<OM>(input)
none_of("\"").flat_map(|c| {
if c != '\\' {
Either::Left(success(c))

Check warning on line 72 in benchmarks/benches/json.rs

Codecov / codecov/patch

benchmarks/benches/json.rs#L70-L72

Added lines #L70 - L72 were not covered by tests
} else {
Ok((input, OM::Output::bind(|| c)))
Either::Right(alt((
value('\\', tag("\\")),
value('\"', tag("\"")),
value('\n', tag("n")),
value('\r', tag("r")),
value('\t', tag("t")),
value('\x08', tag("b")),
value('\x0C', tag("f")),
preceded(char('u'), unicode_escape()),

Check warning on line 82 in benchmarks/benches/json.rs

Codecov / codecov/patch

benchmarks/benches/json.rs#L74-L82

Added lines #L74 - L82 were not covered by tests
)))
}
}
})
}

fn string<
4 changes: 3 additions & 1 deletion src/internal.rs
Original file line number Diff line number Diff line change
@@ -781,8 +781,10 @@ impl<
}

/// Alternate between two Parser implementations with the same result type.
pub(crate) enum Either<F, G> {
pub enum Either<F, G> {
/// Left parser alternative
Left(F),
/// Right parser alternative
Right(G),
}


Unchanged files with check annotations Beta

enum Operator<P1, P2, P3, Q: Ord + Copy> {
Prefix(P1, Q),
Postfix(P2, Q),
Binary(P3, Q, Assoc),

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (nightly)

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (stable)

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (stable, --features "std")

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (stable, --no-default-features --features "alloc")

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (stable, --no-default-features)

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (nightly, --no-default-features)

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (beta)

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Bench

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Bench

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (nightly, --no-default-features)

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (nightly)

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (beta)

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Bench

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Bench

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (stable)

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (stable, --features "std")

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (stable, --no-default-features --features "alloc")

field `2` is never read

Check warning on line 45 in nom-language/src/precedence/mod.rs

GitHub Actions / Test (stable, --no-default-features)

field `2` is never read
}
impl<P1, P2, P3, Q> Operator<P1, P2, P3, Q>
#![allow(clippy::doc_markdown)]
#![deny(missing_docs)]
#[cfg(feature = "alloc")]
#[macro_use]

Check warning on line 383 in src/lib.rs

GitHub Actions / Test (stable, --no-default-features --features "alloc")

unused `#[macro_use]` import

Check warning on line 383 in src/lib.rs

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

unused `#[macro_use]` import

Check warning on line 383 in src/lib.rs

GitHub Actions / Test (nightly, --no-default-features --features "alloc")

unused `#[macro_use]` import

Check warning on line 383 in src/lib.rs

GitHub Actions / Test (stable, --no-default-features --features "alloc")

unused `#[macro_use]` import
extern crate alloc;
#[cfg(doctest)]
extern crate doc_comment;