public void Or_FirstError_ReturnsSecondResult() { var parser = Combine.Or(Parse.Fail <int>("test"), Parse.Succeed(42)); var result = parser.Parse(""); ParseAssert.ValueEquals(42, result); }
public void Label_Error_ReturnsErrorWithMessage() { var parser = Parse.Fail <int>("Oh noes").Label(() => "Test"); var result = parser.Parse(""); ParseAssert.ErrorEquals("Test", result); }
public void Fail_ReturnsParseError() { var parser = Parse.Fail <int>("test"); var result = parser.Parse(""); ParseAssert.ErrorEquals("test", result); }
public void Aggregate_NoMatch_ReturnsSeed() { var parser = Parse.Fail <char>("error") .Aggregate(() => "test", (acc, c) => acc + c); var result = parser.Parse("zz"); ParseAssert.ValueEquals("test", result); }
public void Select_Error_ReturnsError() { var parser = from i in Parse.Fail <int>("test") select i * 2; var result = parser.Parse(""); ParseAssert.ErrorEquals("test", result); }
public void Where_Error_ReturnsError() { var parser = from x in Parse.Fail <int>("test") where x == 42 select x; var result = parser.Parse(""); ParseAssert.ErrorEquals("test", result); }
public void SelectMany_SuccessThenError_ReturnsErrorValue() { var parser = from x in Parse.Succeed(21) from y in Parse.Fail <int>("test") select x *y; var result = parser.Parse(""); ParseAssert.ErrorEquals("test", result); }
public void Label_Error_SetsInnerError() { var parser = Parse.Fail <int>("Oh noes").Label(() => "Test"); var result = parser.Parse(""); ParseError error = result.FromError(); Assert.IsNotNull(error.InnerError); Assert.AreEqual(error.InnerError.Message, "Oh noes"); }
public void Or_FirstErrorConsumesInput_ReturnsFirstError() { var parserA = from x in Chars.Any() from y in Parse.Fail <char>("test") select y; var parserB = Chars.Any(); var parser = Combine.Or(parserA, parserB); var result = parser.Parse("abc"); ParseAssert.ErrorEquals("test", result); }
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()); }
/// <summary> /// Tries to apply the given parsers in order, until one of them succeeds. /// Returns the value of the succeeding parser. /// </summary> public static IParser <T> Choose <T>(IEnumerable <IParser <T> > parsers) { return(parsers.Aggregate(Parse.Fail <T>("Empty choose sequence") , (acc, p) => acc.Or(p))); }