Pidgin

Class Parser<TToken>

Constructor functions, extension methods and utilities for Parser<TToken, T> This class is intended to be imported statically, with the type parameter set to the type of tokens in your input stream ("using static Pidgin.Parser<char>").

Inheritance
Inherited Members
Declaration
public static class Parser<TToken>
Type Parameters
Name Description

TToken

The type of the tokens in the input stream for parsers created by methods in this class.

Properties

Any

Creates a parser that parses any single character.

Declaration
public static Parser<TToken, TToken> Any { get; }
Property Value
Type Description

Parser<TToken, TToken>

A parser that parses any single character.

Configuration

A parser which returns the current IConfiguration<TToken>.

Declaration
public static Parser<TToken, IConfiguration<TToken>> Configuration { get; }
Property Value
Type Description

Parser<TToken, IConfiguration<TToken>>

CurrentOffset

A parser which returns the number of input tokens which have been consumed.

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

Parser<TToken, int>

CurrentPos

A parser which returns the current source position.

Declaration
public static Parser<TToken, SourcePos> CurrentPos { get; }
Property Value
Type Description

Parser<TToken, SourcePos>

CurrentSourcePosDelta

A parser which returns the current source position.

Declaration
public static Parser<TToken, SourcePosDelta> CurrentSourcePosDelta { get; }
Property Value
Type Description

Parser<TToken, SourcePosDelta>

End

Creates a parser which parses the end of the input stream.

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

Parser<TToken, Unit>

A parser which parses the end of the input stream and returns Value.

Methods

Fail<T>(string)

Creates a parser which always fails without consuming any input.

Declaration
public static Parser<TToken, T> Fail<T>(string message = "Failed")
Parameters
Type Name Description

string

message

A custom error message.

Returns
Type Description

Parser<TToken, T>

A parser which always fails.

Type Parameters
Name Description

T

The return type of the resulting parser.

FromResult<T>(T)

Creates a parser which returns the specified value without consuming any input.

Declaration
public static Parser<TToken, T> FromResult<T>(T result)
Parameters
Type Name Description

T

result

The value to return.

Returns
Type Description

Parser<TToken, T>

A parser which returns the specified value without consuming any input.

Type Parameters
Name Description

T

The type of the value to return.

Return<T>(T)

Creates a parser which returns the specified value without consuming any input.

Declaration
public static Parser<TToken, T> Return<T>(T value)
Parameters
Type Name Description

T

value

The value to return.

Returns
Type Description

Parser<TToken, T>

A parser which returns the specified value without consuming any input.

Type Parameters
Name Description

T

The type of the value to return.

Sequence(params TToken[])

Creates a parser that parses and returns a literal sequence of tokens.

Declaration
public static Parser<TToken, TToken[]> Sequence(params TToken[] tokens)
Parameters
Type Name Description

TToken[]

tokens

A sequence of tokens.

Returns
Type Description

Parser<TToken, TToken[]>

A parser that parses a literal sequence of tokens.

Sequence<T>(params Parser<TToken, T>[])

Creates a parser that applies a sequence of parsers and collects the results. This parser fails if any of its constituent parsers fail.

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

Parser<TToken, T>[]

parsers

A sequence of parsers.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser that applies a sequence of parsers and collects the results.

Type Parameters
Name Description

T

The return type of the parsers.

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

Creates a parser that applies a sequence of parsers and collects the results. This parser fails if any of its constituent parsers fail.

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

IEnumerable<Parser<TToken, T>>

parsers

A sequence of parsers.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser that applies a sequence of parsers and collects the results.

Type Parameters
Name Description

T

The return type of the parsers.

Sequence<TEnumerable>(TEnumerable)

Creates a parser that parses and returns a literal sequence of tokens. The input enumerable is enumerated and copied to a list.

Declaration
public static Parser<TToken, TEnumerable> Sequence<TEnumerable>(TEnumerable tokens) where TEnumerable : IEnumerable<TToken>
Parameters
Type Name Description

TEnumerable

tokens

A sequence of tokens.

Returns
Type Description

Parser<TToken, TEnumerable>

A parser that parses a literal sequence of tokens.

Type Parameters
Name Description

TEnumerable

The type of tokens to parse.

Token(Func<TToken, bool>)

Creates a parser that parses and returns a single token satisfying a predicate.

Declaration
public static Parser<TToken, TToken> Token(Func<TToken, bool> predicate)
Parameters
Type Name Description

Func<TToken, bool>

predicate

A predicate function to apply to a token.

Returns
Type Description

Parser<TToken, TToken>

A parser that parses and returns a single token satisfying a predicate.

Token(TToken)

Creates a parser that parses and returns a single token.

Declaration
public static Parser<TToken, TToken> Token(TToken token)
Parameters
Type Name Description

TToken

token

The token to parse.

Returns
Type Description

Parser<TToken, TToken>

A parser that parses and returns a single token.