public IResult <TOutput> Parse(IParseState <TInput> state)
        {
            Assert.ArgumentNotNull(state, nameof(state));
            var startCheckpoint = state.Input.Checkpoint();

            try
            {
                var seqState = new State <TInput>(state);
                var result   = Function(seqState);
                return(state.Success(this, result, seqState.Consumed, startCheckpoint.Location));
            }
            catch (ParseFailedException spe)
            {
                // This exception is part of normal flow-control for this parser
                // Other exceptions bubble up like normal.
                startCheckpoint.Rewind();
                if (spe.Result != null)
                {
                    var result = spe.Result;
                    state.Log(this, $"Parse failed during sequential callback: {result}\n\n{spe.StackTrace}");
                    return(state.Fail(this, $"Error during parsing: {result.Parser} {result.ErrorMessage} at {result.Location}"));
                }

                state.Log(this, $"Failure triggered during sequential callback: {spe.Message}");
                return(state.Fail(this, $"Failure during parsing: {spe.Message}"));
            }
            catch (Exception e)
            {
                startCheckpoint.Rewind();
                state.Log(this, $"Parse failed during sequential callback: {e.Message}");
                throw;
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Create a Failure result.
 /// </summary>
 /// <typeparam name="TInput"></typeparam>
 /// <param name="state"></param>
 /// <param name="parser"></param>
 /// <param name="error"></param>
 /// <param name="location"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public static IResult Fail <TInput>(this IParseState <TInput> state, IParser <TInput> parser, string error, Location location, IReadOnlyList <object>?data = null)
 {
     state.Log(parser, "Failed with error " + error);
     return(new FailureResult <object>(parser, location, error, data));
 }
Esempio n. 3
0
 /// <summary>
 /// Create a Success result.
 /// </summary>
 /// <typeparam name="TInput"></typeparam>
 /// <param name="state"></param>
 /// <param name="parser"></param>
 /// <param name="output"></param>
 /// <param name="consumed"></param>
 /// <param name="location"></param>
 /// <param name="data"></param>
 /// <returns></returns>
 public static IResult <object> Success <TInput>(this IParseState <TInput> state, IParser <TInput> parser, object output, int consumed, Location location, IReadOnlyList <object>?data = null)
 {
     state.Log(parser, "Succeeded");
     return(new SuccessResult <object>(parser, output, location, consumed, data));
 }