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

Added OneOf combinator and tests #123

Merged
merged 1 commit into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
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
48 changes: 48 additions & 0 deletions src/Superpower/Parse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,5 +592,53 @@ public static TextParser<T> Return<T>(T value)
return Result.Value((rt.Value, ru.Value, rv.Value, rw.Value, rx.Value), input, rx.Remainder);
};
}
/// <summary>
/// Creates a parser which applies one of the specified parsers.
/// </summary>
/// <typeparam name="TKind">The kind of the tokens being parsed.</typeparam>
/// <typeparam name="T">The type of value being parsed.</typeparam>
/// <param name="parsers">The parsers to try from left to right.</param>
/// <returns>A parser which applies one of the specified parsers.</returns>
public static TokenListParser<TKind, T> OneOf<TKind, T>(params TokenListParser<TKind, T>[] parsers)
{
if (parsers == null) throw new ArgumentNullException(nameof(parsers));

if (parsers.Length == 0)
{
return i => TokenListParserResult.Empty<TKind, T>(TokenList<TKind>.Empty);
}

TokenListParser<TKind, T> c = parsers[0];
for (int i = 1; i < parsers.Length; i++)
{
c = c.Or(parsers[i]);
}

return c;
}

/// <summary>
/// Creates a parser which applies one of the specified parsers.
/// </summary>
/// <typeparam name="T">The type of value being parsed.</typeparam>
/// <param name="parsers">The parser to try from left to right.</param>
/// <returns>A parser which applies one of the specified parsers.</returns>
public static TextParser<T> OneOf<T>(params TextParser<T>[] parsers)
{
if (parsers == null) throw new ArgumentNullException(nameof(parsers));

if (parsers.Length == 0)
{
return i => Result.Empty<T>(TextSpan.None);
}

TextParser<T> c = parsers[0];
for (int i = 1; i < parsers.Length; i++)
{
c = c.Or(parsers[i]);
}

return c;
}
}
}
37 changes: 37 additions & 0 deletions test/Superpower.Tests/Combinators/OneOfCombinatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Superpower.Model;
using Superpower.Parsers;
using Superpower.Tests.Support;
using System.Linq;
using Xunit;

namespace Superpower.Tests.Combinators
{
public class OneOfCombinatorTests
{
[Fact]
public void OneOf2Succeeds()
{
var p = Parse.OneOf(Character.Digit, Character.AnyChar);
AssertParser.SucceedsWithAll(Span.MatchedBy(p), "1");
AssertParser.SucceedsWithAll(Span.MatchedBy(p), "w");
}

[Fact]
public void OneOf2TokenSucceeds()
{
var p = Parse.OneOf(Token.EqualTo('1'), Token.EqualTo('w'));
AssertParser.SucceedsWith(p, "1", '1');
AssertParser.SucceedsWith(p, "w", 'w');
}

[Fact]
public void OneOfReportsCorrectError()
{
var names = new[] { "one", "two" };
TextParser<TextSpan> p = Parse.OneOf(names.Select(Span.EqualTo).ToArray());
AssertParser.SucceedsWith(p.Select(t => t.ToStringValue()), "one", "one");
AssertParser.SucceedsWith(p.Select(t => t.ToStringValue()), "two", "two");
AssertParser.FailsWithMessage(p.Select(t => t.ToStringValue()), "four", "Syntax error (line 1, column 1): unexpected `f`, expected `one` or `two`.");
}
}
}