internal sealed override InternalResult <R> Parse(ref ParseState <TToken> state) { var consumedInput = false; var result1 = _p1.Parse(ref state); consumedInput = consumedInput || result1.ConsumedInput; if (!result1.Success) { return(InternalResult.Failure <R>(consumedInput)); } var result2 = _p2.Parse(ref state); consumedInput = consumedInput || result2.ConsumedInput; if (!result2.Success) { return(InternalResult.Failure <R>(consumedInput)); } var result3 = _p3.Parse(ref state); consumedInput = consumedInput || result3.ConsumedInput; if (!result3.Success) { return(InternalResult.Failure <R>(consumedInput)); } var result4 = _p4.Parse(ref state); consumedInput = consumedInput || result4.ConsumedInput; if (!result4.Success) { return(InternalResult.Failure <R>(consumedInput)); } var result5 = _p5.Parse(ref state); consumedInput = consumedInput || result5.ConsumedInput; if (!result5.Success) { return(InternalResult.Failure <R>(consumedInput)); } var result6 = _p6.Parse(ref state); consumedInput = consumedInput || result6.ConsumedInput; if (!result6.Success) { return(InternalResult.Failure <R>(consumedInput)); } var result7 = _p7.Parse(ref state); consumedInput = consumedInput || result7.ConsumedInput; if (!result7.Success) { return(InternalResult.Failure <R>(consumedInput)); } return(InternalResult.Success <R>(_func( result1.Value, result2.Value, result3.Value, result4.Value, result5.Value, result6.Value, result7.Value ), consumedInput)); }
internal sealed override bool TryParse(ref ParseState <TToken> state, ref ExpectedCollector <TToken> expecteds, out R result) { var success1 = _p1.TryParse(ref state, ref expecteds, out var result1); if (!success1) { result = default; return(false); } var success2 = _p2.TryParse(ref state, ref expecteds, out var result2); if (!success2) { result = default; return(false); } var success3 = _p3.TryParse(ref state, ref expecteds, out var result3); if (!success3) { result = default; return(false); } var success4 = _p4.TryParse(ref state, ref expecteds, out var result4); if (!success4) { result = default; return(false); } var success5 = _p5.TryParse(ref state, ref expecteds, out var result5); if (!success5) { result = default; return(false); } var success6 = _p6.TryParse(ref state, ref expecteds, out var result6); if (!success6) { result = default; return(false); } var success7 = _p7.TryParse(ref state, ref expecteds, out var result7); if (!success7) { result = default; return(false); } result = _func( result1, result2, result3, result4, result5, result6, result7 ); return(true); }
private static Result <TToken, T> DoParse <TToken, T>(Parser <TToken, T> parser, ref ParseState <TToken> state) { var internalResult = parser.Parse(ref state); var result = internalResult.Success ? new Result <TToken, T>(internalResult.ConsumedInput, internalResult.Value) : new Result <TToken, T>(internalResult.ConsumedInput, state.BuildError()); state.Dispose(); // ensure we return the state's buffers to the buffer pool return(result); }
internal override InternalResult <SourcePos> Parse(ref ParseState <TToken> state) => InternalResult.Success(state.ComputeSourcePos(), false);
private InternalResult <IEnumerable <T> > Rest(Parser <TToken, T> parser, ref ParseState <TToken> state, List <T> ts, bool consumedInput) { var result = parser.Parse(ref state); while (result.Success) { if (!result.ConsumedInput) { throw new InvalidOperationException("Many() used with a parser which consumed no input"); } consumedInput = true; ts?.Add(result.Value); result = parser.Parse(ref state); } if (result.ConsumedInput) // the most recent parser failed after consuming input { // state.Error set by parser return(InternalResult.Failure <IEnumerable <T> >(true)); } return(InternalResult.Success <IEnumerable <T> >(ts, consumedInput)); }
private static Result <TToken, T> DoParse <TToken, T>(Parser <TToken, T> parser, ITokenStream <TToken> stream, Func <TToken, SourcePos, SourcePos> calculatePos) { var state = new ParseState <TToken>(calculatePos, stream); return(DoParse(parser, ref state)); }
// see comment about expecteds in ParseState.Error.cs public override InternalResult <IEnumerable <T>?> Parse(ref ParseState <TToken> state) { var ts = _keepResults ? new List <T>() : null; var firstItemResult = _parser.Parse(ref state); if (!firstItemResult.Success) { // state.Error set by _parser return(InternalResult.Failure <IEnumerable <T>?>(firstItemResult.ConsumedInput)); } if (!firstItemResult.ConsumedInput) { throw new InvalidOperationException("Until() used with a parser which consumed no input"); } ts?.Add(firstItemResult.Value); while (true) { state.BeginExpectedTran(); var terminatorResult = _terminator.Parse(ref state); if (terminatorResult.Success) { state.EndExpectedTran(false); return(InternalResult.Success <IEnumerable <T>?>(ts, true)); } if (terminatorResult.ConsumedInput) { // state.Error set by _terminator state.EndExpectedTran(true); return(InternalResult.Failure <IEnumerable <T>?>(true)); } state.BeginExpectedTran(); var itemResult = _parser.Parse(ref state); if (!itemResult.Success) { if (!itemResult.ConsumedInput) { // get the expected from both _terminator and _parser state.EndExpectedTran(true); state.EndExpectedTran(true); } else { // throw out the _terminator expecteds and keep only _parser var itemExpected = state.ExpectedTranState(); state.EndExpectedTran(false); state.EndExpectedTran(false); state.AddExpected(itemExpected.AsSpan()); itemExpected.Dispose(clearArray: true); } return(InternalResult.Failure <IEnumerable <T>?>(true)); } // throw out both sets of expecteds state.EndExpectedTran(false); state.EndExpectedTran(false); if (!itemResult.ConsumedInput) { throw new InvalidOperationException("Until() used with a parser which consumed no input"); } ts?.Add(itemResult.Value); } }
internal sealed override InternalResult <T> Parse(ref ParseState <TToken> state) => _lazy.Value.Parse(ref state);
internal sealed override InternalResult <T> Parse(ref ParseState <TToken> state) => InternalResult.Success <T>(_value, false);
internal sealed override bool TryParse(ref ParseState <TToken> state, ref ExpectedCollector <TToken> expecteds, out T result) => _lazy.Value.TryParse(ref state, ref expecteds, out result);
private static Result <TToken, T> DoParse <TToken, T>(Parser <TToken, T> parser, ref ParseState <TToken> state) { var startingLoc = state.Location; var expecteds = new ExpectedCollector <TToken>(); var success = parser.TryParse(ref state, ref expecteds, out var result); var result1 = success ? new Result <TToken, T>(state.Location > startingLoc, result) : new Result <TToken, T>(state.Location > startingLoc, state.BuildError(ref expecteds)); expecteds.Dispose(); state.Dispose(); // ensure we return the state's buffers to the buffer pool return(result1); }
// see comment about expecteds in ParseState.Error.cs internal sealed override bool TryParse(ref ParseState <TToken> state, ref ExpectedCollector <TToken> expecteds, out IEnumerable <T>?result) { var ts = _keepResults ? new List <T>() : null; var firstItemStartLoc = state.Location; var firstItemSuccess = _parser.TryParse(ref state, ref expecteds, out var result1); if (!firstItemSuccess) { // state.Error set by _parser result = null; return(false); } if (state.Location <= firstItemStartLoc) { throw new InvalidOperationException("Until() used with a parser which consumed no input"); } ts?.Add(result1); var terminatorExpecteds = new ExpectedCollector <TToken>(); var itemExpecteds = new ExpectedCollector <TToken>(); while (true) { var terminatorStartLoc = state.Location; var terminatorSuccess = _terminator.TryParse(ref state, ref terminatorExpecteds, out var terminatorResult); if (terminatorSuccess) { terminatorExpecteds.Dispose(); itemExpecteds.Dispose(); result = ts; return(true); } if (state.Location > terminatorStartLoc) { // state.Error set by _terminator expecteds.Add(ref terminatorExpecteds); terminatorExpecteds.Dispose(); itemExpecteds.Dispose(); result = null; return(false); } var itemStartLoc = state.Location; var itemSuccess = _parser.TryParse(ref state, ref itemExpecteds, out var itemResult); var itemConsumedInput = state.Location > itemStartLoc; if (!itemSuccess) { if (!itemConsumedInput) { // get the expected from both _terminator and _parser expecteds.Add(ref terminatorExpecteds); expecteds.Add(ref itemExpecteds); } else { // throw out the _terminator expecteds and keep only _parser expecteds.Add(ref itemExpecteds); } terminatorExpecteds.Dispose(); itemExpecteds.Dispose(); result = null; return(false); } // throw out both sets of expecteds terminatorExpecteds.Clear(); itemExpecteds.Clear(); if (!itemConsumedInput) { throw new InvalidOperationException("Until() used with a parser which consumed no input"); } ts?.Add(itemResult); } }
internal sealed override bool TryParse(ref ParseState <TToken> state, ref ExpectedCollector <TToken> expecteds, out IEnumerable <T> result) { var success = _parser.TryParse(ref state, ref expecteds, out var result1); if (!success) { // state.Error set by _parser result = null; return(false); } var ts = new List <T> { result1 }; var childExpecteds = new ExpectedCollector <TToken>(); while (true) { var sepStartLoc = state.Location; var sepSuccess = _separator.TryParse(ref state, ref childExpecteds, out var _); var sepConsumedInput = state.Location > sepStartLoc; expecteds.AddIf(ref childExpecteds, !sepSuccess && sepConsumedInput); childExpecteds.Clear(); if (!sepSuccess) { childExpecteds.Dispose(); if (sepConsumedInput) { // state.Error set by _separator result = null; return(false); } result = ts; return(true); } var itemStartLoc = state.Location; var itemSuccess = _parser.TryParse(ref state, ref childExpecteds, out var itemResult); var itemConsumedInput = state.Location > itemStartLoc; expecteds.AddIf(ref childExpecteds, !itemSuccess && itemConsumedInput); childExpecteds.Clear(); if (!itemSuccess) { childExpecteds.Dispose(); if (itemConsumedInput) { // state.Error set by _parser result = null; return(false); } result = ts; return(true); } ts.Add(itemResult); } }