1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00

Added param to member expression

This commit is contained in:
Tim Voronov 2019-09-07 01:21:43 -04:00
parent 24370d8178
commit 9c0e81a2f4
9 changed files with 1171 additions and 987 deletions

View File

@ -63,4 +63,21 @@ func TestParam(t *testing.T) {
So(string(out), ShouldEqual, `[1,2,3,4]`) So(string(out), ShouldEqual, `[1,2,3,4]`)
}) })
Convey("Should be possible to use in member expression", t, func() {
prog := compiler.New().
MustCompile(`
RETURN @param.value
`)
out := prog.MustRun(
context.Background(),
runtime.WithParam("param", map[string]interface{}{
"value": "foobar",
}),
)
So(string(out), ShouldEqual, `"foobar"`)
})
} }

View File

@ -736,39 +736,13 @@ func (v *visitor) doVisitForExpressionStatement(ctx *fql.ForExpressionStatementC
} }
func (v *visitor) doVisitMemberExpression(ctx *fql.MemberExpressionContext, scope *scope) (core.Expression, error) { func (v *visitor) doVisitMemberExpression(ctx *fql.MemberExpressionContext, scope *scope) (core.Expression, error) {
var source core.Expression member, err := v.doVisitMember(ctx.Member().(*fql.MemberContext), scope)
var children []antlr.Tree
identifier := ctx.Identifier() if err != nil {
return nil, err
if identifier != nil {
varName := ctx.Identifier().GetText()
_, err := scope.GetVariable(varName)
if err != nil {
return nil, err
}
varExp, err := expressions.NewVariableExpression(v.getSourceMap(ctx), varName)
if err != nil {
return nil, err
}
source = varExp
children = ctx.GetChildren()
} else {
fcall, err := v.doVisitFunctionCallExpression(ctx.FunctionCallExpression().(*fql.FunctionCallExpressionContext), scope)
if err != nil {
return nil, err
}
source = fcall
children = ctx.GetChildren()[1:]
} }
children := ctx.MemberPath().GetChildren()
path := make([]core.Expression, 0, len(children)) path := make([]core.Expression, 0, len(children))
for _, child := range children { for _, child := range children {
@ -808,9 +782,9 @@ func (v *visitor) doVisitMemberExpression(ctx *fql.MemberExpressionContext, scop
path = append(path, exp) path = append(path, exp)
} }
member, err := expressions.NewMemberExpression( exp, err := expressions.NewMemberExpression(
v.getSourceMap(ctx), v.getSourceMap(ctx),
source, member,
path, path,
) )
@ -818,7 +792,51 @@ func (v *visitor) doVisitMemberExpression(ctx *fql.MemberExpressionContext, scop
return nil, err return nil, err
} }
return member, nil 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
} }
func (v *visitor) doVisitObjectLiteral(ctx *fql.ObjectLiteralContext, scope *scope) (core.Expression, error) { func (v *visitor) doVisitObjectLiteral(ctx *fql.ObjectLiteralContext, scope *scope) (core.Expression, error) {

View File

@ -209,11 +209,19 @@ functionCallExpression
: namespace Identifier arguments : namespace Identifier arguments
; ;
member
: Identifier
| functionCallExpression
| param
;
memberPath
: (Dot propertyName (computedPropertyName)*)+
| computedPropertyName (Dot propertyName (computedPropertyName)*)* (computedPropertyName (Dot propertyName)*)*
;
memberExpression memberExpression
: Identifier (Dot propertyName (computedPropertyName)*)+ : member memberPath
| Identifier computedPropertyName (Dot propertyName (computedPropertyName)*)* (computedPropertyName (Dot propertyName)*)*
| functionCallExpression (Dot propertyName (computedPropertyName)*)+
| functionCallExpression computedPropertyName (Dot propertyName (computedPropertyName)*)* (computedPropertyName (Dot propertyName)*)*
; ;
arguments arguments

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -286,6 +286,18 @@ func (s *BaseFqlParserListener) EnterFunctionCallExpression(ctx *FunctionCallExp
// ExitFunctionCallExpression is called when production functionCallExpression is exited. // ExitFunctionCallExpression is called when production functionCallExpression is exited.
func (s *BaseFqlParserListener) ExitFunctionCallExpression(ctx *FunctionCallExpressionContext) {} func (s *BaseFqlParserListener) ExitFunctionCallExpression(ctx *FunctionCallExpressionContext) {}
// EnterMember is called when production member is entered.
func (s *BaseFqlParserListener) EnterMember(ctx *MemberContext) {}
// ExitMember is called when production member is exited.
func (s *BaseFqlParserListener) ExitMember(ctx *MemberContext) {}
// EnterMemberPath is called when production memberPath is entered.
func (s *BaseFqlParserListener) EnterMemberPath(ctx *MemberPathContext) {}
// ExitMemberPath is called when production memberPath is exited.
func (s *BaseFqlParserListener) ExitMemberPath(ctx *MemberPathContext) {}
// EnterMemberExpression is called when production memberExpression is entered. // EnterMemberExpression is called when production memberExpression is entered.
func (s *BaseFqlParserListener) EnterMemberExpression(ctx *MemberExpressionContext) {} func (s *BaseFqlParserListener) EnterMemberExpression(ctx *MemberExpressionContext) {}

View File

@ -183,6 +183,14 @@ func (v *BaseFqlParserVisitor) VisitFunctionCallExpression(ctx *FunctionCallExpr
return v.VisitChildren(ctx) return v.VisitChildren(ctx)
} }
func (v *BaseFqlParserVisitor) VisitMember(ctx *MemberContext) interface{} {
return v.VisitChildren(ctx)
}
func (v *BaseFqlParserVisitor) VisitMemberPath(ctx *MemberPathContext) interface{} {
return v.VisitChildren(ctx)
}
func (v *BaseFqlParserVisitor) VisitMemberExpression(ctx *MemberExpressionContext) interface{} { func (v *BaseFqlParserVisitor) VisitMemberExpression(ctx *MemberExpressionContext) interface{} {
return v.VisitChildren(ctx) return v.VisitChildren(ctx)
} }

View File

@ -139,6 +139,12 @@ type FqlParserListener interface {
// EnterFunctionCallExpression is called when entering the functionCallExpression production. // EnterFunctionCallExpression is called when entering the functionCallExpression production.
EnterFunctionCallExpression(c *FunctionCallExpressionContext) EnterFunctionCallExpression(c *FunctionCallExpressionContext)
// EnterMember is called when entering the member production.
EnterMember(c *MemberContext)
// EnterMemberPath is called when entering the memberPath production.
EnterMemberPath(c *MemberPathContext)
// EnterMemberExpression is called when entering the memberExpression production. // EnterMemberExpression is called when entering the memberExpression production.
EnterMemberExpression(c *MemberExpressionContext) EnterMemberExpression(c *MemberExpressionContext)
@ -310,6 +316,12 @@ type FqlParserListener interface {
// ExitFunctionCallExpression is called when exiting the functionCallExpression production. // ExitFunctionCallExpression is called when exiting the functionCallExpression production.
ExitFunctionCallExpression(c *FunctionCallExpressionContext) ExitFunctionCallExpression(c *FunctionCallExpressionContext)
// ExitMember is called when exiting the member production.
ExitMember(c *MemberContext)
// ExitMemberPath is called when exiting the memberPath production.
ExitMemberPath(c *MemberPathContext)
// ExitMemberExpression is called when exiting the memberExpression production. // ExitMemberExpression is called when exiting the memberExpression production.
ExitMemberExpression(c *MemberExpressionContext) ExitMemberExpression(c *MemberExpressionContext)

View File

@ -139,6 +139,12 @@ type FqlParserVisitor interface {
// Visit a parse tree produced by FqlParser#functionCallExpression. // Visit a parse tree produced by FqlParser#functionCallExpression.
VisitFunctionCallExpression(ctx *FunctionCallExpressionContext) interface{} VisitFunctionCallExpression(ctx *FunctionCallExpressionContext) interface{}
// Visit a parse tree produced by FqlParser#member.
VisitMember(ctx *MemberContext) interface{}
// Visit a parse tree produced by FqlParser#memberPath.
VisitMemberPath(ctx *MemberPathContext) interface{}
// Visit a parse tree produced by FqlParser#memberExpression. // Visit a parse tree produced by FqlParser#memberExpression.
VisitMemberExpression(ctx *MemberExpressionContext) interface{} VisitMemberExpression(ctx *MemberExpressionContext) interface{}