From f2431765b256c8a82e188a4e816392447faa820f Mon Sep 17 00:00:00 2001 From: Philipp Binder Date: Mon, 30 Sep 2024 21:51:56 +0200 Subject: [PATCH 1/2] reintroduce TextCombinator as an easy shortcut for converting a char[]-Parser into a string-Parser --- src/Superpower/Combinators.cs | 8 ++++++++ .../Combinators/TextCombinatorTests.cs | 14 ++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/Superpower.Tests/Combinators/TextCombinatorTests.cs diff --git a/src/Superpower/Combinators.cs b/src/Superpower/Combinators.cs index 7683a84..8a934c7 100644 --- a/src/Superpower/Combinators.cs +++ b/src/Superpower/Combinators.cs @@ -531,6 +531,14 @@ public static TextParser ManyDelimitedBy(this TextParser parser, T .OptionalOrDefault(Array.Empty()); } + /// + /// Constructs a parser that converts a char[]-parser to a string-parser. + /// + /// The parser. + /// The resulting parser. + public static TextParser Text(this TextParser parser) + => parser.Select(chars => new string(chars)); + /// /// Construct a parser that fails with error message when fails. /// diff --git a/test/Superpower.Tests/Combinators/TextCombinatorTests.cs b/test/Superpower.Tests/Combinators/TextCombinatorTests.cs new file mode 100644 index 0000000..25f4a49 --- /dev/null +++ b/test/Superpower.Tests/Combinators/TextCombinatorTests.cs @@ -0,0 +1,14 @@ +using Superpower.Parsers; +using Superpower.Tests.Support; +using Xunit; + +namespace Superpower.Tests.Combinators; + +public class TextCombinatorTests +{ + [Fact] + public void TextSucceedsWithAnyCharArrayInput() + { + AssertParser.SucceedsWith(Character.AnyChar.Many().Text(), "ab", "ab"); + } +} From ea59437bda89c6d74c873b31e796b7d84bc97a4c Mon Sep 17 00:00:00 2001 From: Philipp Binder Date: Mon, 30 Sep 2024 22:09:03 +0200 Subject: [PATCH 2/2] add the same behavior for TextSpan-parsers, to have this easy shortcut there too --- src/Superpower/Combinators.cs | 8 ++++++++ test/Superpower.Tests/Combinators/TextCombinatorTests.cs | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/src/Superpower/Combinators.cs b/src/Superpower/Combinators.cs index 8a934c7..7d06abf 100644 --- a/src/Superpower/Combinators.cs +++ b/src/Superpower/Combinators.cs @@ -539,6 +539,14 @@ public static TextParser ManyDelimitedBy(this TextParser parser, T public static TextParser Text(this TextParser parser) => parser.Select(chars => new string(chars)); + /// + /// Constructs a parser that converts a TextSpan-parser to a string-parser. + /// + /// The parser. + /// The resulting parser. + public static TextParser Text(this TextParser parser) + => parser.Select(textSpan => textSpan.ToStringValue()); + /// /// Construct a parser that fails with error message when fails. /// diff --git a/test/Superpower.Tests/Combinators/TextCombinatorTests.cs b/test/Superpower.Tests/Combinators/TextCombinatorTests.cs index 25f4a49..5568ef0 100644 --- a/test/Superpower.Tests/Combinators/TextCombinatorTests.cs +++ b/test/Superpower.Tests/Combinators/TextCombinatorTests.cs @@ -11,4 +11,10 @@ public void TextSucceedsWithAnyCharArrayInput() { AssertParser.SucceedsWith(Character.AnyChar.Many().Text(), "ab", "ab"); } + + [Fact] + public void TextSucceedsWithTextSpanInput() + { + AssertParser.SucceedsWith(Span.Length(2).Text(), "ab", "ab"); + } }