Pidgin

Class PermutationParser<TToken, T>

A permutation parser represents a collection of parsers which can be run in an order-insensitive manner.

Declaration modifiers in C# are an example of an order-insensitive grammar. Modifiers can appear in any order: protected internal static readonly int x; means the same as internal readonly protected static int x;.

Usage of this class involves calling Add<U>(Parser<TToken, U>) or AddOptional<U>(Parser<TToken, U>, U) to add parsers to the permutation parser, and then calling Build() to create a parser which runs them in an order-insensitive manner and returns the results in a nested tuple.

Note that the parsers that are added to the permutation parser must always consume input before succeeding. If a parser succeeds on empty input the permutation parser will not work correctly. If you want to run a parser optionally, use AddOptional<U>(Parser<TToken, U>, U).

This class is immutable.

Inheritance
  • Object
  • PermutationParser<TToken, T>
Declaration
public sealed class PermutationParser<TToken, T> : Object
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.

Methods

Add<U>(Parser<TToken, U>)

Adds a parser to the collection.

Declaration
public PermutationParser<TToken, (T, U)> Add<U>(Parser<TToken, U> parser)
Parameters
Type Name Description

Parser<TToken, U>

parser

The parser to add to the collection.

Returns
Type Description

PermutationParser<TToken, (, )<T, U>>

A new permutation parser representing the current collection of parsers with parser added.

Type Parameters
Name Description

U

The return type of the parser to add to the collection.

Add<U, R>(Parser<TToken, U>, Func<T, U, R>)

Adds a parser to the collection.

Declaration
public PermutationParser<TToken, R> Add<U, R>(Parser<TToken, U> parser, Func<T, U, R> resultSelector)
Parameters
Type Name Description

Parser<TToken, U>

parser

The parser to add to the collection.

Func<T, U, R>

resultSelector

A transformation function to apply to the result of the current permutation parser and the result of parser.

Returns
Type Description

PermutationParser<TToken, R>

A new permutation parser representing the current collection of parsers with parser added.

Type Parameters
Name Description

U

The return type of the parser to add to the collection.

R

The return type of the resulting permutation parser.

AddOptional<U>(Parser<TToken, U>)

Adds an optional parser to the collection.

The resulting permutation parser will successfully parse a phrase even if parser never succeeds. In that case, Nothing<T>() will be returned.

Declaration
public PermutationParser<TToken, (T, Maybe<U>)> AddOptional<U>(Parser<TToken, U> parser)
Parameters
Type Name Description

Parser<TToken, U>

parser

The parser to add to the collection.

Returns
Type Description

PermutationParser<TToken, (, )<T, Maybe<U>>>

A new permutation parser representing the current collection of parsers with parser added optionally.

Type Parameters
Name Description

U

The return type of the parser to add to the collection.

AddOptional<U>(Parser<TToken, U>, U)

Adds an optional parser to the collection.

The resulting permutation parser will successfully parse a phrase even if parser never succeeds. In that case, defaultValue will be returned.

Declaration
public PermutationParser<TToken, (T, U)> AddOptional<U>(Parser<TToken, U> parser, U defaultValue)
Parameters
Type Name Description

Parser<TToken, U>

parser

The parser to add to the collection.

U

defaultValue

A default value to return if parser fails.

Returns
Type Description

PermutationParser<TToken, (, )<T, U>>

A new permutation parser representing the current collection of parsers with parser added optionally.

Type Parameters
Name Description

U

The return type of the parser to add to the collection.

AddOptional<U>(Parser<TToken, U>, Func<U>)

Adds an optional parser to the collection.

The resulting permutation parser will successfully parse a phrase even if parser never succeeds. In that case, defaultValueFactory will be called to get a value to return.

Declaration
public PermutationParser<TToken, (T, U)> AddOptional<U>(Parser<TToken, U> parser, Func<U> defaultValueFactory)
Parameters
Type Name Description

Parser<TToken, U>

parser

The parser to add to the collection.

Func<U>

defaultValueFactory

A factory for a default value to return if parser fails.

Returns
Type Description

PermutationParser<TToken, (, )<T, U>>

A new permutation parser representing the current collection of parsers with parser added optionally.

Type Parameters
Name Description

U

The return type of the parser to add to the collection.

AddOptional<U, R>(Parser<TToken, U>, U, Func<T, U, R>)

Adds an optional parser to the collection.

The resulting permutation parser will successfully parse a phrase even if parser never succeeds. In that case, defaultValue will be returned.

Declaration
public PermutationParser<TToken, R> AddOptional<U, R>(Parser<TToken, U> parser, U defaultValue, Func<T, U, R> resultSelector)
Parameters
Type Name Description

Parser<TToken, U>

parser

The parser to add to the collection.

U

defaultValue

A default value to return if parser fails.

Func<T, U, R>

resultSelector

A transformation function to apply to the result of the current permutation parser and the result of parser.

Returns
Type Description

PermutationParser<TToken, R>

A new permutation parser representing the current collection of parsers with parser added optionally.

Type Parameters
Name Description

U

The return type of the parser to add to the collection.

R

The return type of the resulting permutation parser.

AddOptional<U, R>(Parser<TToken, U>, Func<U>, Func<T, U, R>)

Adds an optional parser to the collection.

The resulting permutation parser will successfully parse a phrase even if parser never succeeds. In that case, defaultValueFactory will be called to get a value to return.

Declaration
public PermutationParser<TToken, R> AddOptional<U, R>(Parser<TToken, U> parser, Func<U> defaultValueFactory, Func<T, U, R> resultSelector)
Parameters
Type Name Description

Parser<TToken, U>

parser

The parser to add to the collection.

Func<U>

defaultValueFactory

A factory for a default value to return if parser fails.

Func<T, U, R>

resultSelector

A transformation function to apply to the result of the current permutation parser and the result of parser.

Returns
Type Description

PermutationParser<TToken, R>

A new permutation parser representing the current collection of parsers with parser added optionally.

Type Parameters
Name Description

U

The return type of the parser to add to the collection.

R

The return type of the resulting permutation parser.

AddOptional<U, R>(Parser<TToken, U>, Func<T, Maybe<U>, R>)

Adds an optional parser to the collection.

The resulting permutation parser will successfully parse a phrase even if parser never succeeds. In that case, Nothing<T>() will be returned.

Declaration
public PermutationParser<TToken, R> AddOptional<U, R>(Parser<TToken, U> parser, Func<T, Maybe<U>, R> resultSelector)
Parameters
Type Name Description

Parser<TToken, U>

parser

The parser to add to the collection.

Func<T, Maybe<U>, R>

resultSelector

A transformation function to apply to the result of the current permutation parser and the result of parser.

Returns
Type Description

PermutationParser<TToken, R>

A new permutation parser representing the current collection of parsers with parser added optionally.

Type Parameters
Name Description

U

The return type of the parser to add to the collection.

R

The return type of the resulting permutation parser.

Build()

Creates a Parser<TToken, T> which runs the current collection of parsers in an order-insensitive manner.

Declaration
public Parser<TToken, T> Build()
Returns
Type Description

Parser<TToken, T>

A Parser<TToken, T> which runs the current collection of parsers in an order-insensitive manner.