Extensible SQL Lexer and Parser in MoonBit
pub(all) enum Asc {
Ascending
Descending
}pub(all) enum BinaryOperator {
Eq
Neq
Lt
Gt
LtEq
GtEq
Spaceship
Plus
Minus
Mul
Div
Mod
And
Or
}pub(all) enum DatetimeUnit {
Year
Month
Day
Hour
Minute
Second
}impl Eq for DatetimeUnitpub(all) enum Expr {
Identifier(String)
Literal(Literal)
BinaryOperation(Expr, BinaryOperator, Expr)
UnaryOperation(UnaryOperator, Expr)
FunctionCall(String, Array[Expr])
Wildcard
Datetime(String)
Interval(String, IntervalQualifier)
Like(Bool, Expr, Expr)
ILike(Bool, Expr, Expr)
SubQuery(SelectStmt)
}pub(all) enum IntervalQualifier {
Single(PrimaryDatetimeField)
Range(PrimaryDatetimeField, PrimaryDatetimeField)
}impl Eq for IntervalQualifierpub(all) enum Keyword {
Select
From
Where
As
Group
Order
By
Asc
Desc
Nulls
First
Last
Year
Month
Day
Hour
Minute
Second
Date
Interval
To
Like
Not
ILike
And
Or
}type Lexerpub(all) enum Literal {
Integer(Int)
Double(Double)
String(String)
Boolean(Bool)
Null
}pub(all) enum Precedence {
PlusMinus
MulDivMod
Eq
Like
And
Or
}impl Eq for Projectionpub struct SelectStmt {
projections : Array[Projection]
from : Array[TableRef]
where_clause : Expr?
group_by : Array[Expr]
having : Expr?
order_by : Array[OrderByExpr]
limit : Int?
offset : Int?
}impl Eq for SelectStmtpub(all) enum Token {
Keyword(Keyword)
Identifier(String)
Number(String)
StringLiteral(String)
Comma
Semicolon
Eq
DoubleEq
Neq
Lt
Gt
LtEq
GtEq
Spaceship
Plus
Minus
Mul
Div
Mod
LBracket
RBracket
LBrace
RBrace
LParen
RParen
Unknown(Char)
Eof
}pub(all) enum UnaryOperator {
Negate
Not
}impl Eq for UnaryOperator