Table of Contents

Class ParserExtensions

Namespace
Farkle
Assembly
Farkle.dll

Provides extension methods on IParser<TChar, T> to easily parse text from various sources.

public static class ParserExtensions
Inheritance
ParserExtensions

Remarks

This is the highest-level parser API of Farkle. It is recommended for most use cases.

Methods

ParseAsync<T>(IParser<char, T>, TextReader, CancellationToken)

Asynchronously parses a stream of characters read from a TextReader.

public static ValueTask<ParserResult<T>> ParseAsync<T>(this IParser<char, T> parser, TextReader reader, CancellationToken cancellationToken = default)

Parameters

parser IParser<char, T>

The IParser<TChar, T> to use.

reader TextReader

The TextReader to read the characters from.

cancellationToken CancellationToken

Used to cancel the parsing operation. Optional.

Returns

ValueTask<ParserResult<T>>

A ValueTask<TResult> that will return a ParserResult<T> containing the result of the parsing operation.

Type Parameters

T

The type of result the parser produces in case of success.

Remarks

reader will be read from until it ends or the parsing operation fails. reader will not be automatically disposed.

On frameworks not compatible with .NET Standard 2.1, cancelling the operation will not have effect until the next time the characters are read. This is due to TextReader not supporting passing a CancellationToken to its ReadAsync method.

Exceptions

ArgumentNullException

parser or reader is null.

ParseFileAsync<T>(IParser<char, T>, string, CancellationToken)

Asynchronously parses the content of a file. This method wraps ParseAsync<T>(IParser<char, T>, TextReader, CancellationToken).

public static ValueTask<ParserResult<T>> ParseFileAsync<T>(this IParser<char, T> parser, string path, CancellationToken cancellationToken = default)

Parameters

parser IParser<char, T>

The IParser<TChar, T> to use.

path string

The path to the file to parse.

cancellationToken CancellationToken

Used to cancel the parsing operation. Optional.

Returns

ValueTask<ParserResult<T>>

A ValueTask<TResult> that will return a ParserResult<T> containing the result of the parsing operation.

Type Parameters

T

The type of result the parser produces in case of success.

Remarks

The file will be read from until it ends or until the parsing operation fails.

On frameworks not compatible with .NET Standard 2.1, cancelling the operation will not have effect until the next time the characters are read. This is due to TextReader not supporting passing a CancellationToken to its ReadAsync method.

Exceptions

ArgumentNullException

parser or path is null.

ParseFile<T>(IParser<char, T>, string)

Parses the content of a file. This method wraps Parse<T>(IParser<char, T>, TextReader).

public static ParserResult<T> ParseFile<T>(this IParser<char, T> parser, string path)

Parameters

parser IParser<char, T>

The IParser<TChar, T> to use.

path string

The path to the file to parse.

Returns

ParserResult<T>

A ParserResult<T> containing the result of the parsing operation.

Type Parameters

T

The type of result the parser produces in case of success.

Remarks

The file will be read from until it ends or until the parsing operation fails.

Exceptions

ArgumentNullException

parser or path is null.

Parse<T>(IParser<char, T>, TextReader)

Parses a stream of characters read from a TextReader.

public static ParserResult<T> Parse<T>(this IParser<char, T> parser, TextReader reader)

Parameters

parser IParser<char, T>

The IParser<TChar, T> to use.

reader TextReader

The TextReader to read the characters from.

Returns

ParserResult<T>

A ParserResult<T> containing the result of the parsing operation.

Type Parameters

T

The type of result the parser produces in case of success.

Remarks

reader will be read from until it ends or the parsing operation fails. reader will not be automatically disposed.

Exceptions

ArgumentNullException

parser or reader is null.

Parse<T>(IParser<char, T>, string)

Parses a string.

public static ParserResult<T> Parse<T>(this IParser<char, T> parser, string s)

Parameters

parser IParser<char, T>

The IParser<TChar, T> to use.

s string

The string to parse.

Returns

ParserResult<T>

A ParserResult<T> containing the result of the parsing operation.

Type Parameters

T

The type of result the parser produces in case of success.

Exceptions

ArgumentNullException

parser or s is null.

Parse<TChar, T>(IParser<TChar, T>, ReadOnlySpan<TChar>)

Parses a ReadOnlySpan<T>.

public static ParserResult<T> Parse<TChar, T>(this IParser<TChar, T> parser, ReadOnlySpan<TChar> s)

Parameters

parser IParser<TChar, T>

The IParser<TChar, T> to use.

s ReadOnlySpan<TChar>

The span to parse.

Returns

ParserResult<T>

A ParserResult<T> containing the result of the parsing operation.

Type Parameters

TChar

The type of characters.

T

The type of result the parser produces in case of success.

Exceptions

ArgumentNullException

parser is null.