3
0

Add tests, improve parsing of nested expressions

This commit is contained in:
Denis Arh
2019-12-02 10:46:27 +01:00
parent be1420f1bc
commit 63e3cff8ab
2 changed files with 36 additions and 2 deletions

View File

@@ -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
}

View File

@@ -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 {