Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed access to a member property right after a function call #368

Merged
merged 2 commits into from
Sep 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (r *Runner) runQueries(ctx context.Context, dir string) ([]Result, error) {
return results, nil
}

func (r *Runner) runQuery(ctx context.Context, c *compiler.FqlCompiler, name, script string) Result {
func (r *Runner) runQuery(ctx context.Context, c *compiler.Compiler, name, script string) Result {
start := time.Now()

p, err := c.Compile(script)
Expand Down
13 changes: 7 additions & 6 deletions pkg/compiler/compiler.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package compiler

import (
"github.com/pkg/errors"

"github.com/MontFerret/ferret/pkg/parser"
"github.com/MontFerret/ferret/pkg/runtime"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/stdlib"
"github.com/pkg/errors"
)

type FqlCompiler struct {
type Compiler struct {
*NamespaceContainer
}

func New(setters ...Option) *FqlCompiler {
c := &FqlCompiler{}
func New(setters ...Option) *Compiler {
c := &Compiler{}
c.NamespaceContainer = newRootNamespace()
c.funcs = make(map[string]core.Function)

Expand All @@ -32,7 +33,7 @@ func New(setters ...Option) *FqlCompiler {
return c
}

func (c *FqlCompiler) Compile(query string) (program *runtime.Program, err error) {
func (c *Compiler) Compile(query string) (program *runtime.Program, err error) {
if query == "" {
return nil, ErrEmptyQuery
}
Expand Down Expand Up @@ -69,7 +70,7 @@ func (c *FqlCompiler) Compile(query string) (program *runtime.Program, err error
return program, err
}

func (c *FqlCompiler) MustCompile(query string) *runtime.Program {
func (c *Compiler) MustCompile(query string) *runtime.Program {
program, err := c.Compile(query)

if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions pkg/compiler/compiler_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,42 @@ func TestMember(t *testing.T) {

So(string(out), ShouldEqual, `"wsx"`)
})

Convey("Prop after a func call", func() {
c := compiler.New()

p, err := c.Compile(`
LET arr = [{ name: "Bob" }]

RETURN FIRST(arr).name
`)

So(err, ShouldBeNil)

out, err := p.Run(context.Background())

So(err, ShouldBeNil)

So(string(out), ShouldEqual, `"Bob"`)
})

Convey("Computed prop after a func call", func() {
c := compiler.New()

p, err := c.Compile(`
LET arr = [{ name: { first: "Bob" } }]

RETURN FIRST(arr)['name'].first
`)

So(err, ShouldBeNil)

out, err := p.Run(context.Background())

So(err, ShouldBeNil)

So(string(out), ShouldEqual, `"Bob"`)
})
})
}

Expand Down
36 changes: 30 additions & 6 deletions pkg/compiler/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,15 +736,39 @@ func (v *visitor) doVisitForExpressionStatement(ctx *fql.ForExpressionStatementC
}

func (v *visitor) doVisitMemberExpression(ctx *fql.MemberExpressionContext, scope *scope) (core.Expression, error) {
varName := ctx.Identifier().GetText()
var source core.Expression
var children []antlr.Tree

_, err := scope.GetVariable(varName)
identifier := ctx.Identifier()

if err != nil {
return nil, err
if identifier != nil {
varName := ctx.Identifier().GetText()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just identifier.GetText()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♂️for clarity, I guess 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I use the variable to do an existence check


_, 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.GetChildren()
path := make([]core.Expression, 0, len(children))

for _, child := range children {
Expand Down Expand Up @@ -786,7 +810,7 @@ func (v *visitor) doVisitMemberExpression(ctx *fql.MemberExpressionContext, scop

member, err := expressions.NewMemberExpression(
v.getSourceMap(ctx),
varName,
source,
path,
)

Expand Down
12 changes: 7 additions & 5 deletions pkg/parser/antlr/FqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,6 @@ propertyAssignment
| shorthandPropertyName
;

memberExpression
: Identifier (Dot propertyName (computedPropertyName)*)+
| Identifier computedPropertyName (Dot propertyName (computedPropertyName)*)* (computedPropertyName (Dot propertyName)*)*
;

shorthandPropertyName
: variable
;
Expand All @@ -215,6 +210,13 @@ functionCallExpression
: namespace Identifier arguments
;

memberExpression
: Identifier (Dot propertyName (computedPropertyName)*)+
| Identifier computedPropertyName (Dot propertyName (computedPropertyName)*)* (computedPropertyName (Dot propertyName)*)*
| functionCallExpression (Dot propertyName (computedPropertyName)*)+
| functionCallExpression computedPropertyName (Dot propertyName (computedPropertyName)*)* (computedPropertyName (Dot propertyName)*)*
;

arguments
: OpenParen(expression (Comma expression)*)?CloseParen
;
Expand Down
4 changes: 2 additions & 2 deletions pkg/parser/fql/FqlParser.interp

Large diffs are not rendered by default.

Loading