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
Inherited Members
Declaration
public static class Parser

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

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>

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

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>

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>

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

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>

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

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

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>

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>

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>

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>

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>

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>

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>

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>

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

Methods

AnyCharExcept(params 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.AtLeastOnce().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

The type of the tokens in the parser's input stream.

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

The type of the tokens in the parser's input stream.

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
Returns
Type Description

Parser<char, TEnum>

A parser that parses and returns one of enum values.

Type Parameters
Name Description

TEnum

Enum type.

CIOneOf(params 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.

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.

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
Returns
Type Description

Parser<char, TEnum>

A parser that parses and returns one of enum values.

Type Parameters
Name Description

TEnum

Enum type.

Int(int)

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

int

base

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

Returns
Type Description

Parser<char, int>

A parser which parses an integer with an optional sign.

Long(int)

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

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

int

base

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

Returns
Type Description

Parser<char, long>

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

The type of the tokens in the parser's input stream.

T

The type of the value returned by the parser.

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

The type of the tokens in the parser's input stream.

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.AtLeastOnce().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

The type of the tokens in the parser's input stream.

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

The type of the tokens in the parser's input stream.

T

The type of the value returned by the parser.

OneOf(params 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>(params 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>, int)

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.

int

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(int)

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

int

base

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

Returns
Type Description

Parser<char, int>

A parser which parses an integer without a sign.

UnsignedLong(int)

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

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

int

base

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

Returns
Type Description

Parser<char, long>

A parser which parses a long integer without a sign.

In This Article