Powered by Ruby syntax, parser written in rparsec can be very concise. You have seen how easy the calculator and s-expression parser is. In this article we will demonstrate a more practical parser that parses sql-like language.
Informally, the syntax supported are:
- number literal
- string literal (quoted by single quote and escaped by double single quote)
- variables starting with a "$" character.
- Line commment indicated by a "#".
- Standard sql constructs such as select, from, where, group by, order by, union
- Joins such as inner join, left join, right join, full join, cross join
- Expression with +, -, *, /, % operators.
- "case-when-else-end" expression in Ruby syntax.
- Comparisons such as "=", "!=", ">", "<", ">=", "<=" etc.
- and, or, not
- in, not in, exists, not exists, between, not between
- Group compasison as in "(a,b)=(1,2)"
- Table alias
- Words quoted within "[]".
The following code is the full parser:
To get a parser for relation, we can write
where SqlParser::relation is the grammar parser and SqlParser::make connects it to the lexer.
Created by benyu benyu
On Wed Oct 11 22:18:03 CDT 2006
Using TimTam
Labels
