Sprache Part 10: Optional and XOptional
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 Optional
and XOptional
which are helper methods for parsing optional elements.
Optional
Construct a parser that indicates that the given parser is optional. The returned parser will succeed on any input no matter whether the given parser succeeds or not. Signature:
Parser<IOption<T>> Optional<T>(this Parser<T> parser)
In the following example based on an assembly language parser, the label is optional.
|
|
XOptional
Exclusive version of Optional
, differs from Optional
in that the parser fails if the supplied parser partially matches.
Parser<IOption<T>> XOptional<T>(this Parser<T> parser)
In this example (which is a simplification of the above) an error is thrown if a label is partially parsed.
|
|
Author Justin Pealing
LastMod 2020-06-08