Ejemplo n.º 1
0
 /// <summary>
 /// Creates a parser which parses a character which satisfies the specified condition.
 /// </summary>
 /// <param name="expectedChars"></param>
 /// <returns></returns>
 public static Parser <char> Satisfy(Func <char, bool> satisfies)
 {
     return
         (Create(source =>
                 ParseResultModule.If(
                     source.Length >= 1 && satisfies(source[0]),
                     () => (source[0], source.Substring(1)),
                     () => source
                     )));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Creates a parser which parses the specified string.
 /// </summary>
 /// <param name="expectedString"></param>
 /// <returns></returns>
 public static Parser <string> OfString(string expectedString)
 {
     return
         (Create(source =>
                 ParseResultModule.If(
                     source.StartsWith(expectedString),
                     () => (expectedString, source.Substring(expectedString.Length)),
                     () => source
                     )));
 }
Ejemplo n.º 3
0
 public Parser <Y> SelectMany <Y>(Func <TValue, Parser <Y> > f)
 {
     return
         (ParserModule.Create(source =>
     {
         var firstResult = Parse(source);
         if (firstResult.IsOk)
         {
             var secondParser = f(firstResult.Value);
             return secondParser.Parse(firstResult.Rest);
         }
         return ParseResultModule.Error <Y>(source);
     }));
 }
Ejemplo n.º 4
0
 public Parser <Y> Select <Y>(Func <TValue, Y> func)
 {
     return
         (ParserModule.Create(source =>
     {
         var result = Parse(source);
         return
         ParseResultModule.If(
             result.IsOk,
             () => (func(result.Value), result.Rest),
             () => source
             );
     }));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates a parser which parses nothing and returns a successful result.
 /// </summary>
 /// <typeparam name="X"></typeparam>
 /// <param name="value"></param>
 /// <returns></returns>
 public static Parser <X> FromResult <X>(X value)
 {
     return(Create(source => ParseResultModule.Ok(value, source)));
 }