Scala Parser Combinators - SQL Parser Example
Scala Parser Combinators: https://github.com/scala/scala-parser-combinators
Scala Parser Combinators is basically a parsing framework for extracting data when there is a pattern in the given input. This framework provides a more statically typed, functional way of extracting instead of using regex expression which can get hard to read.
In this post, lets build a SQL parser where given a valid sql statement we can identify the "table" name, "column" names and other sql properties. Following are some fundamental functions, operations that Scala combinator provides which would help in parsing:
- " | ": says “succeed if either the left or right operand parse successfully”
- " ~ ": says “succeed if the left operand parses successfully, and then the right parses successfully on the remaining input”
- " ~> ": says “succeed if the left operand parses successfully followed by the right, but do not include the left content in the result”
- " <~ ": is the reverse, “succeed if the left operand is parsed successfully followed by the right, but do not include the right content in the result”
- " ^^ ": says “if the left operand parses successfully, transform the result using the function on the right”
- " ^^^ ": says “if the left operand parses successfully, ignore the result and use the value from the right”
- " rep(fn) ": says "parse the given input using the parser function fn"
- " repsep(ident, char) ": says "parse the given input and split the input using the given 'char'"
select * from users
select name,age from users
select count(name) from users
select * from users order by age desc
select * from users order by name, age desc
select age from users where age>30
Labels: Scala
<< Home