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

fix(parser): Switch literal impl's from one_of to literal #464

Merged
merged 2 commits into from
Feb 7, 2024
Merged
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
34 changes: 11 additions & 23 deletions assets/trace.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 19 additions & 16 deletions src/ascii/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@
/// }
///
/// assert_eq!(parser.parse_peek("\nc"), Ok(("c", '\n')));
/// assert_eq!(parser.parse_peek("\r\nc"), Err(ErrMode::Backtrack(InputError::new("\r\nc", ErrorKind::Verify))));
/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token))));
/// assert_eq!(parser.parse_peek("\r\nc"), Err(ErrMode::Backtrack(InputError::new("\r\nc", ErrorKind::Tag))));
/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag))));
/// ```
///
/// ```
Expand All @@ -245,17 +245,17 @@
/// # use winnow::Partial;
/// # use winnow::ascii::newline;
/// assert_eq!(newline::<_, InputError<_>>.parse_peek(Partial::new("\nc")), Ok((Partial::new("c"), '\n')));
/// assert_eq!(newline::<_, InputError<_>>.parse_peek(Partial::new("\r\nc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("\r\nc"), ErrorKind::Verify))));
/// assert_eq!(newline::<_, InputError<_>>.parse_peek(Partial::new("\r\nc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("\r\nc"), ErrorKind::Tag))));
/// assert_eq!(newline::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1))));
/// ```
#[inline(always)]
pub fn newline<I, Error: ParserError<I>>(input: &mut I) -> PResult<char, Error>
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
I: Compare<char>,
{
trace("newline", '\n'.map(AsChar::as_char)).parse_next(input)
trace("newline", '\n').parse_next(input)

Check warning on line 258 in src/ascii/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/ascii/mod.rs#L258

Added line #L258 was not covered by tests
}

/// Matches a tab character `'\t'`.
Expand All @@ -275,8 +275,8 @@
/// }
///
/// assert_eq!(parser.parse_peek("\tc"), Ok(("c", '\t')));
/// assert_eq!(parser.parse_peek("\r\nc"), Err(ErrMode::Backtrack(InputError::new("\r\nc", ErrorKind::Verify))));
/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token))));
/// assert_eq!(parser.parse_peek("\r\nc"), Err(ErrMode::Backtrack(InputError::new("\r\nc", ErrorKind::Tag))));
/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag))));
/// ```
///
/// ```
Expand All @@ -285,17 +285,17 @@
/// # use winnow::Partial;
/// # use winnow::ascii::tab;
/// assert_eq!(tab::<_, InputError<_>>.parse_peek(Partial::new("\tc")), Ok((Partial::new("c"), '\t')));
/// assert_eq!(tab::<_, InputError<_>>.parse_peek(Partial::new("\r\nc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("\r\nc"), ErrorKind::Verify))));
/// assert_eq!(tab::<_, InputError<_>>.parse_peek(Partial::new("\r\nc")), Err(ErrMode::Backtrack(InputError::new(Partial::new("\r\nc"), ErrorKind::Tag))));
/// assert_eq!(tab::<_, InputError<_>>.parse_peek(Partial::new("")), Err(ErrMode::Incomplete(Needed::new(1))));
/// ```
#[inline(always)]
pub fn tab<I, Error: ParserError<I>>(input: &mut I) -> PResult<char, Error>
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
I: Compare<char>,
{
trace("tab", '\t'.map(AsChar::as_char)).parse_next(input)
trace("tab", '\t').parse_next(input)

Check warning on line 298 in src/ascii/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/ascii/mod.rs#L298

Added line #L298 was not covered by tests
}

/// Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: `'a'..='z'`, `'A'..='Z'`
Expand Down Expand Up @@ -1346,6 +1346,7 @@
I: StreamIsPartial,
I: Stream,
I: Compare<Caseless<&'static str>>,
I: Compare<char>,
<I as Stream>::Slice: ParseSlice<O>,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::IterOffsets: Clone,
Expand All @@ -1367,6 +1368,7 @@
I: StreamIsPartial,
I: Stream,
I: Compare<Caseless<&'static str>>,
I: Compare<char>,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::IterOffsets: Clone,
I: AsBStr,
Expand All @@ -1393,6 +1395,7 @@
where
I: StreamIsPartial,
I: Stream,
I: Compare<char>,
<I as Stream>::Token: AsChar + Clone,
<I as Stream>::IterOffsets: Clone,
I: AsBStr,
Expand Down Expand Up @@ -1458,7 +1461,7 @@
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
I: Compare<char>,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Error: ParserError<I>,
Expand All @@ -1481,7 +1484,7 @@
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
I: Compare<char>,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Error: ParserError<I>,
Expand Down Expand Up @@ -1523,7 +1526,7 @@
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: crate::stream::AsChar + Clone,
I: Compare<char>,
F: Parser<I, O1, Error>,
G: Parser<I, O2, Error>,
Error: ParserError<I>,
Expand Down Expand Up @@ -1625,7 +1628,7 @@
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: crate::stream::AsChar + Clone,
I: Compare<char>,
Output: crate::stream::Accumulate<<I as Stream>::Slice>,
F: Parser<I, <I as Stream>::Slice, Error>,
G: Parser<I, <I as Stream>::Slice, Error>,
Expand All @@ -1649,7 +1652,7 @@
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: crate::stream::AsChar + Clone,
I: Compare<char>,
Output: crate::stream::Accumulate<<I as Stream>::Slice>,
F: Parser<I, <I as Stream>::Slice, Error>,
G: Parser<I, <I as Stream>::Slice, Error>,
Expand Down Expand Up @@ -1688,7 +1691,7 @@
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: crate::stream::AsChar + Clone,
I: Compare<char>,
Output: crate::stream::Accumulate<<I as Stream>::Slice>,
F: Parser<I, <I as Stream>::Slice, Error>,
G: Parser<I, <I as Stream>::Slice, Error>,
Expand Down
2 changes: 1 addition & 1 deletion src/combinator/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub trait Permutation<I, O, E> {
///
/// // any parses 'a', then char('a') fails on 'b',
/// // even though char('a') followed by any would succeed
/// assert_eq!(parser("ab"), Err(ErrMode::Backtrack(InputError::new("b", ErrorKind::Verify))));
/// assert_eq!(parser("ab"), Err(ErrMode::Backtrack(InputError::new("b", ErrorKind::Tag))));
/// ```
///
pub fn permutation<I: Stream, O, E: ParserError<I>, List: Permutation<I, O, E>>(
Expand Down
35 changes: 18 additions & 17 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#[cfg(feature = "unstable-recover")]
use crate::error::FromRecoverableError;
use crate::error::{AddContext, FromExternalError, IResult, PResult, ParseError, ParserError};
use crate::stream::{AsChar, Compare, Location, ParseSlice, Stream, StreamIsPartial};
use crate::stream::{Compare, Location, ParseSlice, Stream, StreamIsPartial};
#[cfg(feature = "unstable-recover")]
use crate::stream::{Recover, Recoverable};

Expand Down Expand Up @@ -256,7 +256,7 @@
/// let mut parser = separated_pair(alpha1, ',', alpha1).recognize();
///
/// assert_eq!(parser.parse_peek("abcd,efgh"), Ok(("", "abcd,efgh")));
/// assert_eq!(parser.parse_peek("abcd;"),Err(ErrMode::Backtrack(InputError::new(";", ErrorKind::Verify))));
/// assert_eq!(parser.parse_peek("abcd;"),Err(ErrMode::Backtrack(InputError::new(";", ErrorKind::Tag))));
/// # }
/// ```
#[doc(alias = "concat")]
Expand Down Expand Up @@ -295,7 +295,7 @@
/// let mut consumed_parser = separated_pair(alpha1, ',', alpha1).value(true).with_recognized();
///
/// assert_eq!(consumed_parser.parse_peek("abcd,efgh1"), Ok(("1", (true, "abcd,efgh"))));
/// assert_eq!(consumed_parser.parse_peek("abcd;"),Err(ErrMode::Backtrack(InputError::new(";", ErrorKind::Verify))));
/// assert_eq!(consumed_parser.parse_peek("abcd;"),Err(ErrMode::Backtrack(InputError::new(";", ErrorKind::Tag))));
///
/// // the second output (representing the consumed input)
/// // should be the same as that of the `recognize` parser.
Expand Down Expand Up @@ -329,7 +329,7 @@
/// let mut parser = separated_pair(alpha1.span(), ',', alpha1.span());
///
/// assert_eq!(parser.parse(Located::new("abcd,efgh")), Ok((0..4, 5..9)));
/// assert_eq!(parser.parse_peek(Located::new("abcd;")),Err(ErrMode::Backtrack(InputError::new(Located::new("abcd;").peek_slice(4).0, ErrorKind::Verify))));
/// assert_eq!(parser.parse_peek(Located::new("abcd;")),Err(ErrMode::Backtrack(InputError::new(Located::new("abcd;").peek_slice(4).0, ErrorKind::Tag))));
/// ```
#[inline(always)]
fn span(self) -> Span<Self, I, O, E>
Expand Down Expand Up @@ -369,7 +369,7 @@
/// let mut consumed_parser = separated_pair(alpha1.value(1).with_span(), ',', alpha1.value(2).with_span());
///
/// assert_eq!(consumed_parser.parse(Located::new("abcd,efgh")), Ok(((1, 0..4), (2, 5..9))));
/// assert_eq!(consumed_parser.parse_peek(Located::new("abcd;")),Err(ErrMode::Backtrack(InputError::new(Located::new("abcd;").peek_slice(4).0, ErrorKind::Verify))));
/// assert_eq!(consumed_parser.parse_peek(Located::new("abcd;")),Err(ErrMode::Backtrack(InputError::new(Located::new("abcd;").peek_slice(4).0, ErrorKind::Tag))));
///
/// // the second output (representing the consumed input)
/// // should be the same as that of the `span` parser.
Expand Down Expand Up @@ -727,19 +727,20 @@
/// b'a'.parse_next(i)
/// }
/// assert_eq!(parser.parse_peek(&b"abc"[..]), Ok((&b"bc"[..], b'a')));
/// assert_eq!(parser.parse_peek(&b" abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b" abc"[..], ErrorKind::Verify))));
/// assert_eq!(parser.parse_peek(&b"bc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"bc"[..], ErrorKind::Verify))));
/// assert_eq!(parser.parse_peek(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&b""[..], ErrorKind::Token))));
/// assert_eq!(parser.parse_peek(&b" abc"[..]), Err(ErrMode::Backtrack(InputError::new(&b" abc"[..], ErrorKind::Tag))));
/// assert_eq!(parser.parse_peek(&b"bc"[..]), Err(ErrMode::Backtrack(InputError::new(&b"bc"[..], ErrorKind::Tag))));
/// assert_eq!(parser.parse_peek(&b""[..]), Err(ErrMode::Backtrack(InputError::new(&b""[..], ErrorKind::Tag))));
/// ```
impl<I, E> Parser<I, u8, E> for u8
where
I: StreamIsPartial,
I: Stream<Token = u8>,
I: Stream,
I: Compare<u8>,
E: ParserError<I>,
{
#[inline(always)]
fn parse_next(&mut self, i: &mut I) -> PResult<u8, E> {
crate::token::one_of(*self).parse_next(i)
crate::token::literal(*self).value(*self).parse_next(i)

Check warning on line 743 in src/parser.rs

View check run for this annotation

Codecov / codecov/patch

src/parser.rs#L743

Added line #L743 was not covered by tests
}
}

Expand All @@ -754,20 +755,20 @@
/// 'a'.parse_next(i)
/// }
/// assert_eq!(parser.parse_peek("abc"), Ok(("bc", 'a')));
/// assert_eq!(parser.parse_peek(" abc"), Err(ErrMode::Backtrack(InputError::new(" abc", ErrorKind::Verify))));
/// assert_eq!(parser.parse_peek("bc"), Err(ErrMode::Backtrack(InputError::new("bc", ErrorKind::Verify))));
/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Token))));
/// assert_eq!(parser.parse_peek(" abc"), Err(ErrMode::Backtrack(InputError::new(" abc", ErrorKind::Tag))));
/// assert_eq!(parser.parse_peek("bc"), Err(ErrMode::Backtrack(InputError::new("bc", ErrorKind::Tag))));
/// assert_eq!(parser.parse_peek(""), Err(ErrMode::Backtrack(InputError::new("", ErrorKind::Tag))));
/// ```
impl<I, E> Parser<I, <I as Stream>::Token, E> for char
impl<I, E> Parser<I, char, E> for char
where
I: StreamIsPartial,
I: Stream,
<I as Stream>::Token: AsChar + Clone,
I: Compare<char>,
E: ParserError<I>,
{
#[inline(always)]
fn parse_next(&mut self, i: &mut I) -> PResult<<I as Stream>::Token, E> {
crate::token::one_of(*self).parse_next(i)
fn parse_next(&mut self, i: &mut I) -> PResult<char, E> {

Check warning on line 770 in src/parser.rs

View check run for this annotation

Codecov / codecov/patch

src/parser.rs#L770

Added line #L770 was not covered by tests
crate::token::literal(*self).value(*self).parse_next(i)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/token/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn partial_one_of_test() {

#[test]
fn char_byteslice() {
fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, u8> {
fn f(i: Partial<&[u8]>) -> IResult<Partial<&[u8]>, char> {
'c'.parse_peek(i)
}

Expand All @@ -265,12 +265,12 @@ fn char_byteslice() {
f(Partial::new(a)),
Err(ErrMode::Backtrack(error_position!(
&Partial::new(a),
ErrorKind::Verify
ErrorKind::Tag
)))
);

let b = &b"cde"[..];
assert_eq!(f(Partial::new(b)), Ok((Partial::new(&b"de"[..]), b'c')));
assert_eq!(f(Partial::new(b)), Ok((Partial::new(&b"de"[..]), 'c')));
}

#[test]
Expand All @@ -284,7 +284,7 @@ fn char_str() {
f(Partial::new(a)),
Err(ErrMode::Backtrack(error_position!(
&Partial::new(a),
ErrorKind::Verify
ErrorKind::Tag
)))
);

Expand Down
Loading