43 lines
979 B
Go
43 lines
979 B
Go
|
package rql
|
||
|
|
||
|
import (
|
||
|
"github.com/alecthomas/participle/v2"
|
||
|
"github.com/alecthomas/participle/v2/lexer"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
rqlLexer = lexer.MustSimple([]lexer.SimpleRule{
|
||
|
{Name: "whitespace", Pattern: `\s+`},
|
||
|
{Name: "String", Pattern: `"[^"]*"`},
|
||
|
{Name: "Ident", Pattern: `[a-zA-Z_][a-zA-Z_0-9]*`},
|
||
|
{Name: "Punct", Pattern: `[-[!@#$%^&*()+_={};':",.<>?/]|]`},
|
||
|
})
|
||
|
|
||
|
rqlParser = participle.MustBuild[Query](
|
||
|
participle.Lexer(rqlLexer),
|
||
|
participle.Unquote("String"),
|
||
|
participle.UseLookahead(2),
|
||
|
)
|
||
|
)
|
||
|
|
||
|
func Parse(query string) (*Query, error) {
|
||
|
return rqlParser.ParseString("", query)
|
||
|
}
|
||
|
|
||
|
type Operator string
|
||
|
|
||
|
type Values struct {
|
||
|
Single *string `parser:"@String"`
|
||
|
List []string `parser:"| ( '[' @String (',' @String)* ']' )"`
|
||
|
}
|
||
|
|
||
|
type Predicate struct {
|
||
|
Key string `parser:"@Ident '.'"`
|
||
|
Operator Operator `parser:"@('eq'|'lt'|'lte'|'gt'|'gte'|'like'|'in')"`
|
||
|
Values Values `parser:"'=' @@"`
|
||
|
}
|
||
|
|
||
|
type Query struct {
|
||
|
Predicates []*Predicate `parser:"@@*"`
|
||
|
}
|