diff --git a/CanonSharp.Combinator/Abstractions/FailedResult.cs b/CanonSharp.Combinator/Abstractions/FailedResult.cs
deleted file mode 100644
index 9fd8a42..0000000
--- a/CanonSharp.Combinator/Abstractions/FailedResult.cs
+++ /dev/null
@@ -1,49 +0,0 @@
-namespace CanonSharp.Combinator.Abstractions;
-
-///
-/// 失败解析结果基类
-///
-/// 输入流类型
-/// 解析结果类型
-public abstract class FailedResult : ParseResult
-{
- public override T Value => throw Exception;
-
- ///
- /// 当前读取到的状态
- ///
- public abstract IReadState State { get; }
-
- ///
- /// 解析失败的消息
- ///
- public abstract string Message { get; }
-
- ///
- /// 解析失败的异常
- ///
- public virtual ParseException Exception => new(ToString());
-
- ///
- /// 转换该失败结果的类型
- ///
- /// 转换之后的结果类型
- /// 转换之后的失败解析类型
- public abstract FailedResult Convert();
-
- internal override ParseResult Next(Func> nextParser,
- Func, ParseResult> continuation)
- => continuation(Convert());
-
- public override ParseResult Map(Func map)
- => Convert();
-
- public override TResult CaseOf(Func, TResult> successfulHandler,
- Func, TResult> failedHandler)
- => failedHandler(this);
-
- public override string ToString()
- {
- return $"Parse Failed: {Message}.";
- }
-}
diff --git a/CanonSharp.Combinator/Abstractions/IFailedResult.cs b/CanonSharp.Combinator/Abstractions/IFailedResult.cs
new file mode 100644
index 0000000..7f525cc
--- /dev/null
+++ b/CanonSharp.Combinator/Abstractions/IFailedResult.cs
@@ -0,0 +1,47 @@
+namespace CanonSharp.Combinator.Abstractions;
+
+///
+/// 失败解析结果基类
+///
+/// 输入流类型
+/// 解析结果类型
+public interface IFailedResult : IParseResult
+{
+ T IParseResult.Value => throw Exception;
+
+ ///
+ /// 当前读取到的状态
+ ///
+ public IReadState State { get; }
+
+ ///
+ /// 解析失败的消息
+ ///
+ public string Message { get; }
+
+ ///
+ /// 解析失败的异常
+ ///
+ public ParseException Exception => new(ToString());
+
+ ///
+ /// 转换该失败结果的类型
+ ///
+ /// 转换之后的结果类型
+ /// 转换之后的失败解析类型
+ public IFailedResult Convert();
+
+ IParseResult IParseResult.Next(
+ Func> nextParser,
+ Func, IParseResult> continuation)
+ => continuation(Convert());
+
+ IParseResult IParseResult.Map(Func map)
+ => Convert();
+
+ TResult IParseResult.CaseOf(Func, TResult> successfulHandler,
+ Func, TResult> failedHandler)
+ => failedHandler(this);
+
+ string IParseResult.ToString() => $"Parse Failed: {Message}";
+}
diff --git a/CanonSharp.Combinator/Abstractions/ParseResult.cs b/CanonSharp.Combinator/Abstractions/IParseResult.cs
similarity index 71%
rename from CanonSharp.Combinator/Abstractions/ParseResult.cs
rename to CanonSharp.Combinator/Abstractions/IParseResult.cs
index e38be41..569c198 100644
--- a/CanonSharp.Combinator/Abstractions/ParseResult.cs
+++ b/CanonSharp.Combinator/Abstractions/IParseResult.cs
@@ -5,12 +5,12 @@ namespace CanonSharp.Combinator.Abstractions;
///
/// 输入流类型
/// 实际结果类型
-public abstract class ParseResult
+public interface IParseResult
{
///
/// 实际结果对象
///
- public abstract T Value { get; }
+ public T Value { get; }
///
/// 在当前结果上应用下一个解析器
@@ -20,8 +20,8 @@ public abstract class ParseResult
/// 下一个解析器函数返回的解析结果类型
/// 最终的解析结果类型
///
- internal abstract ParseResult Next(Func> nextParser,
- Func, ParseResult> continuation);
+ internal IParseResult Next(Func> nextParser,
+ Func, IParseResult> continuation);
///
/// 映射结果
@@ -29,7 +29,7 @@ public abstract class ParseResult
/// 映射结果的函数
/// 映射结果函数返回解析结果的类型
/// 最终的解析结果
- public abstract ParseResult Map(Func map);
+ public IParseResult Map(Func map);
///
/// 在成功或者失败解析结果上应用不同的后继函数
@@ -38,6 +38,8 @@ public abstract class ParseResult
/// 在失败解析结构上应用的函数
/// 最后返回解析结果的类型
/// 最后的解析结果
- public abstract TResult CaseOf(Func, TResult> successfulHandler,
- Func, TResult> failedHandler);
+ public TResult CaseOf(Func, TResult> successfulHandler,
+ Func, TResult> failedHandler);
+
+ public string ToString();
}
diff --git a/CanonSharp.Combinator/Abstractions/Parser.cs b/CanonSharp.Combinator/Abstractions/IParser.cs
similarity index 68%
rename from CanonSharp.Combinator/Abstractions/Parser.cs
rename to CanonSharp.Combinator/Abstractions/IParser.cs
index 2159a95..edeb2e3 100644
--- a/CanonSharp.Combinator/Abstractions/Parser.cs
+++ b/CanonSharp.Combinator/Abstractions/IParser.cs
@@ -7,7 +7,7 @@ namespace CanonSharp.Combinator.Abstractions;
///
/// 输入流类型
/// 解析结果类型
-public abstract class Parser
+public interface IParser
{
///
/// 解析器运行函数
@@ -17,16 +17,16 @@ public abstract class Parser
/// 输入流状态类型
/// 后继函数运行之后的解析结果类型
///
- internal abstract ParseResult Run(TState state,
- Func, ParseResult> continuation)
+ internal IParseResult Run(TState state,
+ Func, IParseResult> continuation)
where TState : IReadState;
- public ParseResult Parse(TState state) where TState : IReadState
+ public IParseResult Parse(TState state) where TState : IReadState
{
return Run(state);
}
- private ParseResult Run(TState state) where TState : IReadState
+ private IParseResult Run(TState state) where TState : IReadState
{
try
{
@@ -38,6 +38,6 @@ public abstract class Parser
}
}
- public static Parser operator |(Parser a, Parser b)
+ public static IParser operator |(IParser a, IParser b)
=> a.Alternative(b);
}
diff --git a/CanonSharp.Combinator/Abstractions/ISuccessfulResult.cs b/CanonSharp.Combinator/Abstractions/ISuccessfulResult.cs
new file mode 100644
index 0000000..c8372eb
--- /dev/null
+++ b/CanonSharp.Combinator/Abstractions/ISuccessfulResult.cs
@@ -0,0 +1,31 @@
+namespace CanonSharp.Combinator.Abstractions;
+
+///
+/// 成功解析结果基类
+///
+/// 输入流类型
+/// 实际的解析结果类型
+public interface ISuccessfulResult : IParseResult
+{
+ ///
+ /// 运行下一个解析器
+ ///
+ /// 下一个解析器
+ /// 处理解析结果的后继函数
+ /// 下一个解析器返回的结果类型
+ /// 最终的结果类型
+ /// 最终的结果
+ protected IParseResult RunNext(IParser parser,
+ Func, IParseResult> continuation);
+
+ IParseResult IParseResult.Next(
+ Func> nextParser,
+ Func, IParseResult> continuation)
+ => RunNext(nextParser(Value), continuation);
+
+ TResult IParseResult.CaseOf(Func, TResult> successfulHandler,
+ Func, TResult> failedHandler)
+ => successfulHandler(this);
+
+ string IParseResult.ToString() => Value?.ToString() ?? string.Empty;
+}
diff --git a/CanonSharp.Combinator/Abstractions/SuccessfulResult.cs b/CanonSharp.Combinator/Abstractions/SuccessfulResult.cs
deleted file mode 100644
index fc76084..0000000
--- a/CanonSharp.Combinator/Abstractions/SuccessfulResult.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-namespace CanonSharp.Combinator.Abstractions;
-
-///
-/// 成功解析结果基类
-///
-/// 实际的解析结果
-/// 输入流类型
-/// 实际的解析结果类型
-public abstract class SuccessfulResult(T value) : ParseResult
-{
- public override T Value => value;
-
- ///
- /// 运行下一个解析器
- ///
- /// 下一个解析器
- /// 处理解析结果的后继函数
- /// 下一个解析器返回的结果类型
- /// 最终的结果类型
- /// 最终的结果
- protected abstract ParseResult RunNext(Parser parser,
- Func, ParseResult> continuation);
-
- internal override ParseResult Next(Func> nextParser,
- Func, ParseResult> continuation)
- => RunNext(nextParser(Value), continuation);
-
- public override TResult CaseOf(Func, TResult> successfulHandler,
- Func, TResult> failedHandler)
- => successfulHandler(this);
-
- public override string ToString() => Value?.ToString() ?? string.Empty;
-}
diff --git a/CanonSharp.Combinator/Extensions/ParserExtensions.cs b/CanonSharp.Combinator/Extensions/ParserExtensions.cs
index 1493e2b..dc8c3dc 100644
--- a/CanonSharp.Combinator/Extensions/ParserExtensions.cs
+++ b/CanonSharp.Combinator/Extensions/ParserExtensions.cs
@@ -19,7 +19,7 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Alternative(this Parser first, Parser second)
+ public static IParser Alternative(this IParser first, IParser second)
=> new AlternativeParser(first, second);
///
@@ -32,8 +32,8 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Alternative(this Parser parser,
- Func, Parser> resume)
+ public static IParser Alternative(this IParser parser,
+ Func, IParser> resume)
=> new ResumeParser(parser, resume);
///
@@ -46,8 +46,8 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Bind(this Parser parser,
- Func> next)
+ public static IParser Bind(this IParser parser,
+ Func> next)
=> new BindParser(parser, next);
///
@@ -60,7 +60,7 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Map(this Parser parser, Func map)
+ public static IParser Map(this IParser parser, Func map)
=> new MapParser(parser, map);
///
@@ -73,7 +73,7 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Map(this Parser parser, TResult result)
+ public static IParser Map(this IParser parser, TResult result)
=> parser.Map(_ => result);
///
@@ -87,9 +87,9 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Next(this Parser parser,
- Func> next,
- Func, Parser> failedNext)
+ public static IParser Next(this IParser parser,
+ Func> next,
+ Func, IParser> failedNext)
=> new NextParser(parser, next, failedNext);
///
@@ -103,8 +103,8 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Next(this Parser parser,
- Func> next, Func, TResult> failedHandler)
+ public static IParser Next(this IParser parser,
+ Func> next, Func, TResult> failedHandler)
=> parser.Next(next, failedResult => Pure(failedHandler(failedResult)));
///
@@ -118,8 +118,8 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Next(this Parser parser,
- Func> next, TResult failedResult)
+ public static IParser Next(this IParser parser,
+ Func> next, TResult failedResult)
=> parser.Next(next, _ => Pure(failedResult));
///
@@ -133,8 +133,8 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Next(this Parser parser,
- Func nextResult, Func, Parser> failedResume)
+ public static IParser Next(this IParser parser,
+ Func nextResult, Func, IParser> failedResume)
=> parser.Next(x => Pure(nextResult(x)), failedResume);
///
@@ -148,8 +148,8 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Next(this Parser parser,
- Func nextResult, Func, TResult> failedResult)
+ public static IParser Next(this IParser parser,
+ Func nextResult, Func, TResult> failedResult)
=> new SuccessfulMapParser(parser, nextResult, failedResult);
///
@@ -163,7 +163,7 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Next(this Parser parser,
+ public static IParser Next(this IParser parser,
Func successfulHandler, TResult failedResult)
=> parser.Next(successfulHandler, _ => failedResult);
@@ -181,8 +181,8 @@ public static class ParserExtensions
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Parser Do(this Parser parser, Action successfulAction,
- Action> failedAction)
+ public static IParser Do(this IParser parser, Action