Skip to content
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

Add not_ref() #570

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions src/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,54 @@ where
go_extra!(());
}

/// See [`Parser::not_ref`].
pub struct NotRef<A, OA> {
pub(crate) parser: A,
#[allow(dead_code)]
pub(crate) phantom: EmptyPhantom<OA>,
}

impl<A: Copy, OA> Copy for NotRef<A, OA> {}
impl<A: Clone, OA> Clone for NotRef<A, OA> {
fn clone(&self) -> Self {
Self {
parser: self.parser.clone(),
phantom: EmptyPhantom::new(),
}
}
}

impl<'a, I, E, A, OA> ParserSealed<'a, I, (), E> for NotRef<A, OA>
where
I: BorrowInput<'a>,
E: ParserExtra<'a, I>,
A: Parser<'a, I, OA, E>,
{
#[inline(always)]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, ()> {
let before = inp.save();

let alt = inp.errors.alt.take();

let result = self.parser.go::<Check>(inp);
let result_span = inp.span_since(before.offset());
inp.rewind(before);

inp.errors.alt = alt;

match result {
Ok(()) => {
let (at, found) = inp.next_ref_inner();
inp.add_alt(at, None, found.map(|f| f.into()), result_span);
Err(())
}
Err(()) => Ok(M::bind(|| ())),
}
}

go_extra!(());
}

/// See [`Parser::and_is`].
pub struct AndIs<A, B, OB> {
pub(crate) parser_a: A,
Expand Down
32 changes: 32 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,38 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>:
}
}

/// Invert the result of the contained parser, failing if it succeeds and succeeding if it fails.
/// The output of this parser is always `()`, the unit type.
///
/// This function is the borrowing equivalent of [`Parser::not`]. Where possible, it's recommended to use [`Parser::not`] instead.
///
/// # Examples
///
/// ```
/// # use chumsky::{prelude::*, error::Simple};
/// let not_dashes = any_ref::<_, extra::Err<Simple<char>>>()
/// .and_is(just('-').not_ref())
/// .map(|c| *c)
/// .repeated()
/// .collect::<String>();
///
/// let v1 = "abcdef".chars().collect::<Vec<_>>();
/// assert_eq!(not_dashes.parse(v1.as_slice()).into_result(), Ok("abcdef".to_string()));
/// let v2 = "09Qr-X-*&".chars().collect::<Vec<_>>();
/// assert!(not_dashes.parse(v2.as_slice()).has_errors());
/// let v3 = "-91024".chars().collect::<Vec<_>>();
/// assert!(not_dashes.parse(v3.as_slice()).has_errors());
/// ```
fn not_ref(self) -> NotRef<Self, O>
where
Self: Sized,
{
NotRef {
parser: self,
phantom: EmptyPhantom::new(),
}
}

/// Parse a pattern zero or more times (analog to Regex's `<PAT>*`).
///
/// Input is eagerly parsed. Be aware that the parser will accept no occurrences of the pattern too. Consider using
Expand Down