1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
// nolint: govet
package main
import (
"github.com/alecthomas/kong"
"github.com/alecthomas/repr"
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
)
type Boolean bool
func (b *Boolean) Capture(values []string) error {
*b = values[0] == "TRUE"
return nil
}
// Select based on http://www.h2database.com/html/grammar.html
type Select struct {
Top *Term `"SELECT" ( "TOP" @@ )?`
Distinct bool `( @"DISTINCT"`
All bool ` | @"ALL" )?`
Expression *SelectExpression `@@`
From *From `"FROM" @@`
Limit *Expression `( "LIMIT" @@ )?`
Offset *Expression `( "OFFSET" @@ )?`
GroupBy *Expression `( "GROUP" "BY" @@ )?`
}
type From struct {
TableExpressions []*TableExpression `@@ ( "," @@ )*`
Where *Expression `( "WHERE" @@ )?`
}
type TableExpression struct {
Table string `( @Ident ( "." @Ident )*`
Select *Select ` | "(" @@ ")"`
Values []*Expression ` | "VALUES" "(" @@ ( "," @@ )* ")")`
As string `( "AS" @Ident )?`
}
type SelectExpression struct {
All bool ` @"*"`
Expressions []*AliasedExpression `| @@ ( "," @@ )*`
}
type AliasedExpression struct {
Expression *Expression `@@`
As string `( "AS" @Ident )?`
}
type Expression struct {
Or []*OrCondition `@@ ( "OR" @@ )*`
}
type OrCondition struct {
And []*Condition `@@ ( "AND" @@ )*`
}
type Condition struct {
Operand *ConditionOperand ` @@`
Not *Condition `| "NOT" @@`
Exists *Select `| "EXISTS" "(" @@ ")"`
}
type ConditionOperand struct {
Operand *Operand `@@`
ConditionRHS *ConditionRHS `@@?`
}
type ConditionRHS struct {
Compare *Compare ` @@`
Is *Is `| "IS" @@`
Between *Between `| "BETWEEN" @@`
In *In `| "IN" "(" @@ ")"`
Like *Like `| "LIKE" @@`
}
type Compare struct {
Operator string `@( "<>" | "<=" | ">=" | "=" | "<" | ">" | "!=" )`
Operand *Operand `( @@`
Select *CompareSelect ` | @@ )`
}
type CompareSelect struct {
All bool `( @"ALL"`
Any bool ` | @"ANY"`
Some bool ` | @"SOME" )`
Select *Select `"(" @@ ")"`
}
type Like struct {
Not bool `[ @"NOT" ]`
Operand *Operand `@@`
}
type Is struct {
Not bool `[ @"NOT" ]`
Null bool `( @"NULL"`
DistinctFrom *Operand ` | "DISTINCT" "FROM" @@ )`
}
type Between struct {
Start *Operand `@@`
End *Operand `"AND" @@`
}
type In struct {
Select *Select ` @@`
Expressions []*Expression `| @@ ( "," @@ )*`
}
type Operand struct {
Summand []*Summand `@@ ( "|" "|" @@ )*`
}
type Summand struct {
LHS *Factor `@@`
Op string `[ @("+" | "-")`
RHS *Factor ` @@ ]`
}
type Factor struct {
LHS *Term `@@`
Op string `( @("*" | "/" | "%")`
RHS *Term ` @@ )?`
}
type Term struct {
Select *Select ` @@`
Value *Value `| @@`
SymbolRef *SymbolRef `| @@`
SubExpression *Expression `| "(" @@ ")"`
}
type SymbolRef struct {
Symbol string `@Ident @( "." Ident )*`
Parameters []*Expression `( "(" @@ ( "," @@ )* ")" )?`
}
type Value struct {
Wildcard bool `( @"*"`
Number *float64 ` | @Number`
String *string ` | @String`
Boolean *Boolean ` | @("TRUE" | "FALSE")`
Null bool ` | @"NULL"`
Array *Array ` | @@ )`
}
type Array struct {
Expressions []*Expression `"(" @@ ( "," @@ )* ")"`
}
var (
cli struct {
SQL string `arg:"" required:"" help:"SQL to parse."`
}
sqlLexer = lexer.MustSimple([]lexer.SimpleRule{
{`Keyword`, `(?i)\b(SELECT|FROM|TOP|DISTINCT|ALL|WHERE|GROUP|BY|HAVING|UNION|MINUS|EXCEPT|INTERSECT|ORDER|LIMIT|OFFSET|TRUE|FALSE|NULL|IS|NOT|ANY|SOME|BETWEEN|AND|OR|LIKE|AS|IN)\b`},
{`Ident`, `[a-zA-Z_][a-zA-Z0-9_]*`},
{`Number`, `[-+]?\d*\.?\d+([eE][-+]?\d+)?`},
{`String`, `'[^']*'|"[^"]*"`},
{`Operators`, `<>|!=|<=|>=|[-+*/%,.()=<>]`},
{"whitespace", `\s+`},
})
parser = participle.MustBuild[Select](
participle.Lexer(sqlLexer),
participle.Unquote("String"),
participle.CaseInsensitive("Keyword"),
// participle.Elide("Comment"),
// Need to solve left recursion detection first, if possible.
// participle.UseLookahead(),
)
)
func main() {
ctx := kong.Parse(&cli)
sql, err := parser.ParseString("", cli.SQL)
repr.Println(sql, repr.Indent(" "), repr.OmitEmpty(true))
ctx.FatalIfErrorf(err)
}
|