rql/parser.go

43 lines
979 B
Go
Raw Permalink Normal View History

2024-05-12 04:04:35 +00:00
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:"@@*"`
}