From fc8e769d71e0e5977522b3842690236453e6bab5 Mon Sep 17 00:00:00 2001 From: Harkishen Singh Date: Wed, 9 Sep 2020 15:40:02 +0530 Subject: [PATCH] Use ASSIGN when using = inside braces (#7911) * Fix EQL when using = inside braces. Signed-off-by: Harkishen-Singh * EQL => EQLC and ASSIGN => EQL Signed-off-by: Harkishen-Singh * Aligned yacc code. Signed-off-by: Harkishen-Singh --- promql/engine.go | 4 +- promql/parser/generated_parser.y | 8 +-- promql/parser/generated_parser.y.go | 76 ++++++++++++++--------------- promql/parser/lex.go | 10 ++-- promql/parser/lex_test.go | 7 +-- promql/parser/parse_test.go | 10 ++-- 6 files changed, 58 insertions(+), 57 deletions(-) diff --git a/promql/engine.go b/promql/engine.go index a624a2b81..c21319b41 100644 --- a/promql/engine.go +++ b/promql/engine.go @@ -1811,7 +1811,7 @@ func scalarBinop(op parser.ItemType, lhs, rhs float64) float64 { return math.Pow(lhs, rhs) case parser.MOD: return math.Mod(lhs, rhs) - case parser.EQL: + case parser.EQLC: return btos(lhs == rhs) case parser.NEQ: return btos(lhs != rhs) @@ -1842,7 +1842,7 @@ func vectorElemBinop(op parser.ItemType, lhs, rhs float64) (float64, bool) { return math.Pow(lhs, rhs), true case parser.MOD: return math.Mod(lhs, rhs), true - case parser.EQL: + case parser.EQLC: return lhs, lhs == rhs case parser.NEQ: return lhs, lhs != rhs diff --git a/promql/parser/generated_parser.y b/promql/parser/generated_parser.y index 501545b98..f0bdc320f 100644 --- a/promql/parser/generated_parser.y +++ b/promql/parser/generated_parser.y @@ -41,7 +41,7 @@ import ( %token -ASSIGN +EQL BLANK COLON COMMA @@ -68,7 +68,7 @@ TIMES %token ADD DIV -EQL +EQLC EQL_REGEX GTE GTR @@ -146,7 +146,7 @@ START_METRIC_SELECTOR // Operators are listed with increasing precedence. %left LOR %left LAND LUNLESS -%left EQL GTE GTR LSS LTE NEQ +%left EQLC GTE GTR LSS LTE NEQ %left ADD SUB %left MUL DIV MOD %right POW @@ -229,7 +229,7 @@ aggregate_modifier: // Operator precedence only works if each of those is listed separately. binary_expr : expr ADD bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) } | expr DIV bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) } - | expr EQL bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) } + | expr EQLC bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) } | expr GTE bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) } | expr GTR bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) } | expr LAND bin_modifier expr { $$ = yylex.(*parser).newBinaryExpression($1, $2, $3, $4) } diff --git a/promql/parser/generated_parser.y.go b/promql/parser/generated_parser.y.go index 1acb4dc6b..cf26fcc47 100644 --- a/promql/parser/generated_parser.y.go +++ b/promql/parser/generated_parser.y.go @@ -33,7 +33,7 @@ type yySymType struct { duration time.Duration } -const ASSIGN = 57346 +const EQL = 57346 const BLANK = 57347 const COLON = 57348 const COMMA = 57349 @@ -57,7 +57,7 @@ const TIMES = 57366 const operatorsStart = 57367 const ADD = 57368 const DIV = 57369 -const EQL = 57370 +const EQLC = 57370 const EQL_REGEX = 57371 const GTE = 57372 const GTR = 57373 @@ -108,7 +108,7 @@ var yyToknames = [...]string{ "$end", "error", "$unk", - "ASSIGN", + "EQL", "BLANK", "COLON", "COMMA", @@ -132,7 +132,7 @@ var yyToknames = [...]string{ "operatorsStart", "ADD", "DIV", - "EQL", + "EQLC", "EQL_REGEX", "GTE", "GTR", @@ -340,32 +340,32 @@ var yyAct = [...]int{ 247, 197, 35, 136, 237, 238, 168, 169, 108, 74, 97, 96, 99, 174, 121, 175, 98, 250, 100, 176, 177, 230, 95, 54, 231, 229, 171, 48, 69, 101, - 50, 22, 49, 163, 245, 148, 251, 248, 51, 244, - 116, 67, 172, 6, 170, 101, 228, 18, 19, 92, - 115, 20, 243, 103, 162, 104, 69, 68, 117, 102, + 50, 22, 49, 163, 245, 148, 251, 225, 51, 244, + 116, 67, 172, 6, 248, 170, 228, 18, 19, 92, + 224, 20, 243, 103, 162, 104, 69, 68, 117, 102, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, - 65, 66, 94, 95, 99, 13, 114, 105, 31, 24, + 65, 66, 94, 95, 99, 13, 101, 105, 31, 24, 100, 30, 7, 252, 8, 79, 80, 81, 33, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 139, - 92, 93, 225, 145, 149, 143, 146, 141, 110, 142, - 2, 3, 4, 5, 242, 224, 144, 78, 109, 32, + 92, 93, 145, 78, 149, 143, 146, 141, 110, 142, + 2, 3, 4, 5, 242, 144, 32, 115, 109, 114, 173, 138, 161, 94, 226, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 153, 46, 140, 10, 137, 152, 1, - 70, 227, 138, 155, 138, 240, 71, 45, 44, 151, + 70, 227, 138, 155, 138, 156, 71, 240, 45, 151, 34, 95, 48, 69, 232, 50, 22, 49, 233, 234, - 235, 236, 239, 51, 80, 43, 67, 194, 42, 156, - 158, 193, 18, 19, 89, 90, 20, 241, 92, 120, - 157, 159, 68, 41, 192, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 65, 66, 40, 165, 76, - 13, 94, 39, 118, 24, 167, 30, 147, 246, 75, - 170, 38, 249, 48, 69, 160, 50, 22, 49, 171, - 113, 119, 110, 37, 51, 112, 254, 67, 36, 76, - 166, 255, 109, 18, 19, 172, 111, 20, 107, 75, - 77, 164, 195, 68, 72, 73, 55, 56, 57, 58, - 59, 60, 61, 62, 63, 64, 65, 66, 199, 53, - 52, 13, 9, 9, 198, 24, 154, 30, 209, 47, + 235, 236, 239, 51, 80, 53, 67, 194, 9, 9, + 158, 193, 18, 19, 89, 90, 20, 241, 92, 44, + 157, 159, 68, 43, 192, 55, 56, 57, 58, 59, + 60, 61, 62, 63, 64, 65, 66, 42, 165, 76, + 13, 94, 120, 41, 24, 167, 30, 40, 246, 75, + 170, 39, 249, 48, 69, 160, 50, 22, 49, 171, + 113, 118, 110, 147, 51, 112, 254, 67, 38, 76, + 119, 255, 109, 18, 19, 172, 111, 20, 107, 75, + 37, 36, 166, 68, 77, 73, 55, 56, 57, 58, + 59, 60, 61, 62, 63, 64, 65, 66, 199, 164, + 195, 13, 72, 52, 198, 24, 154, 30, 209, 47, 106, 0, 215, 0, 0, 0, 253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 211, 212, 0, 0, 213, 0, 0, 0, 0, 0, 0, 0, @@ -403,37 +403,37 @@ var yyPact = [...]int{ 41, 72, 410, 410, 160, 385, -1000, -1000, -1000, 65, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, - -1000, 247, -1000, 115, -1000, 59, -1000, -1000, -1000, -1000, - -1000, -1000, -1000, -1000, -1000, -1000, -1000, 14, 30, -1000, + -1000, 247, -1000, 101, -1000, 59, -1000, -1000, -1000, -1000, + -1000, -1000, -1000, -1000, -1000, -1000, -1000, 14, 61, -1000, 221, -1000, 221, 43, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 240, - -1000, -1000, 238, -1000, -1000, 48, -1000, 18, -1000, -45, + -1000, -1000, 238, -1000, -1000, 115, -1000, 18, -1000, -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, -45, - -45, -45, -45, -45, 155, 153, 30, -48, -1000, 101, - 101, 15, -1000, 453, 8, -1000, 151, -1000, -1000, 161, + -45, -45, -45, -45, 155, 153, 61, -48, -1000, 100, + 100, 15, -1000, 453, 8, -1000, 151, -1000, -1000, 161, -1000, -1000, 217, -1000, 31, -1000, 213, 221, -1000, -50, -42, -1000, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, 221, -1000, -1000, -1000, 185, - -1000, -1000, -1000, -1000, 331, -1000, -1000, 95, -1000, 59, + -1000, -1000, -1000, -1000, 331, -1000, -1000, 30, -1000, 59, -1000, -1000, 106, -1000, 23, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -3, 0, -1000, -1000, -1000, - -1000, 27, 27, 157, 101, 101, 101, 101, 8, 533, + -1000, 28, 28, 157, 100, 100, 100, 100, 8, 533, 533, 533, 515, 484, 533, 533, 515, 8, 8, 533, 8, 157, -1000, 112, -1000, 32, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 221, -1000, -1000, -1000, -1000, - 20, 20, -7, -1000, -1000, -1000, -1000, -1000, -1000, -1000, + 27, 27, -7, -1000, -1000, -1000, -1000, -1000, -1000, -1000, 17, 81, -1000, -1000, 276, -1000, 59, -1000, -1000, -1000, - 20, -1000, -1000, -1000, -1000, -1000, + 27, -1000, -1000, -1000, -1000, -1000, } var yyPgo = [...]int{ - 0, 290, 8, 289, 1, 286, 284, 279, 280, 156, - 264, 84, 9, 262, 5, 4, 261, 260, 0, 6, - 250, 7, 248, 11, 58, 243, 241, 2, 231, 227, - 10, 223, 23, 222, 217, 203, 199, 188, 185, 168, - 167, 154, 3, 165, 159, 119, + 0, 290, 8, 289, 1, 286, 284, 185, 283, 156, + 282, 84, 9, 280, 5, 4, 279, 264, 0, 6, + 262, 7, 261, 11, 58, 260, 250, 2, 248, 243, + 10, 241, 23, 231, 227, 223, 222, 217, 203, 199, + 168, 154, 3, 167, 159, 116, } var yyR1 = [...]int{ @@ -492,11 +492,11 @@ var yyChk = [...]int{ 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 64, 14, -23, -30, 2, 60, 66, 15, -30, -27, -27, -32, -1, 18, -2, 12, - 2, 18, 7, 2, 28, 2, 22, -24, -31, -26, + 2, 18, 7, 2, 4, 2, 22, -24, -31, -26, -36, 59, -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, -24, -42, 2, 9, -42, 2, -30, -23, -14, 15, 2, -14, -29, 20, -27, - 20, 18, 7, 2, -5, 2, 28, 39, 29, 40, + 20, 18, 7, 2, -5, 2, 4, 39, 29, 40, 18, -12, 23, 2, -16, 5, -20, 12, -19, -21, 17, 26, 42, -27, 63, 65, 61, 62, -27, -27, -27, -27, -27, -27, -27, -27, -27, -27, -27, -27, diff --git a/promql/parser/lex.go b/promql/parser/lex.go index 42075158c..ada5d70d1 100644 --- a/promql/parser/lex.go +++ b/promql/parser/lex.go @@ -70,7 +70,7 @@ func (i ItemType) IsKeyword() bool { return i > keywordsStart && i < keywordsEnd // Returns false otherwise. func (i ItemType) IsComparisonOperator() bool { switch i { - case EQL, NEQ, LTE, LSS, GTE, GTR: + case EQLC, NEQ, LTE, LSS, GTE, GTR: return true default: return false @@ -133,7 +133,7 @@ var ItemTypeStr = map[ItemType]string{ LEFT_BRACKET: "[", RIGHT_BRACKET: "]", COMMA: ",", - ASSIGN: "=", + EQL: "=", COLON: ":", SEMICOLON: ";", BLANK: "_", @@ -145,7 +145,7 @@ var ItemTypeStr = map[ItemType]string{ MUL: "*", MOD: "%", DIV: "/", - EQL: "==", + EQLC: "==", NEQ: "!=", LTE: "<=", LSS: "<", @@ -363,11 +363,11 @@ func lexStatements(l *Lexer) stateFn { case r == '=': if t := l.peek(); t == '=' { l.next() - l.emit(EQL) + l.emit(EQLC) } else if t == '~' { return l.errorf("unexpected character after '=': %q", t) } else { - l.emit(ASSIGN) + l.emit(EQL) } case r == '!': if t := l.next(); t == '=' { diff --git a/promql/parser/lex_test.go b/promql/parser/lex_test.go index c46f92e60..9e95f7ae6 100644 --- a/promql/parser/lex_test.go +++ b/promql/parser/lex_test.go @@ -225,14 +225,15 @@ var tests = []struct { tests: []testCase{ { input: `=`, - expected: []Item{{ASSIGN, 0, `=`}}, + expected: []Item{{EQL, 0, `=`}}, }, { - // Inside braces equality is a single '=' character. + // Inside braces equality is a single '=' character but in terms of a token + // it should be treated as ASSIGN. input: `{=}`, expected: []Item{{LEFT_BRACE, 0, `{`}, {EQL, 1, `=`}, {RIGHT_BRACE, 2, `}`}}, }, { input: `==`, - expected: []Item{{EQL, 0, `==`}}, + expected: []Item{{EQLC, 0, `==`}}, }, { input: `!=`, expected: []Item{{NEQ, 0, `!=`}}, diff --git a/promql/parser/parse_test.go b/promql/parser/parse_test.go index f1140f833..717ecdc00 100644 --- a/promql/parser/parse_test.go +++ b/promql/parser/parse_test.go @@ -173,7 +173,7 @@ var testExpr = []struct { }, { input: "1 == bool 1", expected: &BinaryExpr{ - Op: EQL, + Op: EQLC, LHS: &NumberLiteral{ Val: 1, PosRange: PositionRange{Start: 0, End: 1}, @@ -593,7 +593,7 @@ var testExpr = []struct { }, { input: "foo == 1", expected: &BinaryExpr{ - Op: EQL, + Op: EQLC, LHS: &VectorSelector{ Name: "foo", LabelMatchers: []*labels.Matcher{ @@ -612,7 +612,7 @@ var testExpr = []struct { }, { input: "foo == bool 1", expected: &BinaryExpr{ - Op: EQL, + Op: EQLC, LHS: &VectorSelector{ Name: "foo", LabelMatchers: []*labels.Matcher{ @@ -1538,7 +1538,7 @@ var testExpr = []struct { }, { input: `foo{__name__= =}`, fail: true, - errMsg: "unexpected in label matching, expected string", + errMsg: `1:15: parse error: unexpected "=" in label matching, expected string`, }, { input: `foo{,}`, fail: true, @@ -1546,7 +1546,7 @@ var testExpr = []struct { }, { input: `foo{__name__ == "bar"}`, fail: true, - errMsg: "unexpected in label matching, expected string", + errMsg: `1:15: parse error: unexpected "=" in label matching, expected string`, }, { input: `foo{__name__="bar" lol}`, fail: true,