@@ -3,8 +3,8 @@ use crate::parser::combinator::{
33 Map , MapResult , Optional , Or , RepeatN , RepeatVec , WithPrefix , WithSuffix ,
44} ;
55use crate :: parser:: error:: WithErrorMsg ;
6- use crate :: parser:: simple:: Eol ;
7- use crate :: parser:: then:: Then2 ;
6+ use crate :: parser:: simple:: { Constant , Eol } ;
7+ use crate :: parser:: then:: { Then2 , Unimplemented } ;
88use crate :: parser:: ParseError ;
99
1010/// [`Result`] type returned by [`Parser::parse`].
@@ -182,42 +182,18 @@ pub trait Parser: Sized {
182182 /// If the number of items is constant and known in advance, prefer [`repeat_n`](Self::repeat_n)
183183 /// as it avoids allocating.
184184 ///
185- /// See also [`repeat_min`](Self::repeat_min), which ensures at least N items are parsed.
186- ///
187- /// # Examples
188- /// ```
189- /// # use utils::parser::{self, Parser};
190- /// assert_eq!(
191- /// parser::u32()
192- /// .with_suffix(",".optional())
193- /// .repeat()
194- /// .parse(b"12,34,56,78"),
195- /// Ok((vec![12, 34, 56, 78], &b""[..]))
196- /// );
197- /// ```
198- fn repeat ( self ) -> RepeatVec < Self > {
199- RepeatVec {
200- parser : self ,
201- min_elements : 0 ,
202- }
203- }
204-
205- /// Repeat this parser at least N times, returning a [`Vec`].
206- ///
207- /// See also [`repeat`](Self::repeat).
208- ///
209185 /// # Examples
210186 /// ```
211187 /// # use utils::parser::{self, Parser};
212188 /// let parser = parser::u32()
213- /// .with_suffix(",".optional())
214- /// .repeat_min(3);
189+ /// .repeat(",", 3);
215190 /// assert_eq!(parser.parse(b"12,34,56,78"), Ok((vec![12, 34, 56, 78], &b""[..])));
216191 /// assert!(parser.parse(b"12,34").is_err());
217192 /// ```
218- fn repeat_min ( self , min_elements : usize ) -> RepeatVec < Self > {
193+ fn repeat < S : Parser > ( self , separator : S , min_elements : usize ) -> RepeatVec < Self , S > {
219194 RepeatVec {
220195 parser : self ,
196+ separator,
221197 min_elements,
222198 }
223199 }
@@ -287,8 +263,25 @@ pub trait Parser: Sized {
287263 }
288264 }
289265
266+ /// Apply this parser once, checking the provided input is fully consumed.
267+ ///
268+ /// # Examples
269+ /// ```
270+ /// # use utils::parser::{self, Parser};
271+ /// assert_eq!(parser::u32().parse_complete("1234").unwrap(), 1234);
272+ /// assert!(parser::u32().parse_complete("1234abc").is_err());
273+ /// ```
274+ fn parse_complete < ' i > ( & self , input : & ' i str ) -> Result < Self :: Output < ' i > , InputError > {
275+ match self . parse ( input. as_bytes ( ) ) . map_with_input ( input) ? {
276+ ( v, [ ] ) => Ok ( v) ,
277+ ( _, remaining) => Err ( InputError :: new ( input, remaining, "expected end of input" ) ) ,
278+ }
279+ }
280+
290281 /// Apply this parser repeatedly until the provided input is fully consumed.
291282 ///
283+ /// Equivalent to `parser.repeat(parser::noop(), 0).parse_complete(input)`.
284+ ///
292285 /// # Examples
293286 /// ```
294287 /// # use utils::parser::{self, Parser};
@@ -306,20 +299,9 @@ pub trait Parser: Sized {
306299 /// );
307300 /// ```
308301 fn parse_all < ' i > ( & self , input : & ' i str ) -> Result < Vec < Self :: Output < ' i > > , InputError > {
309- let mut results = Vec :: new ( ) ;
310- let mut remaining = input. as_bytes ( ) ;
311- while !remaining. is_empty ( ) {
312- let ( v, new_remaining) = self . parse ( remaining) . map_with_input ( input) ?;
313-
314- if results. is_empty ( ) {
315- let length = remaining. len ( ) - new_remaining. len ( ) ;
316- results. reserve ( 2 + ( ( remaining. len ( ) / length) * 6 / 5 ) ) ;
317- }
318-
319- remaining = new_remaining;
320- results. push ( v) ;
321- }
322- Ok ( results)
302+ ParserRef ( self )
303+ . repeat ( Constant ( ( ) ) , 0 )
304+ . parse_complete ( input)
323305 }
324306
325307 /// Similar to [`parse_all`](Self::parse_all) but expects a newline after each item.
@@ -342,44 +324,26 @@ pub trait Parser: Sized {
342324 /// );
343325 /// ```
344326 fn parse_lines < ' i > ( & self , input : & ' i str ) -> Result < Vec < Self :: Output < ' i > > , InputError > {
345- // Can't use WithSuffix as it consumes the input parser
346- struct LineParser < ' a , P > ( & ' a P ) ;
347- impl < ' a , P : Parser > Parser for LineParser < ' a , P > {
348- type Output < ' i > = P :: Output < ' i > ;
349- type Then < T : Parser > = Then2 < Self , T > ;
327+ ParserRef ( self )
328+ . with_suffix ( Eol ( ) )
329+ . repeat ( Constant ( ( ) ) , 0 )
330+ . parse_complete ( input)
331+ }
332+ }
350333
351- #[ inline]
352- fn parse < ' i > ( & self , input : & ' i [ u8 ] ) -> ParseResult < ' i , Self :: Output < ' i > > {
353- match self . 0 . parse ( input) {
354- Ok ( ( v, remaining) ) => match Eol ( ) . parse ( remaining) {
355- Ok ( ( ( ) , remaining) ) => Ok ( ( v, remaining) ) ,
356- Err ( e) => Err ( e) ,
357- } ,
358- Err ( e) => Err ( e) ,
359- }
360- }
334+ // Workaround to allow using methods which consume a parser in methods which take references.
335+ struct ParserRef < ' a , P > ( & ' a P ) ;
336+ impl < ' a , P : Parser > Parser for ParserRef < ' a , P > {
337+ type Output < ' i > = P :: Output < ' i > ;
338+ type Then < T : Parser > = Unimplemented ;
361339
362- fn then < T : Parser > ( self , _: T ) -> Self :: Then < T > {
363- unreachable ! ( ) ;
364- }
365- }
366-
367- LineParser ( self ) . parse_all ( input)
340+ #[ inline]
341+ fn parse < ' i > ( & self , input : & ' i [ u8 ] ) -> ParseResult < ' i , Self :: Output < ' i > > {
342+ self . 0 . parse ( input)
368343 }
369344
370- /// Apply this parser once, checking the provided input is fully consumed.
371- ///
372- /// # Examples
373- /// ```
374- /// # use utils::parser::{self, Parser};
375- /// assert_eq!(parser::u32().parse_complete("1234").unwrap(), 1234);
376- /// assert!(parser::u32().parse_complete("1234abc").is_err());
377- /// ```
378- fn parse_complete < ' i > ( & self , input : & ' i str ) -> Result < Self :: Output < ' i > , InputError > {
379- match self . parse ( input. as_bytes ( ) ) . map_with_input ( input) ? {
380- ( v, [ ] ) => Ok ( v) ,
381- ( _, remaining) => Err ( InputError :: new ( input, remaining, "expected end of input" ) ) ,
382- }
345+ fn then < T : Parser > ( self , _: T ) -> Self :: Then < T > {
346+ unreachable ! ( ) ;
383347 }
384348}
385349
0 commit comments