From fee62ec289993df30c1e8bc4bcb0ce85032b63b2 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 14 Aug 2024 22:47:45 +0800 Subject: [PATCH] chore: keyword 'div' instead of 'divide'. rename 'ScannerTest' to 'ScannerTests'. --- CanonSharp.Combinator/Abstractions/IFailedResult.cs | 4 +--- CanonSharp.Combinator/Abstractions/IParseResult.cs | 2 -- CanonSharp.Combinator/Abstractions/ISuccessfulResult.cs | 2 -- CanonSharp.Combinator/Results/FailedResultWithError.cs | 2 ++ CanonSharp.Combinator/Results/FailedResultWithException.cs | 7 +++++-- CanonSharp.Combinator/Results/FailedResultWithMessage.cs | 5 ++++- CanonSharp.Combinator/Results/InternalSuccessfulResult.cs | 2 ++ CanonSharp.Pascal/Scanner/LexicalScanner.cs | 2 +- .../{ScannerTest => ScannerTests}/LexicalParserTests.cs | 2 +- .../LexicalTokenParserTest.cs | 7 ++++++- 10 files changed, 22 insertions(+), 13 deletions(-) rename CanonSharp.Tests/{ScannerTest => ScannerTests}/LexicalParserTests.cs (99%) rename CanonSharp.Tests/{ScannerTest => ScannerTests}/LexicalTokenParserTest.cs (97%) diff --git a/CanonSharp.Combinator/Abstractions/IFailedResult.cs b/CanonSharp.Combinator/Abstractions/IFailedResult.cs index 7f525cc..607cf84 100644 --- a/CanonSharp.Combinator/Abstractions/IFailedResult.cs +++ b/CanonSharp.Combinator/Abstractions/IFailedResult.cs @@ -22,7 +22,7 @@ public interface IFailedResult : IParseResult /// /// 解析失败的异常 /// - public ParseException Exception => new(ToString()); + public ParseException Exception => new(Message); /// /// 转换该失败结果的类型 @@ -42,6 +42,4 @@ public interface IFailedResult : IParseResult TResult IParseResult.CaseOf(Func, TResult> successfulHandler, Func, TResult> failedHandler) => failedHandler(this); - - string IParseResult.ToString() => $"Parse Failed: {Message}"; } diff --git a/CanonSharp.Combinator/Abstractions/IParseResult.cs b/CanonSharp.Combinator/Abstractions/IParseResult.cs index 569c198..331f021 100644 --- a/CanonSharp.Combinator/Abstractions/IParseResult.cs +++ b/CanonSharp.Combinator/Abstractions/IParseResult.cs @@ -40,6 +40,4 @@ public interface IParseResult /// 最后的解析结果 public TResult CaseOf(Func, TResult> successfulHandler, Func, TResult> failedHandler); - - public string ToString(); } diff --git a/CanonSharp.Combinator/Abstractions/ISuccessfulResult.cs b/CanonSharp.Combinator/Abstractions/ISuccessfulResult.cs index c8372eb..766880c 100644 --- a/CanonSharp.Combinator/Abstractions/ISuccessfulResult.cs +++ b/CanonSharp.Combinator/Abstractions/ISuccessfulResult.cs @@ -26,6 +26,4 @@ public interface ISuccessfulResult : IParseResult TResult IParseResult.CaseOf(Func, TResult> successfulHandler, Func, TResult> failedHandler) => successfulHandler(this); - - string IParseResult.ToString() => Value?.ToString() ?? string.Empty; } diff --git a/CanonSharp.Combinator/Results/FailedResultWithError.cs b/CanonSharp.Combinator/Results/FailedResultWithError.cs index fb5bbec..16a6321 100644 --- a/CanonSharp.Combinator/Results/FailedResultWithError.cs +++ b/CanonSharp.Combinator/Results/FailedResultWithError.cs @@ -19,4 +19,6 @@ internal sealed class FailedResultWithError(TState state) : I { return new FailedResultWithError(state); } + + public override string ToString() => $"Parse failed: {Message}."; } diff --git a/CanonSharp.Combinator/Results/FailedResultWithException.cs b/CanonSharp.Combinator/Results/FailedResultWithException.cs index 39bb609..8c228d0 100644 --- a/CanonSharp.Combinator/Results/FailedResultWithException.cs +++ b/CanonSharp.Combinator/Results/FailedResultWithException.cs @@ -10,15 +10,18 @@ namespace CanonSharp.Combinator.Results; /// 输入流类型 /// 当前输入流状态的类型 /// 解析结果的类型 -public class FailedResultWithException(Exception exception, TState state) : IFailedResult +internal sealed class FailedResultWithException(Exception exception, TState state) + : IFailedResult where TState : IReadState { public IReadState State => state; - public ParseException Exception => new(ToString(), exception); + public ParseException Exception => new(Message, exception); public string Message => $"Exception occured: {exception}."; public IFailedResult Convert() => new FailedResultWithException(exception, state); + + public override string ToString() => $"Parse failed: {Message}."; } diff --git a/CanonSharp.Combinator/Results/FailedResultWithMessage.cs b/CanonSharp.Combinator/Results/FailedResultWithMessage.cs index c870244..dd4f829 100644 --- a/CanonSharp.Combinator/Results/FailedResultWithMessage.cs +++ b/CanonSharp.Combinator/Results/FailedResultWithMessage.cs @@ -10,7 +10,8 @@ namespace CanonSharp.Combinator.Results; /// 输入流的类型 /// 读取状态类型 /// 解析结果的类型 -internal sealed class FailedResultWithMessage(string message, TState state) : IFailedResult +internal sealed class FailedResultWithMessage(string message, TState state) + : IFailedResult where TState : IReadState { public IReadState State => state; @@ -21,4 +22,6 @@ internal sealed class FailedResultWithMessage(string message, { return new FailedResultWithMessage(message, state); } + + public override string ToString() => $"Parse failed: {Message}."; } diff --git a/CanonSharp.Combinator/Results/InternalSuccessfulResult.cs b/CanonSharp.Combinator/Results/InternalSuccessfulResult.cs index 3658d1c..c6c1ce2 100644 --- a/CanonSharp.Combinator/Results/InternalSuccessfulResult.cs +++ b/CanonSharp.Combinator/Results/InternalSuccessfulResult.cs @@ -22,4 +22,6 @@ internal sealed class InternalSuccessfulResult(T result, TSta public IParseResult Map(Func map) => new InternalSuccessfulResult(map(Value), state); + + public override string ToString() => Value?.ToString() ?? string.Empty; } diff --git a/CanonSharp.Pascal/Scanner/LexicalScanner.cs b/CanonSharp.Pascal/Scanner/LexicalScanner.cs index e84b7c8..b302674 100644 --- a/CanonSharp.Pascal/Scanner/LexicalScanner.cs +++ b/CanonSharp.Pascal/Scanner/LexicalScanner.cs @@ -37,7 +37,7 @@ public sealed class LexicalScanner StringIgnoreCase("real"), StringIgnoreCase("boolean"), StringIgnoreCase("char"), - StringIgnoreCase("divide"), + StringIgnoreCase("div"), StringIgnoreCase("not"), StringIgnoreCase("mod"), StringIgnoreCase("and"), diff --git a/CanonSharp.Tests/ScannerTest/LexicalParserTests.cs b/CanonSharp.Tests/ScannerTests/LexicalParserTests.cs similarity index 99% rename from CanonSharp.Tests/ScannerTest/LexicalParserTests.cs rename to CanonSharp.Tests/ScannerTests/LexicalParserTests.cs index bddc9cf..99a6a44 100644 --- a/CanonSharp.Tests/ScannerTest/LexicalParserTests.cs +++ b/CanonSharp.Tests/ScannerTests/LexicalParserTests.cs @@ -1,7 +1,7 @@ using CanonSharp.Pascal.Scanner; using CanonSharp.Tests.Utils; -namespace CanonSharp.Tests.ScannerTest; +namespace CanonSharp.Tests.ScannerTests; public class LexicalParserTests : LexicalTestBase { diff --git a/CanonSharp.Tests/ScannerTest/LexicalTokenParserTest.cs b/CanonSharp.Tests/ScannerTests/LexicalTokenParserTest.cs similarity index 97% rename from CanonSharp.Tests/ScannerTest/LexicalTokenParserTest.cs rename to CanonSharp.Tests/ScannerTests/LexicalTokenParserTest.cs index 7665088..bc3574a 100644 --- a/CanonSharp.Tests/ScannerTest/LexicalTokenParserTest.cs +++ b/CanonSharp.Tests/ScannerTests/LexicalTokenParserTest.cs @@ -4,7 +4,7 @@ using CanonSharp.Combinator.Extensions; using CanonSharp.Pascal.Scanner; using CanonSharp.Tests.Utils; -namespace CanonSharp.Tests.ScannerTest; +namespace CanonSharp.Tests.ScannerTests; public class LexicalTokenParserTest : LexicalTestBase { @@ -27,6 +27,11 @@ public class LexicalTokenParserTest : LexicalTestBase [InlineData("true")] [InlineData("false")] [InlineData("while")] + [InlineData("div")] + [InlineData("not")] + [InlineData("mod")] + [InlineData("and")] + [InlineData("or")] public void KeywordParserTest(string literalValue) { IParser keyword = LexicalScanner.KeywordParser();