Add parsing support for pointer names

This commit is contained in:
Anton Medvedev
2023-08-24 15:27:00 +02:00
parent eb03bcb945
commit 4683fbe2bf
2 changed files with 28 additions and 19 deletions
+6 -7
View File
@@ -46,7 +46,6 @@ type Tree struct {
func Parse(input string) (*Tree, error) {
return ParseWithConfig(input, &conf.Config{
Pipes: false,
Disabled: map[string]bool{},
})
}
@@ -255,10 +254,15 @@ func (p *parser) parsePrimary() Node {
if p.depth > 0 {
if token.Is(Operator, "#") || token.Is(Operator, ".") {
name := ""
if token.Is(Operator, "#") {
p.next()
if p.current.Is(Identifier) {
name = p.current.Value
p.next()
}
}
node := &PointerNode{}
node := &PointerNode{Name: name}
node.SetLocation(token.Location)
return p.parsePostfixExpression(node)
}
@@ -583,11 +587,6 @@ func (p *parser) parsePostfixExpression(node Node) Node {
}
func (p *parser) parsePipe(node Node) Node {
if !p.config.Pipes {
p.error("enable Pipes via expr.ExperimentalPipes()")
return &NilNode{}
}
identifier := p.current
p.expect(Identifier)
+22 -12
View File
@@ -6,16 +6,15 @@ import (
"testing"
. "github.com/antonmedv/expr/ast"
"github.com/antonmedv/expr/conf"
"github.com/antonmedv/expr/parser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParse(t *testing.T) {
parseTests := []struct {
input string
expected Node
tests := []struct {
input string
want Node
}{
{
"a",
@@ -463,14 +462,25 @@ func TestParse(t *testing.T) {
Left: &IdentifierNode{Value: "foo"},
Right: &IdentifierNode{Value: "c"}}},
},
{
`map([], #index)`,
&BuiltinNode{
Name: "map",
Arguments: []Node{
&ArrayNode{},
&ClosureNode{
Node: &PointerNode{Name: "index"},
},
},
},
},
}
for _, test := range parseTests {
actual, err := parser.ParseWithConfig(test.input, &conf.Config{Pipes: true})
if err != nil {
t.Errorf("%s:\n%v", test.input, err)
continue
}
assert.Equal(t, Dump(test.expected), Dump(actual.Node), test.input)
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
actual, err := parser.Parse(test.input)
require.NoError(t, err)
assert.Equal(t, Dump(test.want), Dump(actual.Node))
})
}
}
@@ -669,7 +679,7 @@ func TestParse_pipe_operator(t *testing.T) {
Property: &StringNode{Value: "foo"},
}}}}}}}}
actual, err := parser.ParseWithConfig(input, &conf.Config{Pipes: true})
actual, err := parser.Parse(input)
require.NoError(t, err)
assert.Equal(t, Dump(expect), Dump(actual.Node))
}