Pidgin

Class Parser

Constructor functions, extension methods and utilities for Parser<TToken, T>. This class is intended to be imported statically ("using static Pidgin.Parser").

Inheritance
Declaration
public static class Parser : Object

Properties

DecimalNum

A parser which parses a base-10 integer with an optional sign. The resulting int is not checked for overflow.

Declaration
public static Parser<char, int> DecimalNum { get; }
Property Value
Type Description

Parser<Char, Int32>

A parser which parses a base-10 integer with an optional sign

Digit

A parser that parses and returns a single digit character (0-9)

Declaration
public static Parser<char, char> Digit { get; }
Property Value
Type Description

Parser<Char, Char>

A parser that parses and returns a single digit character

EndOfLine

A parser that parses and returns either the literal string "\r\n" or the literal string "\n"

Declaration
public static Parser<char, string> EndOfLine { get; }
Property Value
Type Description

Parser<Char, String>

A parser that parses and returns either the literal string "\r\n" or the literal string "\n"

HexNum

A parser which parses a base-16 (hexadecimal) integer with an optional sign. The resulting int is not checked for overflow.

Declaration
public static Parser<char, int> HexNum { get; }
Property Value
Type Description

Parser<Char, Int32>

A parser which parses a base-16 (hexadecimal) integer with an optional sign

Letter

A parser that parses and returns a single letter character

Declaration
public static Parser<char, char> Letter { get; }
Property Value
Type Description

Parser<Char, Char>

A parser that parses and returns a single letter character

LetterOrDigit

A parser that parses and returns a single letter or digit character

Declaration
public static Parser<char, char> LetterOrDigit { get; }
Property Value
Type Description

Parser<Char, Char>

A parser that parses and returns a single letter or digit character

LongNum

A parser which parses a base-10 long integer with an optional sign.

Declaration
public static Parser<char, long> LongNum { get; }
Property Value
Type Description

Parser<Char, Int64>

A parser which parses a base-10 long integer with an optional sign

Lowercase

A parser that parses and returns a single lowercase letter character

Declaration
public static Parser<char, char> Lowercase { get; }
Property Value
Type Description

Parser<Char, Char>

A parser that parses and returns a single lowercase letter character

Num

A parser which parses a base-10 integer with an optional sign. The resulting int is not checked for overflow.

Declaration
public static Parser<char, int> Num { get; }
Property Value
Type Description

Parser<Char, Int32>

A parser which parses a base-10 integer with an optional sign

OctalNum

A parser which parses a base-8 (octal) integer with an optional sign. The resulting int is not checked for overflow.

Declaration
public static Parser<char, int> OctalNum { get; }
Property Value
Type Description

Parser<Char, Int32>

A parser which parses a base-8 (octal) integer with an optional sign

Punctuation

A parser that parses and returns a single Unicode punctuation character

Declaration
public static Parser<char, char> Punctuation { get; }
Property Value
Type Description

Parser<Char, Char>

A parser that parses and returns a single Unicode punctuation character

Real

A parser which parses a floating point number with an optional sign.

Declaration
public static Parser<char, double> Real { get; }
Property Value
Type Description

Parser<Char, Double>

A parser which parses a floating point number with an optional sign

Separator

A parser that parses and returns a single Unicode separator character

Declaration
public static Parser<char, char> Separator { get; }
Property Value
Type Description

Parser<Char, Char>

A parser that parses and returns a single Unicode separator character

SkipWhitespaces

A parser that discards a sequence of whitespace characters

Declaration
public static Parser<char, Unit> SkipWhitespaces { get; }
Property Value
Type Description

Parser<Char, Unit>

A parser that discards a sequence of whitespace characters

Symbol

A parser that parses and returns a single Unicode symbol character

Declaration
public static Parser<char, char> Symbol { get; }
Property Value
Type Description

Parser<Char, Char>

A parser that parses and returns a single Unicode symbol character

Uppercase

A parser that parses and returns a single uppercase letter character

Declaration
public static Parser<char, char> Uppercase { get; }
Property Value
Type Description

Parser<Char, Char>

A parser that parses and returns a single uppercase letter character

Whitespace

A parser that parses and returns a single whitespace character

Declaration
public static Parser<char, char> Whitespace { get; }
Property Value
Type Description

Parser<Char, Char>

A parser that parses and returns a single whitespace character

Whitespaces

A parser that parses and returns a sequence of whitespace characters

Declaration
public static Parser<char, IEnumerable<char>> Whitespaces { get; }
Property Value
Type Description

Parser<Char, IEnumerable<Char>>

A parser that parses and returns a sequence of whitespace characters

WhitespaceString

A parser that parses and returns a sequence of whitespace characters packed into a string

Declaration
public static Parser<char, string> WhitespaceString { get; }
Property Value
Type Description

Parser<Char, String>

A parser that parses and returns a sequence of whitespace characters packed into a string

Methods

AnyCharExcept(Char[])

Creates a parser which parses and returns a character if it is not one of the specified characters. When the character is one of the given characters, the parser fails without consuming input.

Declaration
public static Parser<char, char> AnyCharExcept(params char[] chars)
Parameters
Type Name Description

Char[]

chars

A sequence of characters that should not be matched

Returns
Type Description

Parser<Char, Char>

A parser which parses and returns a character that does not match one of the specified characters

AnyCharExcept(IEnumerable<Char>)

Creates a parser which parses and returns a character if it is not one of the specified characters. When the character is one of the given characters, the parser fails without consuming input.

Declaration
public static Parser<char, char> AnyCharExcept(IEnumerable<char> chars)
Parameters
Type Name Description

IEnumerable<Char>

chars

A sequence of characters that should not be matched

Returns
Type Description

Parser<Char, Char>

A parser which parses and returns a character that does not match one of the specified characters

AtLeastOnceString<TToken>(Parser<TToken, Char>)

Creates a parser which applies the current parser one or more times, packing the resulting characters into a string. Equivalent to parser.Many().Select(cs => string.Concat(cs))

Declaration
public static Parser<TToken, string> AtLeastOnceString<TToken>(this Parser<TToken, char> parser)
Parameters
Type Name Description

Parser<TToken, Char>

parser

A parser returning a single character

Returns
Type Description

Parser<TToken, String>

A parser which applies the current parser one or more times, packing the resulting characters into a string.

Type Parameters
Name Description

TToken

AtLeastOnceString<TToken>(Parser<TToken, String>)

Creates a parser which applies the current parser one or more times, concatenating the resulting string pieces. Equivalent to parser.Many().Select(cs => string.Concat(cs))

Declaration
public static Parser<TToken, string> AtLeastOnceString<TToken>(this Parser<TToken, string> parser)
Parameters
Type Name Description

Parser<TToken, String>

parser

A parser returning a single character

Returns
Type Description

Parser<TToken, String>

A parser which applies the current parser one or more times, concatenating the resulting string pieces.

Type Parameters
Name Description

TToken

Char(Char)

Creates a parser which parses and returns a single character.

Declaration
public static Parser<char, char> Char(char character)
Parameters
Type Name Description

Char

character

The character to parse

Returns
Type Description

Parser<Char, Char>

A parser which parses and returns a single character

CIChar(Char)

Creates a parser which parses and returns a single character, in a case insensitive manner. The parser returns the actual character parsed.

Declaration
public static Parser<char, char> CIChar(char character)
Parameters
Type Name Description

Char

character

The character to parse

Returns
Type Description

Parser<Char, Char>

A parser which parses and returns a single character

CIEnum<TEnum>()

Creates a parser that parses and returns one of enum values, in a case insensitive manner.

Declaration
public static Parser<char, TEnum> CIEnum<TEnum>()
    where TEnum : struct, Enum, ValueType
Returns
Type Description

Parser<Char, TEnum>

A parser that parses and returns one of enum values

Type Parameters
Name Description

TEnum

Enum type

CIOneOf(Char[])

Creates a parser which parses and returns one of the specified characters, in a case insensitive manner. The parser returns the actual character parsed.

Declaration
public static Parser<char, char> CIOneOf(params char[] chars)
Parameters
Type Name Description

Char[]

chars

A sequence of characters to choose between

Returns
Type Description

Parser<Char, Char>

A parser which parses and returns one of the specified characters, in a case insensitive manner.

CIOneOf(IEnumerable<Char>)

Creates a parser which parses and returns one of the specified characters, in a case insensitive manner. The parser returns the actual character parsed.

Declaration
public static Parser<char, char> CIOneOf(IEnumerable<char> chars)
Parameters
Type Name Description

IEnumerable<Char>

chars

A sequence of characters to choose between

Returns
Type Description

Parser<Char, Char>

A parser which parses and returns one of the specified characters, in a case insensitive manner.

CIString(String)

Creates a parser that parses and returns a literal string, in a case insensitive manner. The parser returns the actual string parsed.

Declaration
public static Parser<char, string> CIString(string str)
Parameters
Type Name Description

String

str

The string to parse

Returns
Type Description

Parser<Char, String>

A parser that parses and returns a literal string, in a case insensitive manner.

Enum<TEnum>()

Creates a parser that parses and returns one of enum values

Declaration
public static Parser<char, TEnum> Enum<TEnum>()
    where TEnum : struct, Enum, ValueType
Returns
Type Description

Parser<Char, TEnum>

A parser that parses and returns one of enum values

Type Parameters
Name Description

TEnum

Enum type

Int(Int32)

A parser which parses an integer in the given base with an optional sign. The resulting int is not checked for overflow.

Declaration
public static Parser<char, int> Int(int base)
Parameters
Type Name Description

Int32

base

The base in which the number is notated, between 1 and 36

Returns
Type Description

Parser<Char, Int32>

A parser which parses an integer with an optional sign

Long(Int32)

Creates a parser which parses a long integer in the given base with an optional sign. The resulting Int64 is not checked for overflow.

Declaration
public static Parser<char, long> Long(int base)
Parameters
Type Name Description

Int32

base

The base in which the number is notated, between 1 and 36

Returns
Type Description

Parser<Char, Int64>

A parser which parses a long integer with an optional sign

Lookahead<TToken, T>(Parser<TToken, T>)

If parser succeeds, Lookahead(parser) backtracks, behaving as if parser had not consumed any input. No backtracking is performed upon failure.

Declaration
public static Parser<TToken, T> Lookahead<TToken, T>(Parser<TToken, T> parser)
Parameters
Type Name Description

Parser<TToken, T>

parser

The parser to look ahead with

Returns
Type Description

Parser<TToken, T>

A parser which rewinds the input stream if parser succeeds.

Type Parameters
Name Description

TToken

T

ManyString<TToken>(Parser<TToken, Char>)

Creates a parser which applies the current parser zero or more times, packing the resulting characters into a string. Equivalent to parser.Many().Select(cs => string.Concat(cs))

Declaration
public static Parser<TToken, string> ManyString<TToken>(this Parser<TToken, char> parser)
Parameters
Type Name Description

Parser<TToken, Char>

parser

A parser returning a single character

Returns
Type Description

Parser<TToken, String>

A parser which applies the current parser zero or more times, packing the resulting characters into a string.

Type Parameters
Name Description

TToken

ManyString<TToken>(Parser<TToken, String>)

Creates a parser which applies the current parser zero or more times, concatenating the resulting string pieces. Equivalent to parser.Many().Select(cs => string.Concat(cs))

Declaration
public static Parser<TToken, string> ManyString<TToken>(this Parser<TToken, string> parser)
Parameters
Type Name Description

Parser<TToken, String>

parser

A parser returning a single character

Returns
Type Description

Parser<TToken, String>

A parser which applies the current parser zero or more times, concatenating the resulting string pieces.

Type Parameters
Name Description

TToken

Map<TToken, T1, T2, T3, T4, T5, T6, T7, T8, R>(Func<T1, T2, T3, T4, T5, T6, T7, T8, R>, Parser<TToken, T1>, Parser<TToken, T2>, Parser<TToken, T3>, Parser<TToken, T4>, Parser<TToken, T5>, Parser<TToken, T6>, Parser<TToken, T7>, Parser<TToken, T8>)

Creates a parser that applies the specified parsers sequentially and applies the specified transformation function to their results.

Declaration
public static Parser<TToken, R> Map<TToken, T1, T2, T3, T4, T5, T6, T7, T8, R>(Func<T1, T2, T3, T4, T5, T6, T7, T8, R> func, Parser<TToken, T1> parser1, Parser<TToken, T2> parser2, Parser<TToken, T3> parser3, Parser<TToken, T4> parser4, Parser<TToken, T5> parser5, Parser<TToken, T6> parser6, Parser<TToken, T7> parser7, Parser<TToken, T8> parser8)
Parameters
Type Name Description

Func<T1, T2, T3, T4, T5, T6, T7, T8, R>

func

A function to apply to the return values of the specified parsers

Parser<TToken, T1>

parser1

The first parser

Parser<TToken, T2>

parser2

The second parser

Parser<TToken, T3>

parser3

The third parser

Parser<TToken, T4>

parser4

The fourth parser

Parser<TToken, T5>

parser5

The fifth parser

Parser<TToken, T6>

parser6

The sixth parser

Parser<TToken, T7>

parser7

The seventh parser

Parser<TToken, T8>

parser8

The eighth parser

Returns
Type Description

Parser<TToken, R>

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T1

The return type of the first parser

T2

The return type of the second parser

T3

The return type of the third parser

T4

The return type of the fourth parser

T5

The return type of the fifth parser

T6

The return type of the sixth parser

T7

The return type of the seventh parser

T8

The return type of the eighth parser

R

The return type of the resulting parser

Map<TToken, T1, R>(Func<T1, R>, Parser<TToken, T1>)

Creates a parser that applies the specified parsers sequentially and applies the specified transformation function to their results.

Declaration
public static Parser<TToken, R> Map<TToken, T1, R>(Func<T1, R> func, Parser<TToken, T1> parser1)
Parameters
Type Name Description

Func<T1, R>

func

A function to apply to the return values of the specified parsers

Parser<TToken, T1>

parser1

The first parser

Returns
Type Description

Parser<TToken, R>

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T1

The return type of the first parser

R

The return type of the resulting parser

Map<TToken, T1, T2, R>(Func<T1, T2, R>, Parser<TToken, T1>, Parser<TToken, T2>)

Creates a parser that applies the specified parsers sequentially and applies the specified transformation function to their results.

Declaration
public static Parser<TToken, R> Map<TToken, T1, T2, R>(Func<T1, T2, R> func, Parser<TToken, T1> parser1, Parser<TToken, T2> parser2)
Parameters
Type Name Description

Func<T1, T2, R>

func

A function to apply to the return values of the specified parsers

Parser<TToken, T1>

parser1

The first parser

Parser<TToken, T2>

parser2

The second parser

Returns
Type Description

Parser<TToken, R>

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T1

The return type of the first parser

T2

The return type of the second parser

R

The return type of the resulting parser

Map<TToken, T1, T2, T3, R>(Func<T1, T2, T3, R>, Parser<TToken, T1>, Parser<TToken, T2>, Parser<TToken, T3>)

Creates a parser that applies the specified parsers sequentially and applies the specified transformation function to their results.

Declaration
public static Parser<TToken, R> Map<TToken, T1, T2, T3, R>(Func<T1, T2, T3, R> func, Parser<TToken, T1> parser1, Parser<TToken, T2> parser2, Parser<TToken, T3> parser3)
Parameters
Type Name Description

Func<T1, T2, T3, R>

func

A function to apply to the return values of the specified parsers

Parser<TToken, T1>

parser1

The first parser

Parser<TToken, T2>

parser2

The second parser

Parser<TToken, T3>

parser3

The third parser

Returns
Type Description

Parser<TToken, R>

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T1

The return type of the first parser

T2

The return type of the second parser

T3

The return type of the third parser

R

The return type of the resulting parser

Map<TToken, T1, T2, T3, T4, R>(Func<T1, T2, T3, T4, R>, Parser<TToken, T1>, Parser<TToken, T2>, Parser<TToken, T3>, Parser<TToken, T4>)

Creates a parser that applies the specified parsers sequentially and applies the specified transformation function to their results.

Declaration
public static Parser<TToken, R> Map<TToken, T1, T2, T3, T4, R>(Func<T1, T2, T3, T4, R> func, Parser<TToken, T1> parser1, Parser<TToken, T2> parser2, Parser<TToken, T3> parser3, Parser<TToken, T4> parser4)
Parameters
Type Name Description

Func<T1, T2, T3, T4, R>

func

A function to apply to the return values of the specified parsers

Parser<TToken, T1>

parser1

The first parser

Parser<TToken, T2>

parser2

The second parser

Parser<TToken, T3>

parser3

The third parser

Parser<TToken, T4>

parser4

The fourth parser

Returns
Type Description

Parser<TToken, R>

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T1

The return type of the first parser

T2

The return type of the second parser

T3

The return type of the third parser

T4

The return type of the fourth parser

R

The return type of the resulting parser

Map<TToken, T1, T2, T3, T4, T5, R>(Func<T1, T2, T3, T4, T5, R>, Parser<TToken, T1>, Parser<TToken, T2>, Parser<TToken, T3>, Parser<TToken, T4>, Parser<TToken, T5>)

Creates a parser that applies the specified parsers sequentially and applies the specified transformation function to their results.

Declaration
public static Parser<TToken, R> Map<TToken, T1, T2, T3, T4, T5, R>(Func<T1, T2, T3, T4, T5, R> func, Parser<TToken, T1> parser1, Parser<TToken, T2> parser2, Parser<TToken, T3> parser3, Parser<TToken, T4> parser4, Parser<TToken, T5> parser5)
Parameters
Type Name Description

Func<T1, T2, T3, T4, T5, R>

func

A function to apply to the return values of the specified parsers

Parser<TToken, T1>

parser1

The first parser

Parser<TToken, T2>

parser2

The second parser

Parser<TToken, T3>

parser3

The third parser

Parser<TToken, T4>

parser4

The fourth parser

Parser<TToken, T5>

parser5

The fifth parser

Returns
Type Description

Parser<TToken, R>

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T1

The return type of the first parser

T2

The return type of the second parser

T3

The return type of the third parser

T4

The return type of the fourth parser

T5

The return type of the fifth parser

R

The return type of the resulting parser

Map<TToken, T1, T2, T3, T4, T5, T6, R>(Func<T1, T2, T3, T4, T5, T6, R>, Parser<TToken, T1>, Parser<TToken, T2>, Parser<TToken, T3>, Parser<TToken, T4>, Parser<TToken, T5>, Parser<TToken, T6>)

Creates a parser that applies the specified parsers sequentially and applies the specified transformation function to their results.

Declaration
public static Parser<TToken, R> Map<TToken, T1, T2, T3, T4, T5, T6, R>(Func<T1, T2, T3, T4, T5, T6, R> func, Parser<TToken, T1> parser1, Parser<TToken, T2> parser2, Parser<TToken, T3> parser3, Parser<TToken, T4> parser4, Parser<TToken, T5> parser5, Parser<TToken, T6> parser6)
Parameters
Type Name Description

Func<T1, T2, T3, T4, T5, T6, R>

func

A function to apply to the return values of the specified parsers

Parser<TToken, T1>

parser1

The first parser

Parser<TToken, T2>

parser2

The second parser

Parser<TToken, T3>

parser3

The third parser

Parser<TToken, T4>

parser4

The fourth parser

Parser<TToken, T5>

parser5

The fifth parser

Parser<TToken, T6>

parser6

The sixth parser

Returns
Type Description

Parser<TToken, R>

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T1

The return type of the first parser

T2

The return type of the second parser

T3

The return type of the third parser

T4

The return type of the fourth parser

T5

The return type of the fifth parser

T6

The return type of the sixth parser

R

The return type of the resulting parser

Map<TToken, T1, T2, T3, T4, T5, T6, T7, R>(Func<T1, T2, T3, T4, T5, T6, T7, R>, Parser<TToken, T1>, Parser<TToken, T2>, Parser<TToken, T3>, Parser<TToken, T4>, Parser<TToken, T5>, Parser<TToken, T6>, Parser<TToken, T7>)

Creates a parser that applies the specified parsers sequentially and applies the specified transformation function to their results.

Declaration
public static Parser<TToken, R> Map<TToken, T1, T2, T3, T4, T5, T6, T7, R>(Func<T1, T2, T3, T4, T5, T6, T7, R> func, Parser<TToken, T1> parser1, Parser<TToken, T2> parser2, Parser<TToken, T3> parser3, Parser<TToken, T4> parser4, Parser<TToken, T5> parser5, Parser<TToken, T6> parser6, Parser<TToken, T7> parser7)
Parameters
Type Name Description

Func<T1, T2, T3, T4, T5, T6, T7, R>

func

A function to apply to the return values of the specified parsers

Parser<TToken, T1>

parser1

The first parser

Parser<TToken, T2>

parser2

The second parser

Parser<TToken, T3>

parser3

The third parser

Parser<TToken, T4>

parser4

The fourth parser

Parser<TToken, T5>

parser5

The fifth parser

Parser<TToken, T6>

parser6

The sixth parser

Parser<TToken, T7>

parser7

The seventh parser

Returns
Type Description

Parser<TToken, R>

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T1

The return type of the first parser

T2

The return type of the second parser

T3

The return type of the third parser

T4

The return type of the fourth parser

T5

The return type of the fifth parser

T6

The return type of the sixth parser

T7

The return type of the seventh parser

R

The return type of the resulting parser

Not<TToken, T>(Parser<TToken, T>)

Creates a parser which succeeds only if the given parser fails. The resulting parser does not perform any backtracking; it consumes the same amount of input as the supplied parser. Combine this function with Try<TToken, T>(Parser<TToken, T>) if this behaviour is undesirable.

Declaration
public static Parser<TToken, Unit> Not<TToken, T>(Parser<TToken, T> parser)
Parameters
Type Name Description

Parser<TToken, T>

parser

The parser that is expected to fail

Returns
Type Description

Parser<TToken, Unit>

A parser which succeeds only if the given parser fails.

Type Parameters
Name Description

TToken

T

OneOf(Char[])

Creates a parser which parses and returns one of the specified characters.

Declaration
public static Parser<char, char> OneOf(params char[] chars)
Parameters
Type Name Description

Char[]

chars

A sequence of characters to choose between

Returns
Type Description

Parser<Char, Char>

A parser which parses and returns one of the specified characters

OneOf(IEnumerable<Char>)

Creates a parser which parses and returns one of the specified characters.

Declaration
public static Parser<char, char> OneOf(IEnumerable<char> chars)
Parameters
Type Name Description

IEnumerable<Char>

chars

A sequence of characters to choose between

Returns
Type Description

Parser<Char, Char>

A parser which parses and returns one of the specified characters

OneOf<TToken, T>(Parser<TToken, T>[])

Creates a parser which applies one of the specified parsers. The resulting parser fails if all of the input parsers fail without consuming input, or if one of them fails after consuming input

Declaration
public static Parser<TToken, T> OneOf<TToken, T>(params Parser<TToken, T>[] parsers)
Parameters
Type Name Description

Parser<TToken, T>[]

parsers

A sequence of parsers to choose between

Returns
Type Description

Parser<TToken, T>

A parser which applies one of the specified parsers

Type Parameters
Name Description

TToken

The type of tokens in the parsers' input stream

T

The return type of the parsers

OneOf<TToken, T>(IEnumerable<Parser<TToken, T>>)

Creates a parser which applies one of the specified parsers. The resulting parser fails if all of the input parsers fail without consuming input, or if one of them fails after consuming input. The input enumerable is enumerated and copied to a list.

Declaration
public static Parser<TToken, T> OneOf<TToken, T>(IEnumerable<Parser<TToken, T>> parsers)
Parameters
Type Name Description

IEnumerable<Parser<TToken, T>>

parsers

A sequence of parsers to choose between

Returns
Type Description

Parser<TToken, T>

A parser which applies one of the specified parsers

Type Parameters
Name Description

TToken

The type of tokens in the parsers' input stream

T

The return type of the parsers

Rec<TToken, T>(Func<Parser<TToken, T>, Parser<TToken, T>>)

Creates a parser which passes itself to the supplied function and applies the resulting parser. This is the Y combinator for parsers. Rec<TToken, T>(Lazy<Parser<TToken, T>>) Rec<TToken, T>(Func<Parser<TToken, T>>)

Declaration
public static Parser<TToken, T> Rec<TToken, T>(Func<Parser<TToken, T>, Parser<TToken, T>> func)
Parameters
Type Name Description

Func<Parser<TToken, T>, Parser<TToken, T>>

func

A function whose argument is a parser which behaves the same way as its result

Returns
Type Description

Parser<TToken, T>

A parser which lazily calls the supplied function and applies the resulting parser

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T

The return type of the parser

Examples

This example shows how to use mutual recursion to create a parser equivalent to Many()

// many is equivalent to String("foo").Separated(Char(' '))
var many = Rec(self =>
    String("foo").Then(
        Char(' ').Then(self).Optional(),
        (x, y) => x + y.GetValueOrDefault("")
    )
);

Rec<TToken, T>(Func<Parser<TToken, T>>)

Creates a parser which lazily calls the supplied function and applies the resulting parser. This is primarily useful to allow mutual recursion in parsers. Rec<TToken, T>(Lazy<Parser<TToken, T>>) Rec<TToken, T>(Func<Parser<TToken, T>, Parser<TToken, T>>)

Declaration
public static Parser<TToken, T> Rec<TToken, T>(Func<Parser<TToken, T>> parser)
Parameters
Type Name Description

Func<Parser<TToken, T>>

parser

A function which returns a parser

Returns
Type Description

Parser<TToken, T>

A parser which lazily calls the supplied function and applies the resulting parser

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T

The return type of the parser

Examples

This example shows how to use mutual recursion to create a parser equivalent to Many()

// many is equivalent to String("foo").Separated(Char(' '))
Parser<char, string> rest = null;
var many = String("foo").Then(Rec(() => rest).Optional(), (x, y) => x + y.GetValueOrDefault(""));
rest = Char(' ').Then(many);

Rec<TToken, T>(Lazy<Parser<TToken, T>>)

Creates a parser which lazily calls the supplied function and applies the resulting parser. This is primarily useful to allow mutual recursion in parsers. Rec<TToken, T>(Func<Parser<TToken, T>>) Rec<TToken, T>(Func<Parser<TToken, T>, Parser<TToken, T>>)

Declaration
public static Parser<TToken, T> Rec<TToken, T>(Lazy<Parser<TToken, T>> parser)
Parameters
Type Name Description

Lazy<Parser<TToken, T>>

parser

A lazy parser value

Returns
Type Description

Parser<TToken, T>

A parser which lazily applies the specified parser

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T

The return type of the parser

RepeatString<TToken>(Parser<TToken, Char>, Int32)

Creates a parser which applies parser count times, packing the resulting chars into a string.

Equivalent to parser.Repeat(count).Select(string.Concat).

Declaration
public static Parser<TToken, string> RepeatString<TToken>(this Parser<TToken, char> parser, int count)
Parameters
Type Name Description

Parser<TToken, Char>

parser

The parser

Int32

count

The number of times to apply the parser

Returns
Type Description

Parser<TToken, String>

A parser which applies parser count times, packing the resulting chars into a string.

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

Exceptions
Type Condition

ArgumentOutOfRangeException

count was less than 0

String(String)

Creates a parser that parses and returns a literal string

Declaration
public static Parser<char, string> String(string str)
Parameters
Type Name Description

String

str

The string to parse

Returns
Type Description

Parser<Char, String>

A parser that parses and returns a literal string

Try<TToken, T>(Parser<TToken, T>)

Creates a parser which applies parser and backtracks upon failure

Declaration
public static Parser<TToken, T> Try<TToken, T>(Parser<TToken, T> parser)
Parameters
Type Name Description

Parser<TToken, T>

parser

The parser

Returns
Type Description

Parser<TToken, T>

A parser which applies parser and backtracks upon failure

Type Parameters
Name Description

TToken

The type of tokens in the parser's input stream

T

The return type of the parser

UnsignedInt(Int32)

A parser which parses an integer in the given base without a sign. The resulting int is not checked for overflow.

Declaration
public static Parser<char, int> UnsignedInt(int base)
Parameters
Type Name Description

Int32

base

The base in which the number is notated, between 1 and 36

Returns
Type Description

Parser<Char, Int32>

A parser which parses an integer without a sign.

UnsignedLong(Int32)

A parser which parses a long integer in the given base without a sign. The resulting Int64 is not checked for overflow.

Declaration
public static Parser<char, long> UnsignedLong(int base)
Parameters
Type Name Description

Int32

base

The base in which the number is notated, between 1 and 36

Returns
Type Description

Parser<Char, Int64>

A parser which parses a long integer without a sign.

In This Article