示例#1
0
        public void Or_FirstSuccess_ReturnsFirstResult()
        {
            var parser = Combine.Or(Parse.Succeed('x'), Parse.Succeed('y'));
            var result = parser.Parse("");

            ParseAssert.ValueEquals('x', result);
        }
示例#2
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);
        }
示例#3
0
        public void Succeed_ReturnsGivenValue()
        {
            var parser = Parse.Succeed('x');
            var result = parser.Parse("");

            ParseAssert.ValueEquals('x', result);
        }
示例#4
0
        public void Label_Success_ReturnsSuccess()
        {
            var parser = Parse.Succeed(42).Label(() => "test");
            var result = parser.Parse("");

            ParseAssert.ValueEquals(42, result);
        }
示例#5
0
        public void Many_IEnumerable()
        {
            var parser = (from c in Chars.Any()
                          from x in Parse.Succeed(42)
                          select x).Many();
            var result = parser.Parse("xyz");

            ParseAssert.ValueEquals(new[] { 42, 42, 42 }, result);
        }
示例#6
0
        /// <summary>
        /// Repeats a parser a given number of times.
        /// </summary>
        public static IParser <IEnumerable <TValue> > Repeat <TValue>(this IParser <TValue> parser, int repeatCount)
        {
            if (repeatCount <= 0)
            {
                return(Parse.Succeed(Enumerable.Empty <TValue>()));
            }

            return(Parse.Sequence(Enumerable.Repeat(parser, repeatCount)));
        }
示例#7
0
        public void Select_Success_ReturnsSelectedValue()
        {
            var parser = from i in Parse.Succeed(21)
                         select i * 2;

            var result = parser.Parse("");

            ParseAssert.ValueEquals(42, result);
        }
示例#8
0
        public void Try_Success_ReturnsSuccess()
        {
            var parser = from a in Parse.Try(Parse.Succeed("a"))
                         from b in Parse.Try(Parse.Succeed("b"))
                         select a + b;

            var result = parser.Parse("abc");

            ParseAssert.ValueEquals("ab", result);
        }
示例#9
0
        public void Where_PassingPredicate_ReturnsSuccess()
        {
            var parser = from x in Parse.Succeed(42)
                         where x == 42
                         select x;

            var result = parser.Parse("");

            ParseAssert.ValueEquals(42, result);
        }
示例#10
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);
        }
示例#11
0
        public void SelectMany_Successes_ReturnsSelectedValue()
        {
            var parser = from x in Parse.Succeed(21)
                         from y in Parse.Succeed(2)
                         select x *y;

            var result = parser.Parse("");

            ParseAssert.ValueEquals(42, result);
        }
示例#12
0
        public void Where_FailingPredicate_ReturnsError()
        {
            var parser = from x in Parse.Succeed(42)
                         where x != 42
                         select x;

            var result = parser.Parse("");

            ParseAssert.ErrorEquals("Unexpected \"42\"", result);
        }
示例#13
0
        public void SelectMany_ManySuccesses_CombinesAll()
        {
            var parser = from w in Parse.Succeed(21)
                         from x in Parse.Succeed(3)
                         from y in Parse.Succeed(4)
                         from z in Parse.Succeed(6)
                         select(w * x * y) / z;

            var result = parser.Parse("");

            ParseAssert.ValueEquals(42, result);
        }
示例#14
0
 /// <summary>
 /// Parses zero or more occurrences of a parser, each followed by a separator parser.
 /// </summary>
 public static IParser <IEnumerable <T> > SeparatedBy <T, TSep>(this IParser <T> parser, IParser <TSep> separator)
 {
     return(SeparatedBy1(parser, separator).Or(Parse.Succeed(Enumerable.Empty <T>())));
 }