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); } )); }
public SimpleSpace() : base( inp => Prim.Many(Prim.Character(' ')) .Parse(inp) ) { }
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) ) { }
public WhiteSpace() : base( inp => Prim.SkipMany( Prim.Character(' ') | Prim.Character('\t') | Prim.Character('\n') | Prim.Character('\r') ) .Parse(inp) ) { }
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) ) { }
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]))); }
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() ) { }
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)); }
public Many(Parser <A> parser) : base(inp => (Prim.Many1(parser) | Prim.Return(new ImmutableList <A>(new A[0]))).Parse(inp)) { }
public Try <A> OrTry(Parser <A> p) { return(Prim.Try <A>(p)); }
/// <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"))); }