Skip to content

Commit 9be4ea0

Browse files
authored
Merge pull request #131 from AndrewSav/where-message
Added optional error message to the Where combinator
2 parents 97acd62 + 09a8b92 commit 9be4ea0

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/Superpower/Combinators.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,11 +1034,13 @@ public static TextParser<U> Value<T, U>(this TextParser<T> parser, U value)
10341034
/// <typeparam name="T">The type of value being parsed.</typeparam>
10351035
/// <param name="parser">The parser.</param>
10361036
/// <param name="predicate">The predicate to apply.</param>
1037+
/// <param name="message">An optional error message when parsing fails.</param>
10371038
/// <returns>The resulting parser.</returns>
1038-
public static TokenListParser<TKind, T> Where<TKind, T>(this TokenListParser<TKind, T> parser, Func<T, bool> predicate)
1039+
public static TokenListParser<TKind, T> Where<TKind, T>(this TokenListParser<TKind, T> parser, Func<T, bool> predicate, string message = "unsatisfied condition")
10391040
{
10401041
if (parser == null) throw new ArgumentNullException(nameof(parser));
10411042
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
1043+
if (message == null) throw new ArgumentNullException(nameof(message));
10421044

10431045
return input =>
10441046
{
@@ -1049,7 +1051,7 @@ public static TokenListParser<TKind, T> Where<TKind, T>(this TokenListParser<TKi
10491051
if (predicate(rt.Value))
10501052
return rt;
10511053

1052-
return TokenListParserResult.Empty<TKind, T>(input, "unsatisfied condition");
1054+
return TokenListParserResult.Empty<TKind, T>(input, message);
10531055
};
10541056
}
10551057

@@ -1060,11 +1062,13 @@ public static TokenListParser<TKind, T> Where<TKind, T>(this TokenListParser<TKi
10601062
/// <typeparam name="T">The type of value being parsed.</typeparam>
10611063
/// <param name="parser">The parser.</param>
10621064
/// <param name="predicate">The predicate to apply.</param>
1065+
/// <param name="message">An optional error message when parsing fails.</param>
10631066
/// <returns>The resulting parser.</returns>
1064-
public static TextParser<T> Where<T>(this TextParser<T> parser, Func<T, bool> predicate)
1067+
public static TextParser<T> Where<T>(this TextParser<T> parser, Func<T, bool> predicate, string message = "unsatisfied condition")
10651068
{
10661069
if (parser == null) throw new ArgumentNullException(nameof(parser));
10671070
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
1071+
if (message == null) throw new ArgumentNullException(nameof(message));
10681072

10691073
return input =>
10701074
{
@@ -1075,7 +1079,7 @@ public static TextParser<T> Where<T>(this TextParser<T> parser, Func<T, bool> pr
10751079
if (predicate(rt.Value))
10761080
return rt;
10771081

1078-
return Result.Empty<T>(input, "unsatisfied condition");
1082+
return Result.Empty<T>(input, message);
10791083
};
10801084
}
10811085

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Superpower.Parsers;
2+
using Superpower.Tests.Support;
3+
using Xunit;
4+
5+
namespace Superpower.Tests.Combinators
6+
{
7+
public class WhereCombinatorTests
8+
{
9+
[Fact]
10+
public void WhereFailsIfPrecedingParserFails()
11+
{
12+
AssertParser.Fails(Character.EqualTo('a').Where(_ => true), "b");
13+
}
14+
15+
[Fact]
16+
public void WhereSucceedsWhenPredicateMatches()
17+
{
18+
AssertParser.SucceedsWith(Character.EqualTo('a').Where(a => a == 'a'), "a", 'a');
19+
}
20+
21+
[Fact]
22+
public void WhereFailsWhenPredicateDoesNotMatch()
23+
{
24+
AssertParser.FailsWithMessage(
25+
Character.EqualTo('a').Where(a => a != 'a', "character should be an A"),
26+
"a",
27+
"Syntax error (line 1, column 1): character should be an A.");
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)