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>.
Namespace: Pidgin
Assembly: Pidgin.dll
Syntax
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
Methods
| Improve this Doc View SourceAssert(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 |
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 |
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 |
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 |
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 |
Type Parameters
Name | Description |
---|---|
U |
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 |
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 |
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 |
Type Parameters
Name | Description |
---|---|
U | The type to cast the return value to |
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 |
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 |
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 |
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 |
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 |
Select<U>(Func<T, U>)
Creates a parser which applies the specified transformation function to the result of the current parser.
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Type Parameters
Name | Description |
---|---|
U |
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 |
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 |
Type Parameters
Name | Description |
---|---|
U | The return type of |
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 |
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>(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 |
Type Parameters
Name | Description |
---|---|
U | The return type of the second parser |
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 |
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>)
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 |
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.
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 |
Type Parameters
Name | Description |
---|---|
U |
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));
|
Improve this Doc
View Source
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 |
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 |
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. |
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 |
Type Parameters
Name | Description |
---|---|
U | The return type of |
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 |
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 |
Type Parameters
Name | Description |
---|---|
U |
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));