public void TestBetween() { var r = Prim.Between(Prim.Character('['), Prim.Character(']'), Prim.String("abc")).Parse("[abc]"); Assert.True(!r.IsFaulted); Assert.True(r.Value.First().Item1.AsString() == "abc"); }
public CharLiteral() : base( inp => Tok.Lexeme( Prim.Between( Prim.Character('\''), Prim.Character('\'').Fail("end of character"), new CharacterChar() )) .Select(ch => new CharLiteralToken(ch, inp.Head().Location)) .Fail("character") .Parse(inp) ) { }
public StringLiteral() : base( inp => (from l in Tok.Lexeme( from str in Prim.Between( Prim.Character('"'), Prim.Character('"').Fail("end of string"), Prim.Many(new StringChar()) ) select str ) select new StringLiteralToken(l, inp.Head().Location) ) .Fail("literal string") .Parse(inp) ) { }
public Brackets(Parser <A> betweenParser) : base(inp => Prim.Between(Tok.Symbol("["), Tok.Symbol("]"), betweenParser) .Parse(inp)) { }
public Angles(Parser <A> betweenParser) : base(inp => Prim.Between(Tok.Symbol("<"), Tok.Symbol(">"), betweenParser) .Parse(inp)) { }
public Braces(Parser <A> betweenParser) : base(inp => Prim.Between(Tok.Symbol("{"), Tok.Symbol("}"), betweenParser) .Parse(inp)) { }
public Parens(Parser <A> betweenParser) : base(inp => Prim.Between(Tok.Symbol("("), Tok.Symbol(")"), betweenParser) .Parse(inp)) { }
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; }