public void Or_FirstSuccess_ReturnsFirstResult() { var parser = Combine.Or(Parse.Succeed('x'), Parse.Succeed('y')); var result = parser.Parse(""); ParseAssert.ValueEquals('x', result); }
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 Succeed_ReturnsGivenValue() { var parser = Parse.Succeed('x'); var result = parser.Parse(""); ParseAssert.ValueEquals('x', result); }
public void Label_Success_ReturnsSuccess() { var parser = Parse.Succeed(42).Label(() => "test"); var result = parser.Parse(""); ParseAssert.ValueEquals(42, result); }
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); }
/// <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))); }
public void Select_Success_ReturnsSelectedValue() { var parser = from i in Parse.Succeed(21) select i * 2; var result = parser.Parse(""); ParseAssert.ValueEquals(42, result); }
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); }
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); }
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 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); }
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); }
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); }
/// <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>()))); }