From 93bbf9f7ff9032ce19e88edde5f9ce3391bc8abc Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Tue, 4 Dec 2018 13:45:04 -0600 Subject: [PATCH] fix(parser): add missing comparison operators (#369) --- internal/parser/grammar.md | 2 +- internal/parser/parser.go | 12 ++++++++ internal/parser/parser_test.go | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/internal/parser/grammar.md b/internal/parser/grammar.md index b2371d2890..ecaf5bfe18 100644 --- a/internal/parser/grammar.md +++ b/internal/parser/grammar.md @@ -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 } . diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 1d5c4a9016..db13210756 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -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 diff --git a/internal/parser/parser_test.go b/internal/parser/parser_test.go index 3296405e8a..02669979df 100644 --- a/internal/parser/parser_test.go +++ b/internal/parser/parser_test.go @@ -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