Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch to ParserExtensions #162

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/Superpower/ParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,63 @@ public static bool IsMatch<T>(this TextParser<T> parser, TextSpan input)
var result = parser(input);
return result.HasValue && result.Remainder.IsAtEnd;
}

/// <summary>
/// Attempts the parser and invokes the exceptionHandler if the parser throws <see cref="ParseException"/>.
/// </summary>
/// <typeparam name="TKind">The type of tokens consumed by the parser.</typeparam>
/// <typeparam name="T">The type of the result.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="exceptionHandler">A function that handles <see cref="ParseException"/> and returns a <see cref="TokenListParserResult{TKind, T}"/>.</param>
/// <returns>A parser that calls the first parser and handles <see cref="ParseException"/> by calling the exception handler.</returns>
/// <exception cref="ArgumentNullException">Thrown if either the parser or the exceptionHandler is null.</exception>
public static TokenListParser<TKind, T> Catch<TKind, T>(this TokenListParser<TKind, T> parser, Func<ParseException, TokenListParserResult<TKind, T>> exceptionHandler)
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
if (exceptionHandler == null) throw new ArgumentNullException(nameof(exceptionHandler));

return input =>
{
try
{
return parser(input);
}
catch (ParseException ex)
{
return exceptionHandler(ex);
}
};
}

/// <summary>
/// Attempts the parser and invokes the exceptionHandler if the parser throws TException.
/// </summary>
/// <typeparam name="TKind">The type of tokens consumed by the parser.</typeparam>
/// <typeparam name="T">The type of the result.</typeparam>
/// <typeparam name="TException">The type of exception caught and handled by <see cref="Catch{TKind, T, TException}(TokenListParser{TKind, T}, Func{TException, TokenListParserResult{TKind, T}})"/></typeparam>
/// <param name="parser">The parser.</param>
/// <param name="exceptionHandler">A function that handles TException and returns a <see cref="TokenListParserResult{TKind, T}"/>.</param>
/// <returns>A parser that calls the first parser and handles TException by calling the exception handler.</returns>
/// <exception cref="ArgumentNullException">Thrown if either the parser or the exceptionHandler is null.</exception>
public static TokenListParser<TKind, T> Catch<TKind, T, TException>(
this TokenListParser<TKind, T> parser,
Func<TException, TokenListParserResult<TKind, T>> exceptionHandler)
where TException : Exception
{
if (parser == null) throw new ArgumentNullException(nameof(parser));
if (exceptionHandler == null) throw new ArgumentNullException(nameof(exceptionHandler));

return input =>
{
try
{
return parser(input);
}
catch (TException ex)
{
return exceptionHandler(ex);
}
};
}
}
}