2020-08-06 20:58:47 +03:00
|
|
|
// Package ast holds types and functionality for the SLQ AST.
|
|
|
|
//
|
|
|
|
// Note: the SLQ language implementation is fairly rudimentary
|
|
|
|
// and has some incomplete functionality.
|
2016-10-17 07:14:01 +03:00
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
2023-03-22 09:17:34 +03:00
|
|
|
"reflect"
|
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/lg"
|
|
|
|
|
|
|
|
"golang.org/x/exp/slog"
|
2016-10-17 07:14:01 +03:00
|
|
|
|
2023-04-02 22:49:45 +03:00
|
|
|
"github.com/antlr/antlr4/runtime/Go/antlr/v4"
|
2020-08-23 14:16:16 +03:00
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/ast/internal/slq"
|
2020-08-23 13:42:15 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/errz"
|
2016-10-17 07:14:01 +03:00
|
|
|
)
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// Parse parses the SLQ input string and builds the AST.
|
2023-04-02 22:49:45 +03:00
|
|
|
func Parse(log *slog.Logger, input string) (*AST, error) { //nolint:staticcheck
|
2023-03-26 04:20:53 +03:00
|
|
|
// REVISIT: We need a better solution for disabling parser logging.
|
2023-03-19 07:58:00 +03:00
|
|
|
log = lg.Discard() //nolint:staticcheck // Disable parser logging.
|
2020-08-06 20:58:47 +03:00
|
|
|
ptree, err := parseSLQ(log, input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-26 04:20:53 +03:00
|
|
|
ast, err := buildAST(log, ptree)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
if err := verify(ast); err != nil {
|
2023-03-26 04:20:53 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast, nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// buildAST constructs sq's AST from a parse tree.
|
2023-04-02 22:49:45 +03:00
|
|
|
func buildAST(log *slog.Logger, query slq.IQueryContext) (*AST, error) {
|
2020-08-06 20:58:47 +03:00
|
|
|
if query == nil {
|
|
|
|
return nil, errorf("query is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
q, ok := query.(*slq.QueryContext)
|
|
|
|
if !ok {
|
|
|
|
return nil, errorf("unable to convert %T to *parser.QueryContext", query)
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
tree := &parseTreeVisitor{log: log}
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
if err := q.Accept(tree); err != nil {
|
|
|
|
return nil, err.(error)
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
visitors := []struct {
|
|
|
|
typ reflect.Type
|
|
|
|
fn nodeVisitorFn
|
|
|
|
}{
|
|
|
|
{typeSelectorNode, narrowTblSel},
|
|
|
|
{typeSelectorNode, narrowTblColSel},
|
|
|
|
{typeSelectorNode, narrowColSel},
|
|
|
|
{typeJoinNode, determineJoinTables},
|
|
|
|
{typeRowRangeNode, visitCheckRowRange},
|
|
|
|
{typeExprNode, findWhereClause},
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
for _, visitor := range visitors {
|
2023-04-07 11:00:49 +03:00
|
|
|
w := NewWalker(tree.ast).AddVisitor(visitor.typ, visitor.fn)
|
2023-03-22 09:17:34 +03:00
|
|
|
if err := w.Walk(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
return tree.ast, nil
|
2020-08-06 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2023-03-26 04:20:53 +03:00
|
|
|
// verify performs additional checks on the state of the built AST.
|
2023-04-07 11:00:49 +03:00
|
|
|
func verify(ast *AST) error {
|
|
|
|
selCount := NewInspector(ast).CountNodes(typeSelectorNode)
|
2023-03-26 04:20:53 +03:00
|
|
|
if selCount != 0 {
|
|
|
|
return errorf("AST should have zero nodes of type %T but found %d",
|
|
|
|
(*SelectorNode)(nil), selCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Lots more checks could go here
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
var _ Node = (*AST)(nil)
|
|
|
|
|
|
|
|
// AST is the Abstract Syntax Tree. It is the root node of a SQL query/stmt.
|
2016-10-17 07:14:01 +03:00
|
|
|
type AST struct {
|
2020-08-06 20:58:47 +03:00
|
|
|
ctx *slq.QueryContext
|
2023-03-22 09:17:34 +03:00
|
|
|
segs []*SegmentNode
|
|
|
|
text string
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AST) Parent() Node {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AST) SetParent(parent Node) error {
|
|
|
|
return errorf("root node (%T) cannot have parent: tried to add parent %T", a, parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AST) Children() []Node {
|
|
|
|
nodes := make([]Node, len(a.segs))
|
|
|
|
|
|
|
|
for i, seg := range a.segs {
|
|
|
|
nodes[i] = seg
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
func (a *AST) Segments() []*SegmentNode {
|
2016-10-17 07:14:01 +03:00
|
|
|
return a.segs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AST) AddChild(node Node) error {
|
2023-03-22 09:17:34 +03:00
|
|
|
seg, ok := node.(*SegmentNode)
|
2016-10-17 07:14:01 +03:00
|
|
|
if !ok {
|
2023-03-22 09:17:34 +03:00
|
|
|
return errorf("expected *SegmentNode but got: %T", node)
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
a.AddSegment(seg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AST) SetChildren(children []Node) error {
|
2023-03-22 09:17:34 +03:00
|
|
|
segs := make([]*SegmentNode, len(children))
|
2016-10-17 07:14:01 +03:00
|
|
|
|
|
|
|
for i, child := range children {
|
2023-03-22 09:17:34 +03:00
|
|
|
seg, ok := child.(*SegmentNode)
|
2016-10-17 07:14:01 +03:00
|
|
|
if !ok {
|
2023-03-22 09:17:34 +03:00
|
|
|
return errorf("expected child of type %s, but got: %T", typeSegmentNode, child)
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
segs[i] = seg
|
|
|
|
}
|
|
|
|
|
|
|
|
a.segs = segs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AST) Context() antlr.ParseTree {
|
|
|
|
return a.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AST) SetContext(ctx antlr.ParseTree) error {
|
|
|
|
qCtx, ok := ctx.(*slq.QueryContext)
|
|
|
|
if !ok {
|
|
|
|
return errorf("expected *parser.QueryContext, but got %T", ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.ctx = qCtx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AST) String() string {
|
|
|
|
return nodeString(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *AST) Text() string {
|
|
|
|
return a.ctx.GetText()
|
|
|
|
}
|
|
|
|
|
2016-10-31 01:35:56 +03:00
|
|
|
// AddSegment appends seg to the AST.
|
2023-03-22 09:17:34 +03:00
|
|
|
func (a *AST) AddSegment(seg *SegmentNode) {
|
2020-08-06 20:58:47 +03:00
|
|
|
_ = seg.SetParent(a)
|
2016-10-17 07:14:01 +03:00
|
|
|
a.segs = append(a.segs, seg)
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:58:47 +03:00
|
|
|
// errorf builds an error. Error creation for this package
|
|
|
|
// was centralized here in the expectation that an AST-specific
|
|
|
|
// error type (annotated appropriately) would be returned.
|
2022-12-17 02:34:33 +03:00
|
|
|
func errorf(format string, v ...any) error {
|
2020-08-06 20:58:47 +03:00
|
|
|
return errz.Errorf(format, v...)
|
2016-10-17 07:14:01 +03:00
|
|
|
}
|