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

fix(parser): add missing comparison operators #369

Merged
merged 1 commit into from
Dec 4, 2018
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 internal/parser/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ For the parser, the above grammar undergoes a process to have the left-recursion
LogicalOperator = and | or .
ComparisonExpression = MultiplicativeExpression { ComparisonExpressionSuffix } .
ComparisonExpressionSuffix = ComparisonOperator MultiplicativeExpr .
ComparisonOperator = eq | neq | regexeq | regexneq .
ComparisonOperator = eq | neq | lt | lte | gt | gte | regexeq | regexneq .
MultiplicativeExpression = AdditiveExpression { MultiplicativeOperator AdditiveExpression } .
MultiplicativeOperator = mul | div .
AdditiveExpression = PipeExpression { AdditiveExpressionSuffix } .
Expand Down
12 changes: 12 additions & 0 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ func (p *parser) parseComparisonOperator() (ast.OperatorKind, bool) {
case token.NEQ:
p.consume()
return ast.NotEqualOperator, true
case token.LTE:
p.consume()
return ast.LessThanEqualOperator, true
case token.LT:
p.consume()
return ast.LessThanOperator, true
case token.GTE:
p.consume()
return ast.GreaterThanEqualOperator, true
case token.GT:
p.consume()
return ast.GreaterThanOperator, true
case token.REGEXEQ:
p.consume()
return ast.RegexpMatchOperator, true
Expand Down
51 changes: 51 additions & 0 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,57 @@ func testParser(runFn func(name string, fn func(t testing.TB))) {
},
},
},
{
name: "binary expression",
raw: `_value < 10.0`,
want: &ast.Program{
BaseNode: base("1:1", "1:14"),
Body: []ast.Statement{&ast.ExpressionStatement{
BaseNode: base("1:1", "1:14"),
Expression: &ast.BinaryExpression{
BaseNode: base("1:1", "1:14"),
Operator: ast.LessThanOperator,
Left: &ast.Identifier{
BaseNode: base("1:1", "1:7"),
Name: "_value",
},
Right: &ast.FloatLiteral{
BaseNode: base("1:10", "1:14"),
Value: 10.0,
},
},
}},
},
},
{
name: "member expression binary expression",
raw: `r._value < 10.0`,
want: &ast.Program{
BaseNode: base("1:1", "1:16"),
Body: []ast.Statement{&ast.ExpressionStatement{
BaseNode: base("1:1", "1:16"),
Expression: &ast.BinaryExpression{
BaseNode: base("1:1", "1:16"),
Operator: ast.LessThanOperator,
Left: &ast.MemberExpression{
BaseNode: base("1:1", "1:9"),
Object: &ast.Identifier{
BaseNode: base("1:1", "1:2"),
Name: "r",
},
Property: &ast.Identifier{
BaseNode: base("1:3", "1:9"),
Name: "_value",
},
},
Right: &ast.FloatLiteral{
BaseNode: base("1:12", "1:16"),
Value: 10.0,
},
},
}},
},
},
{
name: "var as binary expression of other vars",
raw: `a = 1
Expand Down