@@ -2,10 +2,10 @@ use crate::input::{InputError, MapWithInputExt};
22use crate :: parser:: combinator:: {
33 Map , MapResult , Optional , Or , RepeatArrayVec , RepeatN , RepeatVec , WithPrefix , WithSuffix ,
44} ;
5- use crate :: parser:: error:: WithErrorMsg ;
5+ use crate :: parser:: error:: { ParseError , WithErrorMsg } ;
6+ use crate :: parser:: iterator:: ParserIterator ;
67use crate :: parser:: simple:: { Constant , Eol } ;
78use crate :: parser:: then:: { Then2 , Unimplemented } ;
8- use crate :: parser:: ParseError ;
99
1010/// [`Result`] type returned by [`Parser::parse`].
1111pub type ParseResult < ' i , T > = Result < ( T , & ' i [ u8 ] ) , ( ParseError , & ' i [ u8 ] ) > ;
@@ -164,7 +164,7 @@ pub trait Parser: Sized {
164164 /// # use utils::parser::{self, Parser};
165165 /// assert_eq!(
166166 /// parser::u32()
167- /// .with_suffix(",".optional( ))
167+ /// .with_suffix(",".or(parser::eof() ))
168168 /// .repeat_n() // N = 3 is inferred
169169 /// .parse(b"12,34,56"),
170170 /// Ok(([12, 34, 56], &b""[..]))
@@ -319,7 +319,7 @@ pub trait Parser: Sized {
319319 /// assert_eq!(
320320 /// parser::u32()
321321 /// .then(parser::u32().with_prefix("x"))
322- /// .with_suffix(",".optional( ))
322+ /// .with_suffix(",".or(parser::eof() ))
323323 /// .parse_all("1x2,3x4,1234x5678")
324324 /// .unwrap(),
325325 /// vec![
@@ -360,6 +360,56 @@ pub trait Parser: Sized {
360360 . repeat ( Constant ( ( ) ) , 0 )
361361 . parse_complete ( input)
362362 }
363+
364+ /// Create an iterator which applies this parser repeatedly until the provided input is fully
365+ /// consumed.
366+ ///
367+ /// The returned iterator will lazily parse the provided input string, producing a sequence of
368+ /// [`Result`] values. Once the end of input is reached, or an error is returned, the parser
369+ /// will always return [`None`].
370+ ///
371+ /// # Examples
372+ /// ```
373+ /// # use utils::input::InputError;
374+ /// # use utils::parser::{self, Parser};
375+ /// let iterator = parser::u32()
376+ /// .with_suffix(parser::eol())
377+ /// .parse_iterator("12\n34\n56\n78");
378+ /// for item in iterator {
379+ /// println!("{}", item?);
380+ /// }
381+ /// # Ok::<(), InputError>(())
382+ /// ```
383+ ///
384+ /// ```
385+ /// # use utils::parser::{self, Parser};
386+ /// let mut iterator = parser::u32()
387+ /// .with_suffix(parser::eol())
388+ /// .parse_iterator("12\n34\nnot a integer");
389+ /// assert_eq!(iterator.next().unwrap().unwrap(), 12);
390+ /// assert_eq!(iterator.next().unwrap().unwrap(), 34);
391+ /// assert!(iterator.next().unwrap().is_err());
392+ /// assert!(iterator.next().is_none());
393+ /// ```
394+ ///
395+ /// ```
396+ /// # use utils::input::InputError;
397+ /// # use utils::parser::{self, Parser};
398+ /// let filtered = parser::u32()
399+ /// .with_suffix(parser::eol())
400+ /// .parse_iterator("11\n22\n33\n44\n55")
401+ /// .filter(|r| r.is_err() || r.as_ref().is_ok_and(|v| v % 2 == 0))
402+ /// .collect::<Result<Vec<u32>, InputError>>()?;
403+ /// assert_eq!(filtered, vec![22, 44]);
404+ /// # Ok::<(), InputError>(())
405+ /// ```
406+ fn parse_iterator ( self , input : & str ) -> ParserIterator < Self > {
407+ ParserIterator {
408+ input,
409+ remaining : input. as_bytes ( ) ,
410+ parser : self ,
411+ }
412+ }
363413}
364414
365415// Workaround to allow using methods which consume a parser in methods which take references.
@@ -382,15 +432,15 @@ impl<'a, P: Parser> Parser for ParserRef<'a, P> {
382432///
383433/// Normally used with [`with_prefix`](Parser::with_prefix)/[`with_suffix`](Parser::with_suffix).
384434impl Parser for & ' static str {
385- type Output < ' i > = Self ;
435+ type Output < ' i > = ( ) ;
386436 type Then < T : Parser > = Then2 < Self , T > ;
387437
388438 #[ inline]
389439 fn parse < ' i > ( & self , input : & ' i [ u8 ] ) -> ParseResult < ' i , Self :: Output < ' i > > {
390440 // This is faster than using strip_prefix for the common case where the string is a short
391441 // string literal known at compile time.
392442 if input. len ( ) >= self . len ( ) && self . bytes ( ) . zip ( input) . all ( |( a, & b) | a == b) {
393- Ok ( ( self , & input[ self . len ( ) ..] ) )
443+ Ok ( ( ( ) , & input[ self . len ( ) ..] ) )
394444 } else {
395445 Err ( ( ParseError :: ExpectedLiteral ( self ) , input) )
396446 }
@@ -405,13 +455,13 @@ impl Parser for &'static str {
405455///
406456/// Normally used with [`with_prefix`](Parser::with_prefix)/[`with_suffix`](Parser::with_suffix).
407457impl Parser for u8 {
408- type Output < ' i > = Self ;
458+ type Output < ' i > = ( ) ;
409459 type Then < T : Parser > = Then2 < Self , T > ;
410460
411461 #[ inline]
412462 fn parse < ' i > ( & self , input : & ' i [ u8 ] ) -> ParseResult < ' i , Self :: Output < ' i > > {
413463 if input. first ( ) == Some ( self ) {
414- Ok ( ( * self , & input[ 1 ..] ) )
464+ Ok ( ( ( ) , & input[ 1 ..] ) )
415465 } else {
416466 Err ( ( ParseError :: ExpectedByte ( * self ) , input) )
417467 }
@@ -427,6 +477,7 @@ impl<O, F: Fn(&[u8]) -> ParseResult<O>> Parser for F {
427477 type Output < ' i > = O ;
428478 type Then < T : Parser > = Then2 < Self , T > ;
429479
480+ #[ inline]
430481 fn parse < ' i > ( & self , input : & ' i [ u8 ] ) -> ParseResult < ' i , Self :: Output < ' i > > {
431482 self ( input)
432483 }
0 commit comments