2016-10-17 07:14:01 +03:00
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-11-20 09:44:36 +03:00
|
|
|
"io"
|
2020-08-06 20:58:47 +03:00
|
|
|
"reflect"
|
2023-03-27 05:03:40 +03:00
|
|
|
"strings"
|
2016-10-17 07:14:01 +03:00
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
antlr "github.com/antlr4-go/antlr/v4"
|
2016-10-17 07:14:01 +03:00
|
|
|
)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Node is an AST node.
|
2016-10-17 07:14:01 +03:00
|
|
|
type Node interface {
|
2023-11-22 20:56:19 +03:00
|
|
|
// ast returns the root AST node, or nil.
|
|
|
|
ast() *AST
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// context returns the parse tree context.
|
|
|
|
context() antlr.ParseTree
|
|
|
|
|
|
|
|
// setContext sets the parse tree context, returning an error if illegal.
|
|
|
|
setContext(ctx antlr.ParseTree) error
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Parent returns the node's parent, which may be nil..
|
2016-10-17 07:14:01 +03:00
|
|
|
Parent() Node
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// SetParent sets the node's parent, returning an error if illegal.
|
|
|
|
SetParent(n Node) error
|
|
|
|
|
2023-03-19 07:58:00 +03:00
|
|
|
// Children returns the node's children (which may be empty).
|
2016-10-17 07:14:01 +03:00
|
|
|
Children() []Node
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// SetChildren sets the node's children, returning an error if illegal.
|
|
|
|
SetChildren(children []Node) error
|
|
|
|
|
|
|
|
// AddChild adds a child node, returning an error if illegal.
|
|
|
|
AddChild(child Node) error
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// Text returns the node's raw text value.
|
|
|
|
Text() string
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// String returns a debug-friendly string representation.
|
2016-10-17 07:14:01 +03:00
|
|
|
String() string
|
2023-03-26 04:20:53 +03:00
|
|
|
}
|
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
// Selector is a Node marker interface for selector node types. A selector node
|
|
|
|
// models a selector such as ".first_name" or ".actor.last_name".
|
|
|
|
type Selector interface {
|
2023-03-26 11:01:41 +03:00
|
|
|
Node
|
2023-03-26 04:20:53 +03:00
|
|
|
selector()
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
2016-10-17 07:14:01 +03:00
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
// ResultColumn indicates a column selection expression Node such as a
|
2022-12-18 09:07:38 +03:00
|
|
|
// column name, or context-appropriate function, e.g. "COUNT(*)".
|
2023-03-22 09:17:34 +03:00
|
|
|
// See: https://www.sqlite.org/syntax/result-column.html
|
|
|
|
type ResultColumn interface {
|
2023-03-27 05:03:40 +03:00
|
|
|
Node
|
|
|
|
|
2023-03-19 07:58:00 +03:00
|
|
|
// String returns a log/debug-friendly representation.
|
2020-08-06 20:58:47 +03:00
|
|
|
String() string
|
2023-03-19 07:58:00 +03:00
|
|
|
|
|
|
|
// Alias returns the column alias, which may be empty.
|
2023-03-22 09:17:34 +03:00
|
|
|
// For example, given the selector ".first_name:given_name", the
|
|
|
|
// alias is "given_name".
|
2023-03-19 07:58:00 +03:00
|
|
|
Alias() string
|
2023-03-22 09:17:34 +03:00
|
|
|
|
|
|
|
// Text returns the raw text of the node, e.g. ".actor" or "1*2".
|
|
|
|
Text() string
|
2023-06-25 19:29:24 +03:00
|
|
|
|
|
|
|
resultColumn()
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// baseNode is a base implementation of Node.
|
|
|
|
type baseNode struct {
|
2016-10-17 07:14:01 +03:00
|
|
|
parent Node
|
|
|
|
children []Node
|
|
|
|
ctx antlr.ParseTree
|
2023-03-22 09:17:34 +03:00
|
|
|
text string
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
|
|
|
|
2023-11-22 20:56:19 +03:00
|
|
|
// ast implements ast.Node.
|
|
|
|
func (bn *baseNode) ast() *AST {
|
|
|
|
if bn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
n := NodeRoot(bn.parent)
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if ast, ok := n.(*AST); ok {
|
|
|
|
return ast
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent implements ast.Node.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (bn *baseNode) Parent() Node {
|
2016-10-17 07:14:01 +03:00
|
|
|
return bn.parent
|
|
|
|
}
|
|
|
|
|
2023-11-22 20:56:19 +03:00
|
|
|
// SetParent implements ast.Node.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (bn *baseNode) SetParent(parent Node) error {
|
2016-10-17 07:14:01 +03:00
|
|
|
bn.parent = parent
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-22 20:56:19 +03:00
|
|
|
// Children implements ast.Node.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (bn *baseNode) Children() []Node {
|
2016-10-17 07:14:01 +03:00
|
|
|
return bn.children
|
|
|
|
}
|
|
|
|
|
2023-03-26 04:20:53 +03:00
|
|
|
// AddChild always returns an error. Node implementations should
|
|
|
|
// implement a type-specific method that only accepts a child of
|
|
|
|
// an appropriate type for that node.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (bn *baseNode) AddChild(child Node) error {
|
|
|
|
return errorf(msgNodeNoAddChild, bn, child)
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
func (bn *baseNode) addChild(child Node) {
|
2023-11-22 20:56:19 +03:00
|
|
|
// REVISIT: Why not call setParent() on child?
|
|
|
|
// TODO: add child could be a generic function, returning
|
|
|
|
// and error if the child is not of an appropriate type.
|
2016-10-17 07:14:01 +03:00
|
|
|
bn.children = append(bn.children, child)
|
|
|
|
}
|
|
|
|
|
2023-11-22 20:56:19 +03:00
|
|
|
// SetChildren implements ast.Node. It always returns an error.
|
|
|
|
// Node implementations must provide their own type-specific
|
|
|
|
// implementation if they accept children.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (bn *baseNode) SetChildren(children []Node) error {
|
|
|
|
return errorf(msgNodeNoAddChildren, bn, len(children))
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
func (bn *baseNode) doSetChildren(children []Node) {
|
2023-11-22 20:56:19 +03:00
|
|
|
// REVISIT: Why not call setParent() on each child?
|
|
|
|
// TODO: doSetChildren could be a generic function, returning
|
|
|
|
// and error if the children are not of an appropriate type.
|
2016-10-17 07:14:01 +03:00
|
|
|
bn.children = children
|
|
|
|
}
|
|
|
|
|
2023-11-22 20:56:19 +03:00
|
|
|
// Text implements ast.Node.
|
2020-08-06 20:58:47 +03:00
|
|
|
func (bn *baseNode) Text() string {
|
2016-10-17 07:14:01 +03:00
|
|
|
if bn.ctx == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return bn.ctx.GetText()
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// context implements ast.Node.
|
|
|
|
func (bn *baseNode) context() antlr.ParseTree {
|
2016-10-17 07:14:01 +03:00
|
|
|
return bn.ctx
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
// setContext implements ast.Node.
|
|
|
|
func (bn *baseNode) setContext(ctx antlr.ParseTree) error {
|
2016-10-17 07:14:01 +03:00
|
|
|
bn.ctx = ctx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodeString returns a default value suitable for use by Node.String().
|
|
|
|
func nodeString(n Node) string {
|
|
|
|
return fmt.Sprintf("%T: %s", n, n.Text())
|
|
|
|
}
|
|
|
|
|
2023-03-19 07:58:00 +03:00
|
|
|
// nodeReplace replaces old with new. That is, nu becomes a child
|
2020-08-06 20:58:47 +03:00
|
|
|
// of old's parent.
|
2023-03-19 07:58:00 +03:00
|
|
|
func nodeReplace(old, nu Node) error {
|
2023-07-03 18:34:19 +03:00
|
|
|
err := nu.setContext(old.context())
|
2016-10-17 07:14:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
parent := old.Parent()
|
|
|
|
|
2023-03-19 07:58:00 +03:00
|
|
|
index := nodeChildIndex(parent, old)
|
2016-10-17 07:14:01 +03:00
|
|
|
if index < 0 {
|
2023-04-02 22:49:45 +03:00
|
|
|
return errorf("parent %T(%s) does not appear to have child %T(%s)", parent, parent.Text(), old, old.Text())
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
|
|
|
siblings := parent.Children()
|
|
|
|
siblings[index] = nu
|
|
|
|
|
|
|
|
return parent.SetChildren(siblings)
|
|
|
|
}
|
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
// nodesAreOnlyOfType returns an error if the type of any non-nil element
|
|
|
|
// of nodes is not contained in types.
|
|
|
|
func nodesAreOnlyOfType(nodes []Node, types ...reflect.Type) error {
|
|
|
|
m := map[reflect.Type]struct{}{}
|
|
|
|
typeNames := make([]string, 0, len(types))
|
|
|
|
for _, typ := range types {
|
|
|
|
m[typ] = struct{}{}
|
|
|
|
typeNames = append(typeNames, typ.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, node := range nodes {
|
|
|
|
if node == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := m[reflect.TypeOf(node)]; !ok {
|
|
|
|
return errorf("node[%d] {%s} is not an allowed type in [%s]", i, node, strings.Join(typeNames, ", "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-20 09:44:36 +03:00
|
|
|
// NodeRoot returns the root node of the tree containing node.
|
|
|
|
// This returned node should be an *ast.AST.
|
|
|
|
func NodeRoot(node Node) Node {
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Parent() == nil {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
|
|
|
return NodeRoot(node.Parent())
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
// NodeNextSibling returns the node's next sibling, or nil.
|
|
|
|
func NodeNextSibling(node Node) Node {
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parent := node.Parent()
|
|
|
|
if parent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i := nodeChildIndex(parent, node)
|
|
|
|
if i < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
children := parent.Children()
|
|
|
|
if i >= len(children)-1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return children[i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodePrevSibling returns the node's previous sibling, or nil.
|
|
|
|
func NodePrevSibling(node Node) Node {
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
parent := node.Parent()
|
|
|
|
if parent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i := nodeChildIndex(parent, node)
|
|
|
|
if i < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
children := parent.Children()
|
|
|
|
return children[i-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodesHavingText returns any node whose node.Text()
|
|
|
|
// method returns text.
|
|
|
|
func NodesHavingText(tree Node, text string) []Node {
|
|
|
|
if tree == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodes []Node
|
|
|
|
|
|
|
|
w := NewWalker(tree)
|
|
|
|
w.AddVisitor(typeNode, func(w *Walker, node Node) error {
|
|
|
|
nodeText := node.Text()
|
|
|
|
if nodeText == text {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := w.Walk(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2023-03-19 07:58:00 +03:00
|
|
|
// nodeChildIndex returns the index of child in parent's children, or -1.
|
|
|
|
func nodeChildIndex(parent, child Node) int {
|
2020-08-06 20:58:47 +03:00
|
|
|
for i, node := range parent.Children() {
|
|
|
|
if node == child {
|
2023-03-19 07:58:00 +03:00
|
|
|
return i
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-19 07:58:00 +03:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodeFirstChild returns the first child of parent, or nil.
|
|
|
|
func nodeFirstChild(parent Node) Node { //nolint:unused
|
|
|
|
if parent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
children := parent.Children()
|
|
|
|
if len(children) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return children[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// nodeFirstChild returns the last child of parent, or nil.
|
|
|
|
func nodeLastChild(parent Node) Node {
|
|
|
|
if parent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
children := parent.Children()
|
|
|
|
if len(children) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return children[len(children)-1]
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
// nodesWithType returns a new slice containing each member of nodes that is
|
|
|
|
// of the specified type.
|
|
|
|
func nodesWithType(nodes []Node, typ reflect.Type) []Node {
|
|
|
|
s := make([]Node, 0)
|
|
|
|
|
|
|
|
for _, n := range nodes {
|
|
|
|
if reflect.TypeOf(n) == typ {
|
|
|
|
s = append(s, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-06-17 07:54:25 +03:00
|
|
|
// NodeUnwrap "unwraps" node returning the lowest single contained node.
|
|
|
|
// False is returned if node (or any of its descendants), has more than one
|
|
|
|
// child. If node has no children, it is returned directly. This function
|
|
|
|
// is useful for "unwrapping" a node that is contained in an outer node. For
|
|
|
|
// example, an ExprNode may often contain just a single LiteralNode.
|
|
|
|
func NodeUnwrap[T Node](node Node) (T, bool) {
|
|
|
|
var result T
|
|
|
|
var ok bool
|
|
|
|
if node == nil {
|
|
|
|
return result, false
|
|
|
|
}
|
|
|
|
|
|
|
|
var children []Node
|
|
|
|
for {
|
|
|
|
children = node.Children()
|
|
|
|
switch len(children) {
|
|
|
|
case 0:
|
|
|
|
result, ok = node.(T)
|
|
|
|
return result, ok
|
|
|
|
case 1:
|
|
|
|
node = children[0]
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
return result, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 09:44:36 +03:00
|
|
|
// FindNodes returns the nodes of type T in ast.
|
|
|
|
func FindNodes[T Node](ast *AST) []T {
|
|
|
|
var nodes []T
|
2023-11-22 20:56:19 +03:00
|
|
|
if ast == nil {
|
|
|
|
return nodes
|
|
|
|
}
|
2023-11-20 09:44:36 +03:00
|
|
|
w := NewWalker(ast)
|
|
|
|
w.AddVisitor(reflect.TypeOf((*T)(nil)).Elem(), func(w *Walker, node Node) error {
|
|
|
|
nodes = append(nodes, node.(T))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
_ = w.Walk()
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindFirstNode returns the first node of type T in ast, or
|
|
|
|
// nil if no such node exists.
|
|
|
|
func FindFirstNode[T Node](ast *AST) T {
|
|
|
|
var node T
|
|
|
|
w := NewWalker(ast)
|
|
|
|
w.AddVisitor(reflect.TypeOf((*T)(nil)).Elem(), func(w *Walker, n Node) error {
|
|
|
|
node, _ = n.(T)
|
|
|
|
return io.EOF // Return any error to halt the walk.
|
|
|
|
})
|
|
|
|
|
|
|
|
_ = w.Walk()
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2023-11-22 20:56:19 +03:00
|
|
|
// NodePrevSegmentChild returns the first child of the
|
|
|
|
// previous segment, where the child must be of type T, or
|
|
|
|
// an error if the child is not of type T.
|
|
|
|
func NodePrevSegmentChild[T Node](node Node) (T, error) {
|
|
|
|
var (
|
|
|
|
result T
|
|
|
|
parent Node
|
|
|
|
seg *SegmentNode
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
|
|
|
|
if node == nil {
|
|
|
|
return result, errorf("node is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
seg, ok = node.(*SegmentNode)
|
|
|
|
if !ok {
|
|
|
|
for {
|
|
|
|
parent = node.Parent()
|
|
|
|
if parent == nil {
|
|
|
|
return result, errorf("unable to find segment in node {%T} ancestry", node)
|
|
|
|
}
|
|
|
|
|
|
|
|
seg, ok = parent.(*SegmentNode)
|
|
|
|
if ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if seg == nil {
|
|
|
|
// Can't happen?
|
|
|
|
return result, errorf("unable to find segment in node {%T} ancestry", node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prevSeg := seg.Prev()
|
|
|
|
if prevSeg == nil {
|
|
|
|
return result, errorf("expected preceding segment")
|
|
|
|
}
|
|
|
|
|
|
|
|
prevSegChildren := prevSeg.Children()
|
|
|
|
if len(prevSegChildren) == 0 {
|
|
|
|
return result, errorf("expected preceding segment to have children")
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok = prevSegChildren[0].(T)
|
|
|
|
if ok {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, errorf("expected preceding segment to have child of type {%T} but got {%T}",
|
|
|
|
result, prevSegChildren[0])
|
|
|
|
}
|
|
|
|
|
2023-06-18 04:28:11 +03:00
|
|
|
// Results from reflect.TypeOf for node types.
|
2020-08-06 20:58:47 +03:00
|
|
|
var (
|
2023-03-27 05:03:40 +03:00
|
|
|
typeAST = reflect.TypeOf((*AST)(nil))
|
|
|
|
typeColSelectorNode = reflect.TypeOf((*ColSelectorNode)(nil))
|
2023-11-22 20:56:19 +03:00
|
|
|
typeExprNode = reflect.TypeOf((*ExprNode)(nil))
|
2023-03-27 05:03:40 +03:00
|
|
|
typeFuncNode = reflect.TypeOf((*FuncNode)(nil))
|
2023-04-07 11:00:49 +03:00
|
|
|
typeGroupByNode = reflect.TypeOf((*GroupByNode)(nil))
|
2023-11-22 20:56:19 +03:00
|
|
|
typeHavingNode = reflect.TypeOf((*HavingNode)(nil))
|
2023-04-07 11:00:49 +03:00
|
|
|
typeHandleNode = reflect.TypeOf((*HandleNode)(nil))
|
|
|
|
typeJoinNode = reflect.TypeOf((*JoinNode)(nil))
|
|
|
|
typeNode = reflect.TypeOf((*Node)(nil)).Elem()
|
|
|
|
_ = reflect.TypeOf((*OperatorNode)(nil))
|
|
|
|
typeOrderByNode = reflect.TypeOf((*OrderByNode)(nil))
|
|
|
|
typeRowRangeNode = reflect.TypeOf((*RowRangeNode)(nil))
|
|
|
|
typeSegmentNode = reflect.TypeOf((*SegmentNode)(nil))
|
|
|
|
_ = reflect.TypeOf((*Selector)(nil)).Elem()
|
|
|
|
typeSelectorNode = reflect.TypeOf((*SelectorNode)(nil))
|
|
|
|
typeTblColSelectorNode = reflect.TypeOf((*TblColSelectorNode)(nil))
|
|
|
|
typeTblSelectorNode = reflect.TypeOf((*TblSelectorNode)(nil))
|
2023-03-28 09:48:24 +03:00
|
|
|
typeUniqueNode = reflect.TypeOf((*UniqueNode)(nil))
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|