Beispiel #1
0
        public static Parser <Unit> SkipMany <A>(Parser <A> skipParser)
        {
            if (skipParser == null)
            {
                throw new ArgumentNullException("skipParser");
            }

            return(new Parser <Unit>(
                       inp =>
            {
                if (inp.IsEmpty)
                {
                    return Prim.Return <Unit>(Unit.Default).Parse(inp);
                }

                do
                {
                    var resA = skipParser.Parse(inp);
                    if (resA.IsFaulted || resA.Value.IsEmpty)
                    {
                        return Prim.Return <Unit>(Unit.Default).Parse(inp);
                    }

                    inp = resA.Value.Last().Item2;
                }while (!inp.IsEmpty);

                return Prim.Return <Unit>(Unit.Default).Parse(inp);
            }
                       ));
        }
Beispiel #2
0
 public SimpleSpace()
     :
     base(
         inp => Prim.Many(Prim.Character(' '))
         .Parse(inp)
         )
 {
 }
Beispiel #3
0
 public StringParse(IEnumerable <char> str)
     :
     base(
         inp => str.Count() == 0
                   ? Prim.Return(ImmutableList.Empty <ParserChar>()).Parse(inp)
                   : (from x in Prim.Character(str.First())
                      from xs in Prim.String(str.Skip(1))
                      select x.Cons(xs))
         .Parse(inp)
         )
 {
 }
Beispiel #4
0
 public WhiteSpace()
     :
     base(
         inp => Prim.SkipMany(
             Prim.Character(' ')
             | Prim.Character('\t')
             | Prim.Character('\n')
             | Prim.Character('\r')
             )
         .Parse(inp)
         )
 {
 }
Beispiel #5
0
 public Integer()
     :
     base(inp =>
          (from minus in Prim.Try(Prim.Character('-') | Prim.Return(new ParserChar('+')))
           from digits in Prim.Many1(Prim.Digit())
           let v = DigitsToInt(digits)
                   select minus.Value == '+'
             ? v
             : -v)
          .Parse(inp)
          )
 {
 }
Beispiel #6
0
        public static Parser <ImmutableList <A> > SepBy <A, B>(Parser <A> parser, Parser <B> sepParser)
        {
            if (parser == null)
            {
                throw new ArgumentNullException("parser");
            }
            if (sepParser == null)
            {
                throw new ArgumentNullException("sepParser");
            }

            return(SepBy1 <A, B>(parser, sepParser) | Prim.Return(new ImmutableList <A>(new A[0])));
        }
Beispiel #7
0
 public Satisfy(Func <char, bool> pred, string expecting)
     :
     base(
         inp =>
         inp.IsEmpty
                 ? Prim.Failure <ParserChar>(ParserError.Create(expecting, inp)).Parse(inp)
                 : (from res in Prim.Item().Parse(inp).Value
                    select pred(res.Item1.Value)
                       ? Prim.Return(res.Item1).Parse(inp.Tail())
                       : ParserResult.Fail <ParserChar>(expecting, inp))
         .First()
         )
 {
 }
Beispiel #8
0
        public static Parser <ImmutableList <A> > SepBy1 <A, B>(Parser <A> parser, Parser <B> sepParser)
        {
            if (parser == null)
            {
                throw new ArgumentNullException("parser");
            }
            if (sepParser == null)
            {
                throw new ArgumentNullException("sepParser");
            }

            return(from x in parser
                   from xs in Prim.Many <A>(sepParser.And(parser))
                   select x.Cons(xs));
        }
Beispiel #9
0
 public Many(Parser <A> parser)
     :
     base(inp => (Prim.Many1(parser) | Prim.Return(new ImmutableList <A>(new A[0]))).Parse(inp))
 {
 }
Beispiel #10
0
 public Try <A> OrTry(Parser <A> p)
 {
     return(Prim.Try <A>(p));
 }
Beispiel #11
0
 /// <summary>
 /// Skip spaces - Different to the general-case SkipSpace which will return
 /// the space it consumes.
 /// </summary>
 public static Parser <Unit> SimpleSpace()
 {
     return(Prim.SkipMany1(Prim.OneOf(" \t\n\r")));
 }