Sprache Part 1: Parsing Characters
Contents
Sprache is a C# parser combinator library. Its a really well-written library but it suffers from a lack of documentation, meaning the only way to figure out how to use it is to hunt for blog posts and examples, or to start digging through the source code. I’ve chosen the latter option.
This post will be part of a series picking apart and documenting each method in Sprache with examples in XUnit test syntax. This isn’t an introductory series, the best place to start is this introductory article if you are completely new to 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
To start with, this post covers all the methods for parsing single characters.
Char
Parse a single character c. This method has two overloads:
Parser<char> Char(char c)
Parser<char> Char(Predicate<char> predicate, string description)
The first parses only the supplied character, the second parses any character which matches the predicate and also accepts a string which names the parser (see the Parser.Named
method). The overload accepting a predicate is the building block for most of the remainder of the character parsing methods.
|
|
Chars
Parse a single character from the supplied list. This method has two overloads depending on whether the list of characters shoudl be supplied as a character array or a string:
Parser<char> Chars(params char[] c)
Parser<char> Chars(string c)
|
|
CharExcept
Parses any single character except for the one(s) supplied. This method has 4 overloads:
Parser<char> CharExcept(char c)
Parser<char> CharExcept(IEnumerable<char> c)
Parser<char> CharExcept(string c)
Parser<char> CharExcept(Predicate<char> predicate, string description)
When using the overload that accepts a predicate, the description is of the character that should not be matched:
|
|
IgnoreCase
Parses a single character ignoring case.
Parser<char> IgnoreCase(char c)
|
|
WhiteSpace
Parse a single whitespace character, according to char.IsWhiteSpace
.
|
|
Digit
Parse a single, according to char.IsDigit
.
|
|
Numeric
Parses a single numeric character, according to char.IsNumber
.
|
|
Letter
Parse a single letter, according to char.IsLetter
.
|
|
LetterOrDigit
Parse a single letter or digit, according to char.IsLetterOrDigit
.
|
|
Lower
Parse a single lowercase letter, according to char.IsLower
.
|
|
Upper
Parse a single uppercase letter, according to char.IsUpper
.
|
|
AnyChar
Parse any character.
|
|
Author Justin Pealing
LastMod 2020-03-11