super-graph/qcode/parse.go

567 lines
9.1 KiB
Go
Raw Normal View History

2019-03-24 14:57:29 +01:00
package qcode
import (
"errors"
"fmt"
"sync"
2019-06-15 04:17:21 +02:00
"unsafe"
2019-03-24 14:57:29 +01:00
"github.com/dosco/super-graph/util"
)
var (
errEOT = errors.New("end of tokens")
)
2019-06-08 02:53:08 +02:00
type parserType int32
2019-03-24 14:57:29 +01:00
const (
2019-05-13 01:27:26 +02:00
maxFields = 100
maxArgs = 10
)
2019-04-19 07:55:03 +02:00
const (
2019-03-24 14:57:29 +01:00
parserError parserType = iota
parserEOF
opQuery
opMutate
opSub
NodeStr
NodeInt
NodeFloat
NodeBool
NodeObj
NodeList
NodeVar
2019-03-24 14:57:29 +01:00
)
type Operation struct {
Type parserType
Name string
Args []Arg
argsA [10]Arg
Fields []Field
fieldsA [10]Field
2019-03-24 14:57:29 +01:00
}
var zeroOperation = Operation{}
func (o *Operation) Reset() {
*o = zeroOperation
2019-03-24 14:57:29 +01:00
}
type Field struct {
2019-06-08 02:53:08 +02:00
ID int32
ParentID int32
Name string
Alias string
Args []Arg
2019-06-15 04:17:21 +02:00
argsA [5]Arg
2019-06-08 02:53:08 +02:00
Children []int32
2019-06-15 04:17:21 +02:00
childrenA [5]int32
2019-03-24 14:57:29 +01:00
}
type Arg struct {
Name string
Val *Node
}
type Node struct {
Type parserType
Name string
Val string
Parent *Node
Children []*Node
exp *Exp
2019-03-24 14:57:29 +01:00
}
var zeroNode = Node{}
func (n *Node) Reset() {
*n = zeroNode
}
2019-03-24 14:57:29 +01:00
type Parser struct {
2019-06-15 04:17:21 +02:00
input []byte // the string being scanned
2019-03-24 14:57:29 +01:00
pos int
items []item
err error
}
var nodePool = sync.Pool{
New: func() interface{} { return new(Node) },
}
var opPool = sync.Pool{
New: func() interface{} { return new(Operation) },
}
var lexPool = sync.Pool{
New: func() interface{} { return new(lexer) },
}
2019-06-15 04:17:21 +02:00
func Parse(gql []byte) (*Operation, error) {
2019-09-05 06:09:56 +02:00
return parseSelectionSet(gql)
2019-03-24 14:57:29 +01:00
}
func ParseArgValue(argVal string) (*Node, error) {
l := lexPool.Get().(*lexer)
l.Reset()
2019-06-15 04:17:21 +02:00
if err := lex(l, []byte(argVal)); err != nil {
2019-03-24 14:57:29 +01:00
return nil, err
}
2019-06-15 04:17:21 +02:00
2019-03-24 14:57:29 +01:00
p := &Parser{
2019-06-15 04:17:21 +02:00
input: l.input,
2019-03-24 14:57:29 +01:00
pos: -1,
items: l.items,
}
op, err := p.parseValue()
lexPool.Put(l)
2019-03-24 14:57:29 +01:00
return op, err
2019-03-24 14:57:29 +01:00
}
2019-09-05 06:09:56 +02:00
func parseSelectionSet(gql []byte) (*Operation, error) {
2019-06-15 04:17:21 +02:00
var err error
if len(gql) == 0 {
return nil, errors.New("blank query")
}
l := lexPool.Get().(*lexer)
l.Reset()
2019-06-15 04:17:21 +02:00
if err = lex(l, gql); err != nil {
2019-03-24 14:57:29 +01:00
return nil, err
}
2019-06-15 04:17:21 +02:00
2019-03-24 14:57:29 +01:00
p := &Parser{
2019-06-15 04:17:21 +02:00
input: l.input,
2019-03-24 14:57:29 +01:00
pos: -1,
items: l.items,
}
2019-06-15 04:17:21 +02:00
2019-09-05 06:09:56 +02:00
var op *Operation
2019-06-15 04:17:21 +02:00
2019-09-05 06:09:56 +02:00
if p.peek(itemObjOpen) {
p.ignore()
2019-11-19 06:47:55 +01:00
op, err = p.parseQueryOp()
2019-09-05 06:09:56 +02:00
} else {
op, err = p.parseOp()
2019-11-19 06:47:55 +01:00
}
2019-09-05 06:09:56 +02:00
2019-11-19 06:47:55 +01:00
if err != nil {
return nil, err
2019-06-15 04:17:21 +02:00
}
2020-01-16 07:44:19 +01:00
if p.peek(itemObjClose) {
p.ignore()
} else {
return nil, fmt.Errorf("operation missing closing '}'")
}
2020-01-16 07:44:19 +01:00
if !p.peek(itemEOF) {
p.ignore()
return nil, fmt.Errorf("invalid '%s' found after closing '}'", p.current())
2019-06-15 04:17:21 +02:00
}
2020-01-16 07:44:19 +01:00
// for i := p.pos; i < len(p.items); i++ {
// fmt.Printf("2>>>> %#v\n", p.items[i])
// }
//return nil, fmt.Errorf("unexpected token")
lexPool.Put(l)
return op, err
2019-03-24 14:57:29 +01:00
}
func (p *Parser) next() item {
n := p.pos + 1
if n >= len(p.items) {
p.err = errEOT
2019-12-25 07:24:30 +01:00
return item{_type: itemEOF}
2019-03-24 14:57:29 +01:00
}
p.pos = n
return p.items[p.pos]
}
func (p *Parser) ignore() {
n := p.pos + 1
if n >= len(p.items) {
p.err = errEOT
return
}
p.pos = n
}
2020-01-16 07:44:19 +01:00
func (p *Parser) current() string {
item := p.items[p.pos]
return b2s(p.input[item.pos:item.end])
}
2019-03-24 14:57:29 +01:00
func (p *Parser) peek(types ...itemType) bool {
n := p.pos + 1
2020-01-16 07:44:19 +01:00
// if p.items[n]._type == itemEOF {
// return false
// }
2019-03-24 14:57:29 +01:00
if n >= len(p.items) {
return false
}
for i := 0; i < len(types); i++ {
2019-12-25 07:24:30 +01:00
if p.items[n]._type == types[i] {
2019-03-24 14:57:29 +01:00
return true
}
}
return false
}
2019-06-15 04:17:21 +02:00
func (p *Parser) parseOp() (*Operation, error) {
if !p.peek(itemQuery, itemMutation, itemSub) {
err := errors.New("expecting a query, mutation or subscription")
2019-06-15 04:17:21 +02:00
return nil, err
}
item := p.next()
op := opPool.Get().(*Operation)
op.Reset()
2019-12-25 07:24:30 +01:00
switch item._type {
2019-06-15 04:17:21 +02:00
case itemQuery:
op.Type = opQuery
case itemMutation:
op.Type = opMutate
case itemSub:
op.Type = opSub
}
op.Fields = op.fieldsA[:0]
op.Args = op.argsA[:0]
2019-03-24 14:57:29 +01:00
var err error
if p.peek(itemName) {
2019-06-15 04:17:21 +02:00
op.Name = p.val(p.next())
2019-03-24 14:57:29 +01:00
}
if p.peek(itemArgsOpen) {
p.ignore()
op.Args, err = p.parseArgs(op.Args)
2019-03-24 14:57:29 +01:00
if err != nil {
return nil, err
}
}
if p.peek(itemObjOpen) {
p.ignore()
2019-11-19 06:47:55 +01:00
for n := 0; n < 10; n++ {
if !p.peek(itemName) {
2019-11-19 06:47:55 +01:00
break
}
op.Fields, err = p.parseFields(op.Fields)
if err != nil {
return nil, err
}
}
}
return op, nil
}
func (p *Parser) parseQueryOp() (*Operation, error) {
op := opPool.Get().(*Operation)
op.Reset()
op.Type = opQuery
op.Fields = op.fieldsA[:0]
op.Args = op.argsA[:0]
var err error
for n := 0; n < 10; n++ {
if !p.peek(itemName) {
2019-11-19 06:47:55 +01:00
break
}
op.Fields, err = p.parseFields(op.Fields)
2019-03-24 14:57:29 +01:00
if err != nil {
return nil, err
}
}
return op, nil
}
func (p *Parser) parseFields(fields []Field) ([]Field, error) {
2019-03-24 14:57:29 +01:00
st := util.NewStack()
for {
if len(fields) >= maxFields {
return nil, fmt.Errorf("too many fields (max %d)", maxFields)
2019-05-13 01:27:26 +02:00
}
2019-03-24 14:57:29 +01:00
if p.peek(itemObjClose) {
p.ignore()
st.Pop()
if st.Len() == 0 {
break
2020-01-16 07:44:19 +01:00
} else {
continue
2019-03-24 14:57:29 +01:00
}
}
if !p.peek(itemName) {
2019-05-13 01:27:26 +02:00
return nil, errors.New("expecting an alias or field name")
2019-03-24 14:57:29 +01:00
}
2019-06-08 02:53:08 +02:00
fields = append(fields, Field{ID: int32(len(fields))})
2019-06-15 04:17:21 +02:00
f := &fields[(len(fields) - 1)]
f.Args = f.argsA[:0]
f.Children = f.childrenA[:0]
2019-03-24 14:57:29 +01:00
2020-01-16 07:44:19 +01:00
// Parse the inside of the the fields () parentheses
// in short parse the args like id, where, etc
if err := p.parseField(f); err != nil {
2019-05-13 01:27:26 +02:00
return nil, err
}
2019-03-24 14:57:29 +01:00
2019-11-19 06:47:55 +01:00
intf := st.Peek()
if pid, ok := intf.(int32); ok {
2019-05-13 01:27:26 +02:00
f.ParentID = pid
fields[pid].Children = append(fields[pid].Children, f.ID)
2019-11-19 06:47:55 +01:00
} else {
f.ParentID = -1
2019-03-24 14:57:29 +01:00
}
2020-01-16 07:44:19 +01:00
// The first opening curley brackets after this
// comes the columns or child fields
2019-03-24 14:57:29 +01:00
if p.peek(itemObjOpen) {
p.ignore()
2019-05-13 01:27:26 +02:00
st.Push(f.ID)
2019-03-24 14:57:29 +01:00
}
}
2019-05-13 01:27:26 +02:00
return fields, nil
2019-03-24 14:57:29 +01:00
}
2019-05-13 01:27:26 +02:00
func (p *Parser) parseField(f *Field) error {
2019-03-24 14:57:29 +01:00
var err error
2020-01-08 06:48:04 +01:00
v := p.next()
2019-03-24 14:57:29 +01:00
if p.peek(itemColon) {
p.ignore()
if p.peek(itemName) {
2020-01-08 06:48:04 +01:00
f.Alias = p.val(v)
f.Name = p.vall(p.next())
2019-03-24 14:57:29 +01:00
} else {
2019-05-13 01:27:26 +02:00
return errors.New("expecting an aliased field name")
2019-03-24 14:57:29 +01:00
}
2020-01-08 06:48:04 +01:00
} else {
f.Name = p.vall(v)
2019-03-24 14:57:29 +01:00
}
if p.peek(itemArgsOpen) {
p.ignore()
if f.Args, err = p.parseArgs(f.Args); err != nil {
2019-05-13 01:27:26 +02:00
return err
2019-03-24 14:57:29 +01:00
}
}
2019-05-13 01:27:26 +02:00
return nil
2019-03-24 14:57:29 +01:00
}
func (p *Parser) parseArgs(args []Arg) ([]Arg, error) {
2019-03-24 14:57:29 +01:00
var err error
for {
if len(args) >= maxArgs {
return nil, fmt.Errorf("too many args (max %d)", maxArgs)
}
2019-03-24 14:57:29 +01:00
if p.peek(itemArgsClose) {
p.ignore()
break
}
if !p.peek(itemName) {
2019-03-24 14:57:29 +01:00
return nil, errors.New("expecting an argument name")
}
2019-06-15 04:17:21 +02:00
args = append(args, Arg{Name: p.val(p.next())})
arg := &args[(len(args) - 1)]
2019-03-24 14:57:29 +01:00
if !p.peek(itemColon) {
2019-03-24 14:57:29 +01:00
return nil, errors.New("missing ':' after argument name")
}
p.ignore()
arg.Val, err = p.parseValue()
if err != nil {
return nil, err
}
}
return args, nil
}
func (p *Parser) parseList() (*Node, error) {
nodes := []*Node{}
parent := nodePool.Get().(*Node)
parent.Reset()
2019-03-24 14:57:29 +01:00
var ty parserType
2019-03-24 14:57:29 +01:00
for {
if p.peek(itemListClose) {
p.ignore()
break
}
node, err := p.parseValue()
if err != nil {
return nil, err
}
if ty == 0 {
ty = node.Type
} else {
if ty != node.Type {
return nil, errors.New("All values in a list must be of the same type")
}
}
node.Parent = parent
2019-03-24 14:57:29 +01:00
nodes = append(nodes, node)
}
if len(nodes) == 0 {
return nil, errors.New("List cannot be empty")
}
parent.Type = NodeList
2019-03-24 14:57:29 +01:00
parent.Children = nodes
return parent, nil
2019-03-24 14:57:29 +01:00
}
func (p *Parser) parseObj() (*Node, error) {
nodes := []*Node{}
parent := nodePool.Get().(*Node)
parent.Reset()
2019-03-24 14:57:29 +01:00
for {
if p.peek(itemObjClose) {
p.ignore()
break
}
if !p.peek(itemName) {
2019-03-24 14:57:29 +01:00
return nil, errors.New("expecting an argument name")
}
2019-06-15 04:17:21 +02:00
nodeName := p.val(p.next())
2019-03-24 14:57:29 +01:00
if !p.peek(itemColon) {
2019-03-24 14:57:29 +01:00
return nil, errors.New("missing ':' after Field argument name")
}
p.ignore()
node, err := p.parseValue()
if err != nil {
return nil, err
}
node.Name = nodeName
node.Parent = parent
nodes = append(nodes, node)
}
parent.Type = NodeObj
2019-03-24 14:57:29 +01:00
parent.Children = nodes
return parent, nil
2019-03-24 14:57:29 +01:00
}
func (p *Parser) parseValue() (*Node, error) {
if p.peek(itemListOpen) {
p.ignore()
return p.parseList()
2019-03-24 14:57:29 +01:00
}
if p.peek(itemObjOpen) {
p.ignore()
return p.parseObj()
2019-03-24 14:57:29 +01:00
}
item := p.next()
node := nodePool.Get().(*Node)
node.Reset()
2019-03-24 14:57:29 +01:00
2019-12-25 07:24:30 +01:00
switch item._type {
case itemIntVal:
node.Type = NodeInt
case itemFloatVal:
node.Type = NodeFloat
case itemStringVal:
node.Type = NodeStr
case itemBoolVal:
node.Type = NodeBool
case itemName:
node.Type = NodeStr
case itemVariable:
node.Type = NodeVar
default:
2019-06-15 04:17:21 +02:00
return nil, fmt.Errorf("expecting a number, string, object, list or variable as an argument value (not %s)", p.val(p.next()))
}
2019-06-15 04:17:21 +02:00
node.Val = p.val(item)
2019-03-24 14:57:29 +01:00
return node, nil
}
2019-06-15 04:17:21 +02:00
func (p *Parser) val(v item) string {
return b2s(p.input[v.pos:v.end])
}
func (p *Parser) vall(v item) string {
lowercase(p.input, v.pos, v.end)
return b2s(p.input[v.pos:v.end])
}
2019-06-15 04:17:21 +02:00
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func (t parserType) String() string {
var v string
switch t {
case parserEOF:
v = "EOF"
case parserError:
v = "error"
case opQuery:
v = "query"
case opMutate:
v = "mutation"
case opSub:
v = "subscription"
case NodeStr:
v = "node-string"
case NodeInt:
v = "node-int"
case NodeFloat:
v = "node-float"
case NodeBool:
v = "node-bool"
case NodeVar:
v = "node-var"
case NodeObj:
v = "node-obj"
case NodeList:
v = "node-list"
}
return fmt.Sprintf("<%s>", v)
}
func FreeNode(n *Node) {
nodePool.Put(n)
}