示例#1
0
 public CharEsc()
     :
     base(
         inp =>
         Prim.Choice <ParserChar>(
             EscapeMap.Select(pair => new ParseEsc(pair.Item1, pair.Item2))
             )
         .Parse(inp)
         )
 {
 }
示例#2
0
 public ZeroNumber()
     :
     base(
         inp => (from z in Prim.Character('0')
                 from y in Prim.Choice(
                     Tok.Numbers.Hexadecimal() as Parser <IntegerToken>,
                     Tok.Numbers.Octal() as Parser <IntegerToken>,
                     Tok.Numbers.Decimal() as Parser <IntegerToken>,
                     Prim.Return(new IntegerToken(0, SrcLoc.Null)) as Parser <IntegerToken>
                     )
                 select new IntegerToken(0, !inp.IsEmpty ? inp.Head().Location : SrcLoc.EndOfSource))
         .Fail("")
         .Parse(inp)
         )
 {
 }
示例#3
0
        private void BuildReplParser()
        {
            var ParserParameter = Prim.Many1(Prim.Choice(Prim.OneOf("_>"), Prim.LetterOrDigit()));

            var ParserQuery = from query in parser.TopLevelParser()
                              from _ in Prim.WhiteSpace()
                              select new QueryContainer(query) as ReplParseObject;

            var ParserREPLDirective = from _ in Prim.Character(':')
                                      from option in Prim.Many1(Prim.Choice(Prim.Character('/'), Prim.LetterOrDigit()))
                                      from __ in Prim.WhiteSpace()
                                      from parameters in Prim.SepBy(ParserParameter, Prim.WhiteSpace())
                                      from ___ in Prim.Character(';')
                                      from ____ in Prim.WhiteSpace()
                                      select new REPLDirectiveParsedObject(option.AsString(), parameters.AsStrings(), _.Location) as ReplParseObject;

            replParser = (from ts in Prim.Many1(
                              from lq in Prim.Choice(ParserQuery, ParserREPLDirective)
                              select lq
                              )
                          select ts)
                         .Fail("Expected query or command");
        }
示例#4
0
        public void TestChoice()
        {
            var r = Prim.Choice(Prim.Item(), Prim.Return(Prim.ParserChar('d'))).Parse("abc").Value.Single();

            Assert.True(
                r.Item1.Value == 'a' &&
                r.Item2.AsString() == "bc"
                );

            var inp = "abc".ToParserChar();

            var parser = Prim.Choice(
                Prim.Failure <ParserChar>(ParserError.Create("failed because...", inp)),
                Prim.Return(Prim.ParserChar('d'))
                )
                         .Parse(inp);

            r = parser.Value.Single();

            Assert.True(
                r.Item1.Value == 'd' &&
                r.Item2.AsString() == "abc"
                );
        }
示例#5
0
        private static Parser <A> MakeParser <A>(IEnumerable <Operator <A> > ops, Parser <A> term)
        {
            var empty2 = ImmutableList.Empty <Parser <Func <A, A> > >();
            var empty3 = ImmutableList.Empty <Parser <Func <A, A, A> > >();

            return(ops.Foldr(
                       SplitOp,
                       Tuple.Create(empty3, empty3, empty3, empty2, empty2)
                       )
                   .Apply((rassoc, lassoc, nassoc, prefix, postfix) =>
            {
                var rassocOp = Prim.Choice(rassoc);
                var lassocOp = Prim.Choice(lassoc);
                var nassocOp = Prim.Choice(nassoc);
                var prefixOp = Prim.Choice(prefix).Fail("");
                var postfixOp = Prim.Choice(postfix).Fail("");

                Func <string, Choice <Func <A, A, A> >, Parser <A> > ambiguous = (string assoc, Choice <Func <A, A, A> > op) =>
                                                                                 Prim.Try <A>(
                    (from o in op
                     from fail in
                     Prim.Failure <A>(
                         ParserError.Create("ambiguous use of a " + assoc + " associative operator",
                                            new ParserChar(' ').Cons()
                                            ))
                     select fail)
                    );


                var ambiguousRight = ambiguous("right", rassocOp);
                var ambiguousLeft = ambiguous("left", lassocOp);
                var ambiguousNon = ambiguous("non", nassocOp);

                var postfixP = postfixOp | Prim.Return <Func <A, A> >(a => a);
                var prefixP = prefixOp | Prim.Return <Func <A, A> >(a => a);

                Parser <A> termP = from pre in prefixP
                                   from x in term
                                   from post in postfixP
                                   select(post(pre(x)));

                Func <A, Parser <A> > rassocP1 = null;

                Func <A, Parser <A> > rassocP = x => (from f in rassocOp
                                                      from y in
                                                      (from z in termP
                                                       from rz in rassocP1(z)
                                                       select rz)
                                                      select f(x, y))
                                                | ambiguousLeft
                                                | ambiguousNon;

                rassocP1 = x => rassocP(x) | Prim.Return(x);

                Func <A, Parser <A> > lassocP1 = null;

                Func <A, Parser <A> > lassocP = x => (from f in lassocOp
                                                      from y in termP
                                                      from l in lassocP1(f(x, y))
                                                      select l)
                                                | ambiguousRight;

                lassocP1 = x => (from l in lassocP(x)
                                 select l)
                           | Prim.Return(x);

                Func <A, Parser <A> > nassocP = x => (from f in nassocOp
                                                      from y in termP
                                                      from r in ambiguousRight
                                                      | ambiguousLeft
                                                      | ambiguousNon
                                                      | Prim.Return(f(x, y))
                                                      select r);

                return from x in termP
                from r in (rassocP(x) | lassocP(x) | nassocP(x) | Prim.Return(x)).Fail("operator")
                select r;
            }
                          ));
        }
示例#6
0
        public void BuildScrapeQLParser()
        {
            var def        = new ScrapeQLDef();
            var lexer      = Tok.MakeTokenParser <Term>(def);
            var reserved   = lexer.Reserved;
            var identifier = lexer.Identifier;
            var strings    = lexer.StringLiteral;

            var ParserComma = from _ in Prim.WhiteSpace()
                              from c in Prim.Character(',')
                              from __ in Prim.WhiteSpace()
                              select c;

            var ParserRegularExpression = (from b in Prim.Character('\\')
                                           from r in Prim.Character('r')
                                           from re in strings
                                           select new RegularExpression(re.Value.AsString()))
                                          .Fail("Regex");

            var ParserListLiteralStringToken = (from strs in Prim.SepBy(
                                                    strings,
                                                    ParserComma
                                                    )
                                                select strs);

            var ParserListIdentifierToken = (from strs in Prim.SepBy(
                                                 identifier,
                                                 ParserComma
                                                 )
                                             select strs);

            /*ParserKeyPair = (from left in ParserString
             *         from c in Prim.Character(':')
             *         from right in ParserString
             *         select Tuple.Create(left, right))
             *         .Fail("tuple");
             *
             * ParserDictionary = (from ps in Prim.SepBy1(ParserKeyPair, Prim.Character(','))
             *                 select ps)
             *                 .Fail("dictionary");*/

            var ParserLoadQuery = from _ in reserved("LOAD")
                                  from sources in ParserListLiteralStringToken
                                  from __ in reserved("AS")
                                  from aliases in ParserListIdentifierToken
                                  select new LoadQuery(aliases, sources, _.Location) as Query;

            var ParserWriteQuery = from _ in reserved("WRITE")
                                   from alias in identifier
                                   from __ in reserved("TO")
                                   from src in strings
                                   select new WriteQuery(alias, src, _.Location) as Query;

            var ParserStringSelector = from s in strings select new SelectorString(s, s.Location) as Selector;

            var ParserAttributeSelector = from src in identifier
                                          from xpath in Prim.Between(Prim.Character('['), Prim.Character(']'), strings)
                                          select new AttributeSelector(src, xpath, src.Location) as Selector;

            var Conditional = from sq in reserved("TO")
                              select sq; //TODO: Conditions

            var ParserWhereExpression = from _ in reserved("WHERE")
                                        from clauses in Prim.Many1(Conditional)
                                        select new WhereExpression() as Term; //TODO: Return enumarable conditions

            var ParserSelectQuery = from _ in reserved("SELECT")
                                    from selector in Prim.Choice(ParserStringSelector, ParserAttributeSelector)
                                    from __ in reserved("AS")
                                    from alias in identifier
                                    from ___ in reserved("FROM")
                                    from src in identifier
                                    from whereClasuses in Prim.Try(ParserWhereExpression)
                                    select new SelectQuery(selector, alias, src, _.Location) as Query;



            TopLevel = Prim.Choice(ParserLoadQuery, ParserSelectQuery, ParserWriteQuery);

            TopLevelMany = from ts in Prim.Many1(
                from lq in TopLevel
                select lq
                )
                           select ts;
        }