2023-06-18 04:28:11 +03:00
|
|
|
package ast
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/neilotoole/sq/libsq/ast/internal/slq"
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/libsq/core/stringz"
|
2023-06-18 04:28:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// VisitAlias implements slq.SLQVisitor.
|
|
|
|
func (v *parseTreeVisitor) VisitAlias(ctx *slq.AliasContext) any {
|
|
|
|
if ctx == nil || ctx.ID() == nil && ctx.GetText() == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var alias string
|
|
|
|
if ctx.ID() != nil {
|
|
|
|
alias = ctx.ID().GetText()
|
2023-06-18 09:05:09 +03:00
|
|
|
} else if ctx.STRING() != nil {
|
|
|
|
alias = stringz.StripDoubleQuote(ctx.STRING().GetText())
|
2023-06-18 04:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch node := v.cur.(type) {
|
|
|
|
case *SelectorNode:
|
|
|
|
node.alias = alias
|
2023-07-03 18:34:19 +03:00
|
|
|
case *TblSelectorNode:
|
|
|
|
node.alias = alias
|
2023-06-18 04:28:11 +03:00
|
|
|
case *ExprElementNode:
|
|
|
|
node.alias = alias
|
|
|
|
case *FuncNode:
|
|
|
|
if alias != "" {
|
|
|
|
node.alias = alias
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: The grammar has a dodgy hack to deal with no-arg funcs
|
|
|
|
// with an alias that is a reserved word.
|
|
|
|
//
|
|
|
|
// For example, let's start with this snippet. Note that "count" is
|
|
|
|
// a function, equivalent to count().
|
|
|
|
//
|
|
|
|
// .actor | count
|
|
|
|
//
|
|
|
|
// Then add an alias that is a reserved word, such as a function name.
|
|
|
|
// In this example, we will use an alias of "count" as well.
|
|
|
|
//
|
|
|
|
// .actor | count:count
|
|
|
|
//
|
|
|
|
// Well, the grammar doesn't know how to handle this. Most likely the
|
|
|
|
// grammar could be refactored to deal with this more gracefully. The
|
|
|
|
// hack is to look at the full text of the context (e.g. ":count"),
|
|
|
|
// instead of just ID, and look for the alias after the colon.
|
|
|
|
|
|
|
|
text := ctx.GetText()
|
|
|
|
node.alias = strings.TrimPrefix(text, ":")
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errorf("alias not allowed for type %T: %v", node, ctx.GetText())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|