feat: Parser Combinator库和词法分析器 (#2)
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
All checks were successful
Run unit test / Unit-Test (push) Successful in 41s
Reviewed-on: https://git.bupt-hpc.cn/jackfiled/CanonSharp/pulls/2 Co-authored-by: jackfiled <xcrenchangjun@outlook.com> Co-committed-by: jackfiled <xcrenchangjun@outlook.com>
This commit is contained in:
39
CanonSharp.Tests/Utils/LexicalTestBase.cs
Normal file
39
CanonSharp.Tests/Utils/LexicalTestBase.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using CanonSharp.Combinator;
|
||||
using CanonSharp.Combinator.Abstractions;
|
||||
using CanonSharp.Common.Scanner;
|
||||
|
||||
namespace CanonSharp.Tests.Utils;
|
||||
|
||||
public abstract class LexicalTestBase
|
||||
{
|
||||
protected static void ValidateSuccessfulParser(Parser<char, LexicalToken> parser, LexicalTokenType exceptedType,
|
||||
string literalValue, string input)
|
||||
{
|
||||
StringReadState state = new(input);
|
||||
ParseResult<char, LexicalToken> result = parser.Parse(state);
|
||||
|
||||
Assert.Equal(exceptedType, result.Value.TokenType);
|
||||
Assert.Equal(literalValue, result.Value.LiteralValue);
|
||||
}
|
||||
|
||||
protected static void ValidateFailedParser(Parser<char, LexicalToken> parser, string input)
|
||||
{
|
||||
StringReadState state = new(input);
|
||||
ParseResult<char, LexicalToken> result = parser.Parse(state);
|
||||
Assert.ThrowsAny<ParseException>(() => result.Value);
|
||||
}
|
||||
|
||||
protected static void ValidateLexicalTokens(Parser<char, IEnumerable<LexicalToken>> parser, string input,
|
||||
IEnumerable<(LexicalTokenType, string)> exceptedResult)
|
||||
{
|
||||
StringReadState state = new(input);
|
||||
ParseResult<char, IEnumerable<LexicalToken>> result = parser.Parse(state);
|
||||
|
||||
foreach (((LexicalTokenType exceptedType, string exceptedValue), LexicalToken token) in exceptedResult.Zip(
|
||||
result.Value))
|
||||
{
|
||||
Assert.Equal(exceptedType, token.TokenType);
|
||||
Assert.Equal(exceptedValue, token.LiteralValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
43
CanonSharp.Tests/Utils/ParserTestsBase.cs
Normal file
43
CanonSharp.Tests/Utils/ParserTestsBase.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using CanonSharp.Combinator;
|
||||
using CanonSharp.Combinator.Abstractions;
|
||||
using CanonSharp.Common.Scanner;
|
||||
|
||||
namespace CanonSharp.Tests.Utils;
|
||||
|
||||
public abstract class ParserTestsBase
|
||||
{
|
||||
protected static void ValidateSuccessfulResult<T>(Parser<char, T> parser, T value, string source)
|
||||
{
|
||||
StringReadState state = new(source);
|
||||
|
||||
ParseResult<char, T> result = parser.Parse(state);
|
||||
Assert.Equal(value, result.Value);
|
||||
}
|
||||
|
||||
protected static void ValidateSuccessfulResult<T>(
|
||||
Parser<char, IEnumerable<T>> parser,
|
||||
IEnumerable<T> values, string source)
|
||||
{
|
||||
StringReadState state = new(source);
|
||||
|
||||
ParseResult<char, IEnumerable<T>> result = parser.Parse(state);
|
||||
|
||||
foreach ((T actual, T except) in result.Value.Zip(values))
|
||||
{
|
||||
Assert.Equal(except, actual);
|
||||
}
|
||||
}
|
||||
|
||||
protected static FailedResult<char, T> ValidateFailedResult<T>(Parser<char, T> parser, string source)
|
||||
{
|
||||
StringReadState state = new(source);
|
||||
|
||||
ParseResult<char, T> result = parser.Parse(state);
|
||||
Assert.ThrowsAny<ParseException>(() =>
|
||||
{
|
||||
_ = result.Value;
|
||||
});
|
||||
|
||||
return (FailedResult<char, T>)result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user