Pidgin

Class Parser<TToken, T>

Represents a parser which consumes a stream of values of type TToken and returns a value of type T. A parser can either succeed, and return a value of type T, or fail and return a ParseError<TToken>.

Inheritance
Declaration
public abstract class Parser<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.

Remarks

This type is not intended to be subclassed by users of the library.

Constructors

Parser()

Declaration
protected Parser()

Methods

Assert(Func<T, Boolean>)

Creates a parser that fails if the value returned by the current parser fails to satisfy a predicate.

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

Func<T, Boolean>

predicate

The predicate to apply to the value returned by the current parser.

Returns
Type Description

Parser<TToken, T>

A parser that fails if the value returned by the current parser fails to satisfy predicate.

Assert(Func<T, Boolean>, Func<T, String>)

Creates a parser that fails if the value returned by the current parser fails to satisfy a predicate.

Declaration
public Parser<TToken, T> Assert(Func<T, bool> predicate, Func<T, string> message)
Parameters
Type Name Description

Func<T, Boolean>

predicate

The predicate to apply to the value returned by the current parser.

Func<T, String>

message

A function to produce a custom error message to return when the value returned by the current parser fails to satisfy the predicate.

Returns
Type Description

Parser<TToken, T>

A parser that fails if the value returned by the current parser fails to satisfy predicate.

Assert(Func<T, Boolean>, String)

Creates a parser that fails if the value returned by the current parser fails to satisfy a predicate.

Declaration
public Parser<TToken, T> Assert(Func<T, bool> predicate, string message)
Parameters
Type Name Description

Func<T, Boolean>

predicate

The predicate to apply to the value returned by the current parser.

String

message

A custom error message to return when the value returned by the current parser fails to satisfy the predicate.

Returns
Type Description

Parser<TToken, T>

A parser that fails if the value returned by the current parser fails to satisfy predicate.

AtLeastOnce()

Creates a parser that applies the current parser one or more times. The resulting parser fails if the current parser fails the first time it is applied, or if the current parser fails after consuming input.

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

Parser<TToken, IEnumerable<T>>

A parser that applies the current parser one or more times.

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

Creates a parser which applies this parser one or more times until terminator succeeds. Fails if this parser fails or if terminator fails after consuming input.

Declaration
public Parser<TToken, (IEnumerable<T>, U)> AtLeastOnceThen<U>(Parser<TToken, U> terminator)
Parameters
Type Name Description

Parser<TToken, U>

terminator

A parser to parse a terminator.

Returns
Type Description

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

A parser which applies this parser repeatedly until terminator succeeds.

Type Parameters
Name Description

U

The return type of the terminator parser.

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

Creates a parser which applies this parser one or more times until terminator succeeds. Fails if this parser fails or if terminator fails after consuming input. The return value of terminator is ignored.

Declaration
public Parser<TToken, IEnumerable<T>> AtLeastOnceUntil<U>(Parser<TToken, U> terminator)
Parameters
Type Name Description

Parser<TToken, U>

terminator

A parser to parse a terminator.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser which applies this parser repeatedly until terminator succeeds.

Type Parameters
Name Description

U

The return type of the terminator parser.

Remarks

p.AtLeastOnceUntil(q) is equivalent to p.AtLeastOnceThen(q).Select(t => t.Item1).

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

Creates a parser that applies the current parser followed by the specified parser. The resulting parser returns the result of the current parser, ignoring the result of the second parser.

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

Parser<TToken, U>

parser

The parser to apply after applying the current parser.

Returns
Type Description

Parser<TToken, T>

A parser that applies the current parser followed by the specified parser.

Type Parameters
Name Description

U

The type of the value returned by the second parser.

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

Creates a parser that applies the specified parser both before and after applying the current parser. The resulting parser returns the result of the current parser, ignoring the return value of the bracketing parser.

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

Parser<TToken, U>

parser

The parser to apply before and after applying the current parser.

Returns
Type Description

Parser<TToken, T>

A parser that applies the specified parser before and after applying the current parser.

Type Parameters
Name Description

U

The type of the value returned by the bracketing parser.

Between<U, V>(Parser<TToken, U>, Parser<TToken, V>)

Creates a parser that applies the specified parsers before and after applying the current parser. The resulting parser returns the result of the current parser, ignoring the return values of the bracketing parsers.

Declaration
public Parser<TToken, T> Between<U, V>(Parser<TToken, U> parser1, Parser<TToken, V> parser2)
Parameters
Type Name Description

Parser<TToken, U>

parser1

The parser to apply before applying the current parser.

Parser<TToken, V>

parser2

The parser to apply after applying the current parser.

Returns
Type Description

Parser<TToken, T>

A parser that applies the specified parsers before and after applying the current parser.

Type Parameters
Name Description

U

The type of the value returned by the first parser.

V

The type of the value returned by the second parser.

Bind<U>(Func<T, Parser<TToken, U>>)

Creates a parser that applies a transformation function to the return value of the current parser. The transformation function dynamically chooses a second parser, which is applied after applying the current parser.

Declaration
public Parser<TToken, U> Bind<U>(Func<T, Parser<TToken, U>> selector)
Parameters
Type Name Description

Func<T, Parser<TToken, U>>

selector

A transformation function which returns a parser to apply after applying the current parser.

Returns
Type Description

Parser<TToken, U>

A parser which applies the current parser before applying the result of the selector function.

Type Parameters
Name Description

U

The type of the return value of the second parser.

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

Creates a parser that applies a transformation function to the return value of the current parser. The transformation function dynamically chooses a second parser, which is applied after applying the current parser.

Declaration
public Parser<TToken, R> Bind<U, R>(Func<T, Parser<TToken, U>> selector, Func<T, U, R> result)
Parameters
Type Name Description

Func<T, Parser<TToken, U>>

selector

A transformation function which returns a parser to apply after applying the current parser.

Func<T, U, R>

result

A function to apply to the return values of the two parsers.

Returns
Type Description

Parser<TToken, R>

A parser which applies the current parser before applying the result of the selector function.

Type Parameters
Name Description

U

The type of the return value of the second parser.

R

The type of the return value of the resulting parser.

Cast<U>()

Cast the return value of the current parser to the specified result type.

Declaration
public Parser<TToken, U> Cast<U>()
Returns
Type Description

Parser<TToken, U>

A parser which returns this parser's return value casted to U.

Type Parameters
Name Description

U

The type to cast the return value to.

Exceptions
Type Condition

InvalidCastException

Thrown when the return value is not an instance of U.

IgnoreResult()

Creates a parser which behaves like the current parser but returns Value. Equivalent to p.WithResult(Unit.Value).

Declaration
public Parser<TToken, Unit> IgnoreResult()
Returns
Type Description

Parser<TToken, Unit>

A parser which behaves like the current parser but returns Value.

Labelled(String)

Creates a parser equivalent to the current parser, with a custom label. The label will be reported in an error message if the parser fails, instead of the default error message. Expected Label

Declaration
public Parser<TToken, T> Labelled(string label)
Parameters
Type Name Description

String

label

The custom label to apply to the current parser.

Returns
Type Description

Parser<TToken, T>

A parser equivalent to the current parser, with a custom label.

Many()

Creates a parser which applies the current parser zero or more times. The resulting parser fails if the current parser fails after consuming input.

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

Parser<TToken, IEnumerable<T>>

A parser which applies the current parser zero or more times.

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

Creates a parser which applies this parser zero or more times until terminator succeeds. Fails if this parser fails or if terminator fails after consuming input.

Declaration
public Parser<TToken, (IEnumerable<T>, U)> ManyThen<U>(Parser<TToken, U> terminator)
Parameters
Type Name Description

Parser<TToken, U>

terminator

A parser to parse a terminator.

Returns
Type Description

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

A parser which applies this parser repeatedly until terminator succeeds.

Type Parameters
Name Description

U

The return type of terminator.

Map<U>(Func<T, U>)

Creates a parser which applies the specified transformation function to the result of the current parser. This is an infix synonym for Map<TToken, T1, R>(Func<T1, R>, Parser<TToken, T1>).

Declaration
public Parser<TToken, U> Map<U>(Func<T, U> selector)
Parameters
Type Name Description

Func<T, U>

selector

A transformation function.

Returns
Type Description

Parser<TToken, U>

A parser which applies selector to the result of the current parser.

Type Parameters
Name Description

U

The return type of the transformation function.

MapWithInput<U>(ReadOnlySpanFunc<TToken, T, U>)

Returns a parser which runs the current parser and applies a selector function. The selector function receives a ReadOnlySpan<T> as its first argument, and the result of the current parser as its second argument. The ReadOnlySpan<T> represents the sequence of input tokens which were consumed by the parser.

This allows you to write "pattern"-style parsers which match a sequence of tokens and return a view of the part of the input stream which they matched.

This function is an alternative name for Slice<U>(ReadOnlySpanFunc<TToken, T, U>).

Declaration
public Parser<TToken, U> MapWithInput<U>(ReadOnlySpanFunc<TToken, T, U> selector)
Parameters
Type Name Description

ReadOnlySpanFunc<TToken, T, U>

selector

A selector function which computes a result of type U. The arguments of the selector function are a ReadOnlySpan<T> containing the sequence of input tokens which were consumed by this parser, and the result of this parser.

Returns
Type Description

Parser<TToken, U>

A parser which runs the current parser and applies a selector function.

Type Parameters
Name Description

U

The result type.

OfType<U>()

Creates a parser which casts the return value of the current parser to the specified result type, or fails if the return value is not an instance of U.

Declaration
public Parser<TToken, U> OfType<U>()
Returns
Type Description

Parser<TToken, U>

A parser which returns the current parser's return value casted to U, if the value is an instance of U.

Type Parameters
Name Description

U

The type to cast the return value of the current parser to.

Optional()

Creates a parser which applies the current parser and returns Nothing<T>() if the current parser fails without consuming any input. The resulting parser fails if the current parser fails after consuming input.

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

Parser<TToken, Maybe<T>>

A parser which applies the current parser and returns Nothing<T>() if the current parser fails without consuming any input.

Or(Parser<TToken, T>)

Creates a parser which tries to apply the current parser, applying the specified parser if the current parser fails without consuming any input. The resulting parser fails if both the current parser and the alternative parser fail, or if the current parser fails after consuming input.

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

Parser<TToken, T>

parser

The alternative parser to apply if the current parser fails without consuming any input.

Returns
Type Description

Parser<TToken, T>

A parser which tries to apply the current parser, and applies parser if the current parser fails without consuming any input.

RecoverWith(Func<ParseError<TToken>, Parser<TToken, T>>)

Creates a parser which runs the current parser, running errorHandler on failure.

Declaration
public Parser<TToken, T> RecoverWith(Func<ParseError<TToken>, Parser<TToken, T>> errorHandler)
Parameters
Type Name Description

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

errorHandler

A function which returns a parser to apply when the current parser fails.

Returns
Type Description

Parser<TToken, T>

A parser which runs the current parser, running errorHandler on failure.

Repeat(Int32)

Creates a parser which applies the current parser count times.

Declaration
public Parser<TToken, IEnumerable<T>> Repeat(int count)
Parameters
Type Name Description

Int32

count

The number of times to apply the current parser.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser which applies the current parser count times.

Exceptions
Type Condition

InvalidOperationException

count is less than 0.

Select<U>(Func<T, U>)

Creates a parser which applies the specified transformation function to the result of the current parser. This is an infix synonym for Map<TToken, T1, R>(Func<T1, R>, Parser<TToken, T1>).

Declaration
public Parser<TToken, U> Select<U>(Func<T, U> selector)
Parameters
Type Name Description

Func<T, U>

selector

A transformation function.

Returns
Type Description

Parser<TToken, U>

A parser which applies selector to the result of the current parser.

Type Parameters
Name Description

U

The return type of the transformation function.

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

Creates a parser that applies a transformation function to the return value of the current parser. The transformation function dynamically chooses a second parser, which is applied after applying the current parser.

Declaration
public Parser<TToken, R> SelectMany<U, R>(Func<T, Parser<TToken, U>> selector, Func<T, U, R> result)
Parameters
Type Name Description

Func<T, Parser<TToken, U>>

selector

A transformation function which returns a parser to apply after applying the current parser.

Func<T, U, R>

result

A function to apply to the return values of the two parsers.

Returns
Type Description

Parser<TToken, R>

A parser which applies the current parser before applying the result of the selector function.

Type Parameters
Name Description

U

The type of the return value of the second parser.

R

The type of the return value of the resulting parser.

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

Creates a parser which applies the current parser repeatedly, interleaved with a specified parser. The resulting parser ignores the return value of the separator parser.

Declaration
public Parser<TToken, IEnumerable<T>> Separated<U>(Parser<TToken, U> separator)
Parameters
Type Name Description

Parser<TToken, U>

separator

A parser which parses a separator to be interleaved with the current parser.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser which applies the current parser repeatedly, interleaved by separator.

Type Parameters
Name Description

U

The return type of the separator parser.

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

Creates a parser which applies the current parser repeatedly, interleaved and optionally terminated with a specified parser. The resulting parser ignores the return value of the separator parser.

Declaration
public Parser<TToken, IEnumerable<T>> SeparatedAndOptionallyTerminated<U>(Parser<TToken, U> separator)
Parameters
Type Name Description

Parser<TToken, U>

separator

A parser which parses a separator to be interleaved with the current parser.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser which applies the current parser repeatedly, interleaved and optionally terminated by separator.

Type Parameters
Name Description

U

The return type of the separator parser.

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

Creates a parser which applies the current parser at least once, interleaved and optionally terminated with a specified parser. The resulting parser ignores the return value of the separator parser.

Declaration
public Parser<TToken, IEnumerable<T>> SeparatedAndOptionallyTerminatedAtLeastOnce<U>(Parser<TToken, U> separator)
Parameters
Type Name Description

Parser<TToken, U>

separator

A parser which parses a separator to be interleaved with the current parser.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser which applies the current parser at least once, interleaved and optionally terminated by separator.

Type Parameters
Name Description

U

The return type of the separator parser.

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

Creates a parser which applies the current parser repeatedly, interleaved and terminated with a specified parser. The resulting parser ignores the return value of the separator parser.

Declaration
public Parser<TToken, IEnumerable<T>> SeparatedAndTerminated<U>(Parser<TToken, U> separator)
Parameters
Type Name Description

Parser<TToken, U>

separator

A parser which parses a separator to be interleaved with the current parser.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser which applies the current parser repeatedly, interleaved and terminated by separator.

Type Parameters
Name Description

U

The return type of the separator parser.

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

Creates a parser which applies the current parser at least once, interleaved and terminated with a specified parser. The resulting parser ignores the return value of the separator parser.

Declaration
public Parser<TToken, IEnumerable<T>> SeparatedAndTerminatedAtLeastOnce<U>(Parser<TToken, U> separator)
Parameters
Type Name Description

Parser<TToken, U>

separator

A parser which parses a separator to be interleaved with the current parser.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser which applies the current parser at least once, interleaved and terminated by separator.

Type Parameters
Name Description

U

The return type of the separator parser.

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

Creates a parser which applies the current parser at least once, interleaved with a specified parser. The resulting parser ignores the return value of the separator parser.

Declaration
public Parser<TToken, IEnumerable<T>> SeparatedAtLeastOnce<U>(Parser<TToken, U> separator)
Parameters
Type Name Description

Parser<TToken, U>

separator

A parser which parses a separator to be interleaved with the current parser.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser which applies the current parser at least once, interleaved by separator.

Type Parameters
Name Description

U

The return type of the separator parser.

SkipAtLeastOnce()

Creates a parser that applies the current parser one or more times, discarding the results. This is more efficient than AtLeastOnce(), if you don't need the results. The resulting parser fails if the current parser fails the first time it is applied, or if the current parser fails after consuming input.

Declaration
public Parser<TToken, Unit> SkipAtLeastOnce()
Returns
Type Description

Parser<TToken, Unit>

A parser that applies the current parser one or more times, discarding the results.

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

Creates a parser which applies this parser one or more times until terminator succeeds, discarding the results. This is more efficient than AtLeastOnceThen<U>(Parser<TToken, U>) if you don't need the results. Fails if this parser fails or if terminator fails after consuming input.

Declaration
public Parser<TToken, U> SkipAtLeastOnceThen<U>(Parser<TToken, U> terminator)
Parameters
Type Name Description

Parser<TToken, U>

terminator

A parser to parse a terminator.

Returns
Type Description

Parser<TToken, U>

A parser which applies this parser repeatedly until terminator succeeds, discarding the results.

Type Parameters
Name Description

U

The return type of the terminator parser.

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

Creates a parser which applies this parser one or more times until terminator succeeds, discarding the results. This is more efficient than AtLeastOnceUntil<U>(Parser<TToken, U>) if you don't need the results. Fails if this parser fails or if terminator fails after consuming input. The return value of terminator is ignored.

Declaration
public Parser<TToken, Unit> SkipAtLeastOnceUntil<U>(Parser<TToken, U> terminator)
Parameters
Type Name Description

Parser<TToken, U>

terminator

A parser to parse a terminator.

Returns
Type Description

Parser<TToken, Unit>

A parser which applies this parser repeatedly until terminator succeeds, discarding the results.

Type Parameters
Name Description

U

The return type of the terminator parser.

Remarks

p.SkipAtLeastOnceUntil(q) is equivalent to p.SkipAtLeastOnceThen(q).ThenReturn(Unit.Value).

SkipMany()

Creates a parser which applies the current parser zero or more times, discarding the results. This is more efficient than Many(), if you don't need the results. The resulting parser fails if the current parser fails after consuming input.

Declaration
public Parser<TToken, Unit> SkipMany()
Returns
Type Description

Parser<TToken, Unit>

A parser which applies the current parser zero or more times.

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

Creates a parser which applies this parser zero or more times until terminator succeeds, discarding the results. This is more efficient than ManyThen<U>(Parser<TToken, U>) if you don't need the results. Fails if this parser fails or if terminator fails after consuming input.

Declaration
public Parser<TToken, U> SkipManyThen<U>(Parser<TToken, U> terminator)
Parameters
Type Name Description

Parser<TToken, U>

terminator

A parser to parse a terminator.

Returns
Type Description

Parser<TToken, U>

A parser which applies this parser repeatedly until terminator succeeds, discarding the results.

Type Parameters
Name Description

U

The return type of terminator.

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

Creates a parser which applies this parser zero or more times until terminator succeeds, discarding the results. This is more efficient than Until<U>(Parser<TToken, U>) if you don't need the results. Fails if this parser fails or if terminator fails after consuming input. The return value of terminator is ignored.

Declaration
public Parser<TToken, Unit> SkipUntil<U>(Parser<TToken, U> terminator)
Parameters
Type Name Description

Parser<TToken, U>

terminator

A parser to parse a terminator.

Returns
Type Description

Parser<TToken, Unit>

A parser which applies this parser repeatedly until terminator succeeds, discarding the results.

Type Parameters
Name Description

U

The return type of terminator.

Remarks

p.SkipUntil(q) is equivalent to p.SkipManyThen(q).ThenReturn(Unit.Value).

Slice<U>(ReadOnlySpanFunc<TToken, T, U>)

Returns a parser which runs the current parser and applies a selector function. The selector function receives a ReadOnlySpan<T> as its first argument, and the result of the current parser as its second argument. The ReadOnlySpan<T> represents the sequence of input tokens which were consumed by the parser.

This allows you to write "pattern"-style parsers which match a sequence of tokens and return a view of the part of the input stream which they matched.

This function is an alternative name for MapWithInput<U>(ReadOnlySpanFunc<TToken, T, U>).

Declaration
public Parser<TToken, U> Slice<U>(ReadOnlySpanFunc<TToken, T, U> selector)
Parameters
Type Name Description

ReadOnlySpanFunc<TToken, T, U>

selector

A selector function which computes a result of type U. The arguments of the selector function are a ReadOnlySpan<T> containing the sequence of input tokens which were consumed by this parser, and the result of this parser.

Returns
Type Description

Parser<TToken, U>

A parser which runs the current parser and applies a selector function.

Type Parameters
Name Description

U

The result type.

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

Creates a parser which applies the current parser followed by a specified parser. The resulting parser returns the result of the second parser, ignoring the result of the current parser.

Declaration
public Parser<TToken, U> Then<U>(Parser<TToken, U> parser)
Parameters
Type Name Description

Parser<TToken, U>

parser

A parser to apply after applying the current parser.

Returns
Type Description

Parser<TToken, U>

A parser which applies the current parser followed by parser.

Type Parameters
Name Description

U

The return type of the second parser.

Then<U>(Func<T, Parser<TToken, U>>)

Creates a parser that applies a transformation function to the return value of the current parser. The transformation function dynamically chooses a second parser, which is applied after applying the current parser.

Declaration
public Parser<TToken, U> Then<U>(Func<T, Parser<TToken, U>> selector)
Parameters
Type Name Description

Func<T, Parser<TToken, U>>

selector

A transformation function which returns a parser to apply after applying the current parser.

Returns
Type Description

Parser<TToken, U>

A parser which applies the current parser before applying the result of the selector function.

Type Parameters
Name Description

U

The type of the return value of the second parser.

Remarks

This function is a synonym for Bind<U>(Func<T, Parser<TToken, U>>).

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

Creates a parser which applies the current parser followed by a specified parser, applying a function to the two results.

Declaration
public Parser<TToken, R> Then<U, R>(Parser<TToken, U> parser, Func<T, U, R> result)
Parameters
Type Name Description

Parser<TToken, U>

parser

A parser to apply after applying the current parser.

Func<T, U, R>

result

A function to apply to the two parsed values.

Returns
Type Description

Parser<TToken, R>

A parser which applies the current parser followed by parser.

Type Parameters
Name Description

U

The return type of the second parser.

R

The return type of the composed parser.

Remarks

This is a synonym for Map<TToken, T1, T2, R>(Func<T1, T2, R>, Parser<TToken, T1>, Parser<TToken, T2>) with the arguments rearranged.

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

Creates a parser that applies a transformation function to the return value of the current parser. The transformation function dynamically chooses a second parser, which is applied after applying the current parser.

Declaration
public Parser<TToken, R> Then<U, R>(Func<T, Parser<TToken, U>> selector, Func<T, U, R> result)
Parameters
Type Name Description

Func<T, Parser<TToken, U>>

selector

A transformation function which returns a parser to apply after applying the current parser.

Func<T, U, R>

result

A function to apply to the return values of the two parsers.

Returns
Type Description

Parser<TToken, R>

A parser which applies the current parser before applying the result of the selector function.

Type Parameters
Name Description

U

The type of the return value of the second parser.

R

The type of the return value of the resulting parser.

Remarks

This function is a synonym for Bind<U, R>(Func<T, Parser<TToken, U>>, Func<T, U, R>).

ThenReturn<U>(U)

Creates a parser which behaves like the current parser but returns result after a successful parse. This is a synonym for WithResult<U>(U).

Declaration
public Parser<TToken, U> ThenReturn<U>(U result)
Parameters
Type Name Description

U

result

The result.

Returns
Type Description

Parser<TToken, U>

A parser which behaves like the current parser but returns result.

Type Parameters
Name Description

U

The type of the result.

Examples

Equivalent to using Select<U>(Func<T, U>) with a function that returns a fixed value, or Then<U>(Parser<TToken, U>) with Return<T>(T).

p.ThenReturn(x) == p.Select(_ => x) == p.Then(Return(x));

Trace(Func<T, String>)

For debugging use.

Creates a new parser which runs the current parser and prints the given message to the console.

Declaration
public Parser<TToken, T> Trace(Func<T, string> message)
Parameters
Type Name Description

Func<T, String>

message

A message to write to the console.

Returns
Type Description

Parser<TToken, T>

A parser which runs the current parser and prints the given message to the console.

Trace(String)

For debugging use.

Creates a new parser which runs the current parser and prints the given message to the console.

Declaration
public Parser<TToken, T> Trace(string message)
Parameters
Type Name Description

String

message

A message to write to the console.

Returns
Type Description

Parser<TToken, T>

A parser which runs the current parser and prints the given message to the console.

TraceResult()

For debugging use.

Creates a new parser which runs the current parser and prints the result to the console.

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

Parser<TToken, T>

A parser which runs the current parser and prints the result to the console.

TryParse(ref ParseState<TToken>, ref PooledList<Expected<TToken>>, out T)

Override this method to implement a custom parser. Use this if you can't do what you need using the base parser combinators.

If your parser fails it should return false and call SetError(Maybe<TToken>, Boolean, Int32, String).

WARNING: This API is unstable and subject to change in future versions of the library.

Declaration
public abstract bool TryParse(ref ParseState<TToken> state, ref PooledList<Expected<TToken>> expecteds, out T result)
Parameters
Type Name Description

ParseState<TToken>

state

The parser's state.

PooledList<Expected<TToken>>

expecteds

A list to which the parser can add its expected tokens when it fails.

T

result

The result.

Returns
Type Description

Boolean

True if the parser succeeded, false if it failed.

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

Creates a parser which applies this parser zero or more times until terminator succeeds. Fails if this parser fails or if terminator fails after consuming input. The return value of terminator is ignored.

Declaration
public Parser<TToken, IEnumerable<T>> Until<U>(Parser<TToken, U> terminator)
Parameters
Type Name Description

Parser<TToken, U>

terminator

A parser to parse a terminator.

Returns
Type Description

Parser<TToken, IEnumerable<T>>

A parser which applies this parser repeatedly until terminator succeeds.

Type Parameters
Name Description

U

The return type of terminator.

Remarks

p.Until(q) is equivalent to p.ManyThen(q).Select(t => t.Item1).

Where(Func<T, Boolean>)

Creates a parser that fails if the value returned by the current parser fails to satisfy a predicate.

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

Func<T, Boolean>

predicate

The predicate to apply to the value returned by the current parser.

Returns
Type Description

Parser<TToken, T>

A parser that fails if the value returned by the current parser fails to satisfy predicate.

Remarks

This function is a synonym of Assert(Func<T, Boolean>).

WithResult<U>(U)

Creates a parser which behaves like the current parser but returns result after a successful parse. This is a synonym for ThenReturn<U>(U).

Declaration
public Parser<TToken, U> WithResult<U>(U result)
Parameters
Type Name Description

U

result

The result.

Returns
Type Description

Parser<TToken, U>

A parser which behaves like the current parser but returns result.

Type Parameters
Name Description

U

The type of the result.

Examples

Equivalent to using Select<U>(Func<T, U>) with a function that returns a fixed value, or Then<U>(Parser<TToken, U>) with Return<T>(T).

p.WithResult(x) == p.Select(_ => x) == p.Then(Return(x));

Extension Methods

ParserExtensions.Parse<TToken, T>(Parser<TToken, T>, IList<TToken>, IConfiguration<TToken>)
ParserExtensions.ParseReadOnlyList<TToken, T>(Parser<TToken, T>, IReadOnlyList<TToken>, IConfiguration<TToken>)
ParserExtensions.Parse<TToken, T>(Parser<TToken, T>, IEnumerable<TToken>, IConfiguration<TToken>)
ParserExtensions.Parse<TToken, T>(Parser<TToken, T>, IEnumerator<TToken>, IConfiguration<TToken>)
ParserExtensions.Parse<TToken, T>(Parser<TToken, T>, TToken[], IConfiguration<TToken>)
ParserExtensions.Parse<TToken, T>(Parser<TToken, T>, ReadOnlySpan<TToken>, IConfiguration<TToken>)
ParserExtensions.ParseOrThrow<TToken, T>(Parser<TToken, T>, IList<TToken>, IConfiguration<TToken>)
ParserExtensions.ParseReadOnlyListOrThrow<TToken, T>(Parser<TToken, T>, IReadOnlyList<TToken>, IConfiguration<TToken>)
ParserExtensions.ParseOrThrow<TToken, T>(Parser<TToken, T>, IEnumerable<TToken>, IConfiguration<TToken>)
ParserExtensions.ParseOrThrow<TToken, T>(Parser<TToken, T>, IEnumerator<TToken>, IConfiguration<TToken>)
ParserExtensions.ParseOrThrow<TToken, T>(Parser<TToken, T>, TToken[], IConfiguration<TToken>)
ParserExtensions.ParseOrThrow<TToken, T>(Parser<TToken, T>, ReadOnlySpan<TToken>, IConfiguration<TToken>)
In This Article