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 |
|
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., |
|