Fix unary +/- expressions.

Unary expressions cause parsing errors if they are done in the lexer
by tokenizing them into the number.
This fix moves unary expressions to the parser.
This commit is contained in:
Fabian Reinartz 2015-03-03 12:16:46 +01:00
parent 0b5dd24fd2
commit 182de6b99f
6 changed files with 947 additions and 936 deletions

View File

@ -50,6 +50,15 @@ func CreateAlertingRule(name string, expr ast.Node, holdDurationStr string, labe
return NewAlertingRule(name, expr.(ast.VectorNode), holdDuration, labels, summary, description), nil
}
// NewScalarLiteral returns a ScalarLiteral with the given value. If sign is "-"
// the value is negated.
func NewScalarLiteral(value clientmodel.SampleValue, sign string) *ast.ScalarLiteral {
if sign == "-" {
value = -value
}
return ast.NewScalarLiteral(value)
}
// NewFunctionCall is a convenience function to create a new AST function-call node.
func NewFunctionCall(name string, args []ast.Node) (ast.Node, error) {
function, err := ast.GetFunction(name)

View File

@ -55,7 +55,7 @@ L [a-zA-Z_]
M [a-zA-Z_:]
U [smhdwy]
FLOAT [-+]?({D}*\.?{D}+|{D}+\.?{D}*){EXPONENT}?|[+-]?[iI][nN][fF]|[nN][aA][nN]
FLOAT ({D}*\.?{D}+|{D}+\.?{D}*){EXPONENT}?|[+-]?[iI][nN][fF]|[nN][aA][nN]
EXPONENT [eE][-+]?[0-9]+
STR \"(\\.|[^\\"])*\"|\'(\\.|[^\\'])*\'

File diff suppressed because it is too large Load Diff

View File

@ -228,7 +228,9 @@ rule_expr : '(' rule_expr ')'
if err != nil { yylex.Error(err.Error()); return 1 }
}
| NUMBER
{ $$ = ast.NewScalarLiteral($1)}
{ $$ = NewScalarLiteral($1, "+")}
| ADDITIVE_OP NUMBER
{ $$ = NewScalarLiteral($2, $1)}
;
extra_labels_opts : /* empty */

View File

@ -82,7 +82,7 @@ const yyEofCode = 1
const yyErrCode = 2
const yyMaxDepth = 200
//line parser.y:279
//line parser.y:281
//line yacctab:1
var yyExca = []int{
@ -94,53 +94,53 @@ var yyExca = []int{
-2, 10,
}
const yyNprod = 55
const yyNprod = 56
const yyPrivate = 57344
var yyTokenNames []string
var yyStates []string
const yyLast = 155
const yyLast = 159
var yyAct = []int{
76, 59, 81, 56, 53, 52, 30, 46, 6, 24,
10, 54, 22, 13, 12, 21, 19, 20, 19, 20,
11, 62, 21, 19, 20, 20, 18, 90, 96, 111,
99, 18, 8, 18, 55, 7, 51, 60, 18, 18,
90, 63, 97, 65, 66, 21, 19, 20, 107, 75,
68, 67, 10, 54, 64, 13, 12, 21, 19, 20,
74, 18, 11, 31, 58, 85, 83, 90, 94, 89,
84, 27, 40, 18, 8, 28, 23, 7, 112, 86,
88, 87, 29, 91, 21, 19, 20, 82, 73, 10,
72, 98, 13, 12, 92, 93, 101, 71, 41, 11,
18, 42, 41, 25, 49, 106, 45, 78, 109, 108,
80, 8, 103, 36, 7, 61, 44, 17, 105, 37,
9, 47, 57, 31, 104, 33, 48, 16, 13, 70,
35, 113, 110, 102, 38, 39, 32, 69, 77, 82,
100, 25, 34, 2, 3, 14, 5, 4, 1, 43,
95, 15, 26, 79, 50,
78, 61, 83, 58, 55, 54, 31, 48, 6, 25,
20, 21, 23, 21, 10, 56, 64, 14, 12, 10,
56, 19, 14, 12, 11, 19, 13, 19, 92, 11,
113, 13, 22, 20, 21, 57, 8, 32, 109, 7,
53, 8, 77, 65, 7, 67, 68, 101, 19, 22,
20, 21, 70, 69, 10, 98, 30, 14, 12, 22,
20, 21, 94, 95, 11, 19, 13, 87, 85, 92,
96, 99, 86, 84, 76, 19, 8, 66, 60, 7,
29, 88, 90, 89, 24, 93, 22, 20, 21, 22,
20, 21, 92, 100, 91, 75, 82, 74, 103, 73,
43, 42, 19, 44, 43, 19, 26, 108, 62, 47,
111, 28, 80, 51, 114, 110, 38, 105, 63, 46,
18, 107, 39, 9, 49, 59, 32, 33, 35, 50,
17, 14, 106, 72, 37, 115, 112, 104, 40, 41,
34, 71, 79, 84, 102, 26, 36, 2, 3, 15,
5, 4, 1, 45, 97, 16, 27, 81, 52,
}
var yyPact = []int{
139, -1000, -1000, 83, 106, -1000, 67, 83, 135, 43,
44, 51, -1000, -1000, -1000, 119, 136, -1000, 122, 104,
104, 104, 40, 72, -1000, 89, 107, 97, 4, 83,
109, 33, 9, -1000, 93, -13, 83, 23, 83, 83,
-1000, 135, 107, 130, -1000, -1000, -1000, 121, -1000, 68,
58, -1000, -1000, 67, -1000, 28, 18, -1000, 132, 80,
81, 83, 107, 6, 132, -7, 0, -1000, -1000, -1000,
-1000, -1000, -1000, 46, 111, 83, 37, -1000, 83, 65,
-1000, -1000, 41, 5, -1000, 10, -1000, 109, -2, -1000,
134, 67, -1000, 133, 126, 88, 116, 98, -1000, -1000,
-1000, -1000, -1000, 9, -1000, 17, 84, 132, 125, -3,
52, -1000, 124, -1000,
143, -1000, -1000, 48, 109, -1000, 72, 48, 139, 83,
49, 25, -1000, 117, -1000, -1000, 122, 140, -1000, 126,
107, 107, 107, 69, 74, -1000, 92, 110, 100, 8,
48, 112, 47, -1000, 80, -1000, 96, -18, 48, 46,
48, 48, -1000, 139, 110, 134, -1000, -1000, -1000, 125,
-1000, 70, 65, -1000, -1000, 72, -1000, 42, 11, -1000,
136, 85, 67, 48, 110, -6, 136, -12, -8, -1000,
-1000, -1000, -1000, -1000, -1000, 13, 114, 48, 62, -1000,
48, 33, -1000, -1000, 43, 32, -1000, 39, -1000, 112,
15, -1000, 138, 72, -1000, 137, 130, 93, 124, 101,
-1000, -1000, -1000, -1000, -1000, 80, -1000, 7, 90, 136,
129, -2, 88, -1000, 128, -1000,
}
var yyPgo = []int{
0, 154, 0, 6, 2, 153, 1, 9, 76, 152,
113, 4, 5, 151, 3, 150, 120, 149, 7, 148,
147, 146, 145,
0, 158, 0, 6, 2, 157, 1, 9, 84, 156,
116, 4, 5, 155, 3, 154, 123, 153, 7, 152,
151, 150, 149,
}
var yyR1 = []int{
@ -148,8 +148,8 @@ var yyR1 = []int{
13, 13, 16, 16, 6, 6, 6, 5, 5, 4,
9, 9, 9, 8, 8, 7, 17, 17, 18, 18,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 14, 14, 10, 10, 10, 3, 3, 2,
2, 1, 1, 12, 12,
11, 11, 11, 14, 14, 10, 10, 10, 3, 3,
2, 2, 1, 1, 12, 12,
}
var yyR2 = []int{
@ -157,38 +157,38 @@ var yyR2 = []int{
0, 1, 1, 1, 0, 3, 2, 1, 3, 3,
0, 2, 3, 1, 3, 3, 1, 1, 0, 2,
3, 4, 3, 4, 3, 5, 6, 6, 4, 4,
4, 1, 0, 1, 0, 4, 8, 0, 4, 1,
3, 1, 3, 1, 1,
4, 1, 2, 0, 1, 0, 4, 8, 0, 4,
1, 3, 1, 3, 1, 1,
}
var yyChk = []int{
-1000, -19, 4, 5, -20, -21, -11, 31, 28, -16,
6, 16, 10, 9, -22, -13, 21, 11, 33, 18,
19, 17, -11, -8, -7, 6, -9, 28, 31, 31,
-3, 12, -16, 6, 6, 8, -10, 15, -10, -10,
32, 30, 29, -17, 27, 17, -18, 14, 29, -8,
-1, 32, -12, -11, 7, -11, -14, 13, 31, -6,
28, 22, 34, -11, 31, -11, -11, -7, -18, 7,
8, 29, 32, 30, 32, 31, -2, 6, 27, -5,
29, -4, 6, -11, -18, -2, -12, -3, -11, 32,
30, -11, 29, 30, 27, -15, 23, 32, -14, 32,
6, -4, 7, 24, 8, 20, -6, 31, 25, -2,
7, 32, 26, 7,
6, 16, 10, 18, 9, -22, -13, 21, 11, 33,
18, 19, 17, -11, -8, -7, 6, -9, 28, 31,
31, -3, 12, 10, -16, 6, 6, 8, -10, 15,
-10, -10, 32, 30, 29, -17, 27, 17, -18, 14,
29, -8, -1, 32, -12, -11, 7, -11, -14, 13,
31, -6, 28, 22, 34, -11, 31, -11, -11, -7,
-18, 7, 8, 29, 32, 30, 32, 31, -2, 6,
27, -5, 29, -4, 6, -11, -18, -2, -12, -3,
-11, 32, 30, -11, 29, 30, 27, -15, 23, 32,
-14, 32, 6, -4, 7, 24, 8, 20, -6, 31,
25, -2, 7, 32, 26, 7,
}
var yyDef = []int{
0, -2, 3, 0, -2, 2, 5, 0, 0, 20,
13, 47, 41, 12, 4, 0, 0, 11, 0, 44,
44, 44, 0, 0, 23, 0, 28, 0, 0, 0,
42, 0, 14, 13, 0, 0, 0, 0, 0, 0,
30, 0, 28, 0, 26, 27, 32, 0, 21, 0,
0, 34, 51, 53, 54, 0, 0, 43, 0, 0,
0, 0, 28, 38, 0, 39, 40, 24, 31, 25,
29, 22, 33, 0, 47, 0, 0, 49, 0, 0,
16, 17, 0, 8, 35, 0, 52, 42, 0, 48,
0, 6, 15, 0, 0, 0, 0, 45, 36, 37,
50, 18, 19, 14, 9, 0, 0, 0, 0, 0,
0, 46, 0, 7,
13, 48, 41, 0, 12, 4, 0, 0, 11, 0,
45, 45, 45, 0, 0, 23, 0, 28, 0, 0,
0, 43, 0, 42, 14, 13, 0, 0, 0, 0,
0, 0, 30, 0, 28, 0, 26, 27, 32, 0,
21, 0, 0, 34, 52, 54, 55, 0, 0, 44,
0, 0, 0, 0, 28, 38, 0, 39, 40, 24,
31, 25, 29, 22, 33, 0, 48, 0, 0, 50,
0, 0, 16, 17, 0, 8, 35, 0, 53, 43,
0, 49, 0, 6, 15, 0, 0, 0, 0, 46,
36, 37, 51, 18, 19, 14, 9, 0, 0, 0,
0, 0, 0, 47, 0, 7,
}
var yyTok1 = []int{
@ -697,25 +697,30 @@ yydefault:
case 41:
//line parser.y:231
{
yyVAL.ruleNode = ast.NewScalarLiteral(yyS[yypt-0].num)
yyVAL.ruleNode = NewScalarLiteral(yyS[yypt-0].num, "+")
}
case 42:
//line parser.y:235
//line parser.y:233
{
yyVAL.boolean = false
yyVAL.ruleNode = NewScalarLiteral(yyS[yypt-0].num, yyS[yypt-1].str)
}
case 43:
//line parser.y:237
{
yyVAL.boolean = true
yyVAL.boolean = false
}
case 44:
//line parser.y:241
//line parser.y:239
{
yyVAL.vectorMatching = nil
yyVAL.boolean = true
}
case 45:
//line parser.y:243
{
yyVAL.vectorMatching = nil
}
case 46:
//line parser.y:245
{
var err error
yyVAL.vectorMatching, err = newVectorMatching("", yyS[yypt-1].labelNameSlice, nil)
@ -724,8 +729,8 @@ yydefault:
return 1
}
}
case 46:
//line parser.y:249
case 47:
//line parser.y:251
{
var err error
yyVAL.vectorMatching, err = newVectorMatching(yyS[yypt-3].str, yyS[yypt-5].labelNameSlice, yyS[yypt-1].labelNameSlice)
@ -734,43 +739,43 @@ yydefault:
return 1
}
}
case 47:
//line parser.y:257
{
yyVAL.labelNameSlice = clientmodel.LabelNames{}
}
case 48:
//line parser.y:259
{
yyVAL.labelNameSlice = yyS[yypt-1].labelNameSlice
yyVAL.labelNameSlice = clientmodel.LabelNames{}
}
case 49:
//line parser.y:263
//line parser.y:261
{
yyVAL.labelNameSlice = clientmodel.LabelNames{clientmodel.LabelName(yyS[yypt-0].str)}
yyVAL.labelNameSlice = yyS[yypt-1].labelNameSlice
}
case 50:
//line parser.y:265
{
yyVAL.labelNameSlice = append(yyVAL.labelNameSlice, clientmodel.LabelName(yyS[yypt-0].str))
yyVAL.labelNameSlice = clientmodel.LabelNames{clientmodel.LabelName(yyS[yypt-0].str)}
}
case 51:
//line parser.y:269
//line parser.y:267
{
yyVAL.ruleNodeSlice = []ast.Node{yyS[yypt-0].ruleNode}
yyVAL.labelNameSlice = append(yyVAL.labelNameSlice, clientmodel.LabelName(yyS[yypt-0].str))
}
case 52:
//line parser.y:271
{
yyVAL.ruleNodeSlice = append(yyVAL.ruleNodeSlice, yyS[yypt-0].ruleNode)
yyVAL.ruleNodeSlice = []ast.Node{yyS[yypt-0].ruleNode}
}
case 53:
//line parser.y:275
//line parser.y:273
{
yyVAL.ruleNode = yyS[yypt-0].ruleNode
yyVAL.ruleNodeSlice = append(yyVAL.ruleNodeSlice, yyS[yypt-0].ruleNode)
}
case 54:
//line parser.y:277
{
yyVAL.ruleNode = yyS[yypt-0].ruleNode
}
case 55:
//line parser.y:279
{
yyVAL.ruleNode = ast.NewStringLiteral(yyS[yypt-0].str)
}

View File

@ -1105,6 +1105,18 @@ func TestExpressions(t *testing.T) {
expr: `12.34e-6`,
output: []string{`scalar: 0.00001234 @[%v]`},
},
{
expr: `1+1`,
output: []string{`scalar: 2 @[%v]`},
},
{
expr: `1-1`,
output: []string{`scalar: 0 @[%v]`},
},
{
expr: `1 - -1`,
output: []string{`scalar: 2 @[%v]`},
},
{
expr: `.2`,
output: []string{`scalar: 0.2 @[%v]`},