예제 #1
0
        public void Or_FirstError_ReturnsSecondResult()
        {
            var parser = Combine.Or(Parse.Fail <int>("test"), Parse.Succeed(42));
            var result = parser.Parse("");

            ParseAssert.ValueEquals(42, result);
        }
예제 #2
0
        public void Label_Error_ReturnsErrorWithMessage()
        {
            var parser = Parse.Fail <int>("Oh noes").Label(() => "Test");
            var result = parser.Parse("");

            ParseAssert.ErrorEquals("Test", result);
        }
예제 #3
0
        public void Fail_ReturnsParseError()
        {
            var parser = Parse.Fail <int>("test");
            var result = parser.Parse("");

            ParseAssert.ErrorEquals("test", result);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }
예제 #8
0
        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");
        }
예제 #9
0
        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);
        }
예제 #10
0
        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());
        }
예제 #11
0
 /// <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)));
 }