Skip to content

Commit

Permalink
added Catch extension for datalust#87
Browse files Browse the repository at this point in the history
  • Loading branch information
marklauter committed Jun 13, 2024
1 parent 59cd73b commit 4f7137d
Showing 1 changed file with 58 additions and 0 deletions.
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="TException">The type of exception caught and handled by <see cref="Catch{TException, TKind, T}(TokenListParser{TKind, T}, Func{TException, TokenListParserResult{TKind, T}})"/></typeparam>
/// <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 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<TException, TKind, T>(
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);
}
};
}
}
}

0 comments on commit 4f7137d

Please sign in to comment.