Add tests, improve parsing of nested expressions
This commit is contained in:
@@ -193,17 +193,18 @@ checkToken:
|
||||
list = append(list, String{Value: t.literal})
|
||||
goto next
|
||||
case PARENTHESIS_OPEN:
|
||||
depth := p.level
|
||||
p.level++
|
||||
dpth := p.level
|
||||
if sub, err := p.parseExpr(p.nextToken()); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
list = append(list, sub)
|
||||
}
|
||||
|
||||
// Allow parent level to continue parsing.
|
||||
// Example: ((A) AND (B))
|
||||
// +1 since PARENTHESIS_CLOSE decrease level
|
||||
if p.level > 0 && (p.level+1) != dpth {
|
||||
if (p.level + 1) != depth {
|
||||
p.nextToken()
|
||||
goto next
|
||||
}
|
||||
|
||||
@@ -183,6 +183,39 @@ func TestAstParser_Parser(t *testing.T) {
|
||||
Null{},
|
||||
},
|
||||
},
|
||||
{
|
||||
parser: NewParser().ParseExpression,
|
||||
in: `((foo1) AND (foo2))`,
|
||||
tree: ASTNodes{
|
||||
ASTNodes{Ident{Value: "foo1"}},
|
||||
Operator{"AND"},
|
||||
ASTNodes{Ident{Value: "foo2"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
parser: NewParser().ParseExpression,
|
||||
in: `((foo1) AND (foo2) AND foo3)`,
|
||||
tree: ASTNodes{
|
||||
ASTNodes{Ident{Value: "foo1"}},
|
||||
Operator{"AND"},
|
||||
ASTNodes{Ident{Value: "foo2"}},
|
||||
Operator{"AND"},
|
||||
Ident{Value: "foo3"},
|
||||
},
|
||||
},
|
||||
{
|
||||
parser: NewParser().ParseExpression,
|
||||
in: `((foo1) AND (foo2)) AND foo3`,
|
||||
tree: ASTNodes{
|
||||
ASTNodes{
|
||||
ASTNodes{Ident{Value: "foo1"}},
|
||||
Operator{"AND"},
|
||||
ASTNodes{Ident{Value: "foo2"}},
|
||||
},
|
||||
Operator{"AND"},
|
||||
Ident{Value: "foo3"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
||||
Reference in New Issue
Block a user