2018-09-18 22:42:38 +02:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-09 20:19:53 +02:00
|
|
|
"regexp"
|
2019-03-29 16:48:51 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/parser/fql"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/collections"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/expressions"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/expressions/clauses"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/expressions/literals"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/expressions/operators"
|
|
|
|
"github.com/antlr/antlr4/runtime/Go/antlr"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
type (
|
|
|
|
forOption func(f *expressions.ForExpression) error
|
|
|
|
|
|
|
|
visitor struct {
|
|
|
|
*fql.BaseFqlParserVisitor
|
|
|
|
src string
|
2019-10-14 20:47:31 +02:00
|
|
|
funcs core.Functions
|
2018-10-25 03:30:05 +02:00
|
|
|
}
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2019-10-14 20:47:31 +02:00
|
|
|
func newVisitor(src string, funcs core.Functions) *visitor {
|
2018-09-18 22:42:38 +02:00
|
|
|
return &visitor{
|
|
|
|
&fql.BaseFqlParserVisitor{},
|
2018-09-28 06:28:33 +02:00
|
|
|
src,
|
2018-09-18 22:42:38 +02:00
|
|
|
funcs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) VisitProgram(ctx *fql.ProgramContext) interface{} {
|
|
|
|
return newResultFrom(func() (interface{}, error) {
|
|
|
|
rootScope := newRootScope()
|
|
|
|
block, err := v.doVisitBody(ctx.Body().(*fql.BodyContext), rootScope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-28 06:28:33 +02:00
|
|
|
return runtime.NewProgram(v.src, block)
|
2018-09-18 22:42:38 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitBody(ctx *fql.BodyContext, scope *scope) (core.Expression, error) {
|
|
|
|
statements := ctx.AllBodyStatement()
|
2018-10-28 07:45:26 +02:00
|
|
|
body := expressions.NewBodyExpression(len(statements) + 1)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
for _, stmt := range statements {
|
|
|
|
e, err := v.doVisitBodyStatement(stmt.(*fql.BodyStatementContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
body.Add(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := ctx.BodyExpression()
|
|
|
|
|
|
|
|
if exp != nil {
|
|
|
|
e, err := v.doVisitBodyExpression(exp.(*fql.BodyExpressionContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
body.Add(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitBodyStatement(ctx *fql.BodyStatementContext, scope *scope) (core.Expression, error) {
|
|
|
|
variable := ctx.VariableDeclaration()
|
|
|
|
|
|
|
|
if variable != nil {
|
|
|
|
return v.doVisitVariableDeclaration(variable.(*fql.VariableDeclarationContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
funcCall := ctx.FunctionCallExpression()
|
|
|
|
|
|
|
|
if funcCall != nil {
|
|
|
|
return v.doVisitFunctionCallExpression(funcCall.(*fql.FunctionCallExpressionContext), scope)
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
return nil, core.Error(ErrInvalidToken, ctx.GetText())
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitBodyExpression(ctx *fql.BodyExpressionContext, scope *scope) (core.Expression, error) {
|
|
|
|
forIn := ctx.ForExpression()
|
|
|
|
|
|
|
|
if forIn != nil {
|
|
|
|
return v.doVisitForExpression(forIn.(*fql.ForExpressionContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := ctx.ReturnExpression()
|
|
|
|
|
|
|
|
if ret != nil {
|
|
|
|
return v.doVisitReturnExpression(ret.(*fql.ReturnExpressionContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitReturnExpression(ctx *fql.ReturnExpressionContext, scope *scope) (core.Expression, error) {
|
|
|
|
var exp core.Expression
|
|
|
|
expCtx := ctx.Expression()
|
|
|
|
|
|
|
|
if expCtx != nil {
|
|
|
|
out, err := v.doVisitExpression(expCtx.(*fql.ExpressionContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = out
|
2018-09-28 01:05:56 +02:00
|
|
|
|
|
|
|
return expressions.NewReturnExpression(v.getSourceMap(ctx), exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
forIn := ctx.ForExpression()
|
|
|
|
|
|
|
|
if forIn != nil {
|
2018-09-18 22:42:38 +02:00
|
|
|
out, err := v.doVisitForExpression(ctx.ForExpression().(*fql.ForExpressionContext), scope.Fork())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = out
|
2018-09-28 01:05:56 +02:00
|
|
|
|
|
|
|
return expressions.NewReturnExpression(v.getSourceMap(ctx), exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
forInTernary := ctx.ForTernaryExpression()
|
|
|
|
|
|
|
|
if forInTernary != nil {
|
|
|
|
out, err := v.doVisitForTernaryExpression(forInTernary.(*fql.ForTernaryExpressionContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return expressions.NewReturnExpression(v.getSourceMap(ctx), out)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 01:05:56 +02:00
|
|
|
return nil, ErrNotImplemented
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitForExpression(ctx *fql.ForExpressionContext, scope *scope) (core.Expression, error) {
|
|
|
|
var valVarName string
|
|
|
|
var keyVarName string
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
parsedClauses := make([]forOption, 0, 10)
|
2018-11-30 20:12:00 +02:00
|
|
|
forInScope := scope.Fork()
|
|
|
|
|
|
|
|
srcCtx := ctx.ForExpressionSource().(*fql.ForExpressionSourceContext)
|
|
|
|
srcExp, err := v.doVisitForExpressionSource(srcCtx, forInScope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
valVar := ctx.ForExpressionValueVariable()
|
|
|
|
valVarName = valVar.GetText()
|
2018-11-30 20:12:00 +02:00
|
|
|
|
|
|
|
if err := forInScope.SetVariable(valVarName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
keyVar := ctx.ForExpressionKeyVariable()
|
|
|
|
|
|
|
|
if keyVar != nil {
|
|
|
|
keyVarName = keyVar.GetText()
|
|
|
|
|
2018-11-30 20:12:00 +02:00
|
|
|
if err := forInScope.SetVariable(keyVarName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
src, err := expressions.NewDataSource(
|
|
|
|
v.getSourceMap(srcCtx),
|
|
|
|
valVarName,
|
|
|
|
keyVarName,
|
|
|
|
srcExp,
|
|
|
|
)
|
|
|
|
|
2019-03-29 16:48:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
// Clauses.
|
|
|
|
// We put clauses parsing before parsing the query body because COLLECT clause overrides scope variables
|
2018-10-28 07:45:26 +02:00
|
|
|
for _, e := range ctx.AllForExpressionBody() {
|
|
|
|
e := e.(*fql.ForExpressionBodyContext)
|
|
|
|
clauseCtx := e.ForExpressionClause()
|
|
|
|
statementCtx := e.ForExpressionStatement()
|
|
|
|
|
|
|
|
if clauseCtx != nil {
|
|
|
|
setter, err := v.doVisitForExpressionClause(
|
|
|
|
clauseCtx.(*fql.ForExpressionClauseContext),
|
|
|
|
forInScope,
|
|
|
|
valVarName,
|
|
|
|
keyVarName,
|
|
|
|
)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
parsedClauses = append(parsedClauses, setter)
|
|
|
|
} else if statementCtx != nil {
|
|
|
|
exp, err := v.doVisitForExpressionStatement(
|
|
|
|
statementCtx.(*fql.ForExpressionStatementContext),
|
|
|
|
forInScope,
|
|
|
|
)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
parsedClauses = append(parsedClauses, exp)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var spread bool
|
|
|
|
var distinct bool
|
2018-10-28 07:45:26 +02:00
|
|
|
var predicate core.Expression
|
2018-09-18 22:42:38 +02:00
|
|
|
forRetCtx := ctx.ForExpressionReturn().(*fql.ForExpressionReturnContext)
|
|
|
|
returnCtx := forRetCtx.ReturnExpression()
|
|
|
|
|
|
|
|
if returnCtx != nil {
|
|
|
|
returnCtx := returnCtx.(*fql.ReturnExpressionContext)
|
|
|
|
returnExp, err := v.doVisitReturnExpression(returnCtx, forInScope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-07 07:23:35 +02:00
|
|
|
distinctCtx := returnCtx.Distinct()
|
|
|
|
|
|
|
|
if distinctCtx != nil {
|
|
|
|
distinct = true
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
predicate = returnExp
|
2018-09-18 22:42:38 +02:00
|
|
|
} else {
|
|
|
|
forInCtx := forRetCtx.ForExpression().(*fql.ForExpressionContext)
|
|
|
|
forInExp, err := v.doVisitForExpression(forInCtx, forInScope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
spread = true
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
predicate = forInExp
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
forExp, err := expressions.NewForExpression(
|
|
|
|
v.getSourceMap(ctx),
|
|
|
|
src,
|
|
|
|
predicate,
|
2018-10-25 03:30:05 +02:00
|
|
|
distinct,
|
2018-09-18 22:42:38 +02:00
|
|
|
spread,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
// add all available clauses
|
|
|
|
for _, clause := range parsedClauses {
|
|
|
|
if err := clause(forExp); err != nil {
|
|
|
|
return nil, err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return forExp, nil
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
func (v *visitor) doVisitLimitClause(ctx *fql.LimitClauseContext, scope *scope) (core.Expression, core.Expression, error) {
|
2018-10-08 22:04:49 +02:00
|
|
|
var err error
|
2018-11-13 02:58:12 +02:00
|
|
|
var count core.Expression
|
|
|
|
var offset core.Expression
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
clauseValues := ctx.AllLimitClauseValue()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
if len(clauseValues) > 1 {
|
|
|
|
offset, err = v.doVisitLimitClauseValue(clauseValues[0].(*fql.LimitClauseValueContext), scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-08 22:04:49 +02:00
|
|
|
if err != nil {
|
2018-11-13 02:58:12 +02:00
|
|
|
return nil, nil, err
|
2018-10-08 22:04:49 +02:00
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
count, err = v.doVisitLimitClauseValue(clauseValues[1].(*fql.LimitClauseValueContext), scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-08 22:04:49 +02:00
|
|
|
if err != nil {
|
2018-11-13 02:58:12 +02:00
|
|
|
return nil, nil, err
|
2018-10-08 22:04:49 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-13 02:58:12 +02:00
|
|
|
count, err = v.doVisitLimitClauseValue(clauseValues[0].(*fql.LimitClauseValueContext), scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2018-11-13 02:58:12 +02:00
|
|
|
return nil, nil, err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
2018-11-13 02:58:12 +02:00
|
|
|
|
|
|
|
offset = literals.NewIntLiteral(0)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 22:04:49 +02:00
|
|
|
return count, offset, nil
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
func (v *visitor) doVisitLimitClauseValue(ctx *fql.LimitClauseValueContext, scope *scope) (core.Expression, error) {
|
|
|
|
literalCtx := ctx.IntegerLiteral()
|
|
|
|
|
|
|
|
if literalCtx != nil {
|
|
|
|
i, err := strconv.Atoi(literalCtx.GetText())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return literals.NewIntLiteral(i), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
paramCtx := ctx.Param()
|
|
|
|
|
|
|
|
return v.doVisitParamContext(paramCtx.(*fql.ParamContext), scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
func (v *visitor) doVisitFilterClause(ctx *fql.FilterClauseContext, scope *scope) (core.Expression, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
exp := ctx.Expression().(*fql.ExpressionContext)
|
|
|
|
|
|
|
|
exps, err := v.doVisitAllExpressions(exp.AllExpression(), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-17 17:41:40 +02:00
|
|
|
if len(exps) == 2 {
|
|
|
|
left := exps[0]
|
|
|
|
right := exps[1]
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-17 17:41:40 +02:00
|
|
|
equalityOp := exp.EqualityOperator()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-17 17:41:40 +02:00
|
|
|
if equalityOp != nil {
|
|
|
|
return operators.NewEqualityOperator(v.getSourceMap(ctx), left, right, equalityOp.GetText())
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
logicalAndOp := exp.LogicalAndOperator()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
if logicalAndOp != nil {
|
|
|
|
return operators.NewLogicalOperator(v.getSourceMap(ctx), left, right, logicalAndOp.GetText())
|
|
|
|
}
|
|
|
|
|
|
|
|
logicalOrOp := exp.LogicalOrOperator()
|
|
|
|
|
|
|
|
if logicalOrOp != nil {
|
|
|
|
return operators.NewLogicalOperator(v.getSourceMap(ctx), left, right, logicalOrOp.GetText())
|
2018-10-17 17:41:40 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// should be unary operator
|
|
|
|
return v.doVisitExpression(exp, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, core.Error(ErrInvalidToken, ctx.GetText())
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
func (v *visitor) doVisitSortClause(ctx *fql.SortClauseContext, scope *scope) ([]*clauses.SorterExpression, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
sortExpCtxs := ctx.AllSortClauseExpression()
|
|
|
|
|
|
|
|
res := make([]*clauses.SorterExpression, len(sortExpCtxs))
|
|
|
|
|
|
|
|
for idx, sortExpCtx := range sortExpCtxs {
|
|
|
|
sortExpCtx := sortExpCtx.(*fql.SortClauseExpressionContext)
|
|
|
|
exp, err := v.doVisitExpression(sortExpCtx.Expression().(*fql.ExpressionContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
direction := collections.SortDirectionAsc
|
|
|
|
dir := sortExpCtx.SortDirection()
|
|
|
|
|
|
|
|
if dir != nil {
|
|
|
|
direction = collections.SortDirectionFromString(dir.GetText())
|
|
|
|
}
|
|
|
|
|
|
|
|
sorterExp, err := clauses.NewSorterExpression(
|
|
|
|
exp,
|
|
|
|
direction,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res[idx] = sorterExp
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
func (v *visitor) doVisitCollectClause(ctx *fql.CollectClauseContext, scope *scope, valVarName string) (*clauses.Collect, error) {
|
2018-10-25 03:30:05 +02:00
|
|
|
var err error
|
|
|
|
var selectors []*clauses.CollectSelector
|
|
|
|
var projection *clauses.CollectProjection
|
|
|
|
var count *clauses.CollectCount
|
|
|
|
var aggregate *clauses.CollectAggregate
|
2018-10-28 07:45:26 +02:00
|
|
|
variables := make([]string, 0, 10)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
groupingCtx := ctx.CollectGrouping()
|
|
|
|
|
|
|
|
if groupingCtx != nil {
|
|
|
|
groupingCtx := groupingCtx.(*fql.CollectGroupingContext)
|
|
|
|
collectSelectors := groupingCtx.AllCollectSelector()
|
|
|
|
|
|
|
|
// group selectors
|
2019-05-03 23:10:34 +02:00
|
|
|
if len(collectSelectors) > 0 {
|
2018-10-25 03:30:05 +02:00
|
|
|
selectors = make([]*clauses.CollectSelector, 0, len(collectSelectors))
|
|
|
|
|
|
|
|
for _, cs := range collectSelectors {
|
2018-11-13 02:58:12 +02:00
|
|
|
selector, err := v.doVisitCollectSelector(cs.(*fql.CollectSelectorContext), scope)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
selectors = append(selectors, selector)
|
2018-10-28 07:45:26 +02:00
|
|
|
variables = append(variables, selector.Variable())
|
2018-10-25 03:30:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
projectionCtx := ctx.CollectGroupVariable()
|
|
|
|
|
|
|
|
if projectionCtx != nil {
|
|
|
|
projectionCtx := projectionCtx.(*fql.CollectGroupVariableContext)
|
|
|
|
projectionSelectorCtx := projectionCtx.CollectSelector()
|
|
|
|
var projectionSelector *clauses.CollectSelector
|
|
|
|
|
|
|
|
// if projection expression is defined like WITH group = { foo: i.bar }
|
|
|
|
if projectionSelectorCtx != nil {
|
2018-11-13 02:58:12 +02:00
|
|
|
selector, err := v.doVisitCollectSelector(projectionSelectorCtx.(*fql.CollectSelectorContext), scope)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectionSelector = selector
|
|
|
|
} else {
|
|
|
|
// otherwise, use default expression WITH group = { i }
|
|
|
|
projectionIdentifier := projectionCtx.Identifier(0)
|
|
|
|
|
|
|
|
if projectionIdentifier != nil {
|
|
|
|
varExp, err := expressions.NewVariableExpression(v.getSourceMap(projectionCtx), valVarName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
strLitExp := literals.NewStringLiteral(valVarName)
|
|
|
|
|
|
|
|
propExp, err := literals.NewObjectPropertyAssignment(
|
|
|
|
strLitExp,
|
|
|
|
varExp,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectionSelectorExp := literals.NewObjectLiteralWith(propExp)
|
|
|
|
|
|
|
|
selector, err := clauses.NewCollectSelector(projectionIdentifier.GetText(), projectionSelectorExp)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
projectionSelector = selector
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if projectionSelector != nil {
|
2018-10-28 07:45:26 +02:00
|
|
|
variables = append(variables, projectionSelector.Variable())
|
2018-10-25 03:30:05 +02:00
|
|
|
projection, err = clauses.NewCollectProjection(projectionSelector)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
countCtx := ctx.CollectCounter()
|
|
|
|
|
|
|
|
if countCtx != nil {
|
|
|
|
countCtx := countCtx.(*fql.CollectCounterContext)
|
|
|
|
variable := countCtx.Identifier().GetText()
|
2018-10-28 07:45:26 +02:00
|
|
|
variables = append(variables, variable)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
count, err = clauses.NewCollectCount(variable)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aggrCtx := ctx.CollectAggregator()
|
|
|
|
|
|
|
|
if aggrCtx != nil {
|
|
|
|
aggrCtx := aggrCtx.(*fql.CollectAggregatorContext)
|
|
|
|
|
|
|
|
selectorCtxs := aggrCtx.AllCollectAggregateSelector()
|
|
|
|
selectors := make([]*clauses.CollectAggregateSelector, 0, len(selectorCtxs))
|
|
|
|
|
|
|
|
for _, sc := range selectorCtxs {
|
2018-11-13 02:58:12 +02:00
|
|
|
selector, err := v.doVisitCollectAggregateSelector(sc.(*fql.CollectAggregateSelectorContext), scope)
|
2018-10-25 03:30:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
selectors = append(selectors, selector)
|
2018-10-28 07:45:26 +02:00
|
|
|
variables = append(variables, selector.Variable())
|
2018-10-25 03:30:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
aggregate, err = clauses.NewCollectAggregate(selectors)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
// clear all variables defined before
|
|
|
|
scope.ClearVariables()
|
|
|
|
|
|
|
|
for _, variable := range variables {
|
|
|
|
if err := scope.SetVariable(variable); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
return clauses.NewCollect(selectors, projection, count, aggregate)
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
func (v *visitor) doVisitCollectSelector(ctx *fql.CollectSelectorContext, scope *scope) (*clauses.CollectSelector, error) {
|
2018-10-25 03:30:05 +02:00
|
|
|
variable := ctx.Identifier().GetText()
|
|
|
|
exp, err := v.doVisitExpression(ctx.Expression().(*fql.ExpressionContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return clauses.NewCollectSelector(variable, exp)
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:58:12 +02:00
|
|
|
func (v *visitor) doVisitCollectAggregateSelector(ctx *fql.CollectAggregateSelectorContext, scope *scope) (*clauses.CollectAggregateSelector, error) {
|
2018-10-25 03:30:05 +02:00
|
|
|
variable := ctx.Identifier().GetText()
|
|
|
|
fnCtx := ctx.FunctionCallExpression()
|
|
|
|
|
|
|
|
if fnCtx != nil {
|
|
|
|
exp, err := v.doVisitFunctionCallExpression(fnCtx.(*fql.FunctionCallExpressionContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fnExp, ok := exp.(*expressions.FunctionCallExpression)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, core.Error(core.ErrInvalidType, "expected function expression")
|
|
|
|
}
|
|
|
|
|
|
|
|
return clauses.NewCollectAggregateSelector(variable, fnExp.Arguments(), fnExp.Function())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, core.Error(core.ErrNotFound, "function expression")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitForExpressionSource(ctx *fql.ForExpressionSourceContext, scope *scope) (core.Expression, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
arr := ctx.ArrayLiteral()
|
|
|
|
|
|
|
|
if arr != nil {
|
|
|
|
return v.doVisitArrayLiteral(arr.(*fql.ArrayLiteralContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := ctx.ObjectLiteral()
|
|
|
|
|
|
|
|
if obj != nil {
|
|
|
|
return v.doVisitObjectLiteral(obj.(*fql.ObjectLiteralContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
variable := ctx.Variable()
|
|
|
|
|
|
|
|
if variable != nil {
|
|
|
|
return v.doVisitVariable(variable.(*fql.VariableContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
funcCall := ctx.FunctionCallExpression()
|
|
|
|
|
|
|
|
if funcCall != nil {
|
|
|
|
return v.doVisitFunctionCallExpression(funcCall.(*fql.FunctionCallExpressionContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
memberExp := ctx.MemberExpression()
|
|
|
|
|
|
|
|
if memberExp != nil {
|
|
|
|
return v.doVisitMemberExpression(memberExp.(*fql.MemberExpressionContext), scope)
|
|
|
|
}
|
|
|
|
|
2018-09-23 01:18:10 +02:00
|
|
|
rangeOp := ctx.RangeOperator()
|
|
|
|
|
|
|
|
if rangeOp != nil {
|
|
|
|
return v.doVisitRangeOperator(rangeOp.(*fql.RangeOperatorContext), scope)
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
param := ctx.Param()
|
|
|
|
|
|
|
|
if param != nil {
|
|
|
|
return v.doVisitParamContext(param.(*fql.ParamContext), scope)
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
return nil, core.Error(ErrInvalidDataSource, ctx.GetText())
|
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
func (v *visitor) doVisitForExpressionClause(ctx *fql.ForExpressionClauseContext, scope *scope, valVarName, _ string) (func(f *expressions.ForExpression) error, error) {
|
|
|
|
limitCtx := ctx.LimitClause()
|
|
|
|
|
|
|
|
if limitCtx != nil {
|
2018-11-13 02:58:12 +02:00
|
|
|
limit, offset, err := v.doVisitLimitClause(limitCtx.(*fql.LimitClauseContext), scope)
|
2018-10-28 07:45:26 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(f *expressions.ForExpression) error {
|
|
|
|
return f.AddLimit(v.getSourceMap(limitCtx), limit, offset)
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filterCtx := ctx.FilterClause()
|
|
|
|
|
|
|
|
if filterCtx != nil {
|
2018-11-13 02:58:12 +02:00
|
|
|
filterExp, err := v.doVisitFilterClause(filterCtx.(*fql.FilterClauseContext), scope)
|
2018-10-28 07:45:26 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(f *expressions.ForExpression) error {
|
|
|
|
return f.AddFilter(v.getSourceMap(filterCtx), filterExp)
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sortCtx := ctx.SortClause()
|
|
|
|
|
|
|
|
if sortCtx != nil {
|
|
|
|
sortCtx := sortCtx.(*fql.SortClauseContext)
|
2018-11-13 02:58:12 +02:00
|
|
|
sortExps, err := v.doVisitSortClause(sortCtx, scope)
|
2018-10-28 07:45:26 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(f *expressions.ForExpression) error {
|
|
|
|
return f.AddSort(v.getSourceMap(sortCtx), sortExps...)
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
collectCtx := ctx.CollectClause()
|
|
|
|
|
|
|
|
if collectCtx != nil {
|
|
|
|
collectCtx := collectCtx.(*fql.CollectClauseContext)
|
2018-11-13 02:58:12 +02:00
|
|
|
params, err := v.doVisitCollectClause(collectCtx, scope, valVarName)
|
2018-10-28 07:45:26 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(f *expressions.ForExpression) error {
|
|
|
|
return f.AddCollect(v.getSourceMap(collectCtx), params)
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, v.unexpectedToken(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitForExpressionStatement(ctx *fql.ForExpressionStatementContext, scope *scope) (func(f *expressions.ForExpression) error, error) {
|
|
|
|
variableCtx := ctx.VariableDeclaration()
|
|
|
|
|
|
|
|
if variableCtx != nil {
|
|
|
|
variableExp, err := v.doVisitVariableDeclaration(
|
|
|
|
variableCtx.(*fql.VariableDeclarationContext),
|
|
|
|
scope,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
return func(f *expressions.ForExpression) error {
|
|
|
|
return f.AddStatement(variableExp)
|
|
|
|
}, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
fnCallCtx := ctx.FunctionCallExpression()
|
|
|
|
|
|
|
|
if fnCallCtx != nil {
|
|
|
|
fnCallExp, err := v.doVisitFunctionCallExpression(
|
|
|
|
fnCallCtx.(*fql.FunctionCallExpressionContext),
|
|
|
|
scope,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-28 07:45:26 +02:00
|
|
|
return func(f *expressions.ForExpression) error {
|
|
|
|
return f.AddStatement(fnCallExp)
|
|
|
|
}, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, v.unexpectedToken(ctx)
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
func (v *visitor) doVisitMemberExpression(ctx *fql.MemberExpressionContext, scope *scope) (core.Expression, error) {
|
2019-09-07 07:21:43 +02:00
|
|
|
member, err := v.doVisitMember(ctx.Member().(*fql.MemberContext), scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2019-09-07 07:21:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-07 07:21:43 +02:00
|
|
|
children := ctx.MemberPath().GetChildren()
|
2018-09-18 22:42:38 +02:00
|
|
|
path := make([]core.Expression, 0, len(children))
|
|
|
|
|
|
|
|
for _, child := range children {
|
|
|
|
_, ok := child.(antlr.TerminalNode)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var exp core.Expression
|
|
|
|
var err error
|
|
|
|
var parsed bool
|
|
|
|
|
|
|
|
prop, ok := child.(*fql.PropertyNameContext)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
exp, err = v.doVisitPropertyNameContext(prop, scope)
|
|
|
|
parsed = true
|
|
|
|
} else {
|
|
|
|
computedProp, ok := child.(*fql.ComputedPropertyNameContext)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
exp, err = v.doVisitComputedPropertyNameContext(computedProp, scope)
|
|
|
|
parsed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !parsed {
|
|
|
|
// TODO: add more contextual information
|
|
|
|
return nil, ErrInvalidToken
|
|
|
|
}
|
|
|
|
|
|
|
|
path = append(path, exp)
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:21:43 +02:00
|
|
|
exp, err := expressions.NewMemberExpression(
|
2018-09-18 22:42:38 +02:00
|
|
|
v.getSourceMap(ctx),
|
2019-09-07 07:21:43 +02:00
|
|
|
member,
|
2018-09-18 22:42:38 +02:00
|
|
|
path,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:21:43 +02:00
|
|
|
return exp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitMember(ctx *fql.MemberContext, scope *scope) (core.Expression, error) {
|
|
|
|
identifier := ctx.Identifier()
|
|
|
|
|
|
|
|
if identifier != nil {
|
|
|
|
varName := ctx.Identifier().GetText()
|
|
|
|
|
|
|
|
_, err := scope.GetVariable(varName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exp, err := expressions.NewVariableExpression(v.getSourceMap(ctx), varName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return exp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fnCall := ctx.FunctionCallExpression()
|
|
|
|
|
|
|
|
if fnCall != nil {
|
|
|
|
exp, err := v.doVisitFunctionCallExpression(fnCall.(*fql.FunctionCallExpressionContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return exp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
param := ctx.Param()
|
|
|
|
|
|
|
|
exp, err := v.doVisitParamContext(param.(*fql.ParamContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return exp, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
func (v *visitor) doVisitObjectLiteral(ctx *fql.ObjectLiteralContext, scope *scope) (core.Expression, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
assignments := ctx.AllPropertyAssignment()
|
|
|
|
props := make([]*literals.ObjectPropertyAssignment, 0, len(assignments))
|
|
|
|
|
|
|
|
for _, assignment := range assignments {
|
|
|
|
var name core.Expression
|
|
|
|
var value core.Expression
|
|
|
|
var err error
|
|
|
|
|
|
|
|
assignment := assignment.(*fql.PropertyAssignmentContext)
|
|
|
|
|
|
|
|
prop := assignment.PropertyName()
|
|
|
|
computedProp := assignment.ComputedPropertyName()
|
|
|
|
shortHand := assignment.ShorthandPropertyName()
|
|
|
|
|
2019-03-29 16:48:51 +02:00
|
|
|
switch {
|
|
|
|
case prop != nil:
|
2018-09-18 22:42:38 +02:00
|
|
|
name, err = v.doVisitPropertyNameContext(prop.(*fql.PropertyNameContext), scope)
|
2019-03-29 16:48:51 +02:00
|
|
|
case computedProp != nil:
|
2018-09-18 22:42:38 +02:00
|
|
|
name, err = v.doVisitComputedPropertyNameContext(computedProp.(*fql.ComputedPropertyNameContext), scope)
|
2019-03-29 16:48:51 +02:00
|
|
|
default:
|
2018-09-18 22:42:38 +02:00
|
|
|
name, err = v.doVisitShorthandPropertyNameContext(shortHand.(*fql.ShorthandPropertyNameContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if shortHand == nil {
|
|
|
|
value, err = v.visit(assignment.Expression(), scope)
|
|
|
|
} else {
|
|
|
|
value, err = v.doVisitVariable(shortHand.(*fql.ShorthandPropertyNameContext).Variable().(*fql.VariableContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
pa, err := literals.NewObjectPropertyAssignment(name, value)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
props = append(props, pa)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return literals.NewObjectLiteralWith(props...), nil
|
|
|
|
}
|
|
|
|
|
2019-09-07 07:57:33 +02:00
|
|
|
func (v *visitor) doVisitPropertyNameContext(ctx *fql.PropertyNameContext, scope *scope) (core.Expression, error) {
|
2018-10-22 21:11:00 +02:00
|
|
|
var name string
|
|
|
|
|
|
|
|
identifier := ctx.Identifier()
|
|
|
|
|
|
|
|
if identifier != nil {
|
|
|
|
name = identifier.GetText()
|
|
|
|
} else {
|
|
|
|
stringLiteral := ctx.StringLiteral()
|
|
|
|
|
|
|
|
if stringLiteral != nil {
|
|
|
|
runes := []rune(stringLiteral.GetText())
|
|
|
|
name = string(runes[1 : len(runes)-1])
|
2019-09-07 07:57:33 +02:00
|
|
|
} else {
|
|
|
|
param, err := v.doVisitParamContext(ctx.Param().(*fql.ParamContext), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return param, nil
|
2018-10-22 21:11:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
return nil, core.Error(core.ErrNotFound, "property name")
|
|
|
|
}
|
|
|
|
|
|
|
|
return literals.NewStringLiteral(name), nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitComputedPropertyNameContext(ctx *fql.ComputedPropertyNameContext, scope *scope) (core.Expression, error) {
|
|
|
|
return v.doVisitExpression(ctx.Expression().(*fql.ExpressionContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitShorthandPropertyNameContext(ctx *fql.ShorthandPropertyNameContext, scope *scope) (core.Expression, error) {
|
|
|
|
name := ctx.Variable().GetText()
|
|
|
|
|
|
|
|
_, err := scope.GetVariable(name)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return literals.NewStringLiteral(ctx.Variable().GetText()), nil
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
func (v *visitor) doVisitArrayLiteral(ctx *fql.ArrayLiteralContext, scope *scope) (core.Expression, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
listCtx := ctx.ArrayElementList()
|
|
|
|
|
|
|
|
if listCtx == nil {
|
|
|
|
return literals.NewArrayLiteral(0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list := listCtx.(*fql.ArrayElementListContext)
|
|
|
|
exp := list.AllExpression()
|
|
|
|
elements := make([]core.Expression, 0, len(exp))
|
|
|
|
|
|
|
|
for _, e := range exp {
|
|
|
|
element, err := v.visit(e, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
elements = append(elements, element)
|
|
|
|
}
|
|
|
|
|
|
|
|
return literals.NewArrayLiteralWith(elements...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitFloatLiteral(ctx *fql.FloatLiteralContext) (core.Expression, error) {
|
|
|
|
val, err := strconv.ParseFloat(ctx.GetText(), 64)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return literals.NewFloatLiteral(val), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitIntegerLiteral(ctx *fql.IntegerLiteralContext) (core.Expression, error) {
|
|
|
|
val, err := strconv.Atoi(ctx.GetText())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return literals.NewIntLiteral(val), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitStringLiteral(ctx *fql.StringLiteralContext) (core.Expression, error) {
|
2018-11-22 05:45:00 +02:00
|
|
|
var text string
|
|
|
|
|
|
|
|
strLiteral := ctx.StringLiteral()
|
|
|
|
|
|
|
|
if strLiteral != nil {
|
|
|
|
text = strLiteral.GetText()
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
// remove extra quotes
|
|
|
|
return literals.NewStringLiteral(text[1 : len(text)-1]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitBooleanLiteral(ctx *fql.BooleanLiteralContext) (core.Expression, error) {
|
2019-03-29 16:48:51 +02:00
|
|
|
return literals.NewBooleanLiteral(strings.EqualFold(ctx.GetText(), "TRUE")), nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 23:20:48 +02:00
|
|
|
func (v *visitor) doVisitNoneLiteral(_ *fql.NoneLiteralContext) (core.Expression, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
return literals.None, nil
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
func (v *visitor) doVisitVariable(ctx *fql.VariableContext, scope *scope) (core.Expression, error) {
|
2018-09-18 22:42:38 +02:00
|
|
|
name := ctx.Identifier().GetText()
|
|
|
|
|
|
|
|
// check whether the variable is defined
|
|
|
|
_, err := scope.GetVariable(name)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return expressions.NewVariableExpression(v.getSourceMap(ctx), name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitVariableDeclaration(ctx *fql.VariableDeclarationContext, scope *scope) (core.Expression, error) {
|
|
|
|
var init core.Expression
|
|
|
|
var err error
|
|
|
|
|
|
|
|
exp := ctx.Expression()
|
|
|
|
|
|
|
|
if exp != nil {
|
|
|
|
init, err = v.doVisitExpression(ctx.Expression().(*fql.ExpressionContext), scope)
|
2018-09-28 01:05:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if init == nil && err == nil {
|
2018-09-18 22:42:38 +02:00
|
|
|
forIn := ctx.ForExpression()
|
|
|
|
|
|
|
|
if forIn != nil {
|
|
|
|
init, err = v.doVisitForExpression(forIn.(*fql.ForExpressionContext), scope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 01:05:56 +02:00
|
|
|
if init == nil && err == nil {
|
|
|
|
forTer := ctx.ForTernaryExpression()
|
|
|
|
|
|
|
|
if forTer != nil {
|
|
|
|
init, err = v.doVisitForTernaryExpression(forTer.(*fql.ForTernaryExpressionContext), scope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := ctx.Identifier().GetText()
|
|
|
|
|
|
|
|
scope.SetVariable(name)
|
|
|
|
|
|
|
|
return expressions.NewVariableDeclarationExpression(
|
|
|
|
v.getSourceMap(ctx),
|
|
|
|
name,
|
|
|
|
init,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
func (v *visitor) doVisitRangeOperator(ctx *fql.RangeOperatorContext, scope *scope) (core.Expression, error) {
|
2018-09-28 19:56:25 +02:00
|
|
|
exp, err := v.doVisitChildren(ctx, scope)
|
2018-09-23 01:18:10 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-28 19:56:25 +02:00
|
|
|
if len(exp) < 2 {
|
2018-10-28 07:45:26 +02:00
|
|
|
return nil, core.Error(ErrInvalidToken, ctx.GetText())
|
2018-09-23 01:18:10 +02:00
|
|
|
}
|
|
|
|
|
2018-09-28 19:56:25 +02:00
|
|
|
left := exp[0]
|
|
|
|
right := exp[1]
|
|
|
|
|
2018-09-23 01:18:10 +02:00
|
|
|
return operators.NewRangeOperator(
|
|
|
|
v.getSourceMap(ctx),
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
func (v *visitor) doVisitFunctionCallExpression(context *fql.FunctionCallExpressionContext, scope *scope) (core.Expression, error) {
|
2018-09-29 03:04:16 +02:00
|
|
|
args := make([]core.Expression, 0, 5)
|
|
|
|
argsCtx := context.Arguments()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
if argsCtx != nil {
|
|
|
|
argsCtx := argsCtx.(*fql.ArgumentsContext)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
for _, arg := range argsCtx.AllExpression() {
|
|
|
|
exp, err := v.doVisitExpression(arg.(*fql.ExpressionContext), scope)
|
2018-09-28 01:05:56 +02:00
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-28 01:05:56 +02:00
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
args = append(args, exp)
|
2018-09-28 01:05:56 +02:00
|
|
|
}
|
2018-09-29 03:04:16 +02:00
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2019-05-10 18:53:31 +02:00
|
|
|
var name string
|
|
|
|
|
|
|
|
funcNS := context.Namespace()
|
|
|
|
|
|
|
|
if funcNS != nil {
|
|
|
|
name += funcNS.GetText()
|
|
|
|
}
|
|
|
|
|
|
|
|
name += context.Identifier().GetText()
|
2018-09-29 03:04:16 +02:00
|
|
|
|
2019-10-14 20:47:31 +02:00
|
|
|
fun, exists := v.funcs.Get(name)
|
2018-09-29 03:04:16 +02:00
|
|
|
|
|
|
|
if !exists {
|
2019-05-10 18:53:31 +02:00
|
|
|
return nil, core.Error(core.ErrNotFound, fmt.Sprintf("function: '%s'", name))
|
2018-09-29 03:04:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return expressions.NewFunctionCallExpression(
|
|
|
|
v.getSourceMap(context),
|
|
|
|
fun,
|
|
|
|
args...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
func (v *visitor) doVisitParamContext(context *fql.ParamContext, _ *scope) (core.Expression, error) {
|
2018-09-29 03:04:16 +02:00
|
|
|
name := context.Identifier().GetText()
|
|
|
|
|
|
|
|
return expressions.NewParameterExpression(
|
|
|
|
v.getSourceMap(context),
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitAllExpressions(contexts []fql.IExpressionContext, scope *scope) ([]core.Expression, error) {
|
|
|
|
ret := make([]core.Expression, 0, len(contexts))
|
|
|
|
|
|
|
|
for _, ctx := range contexts {
|
|
|
|
exp, err := v.doVisitExpression(ctx.(*fql.ExpressionContext), scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
ret = append(ret, exp)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
return ret, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
func (v *visitor) doVisitMathOperator(ctx *fql.ExpressionContext, scope *scope) (core.OperatorExpression, error) {
|
2018-12-22 06:14:41 +02:00
|
|
|
var operator operators.MathOperatorType
|
|
|
|
multiCtx := ctx.MultiplicativeOperator()
|
|
|
|
|
|
|
|
if multiCtx != nil {
|
|
|
|
operator = operators.MathOperatorType(multiCtx.GetText())
|
|
|
|
} else {
|
|
|
|
additiveCtx := ctx.AdditiveOperator()
|
|
|
|
|
|
|
|
if additiveCtx == nil {
|
|
|
|
return nil, ErrInvalidToken
|
|
|
|
}
|
|
|
|
|
|
|
|
operator = operators.MathOperatorType(additiveCtx.GetText())
|
|
|
|
}
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
exps, err := v.doVisitAllExpressions(ctx.AllExpression(), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
left := exps[0]
|
|
|
|
right := exps[1]
|
|
|
|
|
|
|
|
return operators.NewMathOperator(
|
2018-12-22 06:14:41 +02:00
|
|
|
v.getSourceMap(ctx),
|
2018-10-07 23:54:02 +02:00
|
|
|
left,
|
|
|
|
right,
|
2018-12-22 06:14:41 +02:00
|
|
|
operator,
|
2018-10-07 23:54:02 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-10-17 17:41:40 +02:00
|
|
|
func (v *visitor) doVisitUnaryOperator(ctx *fql.ExpressionContext, scope *scope) (core.OperatorExpression, error) {
|
|
|
|
op := ctx.UnaryOperator().(*fql.UnaryOperatorContext)
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
exps, err := v.doVisitAllExpressions(ctx.AllExpression(), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := exps[0]
|
|
|
|
|
2018-10-17 17:41:40 +02:00
|
|
|
return operators.NewUnaryOperator(
|
2018-10-07 23:54:02 +02:00
|
|
|
v.getSourceMap(ctx),
|
|
|
|
exp,
|
2018-10-17 17:41:40 +02:00
|
|
|
operators.UnaryOperatorType(op.GetText()),
|
2018-10-07 23:54:02 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitLogicalOperator(ctx *fql.ExpressionContext, scope *scope) (core.OperatorExpression, error) {
|
2018-12-22 06:14:41 +02:00
|
|
|
var operator string
|
|
|
|
|
|
|
|
logicalAndOp := ctx.LogicalAndOperator()
|
|
|
|
|
|
|
|
if logicalAndOp != nil {
|
|
|
|
operator = logicalAndOp.GetText()
|
|
|
|
} else {
|
|
|
|
logicalOrOp := ctx.LogicalOrOperator()
|
|
|
|
|
|
|
|
if logicalOrOp == nil {
|
|
|
|
return nil, ErrInvalidToken
|
|
|
|
}
|
|
|
|
|
|
|
|
operator = logicalOrOp.GetText()
|
|
|
|
}
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
exps, err := v.doVisitAllExpressions(ctx.AllExpression(), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
left := exps[0]
|
|
|
|
right := exps[1]
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
return operators.NewLogicalOperator(v.getSourceMap(ctx), left, right, operator)
|
2018-10-07 23:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitEqualityOperator(ctx *fql.ExpressionContext, scope *scope) (core.OperatorExpression, error) {
|
|
|
|
equalityOp := ctx.EqualityOperator().(*fql.EqualityOperatorContext)
|
|
|
|
exps, err := v.doVisitAllExpressions(ctx.AllExpression(), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
left := exps[0]
|
|
|
|
right := exps[1]
|
|
|
|
|
|
|
|
return operators.NewEqualityOperator(v.getSourceMap(equalityOp), left, right, equalityOp.GetText())
|
|
|
|
}
|
|
|
|
|
2019-07-09 20:19:53 +02:00
|
|
|
func (v *visitor) doVisitRegexpOperator(ctx *fql.ExpressionContext, scope *scope) (core.Expression, error) {
|
|
|
|
regexpOp := ctx.RegexpOperator().(*fql.RegexpOperatorContext)
|
|
|
|
rawExps := ctx.AllExpression()
|
|
|
|
exps, err := v.doVisitAllExpressions(rawExps, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
left := exps[0]
|
|
|
|
right := exps[1]
|
|
|
|
|
|
|
|
switch lit := right.(type) {
|
|
|
|
case literals.StringLiteral:
|
|
|
|
_, err := regexp.Compile(string(lit))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
src := v.getSourceMap(rawExps[1])
|
|
|
|
|
|
|
|
return nil, errors.Wrap(err, src.String())
|
|
|
|
}
|
|
|
|
case *literals.ArrayLiteral, *literals.ObjectLiteral, literals.BooleanLiteral, literals.FloatLiteral, literals.IntLiteral:
|
|
|
|
src := v.getSourceMap(rawExps[1])
|
|
|
|
|
|
|
|
return nil, errors.Wrap(errors.New("expected a string literal or a function call"), src.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return operators.NewRegexpOperator(v.getSourceMap(regexpOp), left, right, regexpOp.GetText())
|
|
|
|
}
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
func (v *visitor) doVisitInOperator(ctx *fql.ExpressionContext, scope *scope) (core.OperatorExpression, error) {
|
|
|
|
exps, err := v.doVisitAllExpressions(ctx.AllExpression(), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-17 17:41:40 +02:00
|
|
|
op := ctx.InOperator().(*fql.InOperatorContext)
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
left := exps[0]
|
|
|
|
right := exps[1]
|
|
|
|
|
|
|
|
if len(exps) != 2 {
|
|
|
|
return nil, v.unexpectedToken(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return operators.NewInOperator(
|
|
|
|
v.getSourceMap(ctx),
|
|
|
|
left,
|
|
|
|
right,
|
2018-10-17 17:41:40 +02:00
|
|
|
op.Not() != nil,
|
2018-10-07 23:54:02 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) doVisitArrayOperator(ctx *fql.ExpressionContext, scope *scope) (core.OperatorExpression, error) {
|
|
|
|
var comparator core.OperatorExpression
|
|
|
|
var err error
|
|
|
|
|
2019-03-29 16:48:51 +02:00
|
|
|
switch {
|
|
|
|
case ctx.InOperator() != nil:
|
2018-10-07 23:54:02 +02:00
|
|
|
comparator, err = v.doVisitInOperator(ctx, scope)
|
2019-03-29 16:48:51 +02:00
|
|
|
case ctx.EqualityOperator() != nil:
|
2018-10-07 23:54:02 +02:00
|
|
|
comparator, err = v.doVisitEqualityOperator(ctx, scope)
|
2019-03-29 16:48:51 +02:00
|
|
|
default:
|
2018-10-07 23:54:02 +02:00
|
|
|
return nil, v.unexpectedToken(ctx)
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:48:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-07 23:54:02 +02:00
|
|
|
exps, err := v.doVisitAllExpressions(ctx.AllExpression(), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(exps) != 2 {
|
|
|
|
return nil, v.unexpectedToken(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
left := exps[0]
|
|
|
|
right := exps[1]
|
|
|
|
|
|
|
|
aotype, err := operators.ToIsValidArrayOperatorType(ctx.ArrayOperator().GetText())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return operators.NewArrayOperator(
|
|
|
|
v.getSourceMap(ctx),
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
aotype,
|
|
|
|
comparator,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
func (v *visitor) doVisitExpressionGroup(ctx *fql.ExpressionGroupContext, scope *scope) (core.Expression, error) {
|
|
|
|
exp := ctx.Expression()
|
|
|
|
|
|
|
|
if exp == nil {
|
|
|
|
return nil, ErrInvalidToken
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.doVisitExpression(exp.(*fql.ExpressionContext), scope)
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
func (v *visitor) doVisitExpression(ctx *fql.ExpressionContext, scope *scope) (core.Expression, error) {
|
2018-12-22 06:14:41 +02:00
|
|
|
seq := ctx.ExpressionGroup()
|
|
|
|
|
|
|
|
if seq != nil {
|
|
|
|
return v.doVisitExpressionGroup(seq.(*fql.ExpressionGroupContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
member := ctx.MemberExpression()
|
|
|
|
|
|
|
|
if member != nil {
|
|
|
|
return v.doVisitMemberExpression(member.(*fql.MemberExpressionContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
funCall := ctx.FunctionCallExpression()
|
|
|
|
|
|
|
|
if funCall != nil {
|
|
|
|
return v.doVisitFunctionCallExpression(funCall.(*fql.FunctionCallExpressionContext), scope)
|
|
|
|
}
|
|
|
|
|
2018-10-17 17:41:40 +02:00
|
|
|
notOp := ctx.UnaryOperator()
|
|
|
|
|
|
|
|
if notOp != nil {
|
|
|
|
return v.doVisitUnaryOperator(ctx, scope)
|
|
|
|
}
|
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
multiOp := ctx.MultiplicativeOperator()
|
|
|
|
|
|
|
|
if multiOp != nil {
|
|
|
|
return v.doVisitMathOperator(ctx, scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
addOp := ctx.AdditiveOperator()
|
|
|
|
|
|
|
|
if addOp != nil {
|
|
|
|
return v.doVisitMathOperator(ctx, scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
arrOp := ctx.ArrayOperator()
|
|
|
|
|
|
|
|
if arrOp != nil {
|
|
|
|
return v.doVisitArrayOperator(ctx, scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
equalityOp := ctx.EqualityOperator()
|
|
|
|
|
|
|
|
if equalityOp != nil {
|
|
|
|
return v.doVisitEqualityOperator(ctx, scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
inOp := ctx.InOperator()
|
|
|
|
|
|
|
|
if inOp != nil {
|
|
|
|
return v.doVisitInOperator(ctx, scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
logicalAndOp := ctx.LogicalAndOperator()
|
|
|
|
|
|
|
|
if logicalAndOp != nil {
|
|
|
|
return v.doVisitLogicalOperator(ctx, scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
logicalOrOp := ctx.LogicalOrOperator()
|
|
|
|
|
|
|
|
if logicalOrOp != nil {
|
|
|
|
return v.doVisitLogicalOperator(ctx, scope)
|
|
|
|
}
|
|
|
|
|
2019-07-09 20:19:53 +02:00
|
|
|
regexpOp := ctx.RegexpOperator()
|
|
|
|
|
|
|
|
if regexpOp != nil {
|
|
|
|
return v.doVisitRegexpOperator(ctx, scope)
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
variable := ctx.Variable()
|
|
|
|
|
|
|
|
if variable != nil {
|
|
|
|
return v.doVisitVariable(variable.(*fql.VariableContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
str := ctx.StringLiteral()
|
|
|
|
|
|
|
|
if str != nil {
|
|
|
|
return v.doVisitStringLiteral(str.(*fql.StringLiteralContext))
|
|
|
|
}
|
|
|
|
|
|
|
|
integ := ctx.IntegerLiteral()
|
|
|
|
|
|
|
|
if integ != nil {
|
|
|
|
return v.doVisitIntegerLiteral(integ.(*fql.IntegerLiteralContext))
|
|
|
|
}
|
|
|
|
|
|
|
|
float := ctx.FloatLiteral()
|
|
|
|
|
|
|
|
if float != nil {
|
|
|
|
return v.doVisitFloatLiteral(float.(*fql.FloatLiteralContext))
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean := ctx.BooleanLiteral()
|
|
|
|
|
|
|
|
if boolean != nil {
|
|
|
|
return v.doVisitBooleanLiteral(boolean.(*fql.BooleanLiteralContext))
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := ctx.ArrayLiteral()
|
|
|
|
|
|
|
|
if arr != nil {
|
|
|
|
return v.doVisitArrayLiteral(arr.(*fql.ArrayLiteralContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := ctx.ObjectLiteral()
|
|
|
|
|
|
|
|
if obj != nil {
|
|
|
|
return v.doVisitObjectLiteral(obj.(*fql.ObjectLiteralContext), scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
none := ctx.NoneLiteral()
|
|
|
|
|
|
|
|
if none != nil {
|
|
|
|
return v.doVisitNoneLiteral(none.(*fql.NoneLiteralContext))
|
|
|
|
}
|
|
|
|
|
2018-09-22 17:24:35 +02:00
|
|
|
questionCtx := ctx.QuestionMark()
|
|
|
|
|
|
|
|
if questionCtx != nil {
|
|
|
|
exps, err := v.doVisitAllExpressions(ctx.AllExpression(), scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-28 01:05:56 +02:00
|
|
|
return v.createTernaryOperator(
|
2018-09-22 17:24:35 +02:00
|
|
|
v.getSourceMap(ctx),
|
2018-09-28 01:05:56 +02:00
|
|
|
exps,
|
|
|
|
scope,
|
2018-09-22 17:24:35 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-09-23 01:18:10 +02:00
|
|
|
rangeOp := ctx.RangeOperator()
|
|
|
|
|
|
|
|
if rangeOp != nil {
|
|
|
|
return v.doVisitRangeOperator(rangeOp.(*fql.RangeOperatorContext), scope)
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
param := ctx.Param()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
if param != nil {
|
|
|
|
return v.doVisitParamContext(param.(*fql.ParamContext), scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
// TODO: Complete it
|
|
|
|
return nil, ErrNotImplemented
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
func (v *visitor) doVisitChildren(node antlr.RuleNode, scope *scope) ([]core.Expression, error) {
|
|
|
|
children := node.GetChildren()
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
if children == nil {
|
2019-05-03 23:10:34 +02:00
|
|
|
return make([]core.Expression, 0), nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
result := make([]core.Expression, 0, len(children))
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
for _, child := range children {
|
|
|
|
_, ok := child.(antlr.TerminalNode)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
out, err := v.visit(child, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
result = append(result, out)
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 03:04:16 +02:00
|
|
|
return result, nil
|
2018-09-18 22:42:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) visit(node antlr.Tree, scope *scope) (core.Expression, error) {
|
|
|
|
var out core.Expression
|
|
|
|
var err error
|
|
|
|
|
2019-02-15 14:41:08 +02:00
|
|
|
switch ctx := node.(type) {
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.BodyContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitBody(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.ExpressionContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitExpression(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.ForExpressionContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitForExpression(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.ReturnExpressionContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitReturnExpression(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.ArrayLiteralContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitArrayLiteral(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.ObjectLiteralContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitObjectLiteral(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.StringLiteralContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitStringLiteral(ctx)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.IntegerLiteralContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitIntegerLiteral(ctx)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.FloatLiteralContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitFloatLiteral(ctx)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.BooleanLiteralContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitBooleanLiteral(ctx)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.NoneLiteralContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitNoneLiteral(ctx)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.VariableContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitVariable(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.VariableDeclarationContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitVariableDeclaration(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
case *fql.FunctionCallExpressionContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitFunctionCallExpression(ctx, scope)
|
2018-09-29 03:04:16 +02:00
|
|
|
case *fql.ParamContext:
|
2019-02-15 14:41:08 +02:00
|
|
|
out, err = v.doVisitParamContext(ctx, scope)
|
2018-09-18 22:42:38 +02:00
|
|
|
default:
|
|
|
|
err = v.unexpectedToken(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
2018-09-28 01:05:56 +02:00
|
|
|
func (v *visitor) doVisitForTernaryExpression(ctx *fql.ForTernaryExpressionContext, scope *scope) (*expressions.ConditionExpression, error) {
|
|
|
|
exps, err := v.doVisitChildren(ctx, scope)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.createTernaryOperator(
|
|
|
|
v.getSourceMap(ctx),
|
|
|
|
exps,
|
|
|
|
scope,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-10-05 23:20:48 +02:00
|
|
|
func (v *visitor) createTernaryOperator(src core.SourceMap, exps []core.Expression, _ *scope) (*expressions.ConditionExpression, error) {
|
2018-09-28 01:05:56 +02:00
|
|
|
var test core.Expression
|
|
|
|
var consequent core.Expression
|
|
|
|
var alternate core.Expression
|
|
|
|
|
|
|
|
if len(exps) == 3 {
|
|
|
|
test = exps[0]
|
|
|
|
consequent = exps[1]
|
|
|
|
alternate = exps[2]
|
|
|
|
} else {
|
|
|
|
test = exps[0]
|
|
|
|
alternate = exps[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return expressions.NewConditionExpression(
|
|
|
|
src,
|
|
|
|
test,
|
|
|
|
consequent,
|
|
|
|
alternate,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:42:38 +02:00
|
|
|
func (v *visitor) unexpectedToken(node antlr.Tree) error {
|
|
|
|
name := "undefined"
|
|
|
|
ctx, ok := node.(antlr.RuleContext)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
name = ctx.GetText()
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Errorf("unexpected token: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *visitor) getSourceMap(rule antlr.ParserRuleContext) core.SourceMap {
|
|
|
|
start := rule.GetStart()
|
|
|
|
|
|
|
|
return core.NewSourceMap(
|
|
|
|
rule.GetText(),
|
|
|
|
start.GetLine(),
|
|
|
|
start.GetColumn(),
|
|
|
|
)
|
|
|
|
}
|