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
parserIParser<char, T>The IParser<TChar, T> to use.
readerTextReaderThe TextReader to read the characters from.
cancellationTokenCancellationTokenUsed 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
TThe 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
parserorreaderis 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
parserIParser<char, T>The IParser<TChar, T> to use.
pathstringThe path to the file to parse.
cancellationTokenCancellationTokenUsed 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
TThe 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
parserorpathis 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
parserIParser<char, T>The IParser<TChar, T> to use.
pathstringThe path to the file to parse.
Returns
- ParserResult<T>
A ParserResult<T> containing the result of the parsing operation.
Type Parameters
TThe 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
parserorpathis 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
parserIParser<char, T>The IParser<TChar, T> to use.
readerTextReaderThe TextReader to read the characters from.
Returns
- ParserResult<T>
A ParserResult<T> containing the result of the parsing operation.
Type Parameters
TThe 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
parserorreaderis null.
Parse<T>(IParser<char, T>, string)
Parses a string.
public static ParserResult<T> Parse<T>(this IParser<char, T> parser, string s)
Parameters
parserIParser<char, T>The IParser<TChar, T> to use.
sstringThe string to parse.
Returns
- ParserResult<T>
A ParserResult<T> containing the result of the parsing operation.
Type Parameters
TThe type of result the parser produces in case of success.
Exceptions
- ArgumentNullException
parserorsis 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
parserIParser<TChar, T>The IParser<TChar, T> to use.
sReadOnlySpan<TChar>The span to parse.
Returns
- ParserResult<T>
A ParserResult<T> containing the result of the parsing operation.
Type Parameters
TCharThe type of characters.
TThe type of result the parser produces in case of success.
Exceptions
- ArgumentNullException
parseris null.