Sprache Part 3: Repetition (Many, AtLeastOnce, Until, Repeat, Once)
Contents
This is part of a series of posts documenting Sprache:
- Sprache Part 1: Parsing Characters
- Sprache Part 2: Parsing Strings
- Sprache Part 3: Repetition (Many, AtLeastOnce, Until, Repeat, Once)
- Sprache Part 4: Or and XOr
- Sprache Part 5: Select, Return, and Regex
- Sprache Part 6: DelimitedBy
- Sprache Part 7: ChainOperator and ChainRightOperator
- Sprache Part 8: Token, Contained, Identifier, LineTerminator
- Sprache Part 9: Positioned
- Sprache Part 10: Optional and XOptional
- Sprache Part 11: Parsing Comments
- Sprache Part 12: Ref, Named, End, Not, Except, Then, Where, Preview, Concat
This post covers the basic building blocks used to parse repeating elements - Many, AtLeastOnce, Until, Repeat, Once. There are some other methods used for parsing specific sorts of repeating elements, like DelimitedByand ChainOperator, however these helpers are built from these primitives.
Many
Parse a stream of 0 or more elements.
Parser<IEnumerable<T>> Many<T>(this Parser<T> parser)
The following example parses quoted strings:
|
|
XMany
Parse a stream of 0 or more elements, failing if any element is only partially parsed. Siganture:
Parser<IEnumerable<T>> XMany<T>(this Parser<T> parser)
The difference between XMany and Many is that XMany will fail if any of the repeating elements is only partially parsed.
|
|
In the above example Many either returns success, or an unhelpful error message if we use End. XMany however saw that the record parser partially matched, and returned the error produced by that parser. The X* methods typically give more helpful errors and are easier to debug than their unqualified counterparts.
AtLeastOnce
Parse a stream of elements with at least one item.
Parser<IEnumerable<T>> AtLeastOnce<T>(this Parser<T> parser)
|
|
XAtLeastOnce
Parse a stream elements with at least one item, however like XMany the parser fail if any element is only partially parsed.
Parser<IEnumerable<T>> XAtLeastOnce<T>(this Parser<T> parser)
See AtLeastOnce for examples, and XMany for examples of how the X* methods handle errors differently.
Until
Parses a sequence of items until a terminator is reached.
Parser<IEnumerable<T>> Until<T, U>(this Parser<T> parser, Parser<U> until)
The following example parses C-style block comments, alternatively see CommentParser for a helper for parsing both single-line and block comments.
|
|
Repeat
Parses a sequence of items until a specific number have been parsed. This method has 2 overloads:
Parser<IEnumerable<T>> Repeat<T>(this Parser<T> parser, int count)Parser<IEnumerable<T>> Repeat<T>(this Parser<T> parser, int? minimumCount, int? maximumCount)
The following parser parses a sequence of digits between 3 and 6 characters long:
|
|
Once
Parses a stream of elements with exactly one item.
Parser<IEnumerable<T>> Once<T>(this Parser<T> parser)
This does not change what inputs the supplied parser consumes, however it does change the return type of the parser into IEnumerable<T> which could make consuming the parser easier, for example:
|
|
Author Justin Pealing
LastMod 2020-03-23