generated from mazharenko/aoc-agent-template-multipleyears
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
368c597
commit 4cd7507
Showing
7 changed files
with
169 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Runtime.CompilerServices; | ||
using Superpower; | ||
using Superpower.Model; | ||
using Superpower.Parsers; | ||
|
||
namespace ParsingExtensions; | ||
|
||
public static class Template | ||
{ | ||
public static TextParser<T> Matching<T>(TemplateParserStringHandler<T> stringHandler) | ||
{ | ||
if (typeof(T).IsAssignableTo(typeof(ITuple))) | ||
return stringHandler.GetParser().Select(t => (T)t); | ||
return stringHandler.GetParser().Select(t => (T)t[0]!); | ||
} | ||
public static TextParser<(T1, T2)> Matching<T1, T2>(TemplateParserStringHandler<(T1, T2)> stringHandler) | ||
{ | ||
return stringHandler.GetParser().Select(t => ((T1, T2))t); | ||
} | ||
public static TextParser<(T1, T2, T3)> Matching<T1, T2, T3>(TemplateParserStringHandler<(T1, T2, T3)> stringHandler) | ||
{ | ||
return stringHandler.GetParser().Select(t => ((T1, T2, T3))t); | ||
} | ||
public static TextParser<(T1, T2, T3, T4)> Matching<T1, T2, T3, T4>(TemplateParserStringHandler<(T1, T2, T3, T4)> stringHandler) | ||
{ | ||
return stringHandler.GetParser().Select(t => ((T1, T2, T3, T4))t); | ||
} | ||
public static TextParser<(T1, T2, T3, T4, T5)> Matching<T1, T2, T3, T4, T5>(TemplateParserStringHandler<(T1, T2, T3, T4, T5)> stringHandler) | ||
{ | ||
return stringHandler.GetParser().Select(t => ((T1, T2, T3, T4, T5))t); | ||
} | ||
} | ||
|
||
[InterpolatedStringHandler] | ||
public class TemplateParserStringHandler<T> | ||
{ | ||
private TextParser<ITuple> accParser = span => Result.Value((ITuple)ValueTuple.Create(), span, span); | ||
|
||
public TemplateParserStringHandler(int literalLength, int formattedCount) | ||
{ | ||
if (typeof(T).IsAssignableTo(typeof(ITuple))) | ||
{ | ||
var argsLength = typeof(T).GenericTypeArguments.Length; | ||
if (argsLength != formattedCount) | ||
throw new ArgumentException( | ||
$"The template contains {formattedCount} formatted values, but type {typeof(T)} has {argsLength} arguments."); | ||
} | ||
else if (1 != formattedCount) | ||
throw new ArgumentException( | ||
$"The template contains {formattedCount} formatted values, but {typeof(T)} is not a tuple type."); | ||
} | ||
|
||
public void AppendLiteral(string value) | ||
{ | ||
accParser = accParser.ThenIgnore(Span.EqualTo(value)); | ||
} | ||
|
||
public void AppendFormatted<T1>(TextParser<T1> t) where T1 : notnull | ||
{ | ||
accParser = accParser.Then(x => t.Select(ITuple (y) => | ||
{ | ||
var tupleArgs = Enumerable.Range(0, x.Length) | ||
.Select(i => x[i]!) | ||
.Append(y).ToArray(); | ||
|
||
var createMethod = typeof(ValueTuple).GetMethods() | ||
.Single(m1 => m1.Name == nameof(ValueTuple.Create) && m1.GetParameters().Length == tupleArgs.Length) | ||
.MakeGenericMethod(tupleArgs.Select(gg => gg.GetType()).ToArray()); | ||
return (ITuple)createMethod.Invoke(null, tupleArgs.ToArray())!; | ||
})); | ||
} | ||
|
||
public TextParser<ITuple> GetParser() | ||
{ | ||
return accParser; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace ParsingExtensions.Tests; | ||
|
||
public class TemplateTests | ||
{ | ||
[Test] | ||
public void Should_Parse_Tuple1() | ||
{ | ||
var parser = Template.Matching<int, int>( | ||
$"p={Numerics.IntegerInt32},{Numerics.IntegerInt32};" | ||
); | ||
|
||
var result = parser.AtEnd().TryParse("p=3434,8787;"); | ||
result.HasValue.Should().BeTrue(); | ||
result.Value.Should().Be((3434, 8787)); | ||
} | ||
|
||
[Test] | ||
public void Should_Parse_Tuple2() | ||
{ | ||
var parser = Template.Matching<(int, int)>( | ||
$"p={Numerics.IntegerInt32},{Numerics.IntegerInt32};" | ||
); | ||
|
||
var result = parser.AtEnd().TryParse("p=3434,8787;"); | ||
result.HasValue.Should().BeTrue(); | ||
result.Value.Should().Be((3434, 8787)); | ||
} | ||
|
||
[Test] | ||
public void Should_Parse_SingleValue() | ||
{ | ||
var parser = Template.Matching<int>( | ||
$"p={Numerics.IntegerInt32};" | ||
); | ||
|
||
var result = parser.AtEnd().TryParse("p=3434;"); | ||
result.HasValue.Should().BeTrue(); | ||
result.Value.Should().Be(3434); | ||
} | ||
|
||
[Test] | ||
public void Should_Return_Remainder() | ||
{ | ||
var parser = Template.Matching<int>( | ||
$"p={Numerics.IntegerInt32};" | ||
); | ||
|
||
var result = parser.TryParse("p=3434;v=9898"); | ||
result.HasValue.Should().BeTrue(); | ||
result.Value.Should().Be(3434); | ||
result.Remainder.ToStringValue().Should().Be("v=9898"); | ||
} | ||
} |