Skip to content

Commit

Permalink
Merge pull request #464 from epage/tagged
Browse files Browse the repository at this point in the history
fix(parser): Switch literal impl's from `one_of` to `literal`
  • Loading branch information
epage authored Feb 7, 2024
2 parents 3373ba1 + 502a36f commit 338b15d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 61 deletions.
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 @@ where
/// }
///
/// 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 @@ where
/// # 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)
}

/// Matches a tab character `'\t'`.
Expand All @@ -275,8 +275,8 @@ where
/// }
///
/// 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 @@ where
/// # 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)
}

/// Recognizes zero or more lowercase and uppercase ASCII alphabetic characters: `'a'..='z'`, `'A'..='Z'`
Expand Down Expand Up @@ -1346,6 +1346,7 @@ where
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 @@ where
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 @@ fn recognize_float<I, E: ParserError<I>>(input: &mut I) -> PResult<<I as Stream>
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 @@ pub fn escaped<'a, I: 'a, Error, F, G, O1, O2>(
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 @@ fn streaming_escaped_internal<I, Error, F, G, O1, O2>(
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 @@ fn complete_escaped_internal<'a, I: 'a, Error, F, G, O1, O2>(
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 @@ pub fn escaped_transform<I, Error, F, G, Output>(
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 @@ fn streaming_escaped_transform_internal<I, Error, F, G, Output>(
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 @@ fn complete_escaped_transform_internal<I, Error, F, G, Output>(
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 @@ use crate::combinator::*;
#[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 @@ pub trait Parser<I, O, E> {
/// 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 @@ pub trait Parser<I, O, E> {
/// 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 @@ pub trait Parser<I, O, E> {
/// 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 @@ pub trait Parser<I, O, E> {
/// 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 @@ where
/// 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)
}
}

Expand All @@ -754,20 +755,20 @@ where
/// '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> {
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

0 comments on commit 338b15d

Please sign in to comment.