Skip to content

Commit 2496e6b

Browse files
Fixed Try
1 parent 907edc4 commit 2496e6b

File tree

1 file changed

+3
-23
lines changed

1 file changed

+3
-23
lines changed

APCSharp/Parser/ParserBuilder.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -239,31 +239,11 @@ private ParserBuilder RemoveEmptyMaybeMatches()
239239
/// <returns></returns>
240240
public ParserBuilder Maybe() => MaybeMatch().RemoveEmptyMaybeMatches();
241241
/// <summary>
242-
/// Fallback parse result if parser threw an exception of a certain type
243-
/// </summary>
244-
/// <param name="whenFail"></param>
245-
/// <returns></returns>
246-
public ParserBuilder Try<T>(PResult whenFail)
247-
where T : Exception
248-
{
249-
return new ParserBuilder(s =>
250-
{
251-
try
252-
{
253-
return Func(s);
254-
}
255-
catch(T)
256-
{
257-
return whenFail;
258-
}
259-
});
260-
}
261-
/// <summary>
262242
/// Generate parse result from custom function based on exception
263243
/// </summary>
264244
/// <param name="whenFail"></param>
265245
/// <returns></returns>
266-
public ParserBuilder Try<T>(Func<T, PResult> whenFail)
246+
public ParserBuilder Try<T>(Func<T, StreamReader, PResult> whenFail)
267247
where T : Exception
268248
{
269249
return new ParserBuilder(s =>
@@ -274,7 +254,7 @@ public ParserBuilder Try<T>(Func<T, PResult> whenFail)
274254
}
275255
catch(T e)
276256
{
277-
return whenFail(e);
257+
return whenFail(e, s);
278258
}
279259
});
280260
}
@@ -283,7 +263,7 @@ public ParserBuilder Try<T>(Func<T, PResult> whenFail)
283263
/// </summary>
284264
/// <param name="whenFail"></param>
285265
/// <returns></returns>
286-
public ParserBuilder Try(PResult whenFail) => Try<Exception>(whenFail);
266+
public ParserBuilder Try(Func<StreamReader, PResult> whenFail) => Try<Exception>((_, s) => whenFail(s));
287267
/// <summary>
288268
/// Match a any amount of trailing whitespace, this will be appended on the previous parsed result.
289269
/// If you only want to allow trailing whitespaces, use IgnoredWhitespaces().

0 commit comments

Comments
 (0)