Skip to content

Commit

Permalink
fix(parser): add missing comparison operators (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsternberg authored Dec 4, 2018
1 parent 13f7dc2 commit 93bbf9f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
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 @@ -382,6 +382,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

0 comments on commit 93bbf9f

Please sign in to comment.