Character Parsers
The XParsec.CharParsers module provides a rich set of parsers specifically optimized for working with char and string inputs. These parsers are the go-to tools for most text-based parsing tasks.
How to Read the Examples
The examples below show how to call each parser. To see what they do, you can run them with a simple test function like this:
open XParsec
open XParsec.CharParsers
// A helper to run a parser and print its result
let testParser parser input =
match parser (Reader.ofString input ()) with
| Ok success -> printfn $"Input: \"{input}\" -> Ok {success.Parsed}"
| Error err ->
let errorMsg = ErrorFormatting.formatStringError input err
printfn $"Input: \"{input}\" -> Error:\n{errorMsg}"
// Example usage:
testParser (pchar 'a') "abc"
// Prints: Input: "abc" -> Ok a
Simple Character Parsers
These parsers consume and match single characters based on various criteria.
Parser |
Description |
Example Usage |
|---|---|---|
|
Parses the specific character |
|
|
Parses and discards the specific character |
|
|
Parses any single character. |
|
|
Parses and discards any single character. |
|
|
Parses any character that satisfies predicate |
|
|
Parses any ASCII digit ('0'-'9'). |
|
|
Parses any ASCII letter ('a'-'z', 'A'-'Z'). |
|
|
Parses any character from the sequence |
|
|
Parses any character not in the sequence |
|
String Parsers
These parsers match sequences of characters.
Parser |
Description |
Example Usage |
|---|---|---|
|
Parses the specific string |
|
|
Parses string |
|
|
Case-insensitive version of |
|
|
Applies char parser |
|
|
Applies char parser |
|
|
Applies |
|
Multi-String Parsers
These parsers are optimized to match one of many strings, such as a list of keywords.
Key Features:
- Greedy Matching: They always attempt to match the longest candidate first. For example, if your candidates are
["=", "=="], matching against==will correctly consume both characters. - Performance: Much faster than using
choiceor<|>over a list of strings. - Canonical Returns: The
CI(Case Insensitive) variants return the string as defined in your list, effectively normalizing the input case.
Parser |
Description |
Example Usage |
|---|---|---|
|
Matches any string in |
|
|
Case-insensitive match. Returns the string from |
|
|
Matches a string key, returns the associated value. |
|
|
Case-insensitive version of |
|
|
Matches any string in |
|
|
Matches any string in |
|
Note: All parsers above have an
Lvariant (e.g.,anyStringL,anyStringReturnL) which accepts amessage: string. These are highly recommended for production parsers as they avoid allocating detailed error lists on failure.// Efficient usage with a custom label let operator = anyStringL ["+"; "-"; "*"; "/"] "arithmetic operator"
Whitespace and Newline Parsers
These are common helpers for handling whitespace and line endings.
Parser |
Description |
Example Usage |
|---|---|---|
|
Skips zero or more whitespace characters ( |
|
|
Skips one or more whitespace characters. Fails if no whitespace is present. |
|
|
Parses a newline ("\n", "\r", or "\r\n"), returning |
|
|
Parses a newline and returns |
|
Numeric Parsers
These powerful parsers can parse various numeric formats from a string. They automatically handle signs (+/-) and different bases.
- Decimal:
123,-45 - Hexadecimal:
0x7B,0xFF - Octal:
0o173 - Binary:
0b1111011
Parser |
Description |
Example Usage |
|---|---|---|
|
Parses a 16-bit signed integer. |
|
|
Parses a 32-bit signed integer. |
|
|
Parses a 64-bit signed integer. |
|
|
Parses a 16-bit unsigned integer. |
|
|
Parses a 32-bit unsigned integer. |
|
|
Parses a 64-bit unsigned integer. |
|
|
Parses an arbitrarily large integer ( |
|
|
Parses a double-precision floating-point number. Also handles "NaN", "Infinity", and hex floats (e.g., |
|