public IEither <T, ParseError> Parse(IInputReader input) { Position position = input.GetPosition(); var result = this.parserA.Parse(input); if (result.IsSuccess || input.GetPosition() != position) { return(result); } return(this.parserB.Parse(input)); }
public void Try_Error_ReturnsErrorAndResetsPosition() { var parser = Parse.Try(from x in Chars.Any() from y in Chars.Any() from f in Parse.Fail <char>("test") select x); IInputReader input = InputReader.Create("abc"); Position expectedPosition = input.GetPosition(); var result = parser.Parse(input); ParseAssert.IsError(result); Assert.AreEqual(expectedPosition, input.GetPosition()); }
public void Seek_UpdatesPosition() { IInputReader stream = CreateInputStream("abc"); Position position = stream.GetPosition(); stream.Read(); stream.Read(); stream.Seek(position); Position newPosition = stream.GetPosition(); Assert.AreEqual(position, newPosition); char result = (char)stream.Read(); Assert.AreEqual('a', result); }
public void GetPosition_NothingRead() { IInputReader stream = CreateInputStream(""); Position position = stream.GetPosition(); Assert.AreEqual(1, position.Line, "Line"); Assert.AreEqual(1, position.Column, "Column"); }
public void GetPosition_Read_DoesNotUpdateReturnedPosition() { IInputReader stream = CreateInputStream("abc"); Position position = stream.GetPosition(); stream.Read(); Assert.AreEqual(1, position.Line, "Line"); Assert.AreEqual(1, position.Column, "Column"); }
public void GetPosition_ReadChar() { IInputReader stream = CreateInputStream("abc\nxyz"); stream.Read(); Position position = stream.GetPosition(); Assert.AreEqual(1, position.Line, "Line"); Assert.AreEqual(2, position.Column, "Column"); }
public IParseResult <Unit> TryParse(IInputReader inp) { var pos = inp.GetPosition(); var result = _parser.TryParse(inp); if (result.IsSuccess()) { inp.Seek(pos); return(Parse.Error <Unit>($"Expect {_parser.Name}, but got '{result.Result}'", inp)); } return(Parse.Success(Unit.Instance)); }
public IParseResult <T> TryParse(IInputReader inp) { var pos = inp.GetPosition(); var parsed = _parser.TryParse(inp); if (!parsed.IsSuccess()) { return(Parse.Error <T>(parsed.Error)); } inp.Seek(pos); return(Parse.Success <T>()); }
public IEither <TResult, ParseError> Parse(IInputReader input) { TAccum acc = this.seed(); IEither <T, ParseError> result = null; Position position = input.GetPosition(); while ((result = this.parser.Parse(input)).IsSuccess) { acc = this.func(acc, result.FromSuccess()); position = input.GetPosition(); } if (input.GetPosition() == position) { return(ParseResult.Success(this.resultSelector(acc))); } else { return(ParseResult.Error <TResult>(result.FromError())); } }
public IEither <T, ParseError> Parse(IInputReader input) { Position position = input.GetPosition(); var result = this.parser.Parse(input); if (result.IsError) { input.Seek(position); } return(result); }
public IParseResult <TResult> TryParse(IInputReader inp) { TAccum acc = _seed(); do { var pos = inp.GetPosition(); var parsed = _parser.TryParse(inp); if (!parsed.IsSuccess()) { inp.Seek(pos); break; } acc = _accFunc(acc, parsed.Result); } while (!inp.Eof()); return(Parse.Success(_resultSelector(acc))); }
public IParseResult <T1> TryParse(IInputReader inp) { var parsed1 = _p1.TryParse(inp); if (!parsed1.IsSuccess()) { return(parsed1); } var pos = inp.GetPosition(); var parsed2 = _p2.TryParse(inp); if (!parsed2.IsSuccess()) { return(Parse.Error <T1>(parsed1.Error)); } inp.Seek(pos); return(parsed1); }
public IParseResult <IEnumerable <T> > TryParse(IInputReader inp) { var acc = new T[_parsers.Length]; var idx = 0; var pos = inp.GetPosition(); foreach (var parser in _parsers) { var parsed = parser.TryParse(inp); if (!parsed.IsSuccess()) { inp.Seek(pos); return(Parse.Error <IEnumerable <T> >(parsed.Error)); } acc[idx] = parsed.Result; idx++; } return(Parse.Success <IEnumerable <T> >(acc)); }
public IParseResult <T> TryParse(IInputReader inp) { var pos1 = inp.GetPosition(); var parsed1 = _parserA.TryParse(inp); if (parsed1.IsSuccess()) { return(parsed1); } inp.Seek(pos1); var parsed2 = _parserB.TryParse(inp); if (parsed2.IsSuccess()) { return(parsed2); } var ch = inp.Substr(20); return(Parse.Error <T>($"Expect {Name}, but got '{ch}'", inp)); }
public static IEither <T, ParseError> UnexpectedValue <T>(IInputReader input, object value) { return(Error <T>(new ParseError(input.GetPosition(), "Unexpected \"" + value.ToString() + "\""))); }
public static IEither <T, ParseError> Error <T>(IInputReader input, Func <string> messageFn, ParseError innerError) { return(Error <T>(new ParseError(input.GetPosition(), messageFn, innerError))); }
public static IEither <T, ParseError> Error <T>(IInputReader input, string message) { return(Error <T>(new ParseError(input.GetPosition(), message))); }