Skip to content
Draft
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
1 change: 0 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,6 @@ where
}
}

#[cfg(test)]
pub(crate) fn as_ref_at<'parse>(
&'parse mut self,
offset: I::Offset,
Expand Down
69 changes: 66 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2306,7 +2306,6 @@ where
}

/// An iterator that wraps an iterable parser. See [`IterParser::parse_iter`].
#[cfg(test)]
pub struct ParserIter<'a, 'iter, P: IterParser<'a, I, O, E>, I: Input<'a>, O, E: ParserExtra<'a, I>>
{
parser: P,
Expand All @@ -2317,7 +2316,6 @@ pub struct ParserIter<'a, 'iter, P: IterParser<'a, I, O, E>, I: Input<'a>, O, E:
phantom: EmptyPhantom<(&'a (), O)>,
}

#[cfg(test)]
impl<'a, 'iter, P, I: Input<'a>, O, E: ParserExtra<'a, I>> Iterator
for ParserIter<'a, 'iter, P, I, O, E>
where
Expand Down Expand Up @@ -2409,6 +2407,72 @@ where
}
}

/// Convert the output of this iterable parser into a stream which can be used by another parser.
///
/// # Examples
///
/// ```
/// use chumsky::{prelude::*, text::ident, input::{Stream, ValueInput}};
///
/// #[derive(Copy, Clone, Eq, PartialEq)]
/// enum Token<'a> {
/// Comma,
/// Symbol(&'a str),
/// }
///
/// fn tokenize<'a>() -> impl Parser<'a, &'a str, Token<'a>> {
/// choice((
/// just(',').to(Token::Comma),
/// ident().map(Token::Symbol),
/// ))
/// }
///
/// #[derive(PartialEq, Eq, Debug)]
/// struct SymbolList<'a> {
/// pub symbols: Vec<&'a str>,
/// }
///
/// fn list<'a, I>() -> impl Parser<'a, I, SymbolList<'a>>
/// where I: ValueInput<'a, Token=Token<'a>, Span=SimpleSpan> {
/// fn symbol<'a, I>() -> impl Parser<'a, I, &'a str>
/// where I: ValueInput<'a, Token=Token<'a>, Span=SimpleSpan> {
/// select! {
/// Token::Symbol(name) => name,
/// }
/// }
///
/// let comma = select! { Token::Comma => () };
///
/// symbol()
/// .then(comma.ignore_then(symbol()).repeated().collect::<Vec<_>>())
/// .map(|(head, tail)| SymbolList {
/// symbols: vec![head].into_iter().chain(tail.into_iter()).collect(),
/// })
/// }
///
/// let input = "a,bb,asdfe";
/// let token_stream = tokenize().repeated().stream(input).into_result().unwrap();
/// let result = list().parse(token_stream).into_result().unwrap();
/// assert_eq!(result, SymbolList { symbols: vec!["a", "bb", "asdfe"] });
/// ```
fn stream(
self,
input: I,
) -> ParseResult<input::Stream<ParserIter<'a, 'static, Self, I, O, E>>, E::Error>
where
Self: IterParser<'a, I, O, E> + Sized,
E::State: Default,
E::Context: Default,
{
let (iter, errs) = self.parse_iter(input).into_output_errors();
if iter.is_none() {
return ParseResult::new(None, errs);
}

let stream = input::Stream::from_iter(iter.unwrap());
ParseResult::new(Some(stream), errs)
}

/// Collect this iterable parser into a [`usize`], outputting the number of elements that were parsed.
///
/// This is sugar for [`.collect::<usize>()`](Self::collect).
Expand Down Expand Up @@ -2553,7 +2617,6 @@ where
///
/// Warning: Trailing errors will be ignored
// TODO: Stabilize once error handling is properly decided on
#[cfg(test)]
fn parse_iter(self, input: I) -> ParseResult<ParserIter<'a, 'static, Self, I, O, E>, E::Error>
where
Self: IterParser<'a, I, O, E> + Sized,
Expand Down
1 change: 1 addition & 0 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ where
}
}

// TODO: impl BorrowInput!
impl<'a, I: Iterator + 'a> ValueInput<'a> for Stream<I>
where
I::Item: Clone,
Expand Down