6.1 Parser overview
- Parsing single chars
- Parsing strings directly
- Parsing strings with the help of other parsers
- Parsing numbers
- Parsing whitespace
- Chaining and piping parsers
- Parsing sequences
- Parsing alternatives and recovering from errors
- Conditional parsing and looking ahead
- Customizing error messages
- User state handling and getting the input stream position
| Parser | Description |
|---|---|
pchar c (variants: skipChar, charReturn)
|
Parses the char c. |
anyChar (variant: skipAnyChar)
|
Parses any one char. |
satisfy f (variants: (skipS|s)atisfy[L])
|
Parses any one char for which the predicate function f returns true.
|
anyOf str (variant: skipAnyOf)
|
Parses any one char in the string str. |
noneOf str (variant: skipNoneOf)
|
Parses any one char not in the string str. |
letter (variants: lower, upper)
|
Parses any one unicode letter char identified by System.Char.IsLetter.
|
asciiLetter
(variants: asciiLower, asciiUpper)
|
Parses any one char in the range 'a' ‐
'z' and 'A' ‐ 'Z'.
|
digit (variants: hex, octal)
|
Parses any one char in the range '0' ‐
'9'.
|
| Parser | Description |
|---|---|
pstring str (variants: skipString, stringReturn)
|
Parses the string str. |
pstringCI str (variants: skipStringCI, stringCIReturn)
|
Parses any string that case‐insensitively matches the string str.
|
anyString n (variants: skipAnyString)
|
Parses any sequence of n chars. |
restOfLine skipNewline (variant: skipRestOfLine)
|
Parses any chars before the end of the line and, if skipNewline is true, skips to the beginning of the next line (if there is one).
|
charsTillString str skipString nMax (variants: charsTillStringCI, skipCharsTillString[CI])
|
Parses all chars before the first occurance of the string str and, if skipString is true, skips over str. Fails if more than nMax chars come
before str.
|
manySatisfy f (variant: skipManySatisfy)
|
Parses a sequence of zero or more chars that satisfy the predicate function f
(i.e. chars for which f returns true).
|
manySatisfy2 f1 f (variant: skipManySatisfy2)
|
Parses a sequence of zero or more chars, where the first char must satisfy the predicate function f1 and the remaining chars must satisfy f.
|
many1Satisfy f (variants: (skipM|m)any1Satisfy[2][L])
|
Parses a sequence of one or more chars that satisfy the predicate function f.
|
manyMinMaxSatisfy nMin nMax f (variants: (skipM|m)anyMinMaxSatisfy[2][L])
|
Parses a sequence of nMin or more chars that satisfy the predicate function f, but not more than nMax chars.
|
regex pattern
|
Parses a sequence of one or more chars matched by the .NET regular expression string pattern.
|
identifier options
|
Parses a Unicode identifier. |
| Parser | Description |
|---|---|
manyChars cp (variants: manyChars2)
|
Parses a sequence of zero or more chars with the char parser cp.
|
many1Chars cp (variants: many1Chars2)
|
Parses a sequence of one or more chars with the char parser cp.
|
manyCharsTill cp endp (variants: manyCharsTill[Apply][2])
|
Parses chars with the char parser cp until the parser endp succeeds. Stops after endp.
|
manyStrings sp (variant: many[1]Strings[2])
|
Parses a sequence of zero or more strings with the parser sp. Returns the parsed
strings in concatenated form.
|
stringsSepBy sp sep
|
Parses a sequence of zero or more occurrences of sp separated by sep. Returns the strings parsed with sp
and sep in concatenated form.
|
skipped p
|
Applies the parser p. Returns the chars skipped over by p as a string.
|
p |> withSkippedString f
|
Applies the parser p. Returns f str x, where str is the string skipped
over by p and x is the result
returned by p.
|
| Parser | Description |
|---|---|
pfloat |
Parses a double‐precision floating‐point number. |
pint64(variants: pint(8|16|32))
|
Parses a 64‐bit signed integer. |
puint64(variants: puint(8|16|32))
|
Parses a 64‐bit unsigned integer. |
numberLiteral options label
|
Parses a number literal and returns the result in form of a NumberLiteral value.
|
| Parser | Description |
|---|---|
newline (variants: skipNewline,
newlineReturn, unicodeNewline)
|
Parses a newline ("\n", "\r\n" or "\r"). Returns '\n'.
|
unicodeNewline
(variants: skipUnicodeNewline, unicodeNewlineReturn)
|
Parses a Unicode newline ("\n", "\r\n", "\r", "\u0085", "\u2028" or "\u2029"). Returns '\n'.
|
spaces (variant: spaces1)
|
Skips over any sequence of whitespace chars (' ', '\t' or a newline).
|
unicodeSpaces
(variant: unicodeSpaces1)
|
Skips over any sequence of Unicode whitespace chars and recognizes ("\n", "\r\n", "\r", "\u0085", "\u2028" and "\u2029") as newlines.
|
eof |
Only succeeds at the end of the input. |
| Parser | Description |
|---|---|
preturn x
|
Returns x. |
p >>% x
|
Applies the parser p. Returns x.
|
p |>> f
|
Applies the parser p. Returns f x, where x is the result returned by p.
|
p1 >>. p2
|
Applies the parsers p1 and p2 in
sequence. Returns the result of p2.
|
p1 .>> p2
|
Applies the parsers p1 and p2 in
sequence. Returns the result of p1.
|
p1 .>>. p2
|
Applies the parsers p1 and p2 in
sequence. Returns the results in a tuple.
|
between pBegin pEnd p
|
Applies the parsers pBegin, p and
pEnd in sequence. Returns the result of p.
|
pipe2 p1 p2 f (variants: pipe(3|4|5)
|
Applies the parsers p1 and p2 in
sequence. Returns f x1 x2, where x1 and x2 are the results returned by p1 and p2.
|
p >>= f
|
First applies the parser p, then applies the function f to the result returned by p and finally applies the parser
returned by f.
|
| Parser | PEG | Description |
|---|---|---|
tuple2 p1 p2 (variants: tuple(3|4|5))
|
p1 p2 |
Applies the parsers p1 and p2 in
sequence. Returns the results in a tuple.
|
parray n p (variants: skipArray)
|
Parses n occurrences of p. Returns
the results in an array.
|
|
many p (variant: skipMany)
|
p* |
Parses zero or more occurrences of p. Returns the results in a list.
|
many1 p (variant: skipMany1)
|
p+ |
Parses one or more occurrences of p. Returns the results in a list.
|
sepBy p sep (variants: sepBy1, skipSepBy[1])
|
(p (sep p)*)? |
Parses zero or more occurrences of p, separated by sep. Returns the results in a list.
|
sepEndBy p sep (variants: sepEndBy1, skipSepEndBy[1])
|
(p (sep p)* sep?)? |
Parses zero or more occurrences of p, separated and optionally ended by sep. Returns the results in a list.
|
manyTill p endp (variants: many1Till, skipMany[1]Till)
|
(!endp p)* endp |
Parses zero or more occurrences of p for as long as endp does not succeed. Stops after endp succeeded. Returns the
results returned by p in a list.
|
chainl1 p op (variants: chain(l|r)[1])
|
p (op p)* |
Parses one or more occurrences of p, separated by sep. Returns f_n (... (f_2 (f_1 x_1 x_2) x_3) ...) x_n+1,
where f_1 to f_n are the functions
returned by the parser op and x_1 to
x_n+1 are the values returned by p.
|
| Parser | Description |
|---|---|
p1 <|> p2
|
Parses p1 or p2. The parser p2 is only tried if p1 fails with a
non‐fatal error and without changing the parser state. The stream position is part of the parser state, so if p1 fails after consuming input, p2 will not
be tried.
|
choice ps (variant: choiceL)
|
Is equivalent to p1 <|> p2 <|> ... <|> pn <|> pzero, where p1 … pn are the parsers in the sequence
ps.
|
p <|>% x
|
Parses p or returns x. Is equivalent
to p1 <|> preturn x.
|
opt p
(variant: optional)
|
Parses an optional occurrence of p as an option value. Is equivalent to (p |>> Some) <|>% None
|
attempt p
|
Parses p. If p fails after changing
the parser state, attempt p will backtrack to the original parser state before reporting a (non‐fatal) error. Thus, attempt p1 <|> p2 will continue to try to
parse p2 even if p1 fails after
consuming input.
|
p1 >>? p2 (variants: .>>?, .>>.?, >>=?)
|
Behaves like p1 >>. p2, but will backtrack to the beginning if p2 fails with a non‐fatal error and with an unchanged parser state, even if p1 has changed the parser state.
|
| Parser | Description |
|---|---|
notEmpty p
|
Behaves like p, but fails when p
succeeds without consuming input or changing the parser state in any other way.
|
followedBy p (variant: notFollowedBy)
|
Succeeds without changing the parser state if the parser p succeeds at the current
position.
|
followedByL p label (variant: notFollowedByL)
|
Behaves like followedBy p, but uses the string label to generate a more descriptive
error message in case p fails. The string label should describe p.
|
notFollowedByEof
|
Is an optimized version of notFollowedByL eof "end of input".
|
followedByString str (variants: (notF|f)ollowedByString[CI])
|
Is an optimized version of followedByL (pstring str) ("'" + str + "'").
|
nextCharSatisfies f (variants: next2CharsSatisfy, previousCharSatisfies)
|
Is an optimized version of followedBy (satisfy f).
|
nextCharSatisfiesNot f (variants: next2CharsSatisfyNot, previousCharSatisfiesNot)
|
Is an optimized version of notFollowedBy (satisfy f).
|
lookAhead p
|
Parses p and restores the original parser state afterwards. |
| Parser | Description |
|---|---|
p <?> label
|
Applies the parser p. If p does not
change the parser state (usually because p failed), the error messages are replaced with
expectedError label. The string label should describe p.
|
p <??> label
|
Behaves like p <?> label, but when p fails
after changing the parser state, a CompoundError message is generated with both the given label
and the error messages generated by p.
|
fail msg
|
Always fails with a messageError msg. The string msg will be displayed together with other error
messages generated for the same input position.
|
failFatally msg
|
Always fails with a messageError msg. Returns with a FatalError, so that no error recovery is attempted (except via backtracking constructs).
|
| Parser | Description |
|---|---|
getUserState
|
Returns the current user state. |
setUserState u
|
Sets the user state to u. |
updateUserState f
|
Sets the user state to f u, where u is the current user state.
|
userStateSatisfies f
|
Succeeds if the current user state satisfies the predicate function f. |
getPosition
|
Returns the current position in the input stream. |