-
Notifications
You must be signed in to change notification settings - Fork 52
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
feat: Add output_stream
/with_output_stream
to Parser
#233
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
|
||
use crate::combinator::*; | ||
use crate::error::{ContextError, FromExternalError, IResult, ParseError}; | ||
use crate::stream::{AsChar, Compare, Location, Offset, ParseSlice, Stream, StreamIsPartial}; | ||
use crate::stream::{ | ||
AsChar, Compare, Location, Offset, ParseSlice, Stream, StreamIsPartial, UpdateSlice, | ||
}; | ||
|
||
/// Core trait for parsing | ||
/// | ||
|
@@ -187,19 +189,21 @@ pub trait Parser<I, O, E> { | |
|
||
/// Produce the consumed input as produced value. | ||
/// | ||
/// The produced value is of type `Stream::Slice`. If you're looking for an alternative that | ||
/// returns the original input `Stream`'s type, use [`output_stream`](Parser::output_stream) | ||
/// instead. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult, Parser}; | ||
/// use winnow::character::{alpha1}; | ||
/// # use winnow::{error::{ErrMode, Error, ErrorKind}, IResult, Parser}; | ||
/// use winnow::character::alpha1; | ||
/// use winnow::sequence::separated_pair; | ||
/// # fn main() { | ||
/// | ||
/// let mut parser = separated_pair(alpha1, ',', alpha1).recognize(); | ||
/// | ||
/// assert_eq!(parser.parse_next("abcd,efgh"), Ok(("", "abcd,efgh"))); | ||
/// assert_eq!(parser.parse_next("abcd;"),Err(ErrMode::Backtrack(Error::new(";", ErrorKind::Verify)))); | ||
/// # } | ||
/// ``` | ||
#[doc(alias = "concat")] | ||
fn recognize(self) -> Recognize<Self, I, O, E> | ||
|
@@ -212,29 +216,29 @@ pub trait Parser<I, O, E> { | |
|
||
/// Produce the consumed input with the output | ||
/// | ||
/// Functions similarly to [recognize][Parser::recognize] except it | ||
/// returns the parser output as well. | ||
/// Functions similarly to [`recognize`][Parser::recognize] except it returns the parser output | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated changes will need to be reverted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can do that, but I thought it would make sense to add the missing backticks around There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If so, that should be a separate commit |
||
/// as well. | ||
/// | ||
/// This can be useful especially in cases where the output is not the same type | ||
/// as the input, or the input is a user defined type. | ||
/// | ||
/// The consumed input's value is of type `Stream::Slice`. If you're looking for an alternative | ||
/// that returns the original input `Stream`'s type, use | ||
/// [`with_output_stream`](Parser::with_output_stream) instead. | ||
/// | ||
/// Returned tuple is of the format `(produced output, consumed input)`. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use winnow::prelude::*; | ||
/// # use winnow::{error::ErrMode,error::ErrorKind, error::Error, IResult}; | ||
/// use winnow::character::{alpha1}; | ||
/// use winnow::bytes::tag; | ||
/// # use winnow::{error::{ErrMode, Error, ErrorKind}, IResult, Parser}; | ||
/// use winnow::character::alpha1; | ||
/// use winnow::sequence::separated_pair; | ||
/// | ||
/// fn inner_parser(input: &str) -> IResult<&str, bool> { | ||
/// "1234".value(true).parse_next(input) | ||
/// } | ||
/// | ||
/// # fn main() { | ||
/// | ||
/// let mut consumed_parser = separated_pair(alpha1, ',', alpha1).value(true).with_recognized(); | ||
/// | ||
/// assert_eq!(consumed_parser.parse_next("abcd,efgh1"), Ok(("1", (true, "abcd,efgh")))); | ||
|
@@ -247,7 +251,6 @@ pub trait Parser<I, O, E> { | |
/// | ||
/// assert_eq!(recognize_parser.parse_next("1234"), consumed_parser.parse_next("1234")); | ||
/// assert_eq!(recognize_parser.parse_next("abcd"), consumed_parser.parse_next("abcd")); | ||
/// # } | ||
/// ``` | ||
#[doc(alias = "consumed")] | ||
fn with_recognized(self) -> WithRecognized<Self, I, O, E> | ||
|
@@ -258,6 +261,99 @@ pub trait Parser<I, O, E> { | |
WithRecognized::new(self) | ||
} | ||
|
||
/// Produce the consumed input as produced value. | ||
/// | ||
/// The produced value is of the same type as the input `Stream`. If you're looking for an | ||
/// alternative that returns `Stream::Slice`, use [`recognize`](Parser::recognize) instead. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use winnow::{error::{ErrMode, Error, ErrorKind}, IResult, Parser}; | ||
/// use winnow::character::alpha1; | ||
/// use winnow::sequence::separated_pair; | ||
/// use winnow::stream::BStr; | ||
/// | ||
/// let mut parser = separated_pair(alpha1, ',', alpha1).output_stream(); | ||
/// | ||
/// assert_eq!( | ||
/// parser.parse_next(BStr::new("abcd,efgh")), | ||
/// Ok((BStr::new(""), BStr::new("abcd,efgh"))), | ||
/// ); | ||
/// assert_eq!( | ||
/// parser.parse_next(BStr::new("abcd;")), | ||
/// Err(ErrMode::Backtrack(Error::new(BStr::new(";"), ErrorKind::Verify))), | ||
/// ); | ||
/// ``` | ||
fn output_stream(self) -> OutputStream<Self, I, O, E> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll have a look. |
||
where | ||
Self: core::marker::Sized, | ||
I: UpdateSlice, | ||
{ | ||
OutputStream::new(self) | ||
} | ||
|
||
/// Produce the consumed input with the output. | ||
/// | ||
/// Functions similarly to [`output_stream`][Parser::output_stream] except it returns the | ||
/// parser output as well. | ||
/// | ||
/// This can be useful especially in cases where the output is not the same type as the input. | ||
/// | ||
/// The consumed input's value is of the same type as the input `Stream`. If you're looking for | ||
/// an alternative that returns `Stream::Slice`, use | ||
/// [`with_recognized`](Parser::with_recognized) instead. | ||
/// | ||
/// Returned tuple is of the format `(produced output, consumed input)`. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use winnow::{error::{ErrMode, Error, ErrorKind}, IResult, Parser}; | ||
/// use winnow::character::alpha1; | ||
/// use winnow::sequence::separated_pair; | ||
/// use winnow::stream::BStr; | ||
/// | ||
/// fn inner_parser(input: &BStr) -> IResult<&BStr, bool> { | ||
/// "1234".value(true).parse_next(input) | ||
/// } | ||
/// | ||
/// let mut consumed_parser = separated_pair(alpha1, ',', alpha1) | ||
/// .value(true) | ||
/// .with_output_stream(); | ||
/// | ||
/// assert_eq!( | ||
/// consumed_parser.parse_next(BStr::new("abcd,efgh1")), | ||
/// Ok((BStr::new("1"), (true, BStr::new("abcd,efgh")))), | ||
/// ); | ||
/// assert_eq!( | ||
/// consumed_parser.parse_next(BStr::new("abcd;")), | ||
/// Err(ErrMode::Backtrack(Error::new(BStr::new(";"), ErrorKind::Verify))), | ||
/// ); | ||
/// | ||
/// // The second output (representing the consumed input) should be the same as that of the | ||
/// // `output_stream` parser. | ||
/// let mut output_stream_parser = inner_parser.output_stream(); | ||
/// let mut consumed_parser = inner_parser.with_output_stream() | ||
/// .map(|(output, output_stream)| output_stream); | ||
/// | ||
/// assert_eq!( | ||
/// output_stream_parser.parse_next(BStr::new("1234")), | ||
/// consumed_parser.parse_next(BStr::new("1234")), | ||
/// ); | ||
/// assert_eq!( | ||
/// output_stream_parser.parse_next(BStr::new("abcd")), | ||
/// consumed_parser.parse_next(BStr::new("abcd")), | ||
/// ); | ||
/// ``` | ||
fn with_output_stream(self) -> WithOutputStream<Self, I, O, E> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered trying these out on an extension trait in your crate first to explore their usage? I am considering doing 0.5 in the next couple months, so we do have room to change things soon if we aren't thrilled with it. I'm just exploring (and reminding myself) what the options are for these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't consider that yet, no. But it's a great idea and I'll do this to see if the additional methods are really that useful as I imagine. Will report back once I did that. I already played around with |
||
where | ||
Self: core::marker::Sized, | ||
I: UpdateSlice, | ||
{ | ||
WithOutputStream::new(self) | ||
} | ||
|
||
/// Produce the location of the consumed input as produced value. | ||
/// | ||
/// # Example | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the streams be marked complete?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm missing something, but how would I mark a stream as complete?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/winnow-rs/winnow/blob/main/src/binary/mod.rs#L2487