2024-08-13 14:46:11 +08:00
|
|
|
using CanonSharp.Combinator.Abstractions;
|
|
|
|
|
|
|
|
namespace CanonSharp.Combinator.Parsers.Modifiers;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 向前看解析器
|
|
|
|
/// 使用传入的解析器向前解析
|
|
|
|
/// 但是返回的结果中输入流读取状态不前移
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="parser">需要向前看的解析器</param>
|
|
|
|
/// <typeparam name="TToken">输入流令牌</typeparam>
|
|
|
|
/// <typeparam name="T">返回的解析结果类型</typeparam>
|
2024-08-18 12:01:27 +08:00
|
|
|
internal sealed class LookAheadParser<TToken, T>(IParser<TToken, T> parser) : ModifiedParser<TToken, T, T>(parser)
|
2024-08-13 14:46:11 +08:00
|
|
|
{
|
2024-08-18 12:01:27 +08:00
|
|
|
protected override IParseResult<TToken, T> Succeed<TState>(TState state,
|
|
|
|
ISuccessfulResult<TToken, T> successfulResult)
|
2024-08-13 14:46:11 +08:00
|
|
|
=> ParseResultBuilder.Succeed<TToken, TState, T>(successfulResult.Value, state);
|
|
|
|
|
2024-08-18 12:01:27 +08:00
|
|
|
protected override IParseResult<TToken, T> Fail<TState>(TState state, IFailedResult<TToken, T> failedResult)
|
2024-08-13 14:46:11 +08:00
|
|
|
=> ParseResultBuilder.Fail<TToken, TState, T>($"Failed when looking ahead: {failedResult}", state);
|
|
|
|
}
|