promql: Use capitalized names for item types (#6371)

For yacc generated parsers there is the convention to capitalize the names of item types provided by the lexer, which makes it easy to distinct lexer tokens (capitalized) from nonterminal symbols (not capitalized) in language grammars.

This convention is also followed by the (non generated) go compiler (see https://golang.org/pkg/go/token/#Token).

Part of the parser rewrite described in #6256.

Signed-off-by: Tobias Guggenmos <tguggenm@redhat.com>
This commit is contained in:
Tobias Guggenmos 2019-11-26 13:29:42 +00:00 committed by Brian Brazil
parent c229ed17e2
commit bbd92b85da
5 changed files with 628 additions and 628 deletions

View File

@ -1070,7 +1070,7 @@ func (ev *evaluator) eval(expr Expr) Value {
case *UnaryExpr: case *UnaryExpr:
mat := ev.eval(e.Expr).(Matrix) mat := ev.eval(e.Expr).(Matrix)
if e.Op == ItemSUB { if e.Op == SUB {
for i := range mat { for i := range mat {
mat[i].Metric = dropMetricName(mat[i].Metric) mat[i].Metric = dropMetricName(mat[i].Metric)
for j := range mat[i].Points { for j := range mat[i].Points {
@ -1092,15 +1092,15 @@ func (ev *evaluator) eval(expr Expr) Value {
}, e.LHS, e.RHS) }, e.LHS, e.RHS)
case lt == ValueTypeVector && rt == ValueTypeVector: case lt == ValueTypeVector && rt == ValueTypeVector:
switch e.Op { switch e.Op {
case ItemLAND: case LAND:
return ev.rangeEval(func(v []Value, enh *EvalNodeHelper) Vector { return ev.rangeEval(func(v []Value, enh *EvalNodeHelper) Vector {
return ev.VectorAnd(v[0].(Vector), v[1].(Vector), e.VectorMatching, enh) return ev.VectorAnd(v[0].(Vector), v[1].(Vector), e.VectorMatching, enh)
}, e.LHS, e.RHS) }, e.LHS, e.RHS)
case ItemLOR: case LOR:
return ev.rangeEval(func(v []Value, enh *EvalNodeHelper) Vector { return ev.rangeEval(func(v []Value, enh *EvalNodeHelper) Vector {
return ev.VectorOr(v[0].(Vector), v[1].(Vector), e.VectorMatching, enh) return ev.VectorOr(v[0].(Vector), v[1].(Vector), e.VectorMatching, enh)
}, e.LHS, e.RHS) }, e.LHS, e.RHS)
case ItemLUnless: case LUNLESS:
return ev.rangeEval(func(v []Value, enh *EvalNodeHelper) Vector { return ev.rangeEval(func(v []Value, enh *EvalNodeHelper) Vector {
return ev.VectorUnless(v[0].(Vector), v[1].(Vector), e.VectorMatching, enh) return ev.VectorUnless(v[0].(Vector), v[1].(Vector), e.VectorMatching, enh)
}, e.LHS, e.RHS) }, e.LHS, e.RHS)
@ -1644,29 +1644,29 @@ func dropMetricName(l labels.Labels) labels.Labels {
// scalarBinop evaluates a binary operation between two Scalars. // scalarBinop evaluates a binary operation between two Scalars.
func scalarBinop(op ItemType, lhs, rhs float64) float64 { func scalarBinop(op ItemType, lhs, rhs float64) float64 {
switch op { switch op {
case ItemADD: case ADD:
return lhs + rhs return lhs + rhs
case ItemSUB: case SUB:
return lhs - rhs return lhs - rhs
case ItemMUL: case MUL:
return lhs * rhs return lhs * rhs
case ItemDIV: case DIV:
return lhs / rhs return lhs / rhs
case ItemPOW: case POW:
return math.Pow(lhs, rhs) return math.Pow(lhs, rhs)
case ItemMOD: case MOD:
return math.Mod(lhs, rhs) return math.Mod(lhs, rhs)
case ItemEQL: case EQL:
return btos(lhs == rhs) return btos(lhs == rhs)
case ItemNEQ: case NEQ:
return btos(lhs != rhs) return btos(lhs != rhs)
case ItemGTR: case GTR:
return btos(lhs > rhs) return btos(lhs > rhs)
case ItemLSS: case LSS:
return btos(lhs < rhs) return btos(lhs < rhs)
case ItemGTE: case GTE:
return btos(lhs >= rhs) return btos(lhs >= rhs)
case ItemLTE: case LTE:
return btos(lhs <= rhs) return btos(lhs <= rhs)
} }
panic(errors.Errorf("operator %q not allowed for Scalar operations", op)) panic(errors.Errorf("operator %q not allowed for Scalar operations", op))
@ -1675,29 +1675,29 @@ func scalarBinop(op ItemType, lhs, rhs float64) float64 {
// vectorElemBinop evaluates a binary operation between two Vector elements. // vectorElemBinop evaluates a binary operation between two Vector elements.
func vectorElemBinop(op ItemType, lhs, rhs float64) (float64, bool) { func vectorElemBinop(op ItemType, lhs, rhs float64) (float64, bool) {
switch op { switch op {
case ItemADD: case ADD:
return lhs + rhs, true return lhs + rhs, true
case ItemSUB: case SUB:
return lhs - rhs, true return lhs - rhs, true
case ItemMUL: case MUL:
return lhs * rhs, true return lhs * rhs, true
case ItemDIV: case DIV:
return lhs / rhs, true return lhs / rhs, true
case ItemPOW: case POW:
return math.Pow(lhs, rhs), true return math.Pow(lhs, rhs), true
case ItemMOD: case MOD:
return math.Mod(lhs, rhs), true return math.Mod(lhs, rhs), true
case ItemEQL: case EQL:
return lhs, lhs == rhs return lhs, lhs == rhs
case ItemNEQ: case NEQ:
return lhs, lhs != rhs return lhs, lhs != rhs
case ItemGTR: case GTR:
return lhs, lhs > rhs return lhs, lhs > rhs
case ItemLSS: case LSS:
return lhs, lhs < rhs return lhs, lhs < rhs
case ItemGTE: case GTE:
return lhs, lhs >= rhs return lhs, lhs >= rhs
case ItemLTE: case LTE:
return lhs, lhs <= rhs return lhs, lhs <= rhs
} }
panic(errors.Errorf("operator %q not allowed for operations between Vectors", op)) panic(errors.Errorf("operator %q not allowed for operations between Vectors", op))
@ -1717,7 +1717,7 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
result := map[uint64]*groupedAggregation{} result := map[uint64]*groupedAggregation{}
var k int64 var k int64
if op == ItemTopK || op == ItemBottomK { if op == TOPK || op == BOTTOMK {
f := param.(float64) f := param.(float64)
if !convertibleToInt64(f) { if !convertibleToInt64(f) {
ev.errorf("Scalar value %v overflows int64", f) ev.errorf("Scalar value %v overflows int64", f)
@ -1728,11 +1728,11 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
} }
} }
var q float64 var q float64
if op == ItemQuantile { if op == QUANTILE {
q = param.(float64) q = param.(float64)
} }
var valueLabel string var valueLabel string
if op == ItemCountValues { if op == COUNT_VALUES {
valueLabel = param.(string) valueLabel = param.(string)
if !model.LabelName(valueLabel).IsValid() { if !model.LabelName(valueLabel).IsValid() {
ev.errorf("invalid label name %q", valueLabel) ev.errorf("invalid label name %q", valueLabel)
@ -1748,7 +1748,7 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
for _, s := range vec { for _, s := range vec {
metric := s.Metric metric := s.Metric
if op == ItemCountValues { if op == COUNT_VALUES {
lb.Reset(metric) lb.Reset(metric)
lb.Set(valueLabel, strconv.FormatFloat(s.V, 'f', -1, 64)) lb.Set(valueLabel, strconv.FormatFloat(s.V, 'f', -1, 64))
metric = lb.Labels() metric = lb.Labels()
@ -1796,15 +1796,15 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
if k > inputVecLen { if k > inputVecLen {
resultSize = inputVecLen resultSize = inputVecLen
} }
if op == ItemStdvar || op == ItemStddev { if op == STDVAR || op == STDDEV {
result[groupingKey].value = 0.0 result[groupingKey].value = 0.0
} else if op == ItemTopK || op == ItemQuantile { } else if op == TOPK || op == QUANTILE {
result[groupingKey].heap = make(vectorByValueHeap, 0, resultSize) result[groupingKey].heap = make(vectorByValueHeap, 0, resultSize)
heap.Push(&result[groupingKey].heap, &Sample{ heap.Push(&result[groupingKey].heap, &Sample{
Point: Point{V: s.V}, Point: Point{V: s.V},
Metric: s.Metric, Metric: s.Metric,
}) })
} else if op == ItemBottomK { } else if op == BOTTOMK {
result[groupingKey].reverseHeap = make(vectorByReverseValueHeap, 0, resultSize) result[groupingKey].reverseHeap = make(vectorByReverseValueHeap, 0, resultSize)
heap.Push(&result[groupingKey].reverseHeap, &Sample{ heap.Push(&result[groupingKey].reverseHeap, &Sample{
Point: Point{V: s.V}, Point: Point{V: s.V},
@ -1815,33 +1815,33 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
} }
switch op { switch op {
case ItemSum: case SUM:
group.value += s.V group.value += s.V
case ItemAvg: case AVG:
group.groupCount++ group.groupCount++
group.mean += (s.V - group.mean) / float64(group.groupCount) group.mean += (s.V - group.mean) / float64(group.groupCount)
case ItemMax: case MAX:
if group.value < s.V || math.IsNaN(group.value) { if group.value < s.V || math.IsNaN(group.value) {
group.value = s.V group.value = s.V
} }
case ItemMin: case MIN:
if group.value > s.V || math.IsNaN(group.value) { if group.value > s.V || math.IsNaN(group.value) {
group.value = s.V group.value = s.V
} }
case ItemCount, ItemCountValues: case COUNT, COUNT_VALUES:
group.groupCount++ group.groupCount++
case ItemStdvar, ItemStddev: case STDVAR, STDDEV:
group.groupCount++ group.groupCount++
delta := s.V - group.mean delta := s.V - group.mean
group.mean += delta / float64(group.groupCount) group.mean += delta / float64(group.groupCount)
group.value += delta * (s.V - group.mean) group.value += delta * (s.V - group.mean)
case ItemTopK: case TOPK:
if int64(len(group.heap)) < k || group.heap[0].V < s.V || math.IsNaN(group.heap[0].V) { if int64(len(group.heap)) < k || group.heap[0].V < s.V || math.IsNaN(group.heap[0].V) {
if int64(len(group.heap)) == k { if int64(len(group.heap)) == k {
heap.Pop(&group.heap) heap.Pop(&group.heap)
@ -1852,7 +1852,7 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
}) })
} }
case ItemBottomK: case BOTTOMK:
if int64(len(group.reverseHeap)) < k || group.reverseHeap[0].V > s.V || math.IsNaN(group.reverseHeap[0].V) { if int64(len(group.reverseHeap)) < k || group.reverseHeap[0].V > s.V || math.IsNaN(group.reverseHeap[0].V) {
if int64(len(group.reverseHeap)) == k { if int64(len(group.reverseHeap)) == k {
heap.Pop(&group.reverseHeap) heap.Pop(&group.reverseHeap)
@ -1863,7 +1863,7 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
}) })
} }
case ItemQuantile: case QUANTILE:
group.heap = append(group.heap, s) group.heap = append(group.heap, s)
default: default:
@ -1874,19 +1874,19 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
// Construct the result Vector from the aggregated groups. // Construct the result Vector from the aggregated groups.
for _, aggr := range result { for _, aggr := range result {
switch op { switch op {
case ItemAvg: case AVG:
aggr.value = aggr.mean aggr.value = aggr.mean
case ItemCount, ItemCountValues: case COUNT, COUNT_VALUES:
aggr.value = float64(aggr.groupCount) aggr.value = float64(aggr.groupCount)
case ItemStdvar: case STDVAR:
aggr.value = aggr.value / float64(aggr.groupCount) aggr.value = aggr.value / float64(aggr.groupCount)
case ItemStddev: case STDDEV:
aggr.value = math.Sqrt(aggr.value / float64(aggr.groupCount)) aggr.value = math.Sqrt(aggr.value / float64(aggr.groupCount))
case ItemTopK: case TOPK:
// The heap keeps the lowest value on top, so reverse it. // The heap keeps the lowest value on top, so reverse it.
sort.Sort(sort.Reverse(aggr.heap)) sort.Sort(sort.Reverse(aggr.heap))
for _, v := range aggr.heap { for _, v := range aggr.heap {
@ -1897,7 +1897,7 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
} }
continue // Bypass default append. continue // Bypass default append.
case ItemBottomK: case BOTTOMK:
// The heap keeps the lowest value on top, so reverse it. // The heap keeps the lowest value on top, so reverse it.
sort.Sort(sort.Reverse(aggr.reverseHeap)) sort.Sort(sort.Reverse(aggr.reverseHeap))
for _, v := range aggr.reverseHeap { for _, v := range aggr.reverseHeap {
@ -1908,7 +1908,7 @@ func (ev *evaluator) aggregation(op ItemType, grouping []string, without bool, p
} }
continue // Bypass default append. continue // Bypass default append.
case ItemQuantile: case QUANTILE:
aggr.value = quantile(q, aggr.heap) aggr.value = quantile(q, aggr.heap)
default: default:
@ -1935,7 +1935,7 @@ func btos(b bool) float64 {
// result of the op operation. // result of the op operation.
func shouldDropMetricName(op ItemType) bool { func shouldDropMetricName(op ItemType) bool {
switch op { switch op {
case ItemADD, ItemSUB, ItemDIV, ItemMUL, ItemPOW, ItemMOD: case ADD, SUB, DIV, MUL, POW, MOD:
return true return true
default: default:
return false return false

View File

@ -30,11 +30,11 @@ type item struct {
// String returns a descriptive string for the item. // String returns a descriptive string for the item.
func (i item) String() string { func (i item) String() string {
switch { switch {
case i.typ == ItemEOF: case i.typ == EOF:
return "EOF" return "EOF"
case i.typ == ItemError: case i.typ == ERROR:
return i.val return i.val
case i.typ == ItemIdentifier || i.typ == ItemMetricIdentifier: case i.typ == IDENTIFIER || i.typ == METRIC_IDENTIFIER:
return fmt.Sprintf("%q", i.val) return fmt.Sprintf("%q", i.val)
case i.typ.isKeyword(): case i.typ.isKeyword():
return fmt.Sprintf("<%s>", i.val) return fmt.Sprintf("<%s>", i.val)
@ -59,7 +59,7 @@ func (i ItemType) isAggregator() bool { return i > aggregatorsStart && i < aggre
// isAggregator returns true if the item is an aggregator that takes a parameter. // isAggregator returns true if the item is an aggregator that takes a parameter.
// Returns false otherwise // Returns false otherwise
func (i ItemType) isAggregatorWithParam() bool { func (i ItemType) isAggregatorWithParam() bool {
return i == ItemTopK || i == ItemBottomK || i == ItemCountValues || i == ItemQuantile return i == TOPK || i == BOTTOMK || i == COUNT_VALUES || i == QUANTILE
} }
// isKeyword returns true if the item corresponds to a keyword. // isKeyword returns true if the item corresponds to a keyword.
@ -70,7 +70,7 @@ func (i ItemType) isKeyword() bool { return i > keywordsStart && i < keywordsEnd
// Returns false otherwise. // Returns false otherwise.
func (i ItemType) isComparisonOperator() bool { func (i ItemType) isComparisonOperator() bool {
switch i { switch i {
case ItemEQL, ItemNEQ, ItemLTE, ItemLSS, ItemGTE, ItemGTR: case EQL, NEQ, LTE, LSS, GTE, GTR:
return true return true
default: default:
return false return false
@ -80,7 +80,7 @@ func (i ItemType) isComparisonOperator() bool {
// isSetOperator returns whether the item corresponds to a set operator. // isSetOperator returns whether the item corresponds to a set operator.
func (i ItemType) isSetOperator() bool { func (i ItemType) isSetOperator() bool {
switch i { switch i {
case ItemLAND, ItemLOR, ItemLUnless: case LAND, LOR, LUNLESS:
return true return true
} }
return false return false
@ -94,17 +94,17 @@ const LowestPrec = 0 // Non-operators.
// is LowestPrec. // is LowestPrec.
func (i ItemType) precedence() int { func (i ItemType) precedence() int {
switch i { switch i {
case ItemLOR: case LOR:
return 1 return 1
case ItemLAND, ItemLUnless: case LAND, LUNLESS:
return 2 return 2
case ItemEQL, ItemNEQ, ItemLTE, ItemLSS, ItemGTE, ItemGTR: case EQL, NEQ, LTE, LSS, GTE, GTR:
return 3 return 3
case ItemADD, ItemSUB: case ADD, SUB:
return 4 return 4
case ItemMUL, ItemDIV, ItemMOD: case MUL, DIV, MOD:
return 5 return 5
case ItemPOW: case POW:
return 6 return 6
default: default:
return LowestPrec return LowestPrec
@ -113,7 +113,7 @@ func (i ItemType) precedence() int {
func (i ItemType) isRightAssociative() bool { func (i ItemType) isRightAssociative() bool {
switch i { switch i {
case ItemPOW: case POW:
return true return true
default: default:
return false return false
@ -124,138 +124,138 @@ func (i ItemType) isRightAssociative() bool {
type ItemType int type ItemType int
const ( const (
ItemError ItemType = iota // Error occurred, value is error message ERROR ItemType = iota // Error occurred, value is error message
ItemEOF EOF
ItemComment COMMENT
ItemIdentifier IDENTIFIER
ItemMetricIdentifier METRIC_IDENTIFIER
ItemLeftParen LEFT_PAREN
ItemRightParen RIGHT_PAREN
ItemLeftBrace LEFT_BRACE
ItemRightBrace RIGHT_BRACE
ItemLeftBracket LEFT_BRACKET
ItemRightBracket RIGHT_BRACKET
ItemComma COMMA
ItemAssign ASSIGN
ItemColon COLON
ItemSemicolon SEMICOLON
ItemString STRING
ItemNumber NUMBER
ItemDuration DURATION
ItemBlank BLANK
ItemTimes TIMES
ItemSpace SPACE
operatorsStart operatorsStart
// Operators. // Operators.
ItemSUB SUB
ItemADD ADD
ItemMUL MUL
ItemMOD MOD
ItemDIV DIV
ItemLAND LAND
ItemLOR LOR
ItemLUnless LUNLESS
ItemEQL EQL
ItemNEQ NEQ
ItemLTE LTE
ItemLSS LSS
ItemGTE GTE
ItemGTR GTR
ItemEQLRegex EQL_REGEX
ItemNEQRegex NEQ_REGEX
ItemPOW POW
operatorsEnd operatorsEnd
aggregatorsStart aggregatorsStart
// Aggregators. // Aggregators.
ItemAvg AVG
ItemCount COUNT
ItemSum SUM
ItemMin MIN
ItemMax MAX
ItemStddev STDDEV
ItemStdvar STDVAR
ItemTopK TOPK
ItemBottomK BOTTOMK
ItemCountValues COUNT_VALUES
ItemQuantile QUANTILE
aggregatorsEnd aggregatorsEnd
keywordsStart keywordsStart
// Keywords. // Keywords.
ItemOffset OFFSET
ItemBy BY
ItemWithout WITHOUT
ItemOn ON
ItemIgnoring IGNORING
ItemGroupLeft GROUP_LEFT
ItemGroupRight GROUP_RIGHT
ItemBool BOOL
keywordsEnd keywordsEnd
) )
var key = map[string]ItemType{ var key = map[string]ItemType{
// Operators. // Operators.
"and": ItemLAND, "and": LAND,
"or": ItemLOR, "or": LOR,
"unless": ItemLUnless, "unless": LUNLESS,
// Aggregators. // Aggregators.
"sum": ItemSum, "sum": SUM,
"avg": ItemAvg, "avg": AVG,
"count": ItemCount, "count": COUNT,
"min": ItemMin, "min": MIN,
"max": ItemMax, "max": MAX,
"stddev": ItemStddev, "stddev": STDDEV,
"stdvar": ItemStdvar, "stdvar": STDVAR,
"topk": ItemTopK, "topk": TOPK,
"bottomk": ItemBottomK, "bottomk": BOTTOMK,
"count_values": ItemCountValues, "count_values": COUNT_VALUES,
"quantile": ItemQuantile, "quantile": QUANTILE,
// Keywords. // Keywords.
"offset": ItemOffset, "offset": OFFSET,
"by": ItemBy, "by": BY,
"without": ItemWithout, "without": WITHOUT,
"on": ItemOn, "on": ON,
"ignoring": ItemIgnoring, "ignoring": IGNORING,
"group_left": ItemGroupLeft, "group_left": GROUP_LEFT,
"group_right": ItemGroupRight, "group_right": GROUP_RIGHT,
"bool": ItemBool, "bool": BOOL,
} }
// These are the default string representations for common items. It does not // These are the default string representations for common items. It does not
// imply that those are the only character sequences that can be lexed to such an item. // imply that those are the only character sequences that can be lexed to such an item.
var itemTypeStr = map[ItemType]string{ var itemTypeStr = map[ItemType]string{
ItemLeftParen: "(", LEFT_PAREN: "(",
ItemRightParen: ")", RIGHT_PAREN: ")",
ItemLeftBrace: "{", LEFT_BRACE: "{",
ItemRightBrace: "}", RIGHT_BRACE: "}",
ItemLeftBracket: "[", LEFT_BRACKET: "[",
ItemRightBracket: "]", RIGHT_BRACKET: "]",
ItemComma: ",", COMMA: ",",
ItemAssign: "=", ASSIGN: "=",
ItemColon: ":", COLON: ":",
ItemSemicolon: ";", SEMICOLON: ";",
ItemBlank: "_", BLANK: "_",
ItemTimes: "x", TIMES: "x",
ItemSpace: "<space>", SPACE: "<space>",
ItemSUB: "-", SUB: "-",
ItemADD: "+", ADD: "+",
ItemMUL: "*", MUL: "*",
ItemMOD: "%", MOD: "%",
ItemDIV: "/", DIV: "/",
ItemEQL: "==", EQL: "==",
ItemNEQ: "!=", NEQ: "!=",
ItemLTE: "<=", LTE: "<=",
ItemLSS: "<", LSS: "<",
ItemGTE: ">=", GTE: ">=",
ItemGTR: ">", GTR: ">",
ItemEQLRegex: "=~", EQL_REGEX: "=~",
ItemNEQRegex: "!~", NEQ_REGEX: "!~",
ItemPOW: "^", POW: "^",
} }
func init() { func init() {
@ -264,8 +264,8 @@ func init() {
itemTypeStr[ty] = s itemTypeStr[ty] = s
} }
// Special numbers. // Special numbers.
key["inf"] = ItemNumber key["inf"] = NUMBER
key["nan"] = ItemNumber key["nan"] = NUMBER
} }
func (i ItemType) String() string { func (i ItemType) String() string {
@ -279,7 +279,7 @@ func (i item) desc() string {
if _, ok := itemTypeStr[i.typ]; ok { if _, ok := itemTypeStr[i.typ]; ok {
return i.String() return i.String()
} }
if i.typ == ItemEOF { if i.typ == EOF {
return i.typ.desc() return i.typ.desc()
} }
return fmt.Sprintf("%s %s", i.typ.desc(), i) return fmt.Sprintf("%s %s", i.typ.desc(), i)
@ -287,21 +287,21 @@ func (i item) desc() string {
func (i ItemType) desc() string { func (i ItemType) desc() string {
switch i { switch i {
case ItemError: case ERROR:
return "error" return "error"
case ItemEOF: case EOF:
return "end of input" return "end of input"
case ItemComment: case COMMENT:
return "comment" return "comment"
case ItemIdentifier: case IDENTIFIER:
return "identifier" return "identifier"
case ItemMetricIdentifier: case METRIC_IDENTIFIER:
return "metric identifier" return "metric identifier"
case ItemString: case STRING:
return "string" return "string"
case ItemNumber: case NUMBER:
return "number" return "number"
case ItemDuration: case DURATION:
return "duration" return "duration"
} }
return fmt.Sprintf("%q", i) return fmt.Sprintf("%q", i)
@ -408,7 +408,7 @@ func (l *lexer) linePosition() int {
// errorf returns an error token and terminates the scan by passing // errorf returns an error token and terminates the scan by passing
// back a nil pointer that will be the next state, terminating l.nextItem. // back a nil pointer that will be the next state, terminating l.nextItem.
func (l *lexer) errorf(format string, args ...interface{}) stateFn { func (l *lexer) errorf(format string, args ...interface{}) stateFn {
l.items = append(l.items, item{ItemError, l.start, fmt.Sprintf(format, args...)}) l.items = append(l.items, item{ERROR, l.start, fmt.Sprintf(format, args...)})
return nil return nil
} }
@ -418,7 +418,7 @@ func (l *lexer) nextItem() item {
if l.state != nil { if l.state != nil {
l.state = l.state(l) l.state = l.state(l)
} else { } else {
l.emit(ItemEOF) l.emit(EOF)
} }
} }
item := l.items[0] item := l.items[0]
@ -469,52 +469,52 @@ func lexStatements(l *lexer) stateFn {
} else if l.bracketOpen { } else if l.bracketOpen {
return l.errorf("unclosed left bracket") return l.errorf("unclosed left bracket")
} }
l.emit(ItemEOF) l.emit(EOF)
return nil return nil
case r == ',': case r == ',':
l.emit(ItemComma) l.emit(COMMA)
case isSpace(r): case isSpace(r):
return lexSpace return lexSpace
case r == '*': case r == '*':
l.emit(ItemMUL) l.emit(MUL)
case r == '/': case r == '/':
l.emit(ItemDIV) l.emit(DIV)
case r == '%': case r == '%':
l.emit(ItemMOD) l.emit(MOD)
case r == '+': case r == '+':
l.emit(ItemADD) l.emit(ADD)
case r == '-': case r == '-':
l.emit(ItemSUB) l.emit(SUB)
case r == '^': case r == '^':
l.emit(ItemPOW) l.emit(POW)
case r == '=': case r == '=':
if t := l.peek(); t == '=' { if t := l.peek(); t == '=' {
l.next() l.next()
l.emit(ItemEQL) l.emit(EQL)
} else if t == '~' { } else if t == '~' {
return l.errorf("unexpected character after '=': %q", t) return l.errorf("unexpected character after '=': %q", t)
} else { } else {
l.emit(ItemAssign) l.emit(ASSIGN)
} }
case r == '!': case r == '!':
if t := l.next(); t == '=' { if t := l.next(); t == '=' {
l.emit(ItemNEQ) l.emit(NEQ)
} else { } else {
return l.errorf("unexpected character after '!': %q", t) return l.errorf("unexpected character after '!': %q", t)
} }
case r == '<': case r == '<':
if t := l.peek(); t == '=' { if t := l.peek(); t == '=' {
l.next() l.next()
l.emit(ItemLTE) l.emit(LTE)
} else { } else {
l.emit(ItemLSS) l.emit(LSS)
} }
case r == '>': case r == '>':
if t := l.peek(); t == '=' { if t := l.peek(); t == '=' {
l.next() l.next()
l.emit(ItemGTE) l.emit(GTE)
} else { } else {
l.emit(ItemGTR) l.emit(GTR)
} }
case isDigit(r) || (r == '.' && isDigit(l.peek())): case isDigit(r) || (r == '.' && isDigit(l.peek())):
l.backup() l.backup()
@ -533,21 +533,21 @@ func lexStatements(l *lexer) stateFn {
if l.gotColon { if l.gotColon {
return l.errorf("unexpected colon %q", r) return l.errorf("unexpected colon %q", r)
} }
l.emit(ItemColon) l.emit(COLON)
l.gotColon = true l.gotColon = true
case r == '(': case r == '(':
l.emit(ItemLeftParen) l.emit(LEFT_PAREN)
l.parenDepth++ l.parenDepth++
return lexStatements return lexStatements
case r == ')': case r == ')':
l.emit(ItemRightParen) l.emit(RIGHT_PAREN)
l.parenDepth-- l.parenDepth--
if l.parenDepth < 0 { if l.parenDepth < 0 {
return l.errorf("unexpected right parenthesis %q", r) return l.errorf("unexpected right parenthesis %q", r)
} }
return lexStatements return lexStatements
case r == '{': case r == '{':
l.emit(ItemLeftBrace) l.emit(LEFT_BRACE)
l.braceOpen = true l.braceOpen = true
return lexInsideBraces(l) return lexInsideBraces(l)
case r == '[': case r == '[':
@ -555,7 +555,7 @@ func lexStatements(l *lexer) stateFn {
return l.errorf("unexpected left bracket %q", r) return l.errorf("unexpected left bracket %q", r)
} }
l.gotColon = false l.gotColon = false
l.emit(ItemLeftBracket) l.emit(LEFT_BRACKET)
if isSpace(l.peek()) { if isSpace(l.peek()) {
skipSpaces(l) skipSpaces(l)
} }
@ -565,7 +565,7 @@ func lexStatements(l *lexer) stateFn {
if !l.bracketOpen { if !l.bracketOpen {
return l.errorf("unexpected right bracket %q", r) return l.errorf("unexpected right bracket %q", r)
} }
l.emit(ItemRightBracket) l.emit(RIGHT_BRACKET)
l.bracketOpen = false l.bracketOpen = false
default: default:
@ -590,7 +590,7 @@ func lexInsideBraces(l *lexer) stateFn {
l.backup() l.backup()
return lexIdentifier return lexIdentifier
case r == ',': case r == ',':
l.emit(ItemComma) l.emit(COMMA)
case r == '"' || r == '\'': case r == '"' || r == '\'':
l.stringOpen = r l.stringOpen = r
return lexString return lexString
@ -599,24 +599,24 @@ func lexInsideBraces(l *lexer) stateFn {
return lexRawString return lexRawString
case r == '=': case r == '=':
if l.next() == '~' { if l.next() == '~' {
l.emit(ItemEQLRegex) l.emit(EQL_REGEX)
break break
} }
l.backup() l.backup()
l.emit(ItemEQL) l.emit(EQL)
case r == '!': case r == '!':
switch nr := l.next(); { switch nr := l.next(); {
case nr == '~': case nr == '~':
l.emit(ItemNEQRegex) l.emit(NEQ_REGEX)
case nr == '=': case nr == '=':
l.emit(ItemNEQ) l.emit(NEQ)
default: default:
return l.errorf("unexpected character after '!' inside braces: %q", nr) return l.errorf("unexpected character after '!' inside braces: %q", nr)
} }
case r == '{': case r == '{':
return l.errorf("unexpected left brace %q", r) return l.errorf("unexpected left brace %q", r)
case r == '}': case r == '}':
l.emit(ItemRightBrace) l.emit(RIGHT_BRACE)
l.braceOpen = false l.braceOpen = false
if l.seriesDesc { if l.seriesDesc {
@ -635,16 +635,16 @@ func lexValueSequence(l *lexer) stateFn {
case r == eof: case r == eof:
return lexStatements return lexStatements
case isSpace(r): case isSpace(r):
l.emit(ItemSpace) l.emit(SPACE)
lexSpace(l) lexSpace(l)
case r == '+': case r == '+':
l.emit(ItemADD) l.emit(ADD)
case r == '-': case r == '-':
l.emit(ItemSUB) l.emit(SUB)
case r == 'x': case r == 'x':
l.emit(ItemTimes) l.emit(TIMES)
case r == '_': case r == '_':
l.emit(ItemBlank) l.emit(BLANK)
case isDigit(r) || (r == '.' && isDigit(l.peek())): case isDigit(r) || (r == '.' && isDigit(l.peek())):
l.backup() l.backup()
lexNumber(l) lexNumber(l)
@ -747,7 +747,7 @@ Loop:
break Loop break Loop
} }
} }
l.emit(ItemString) l.emit(STRING)
return lexStatements return lexStatements
} }
@ -764,7 +764,7 @@ Loop:
break Loop break Loop
} }
} }
l.emit(ItemString) l.emit(STRING)
return lexStatements return lexStatements
} }
@ -784,7 +784,7 @@ func lexLineComment(l *lexer) stateFn {
r = l.next() r = l.next()
} }
l.backup() l.backup()
l.emit(ItemComment) l.emit(COMMENT)
return lexStatements return lexStatements
} }
@ -798,7 +798,7 @@ func lexDuration(l *lexer) stateFn {
return l.errorf("bad duration syntax: %q", l.input[l.start:l.pos]) return l.errorf("bad duration syntax: %q", l.input[l.start:l.pos])
} }
l.backup() l.backup()
l.emit(ItemDuration) l.emit(DURATION)
return lexStatements return lexStatements
} }
return l.errorf("bad duration syntax: %q", l.input[l.start:l.pos]) return l.errorf("bad duration syntax: %q", l.input[l.start:l.pos])
@ -809,14 +809,14 @@ func lexNumber(l *lexer) stateFn {
if !l.scanNumber() { if !l.scanNumber() {
return l.errorf("bad number syntax: %q", l.input[l.start:l.pos]) return l.errorf("bad number syntax: %q", l.input[l.start:l.pos])
} }
l.emit(ItemNumber) l.emit(NUMBER)
return lexStatements return lexStatements
} }
// lexNumberOrDuration scans a number or a duration item. // lexNumberOrDuration scans a number or a duration item.
func lexNumberOrDuration(l *lexer) stateFn { func lexNumberOrDuration(l *lexer) stateFn {
if l.scanNumber() { if l.scanNumber() {
l.emit(ItemNumber) l.emit(NUMBER)
return lexStatements return lexStatements
} }
// Next two chars must be a valid unit and a non-alphanumeric. // Next two chars must be a valid unit and a non-alphanumeric.
@ -825,7 +825,7 @@ func lexNumberOrDuration(l *lexer) stateFn {
return l.errorf("bad number or duration syntax: %q", l.input[l.start:l.pos]) return l.errorf("bad number or duration syntax: %q", l.input[l.start:l.pos])
} }
l.backup() l.backup()
l.emit(ItemDuration) l.emit(DURATION)
return lexStatements return lexStatements
} }
return l.errorf("bad number or duration syntax: %q", l.input[l.start:l.pos]) return l.errorf("bad number or duration syntax: %q", l.input[l.start:l.pos])
@ -862,7 +862,7 @@ func lexIdentifier(l *lexer) stateFn {
// absorb // absorb
} }
l.backup() l.backup()
l.emit(ItemIdentifier) l.emit(IDENTIFIER)
return lexStatements return lexStatements
} }
@ -881,9 +881,9 @@ Loop:
if kw, ok := key[strings.ToLower(word)]; ok { if kw, ok := key[strings.ToLower(word)]; ok {
l.emit(kw) l.emit(kw)
} else if !strings.Contains(word, ":") { } else if !strings.Contains(word, ":") {
l.emit(ItemIdentifier) l.emit(IDENTIFIER)
} else { } else {
l.emit(ItemMetricIdentifier) l.emit(METRIC_IDENTIFIER)
} }
break Loop break Loop
} }

View File

@ -35,40 +35,40 @@ var tests = []struct {
tests: []testCase{ tests: []testCase{
{ {
input: ",", input: ",",
expected: []item{{ItemComma, 0, ","}}, expected: []item{{COMMA, 0, ","}},
}, { }, {
input: "()", input: "()",
expected: []item{{ItemLeftParen, 0, `(`}, {ItemRightParen, 1, `)`}}, expected: []item{{LEFT_PAREN, 0, `(`}, {RIGHT_PAREN, 1, `)`}},
}, { }, {
input: "{}", input: "{}",
expected: []item{{ItemLeftBrace, 0, `{`}, {ItemRightBrace, 1, `}`}}, expected: []item{{LEFT_BRACE, 0, `{`}, {RIGHT_BRACE, 1, `}`}},
}, { }, {
input: "[5m]", input: "[5m]",
expected: []item{ expected: []item{
{ItemLeftBracket, 0, `[`}, {LEFT_BRACKET, 0, `[`},
{ItemDuration, 1, `5m`}, {DURATION, 1, `5m`},
{ItemRightBracket, 3, `]`}, {RIGHT_BRACKET, 3, `]`},
}, },
}, { }, {
input: "[ 5m]", input: "[ 5m]",
expected: []item{ expected: []item{
{ItemLeftBracket, 0, `[`}, {LEFT_BRACKET, 0, `[`},
{ItemDuration, 2, `5m`}, {DURATION, 2, `5m`},
{ItemRightBracket, 4, `]`}, {RIGHT_BRACKET, 4, `]`},
}, },
}, { }, {
input: "[ 5m]", input: "[ 5m]",
expected: []item{ expected: []item{
{ItemLeftBracket, 0, `[`}, {LEFT_BRACKET, 0, `[`},
{ItemDuration, 3, `5m`}, {DURATION, 3, `5m`},
{ItemRightBracket, 5, `]`}, {RIGHT_BRACKET, 5, `]`},
}, },
}, { }, {
input: "[ 5m ]", input: "[ 5m ]",
expected: []item{ expected: []item{
{ItemLeftBracket, 0, `[`}, {LEFT_BRACKET, 0, `[`},
{ItemDuration, 3, `5m`}, {DURATION, 3, `5m`},
{ItemRightBracket, 6, `]`}, {RIGHT_BRACKET, 6, `]`},
}, },
}, { }, {
input: "\r\n\r", input: "\r\n\r",
@ -81,55 +81,55 @@ var tests = []struct {
tests: []testCase{ tests: []testCase{
{ {
input: "1", input: "1",
expected: []item{{ItemNumber, 0, "1"}}, expected: []item{{NUMBER, 0, "1"}},
}, { }, {
input: "4.23", input: "4.23",
expected: []item{{ItemNumber, 0, "4.23"}}, expected: []item{{NUMBER, 0, "4.23"}},
}, { }, {
input: ".3", input: ".3",
expected: []item{{ItemNumber, 0, ".3"}}, expected: []item{{NUMBER, 0, ".3"}},
}, { }, {
input: "5.", input: "5.",
expected: []item{{ItemNumber, 0, "5."}}, expected: []item{{NUMBER, 0, "5."}},
}, { }, {
input: "NaN", input: "NaN",
expected: []item{{ItemNumber, 0, "NaN"}}, expected: []item{{NUMBER, 0, "NaN"}},
}, { }, {
input: "nAN", input: "nAN",
expected: []item{{ItemNumber, 0, "nAN"}}, expected: []item{{NUMBER, 0, "nAN"}},
}, { }, {
input: "NaN 123", input: "NaN 123",
expected: []item{{ItemNumber, 0, "NaN"}, {ItemNumber, 4, "123"}}, expected: []item{{NUMBER, 0, "NaN"}, {NUMBER, 4, "123"}},
}, { }, {
input: "NaN123", input: "NaN123",
expected: []item{{ItemIdentifier, 0, "NaN123"}}, expected: []item{{IDENTIFIER, 0, "NaN123"}},
}, { }, {
input: "iNf", input: "iNf",
expected: []item{{ItemNumber, 0, "iNf"}}, expected: []item{{NUMBER, 0, "iNf"}},
}, { }, {
input: "Inf", input: "Inf",
expected: []item{{ItemNumber, 0, "Inf"}}, expected: []item{{NUMBER, 0, "Inf"}},
}, { }, {
input: "+Inf", input: "+Inf",
expected: []item{{ItemADD, 0, "+"}, {ItemNumber, 1, "Inf"}}, expected: []item{{ADD, 0, "+"}, {NUMBER, 1, "Inf"}},
}, { }, {
input: "+Inf 123", input: "+Inf 123",
expected: []item{{ItemADD, 0, "+"}, {ItemNumber, 1, "Inf"}, {ItemNumber, 5, "123"}}, expected: []item{{ADD, 0, "+"}, {NUMBER, 1, "Inf"}, {NUMBER, 5, "123"}},
}, { }, {
input: "-Inf", input: "-Inf",
expected: []item{{ItemSUB, 0, "-"}, {ItemNumber, 1, "Inf"}}, expected: []item{{SUB, 0, "-"}, {NUMBER, 1, "Inf"}},
}, { }, {
input: "Infoo", input: "Infoo",
expected: []item{{ItemIdentifier, 0, "Infoo"}}, expected: []item{{IDENTIFIER, 0, "Infoo"}},
}, { }, {
input: "-Infoo", input: "-Infoo",
expected: []item{{ItemSUB, 0, "-"}, {ItemIdentifier, 1, "Infoo"}}, expected: []item{{SUB, 0, "-"}, {IDENTIFIER, 1, "Infoo"}},
}, { }, {
input: "-Inf 123", input: "-Inf 123",
expected: []item{{ItemSUB, 0, "-"}, {ItemNumber, 1, "Inf"}, {ItemNumber, 5, "123"}}, expected: []item{{SUB, 0, "-"}, {NUMBER, 1, "Inf"}, {NUMBER, 5, "123"}},
}, { }, {
input: "0x123", input: "0x123",
expected: []item{{ItemNumber, 0, "0x123"}}, expected: []item{{NUMBER, 0, "0x123"}},
}, },
}, },
}, },
@ -138,22 +138,22 @@ var tests = []struct {
tests: []testCase{ tests: []testCase{
{ {
input: "\"test\\tsequence\"", input: "\"test\\tsequence\"",
expected: []item{{ItemString, 0, `"test\tsequence"`}}, expected: []item{{STRING, 0, `"test\tsequence"`}},
}, },
{ {
input: "\"test\\\\.expression\"", input: "\"test\\\\.expression\"",
expected: []item{{ItemString, 0, `"test\\.expression"`}}, expected: []item{{STRING, 0, `"test\\.expression"`}},
}, },
{ {
input: "\"test\\.expression\"", input: "\"test\\.expression\"",
expected: []item{ expected: []item{
{ItemError, 0, "unknown escape sequence U+002E '.'"}, {ERROR, 0, "unknown escape sequence U+002E '.'"},
{ItemString, 0, `"test\.expression"`}, {STRING, 0, `"test\.expression"`},
}, },
}, },
{ {
input: "`test\\.expression`", input: "`test\\.expression`",
expected: []item{{ItemString, 0, "`test\\.expression`"}}, expected: []item{{STRING, 0, "`test\\.expression`"}},
}, },
{ {
// See https://github.com/prometheus/prometheus/issues/939. // See https://github.com/prometheus/prometheus/issues/939.
@ -167,19 +167,19 @@ var tests = []struct {
tests: []testCase{ tests: []testCase{
{ {
input: "5s", input: "5s",
expected: []item{{ItemDuration, 0, "5s"}}, expected: []item{{DURATION, 0, "5s"}},
}, { }, {
input: "123m", input: "123m",
expected: []item{{ItemDuration, 0, "123m"}}, expected: []item{{DURATION, 0, "123m"}},
}, { }, {
input: "1h", input: "1h",
expected: []item{{ItemDuration, 0, "1h"}}, expected: []item{{DURATION, 0, "1h"}},
}, { }, {
input: "3w", input: "3w",
expected: []item{{ItemDuration, 0, "3w"}}, expected: []item{{DURATION, 0, "3w"}},
}, { }, {
input: "1y", input: "1y",
expected: []item{{ItemDuration, 0, "1y"}}, expected: []item{{DURATION, 0, "1y"}},
}, },
}, },
}, },
@ -188,16 +188,16 @@ var tests = []struct {
tests: []testCase{ tests: []testCase{
{ {
input: "abc", input: "abc",
expected: []item{{ItemIdentifier, 0, "abc"}}, expected: []item{{IDENTIFIER, 0, "abc"}},
}, { }, {
input: "a:bc", input: "a:bc",
expected: []item{{ItemMetricIdentifier, 0, "a:bc"}}, expected: []item{{METRIC_IDENTIFIER, 0, "a:bc"}},
}, { }, {
input: "abc d", input: "abc d",
expected: []item{{ItemIdentifier, 0, "abc"}, {ItemIdentifier, 4, "d"}}, expected: []item{{IDENTIFIER, 0, "abc"}, {IDENTIFIER, 4, "d"}},
}, { }, {
input: ":bc", input: ":bc",
expected: []item{{ItemMetricIdentifier, 0, ":bc"}}, expected: []item{{METRIC_IDENTIFIER, 0, ":bc"}},
}, { }, {
input: "0a:bc", input: "0a:bc",
fail: true, fail: true,
@ -209,13 +209,13 @@ var tests = []struct {
tests: []testCase{ tests: []testCase{
{ {
input: "# some comment", input: "# some comment",
expected: []item{{ItemComment, 0, "# some comment"}}, expected: []item{{COMMENT, 0, "# some comment"}},
}, { }, {
input: "5 # 1+1\n5", input: "5 # 1+1\n5",
expected: []item{ expected: []item{
{ItemNumber, 0, "5"}, {NUMBER, 0, "5"},
{ItemComment, 2, "# 1+1"}, {COMMENT, 2, "# 1+1"},
{ItemNumber, 8, "5"}, {NUMBER, 8, "5"},
}, },
}, },
}, },
@ -225,56 +225,56 @@ var tests = []struct {
tests: []testCase{ tests: []testCase{
{ {
input: `=`, input: `=`,
expected: []item{{ItemAssign, 0, `=`}}, expected: []item{{ASSIGN, 0, `=`}},
}, { }, {
// Inside braces equality is a single '=' character. // Inside braces equality is a single '=' character.
input: `{=}`, input: `{=}`,
expected: []item{{ItemLeftBrace, 0, `{`}, {ItemEQL, 1, `=`}, {ItemRightBrace, 2, `}`}}, expected: []item{{LEFT_BRACE, 0, `{`}, {EQL, 1, `=`}, {RIGHT_BRACE, 2, `}`}},
}, { }, {
input: `==`, input: `==`,
expected: []item{{ItemEQL, 0, `==`}}, expected: []item{{EQL, 0, `==`}},
}, { }, {
input: `!=`, input: `!=`,
expected: []item{{ItemNEQ, 0, `!=`}}, expected: []item{{NEQ, 0, `!=`}},
}, { }, {
input: `<`, input: `<`,
expected: []item{{ItemLSS, 0, `<`}}, expected: []item{{LSS, 0, `<`}},
}, { }, {
input: `>`, input: `>`,
expected: []item{{ItemGTR, 0, `>`}}, expected: []item{{GTR, 0, `>`}},
}, { }, {
input: `>=`, input: `>=`,
expected: []item{{ItemGTE, 0, `>=`}}, expected: []item{{GTE, 0, `>=`}},
}, { }, {
input: `<=`, input: `<=`,
expected: []item{{ItemLTE, 0, `<=`}}, expected: []item{{LTE, 0, `<=`}},
}, { }, {
input: `+`, input: `+`,
expected: []item{{ItemADD, 0, `+`}}, expected: []item{{ADD, 0, `+`}},
}, { }, {
input: `-`, input: `-`,
expected: []item{{ItemSUB, 0, `-`}}, expected: []item{{SUB, 0, `-`}},
}, { }, {
input: `*`, input: `*`,
expected: []item{{ItemMUL, 0, `*`}}, expected: []item{{MUL, 0, `*`}},
}, { }, {
input: `/`, input: `/`,
expected: []item{{ItemDIV, 0, `/`}}, expected: []item{{DIV, 0, `/`}},
}, { }, {
input: `^`, input: `^`,
expected: []item{{ItemPOW, 0, `^`}}, expected: []item{{POW, 0, `^`}},
}, { }, {
input: `%`, input: `%`,
expected: []item{{ItemMOD, 0, `%`}}, expected: []item{{MOD, 0, `%`}},
}, { }, {
input: `AND`, input: `AND`,
expected: []item{{ItemLAND, 0, `AND`}}, expected: []item{{LAND, 0, `AND`}},
}, { }, {
input: `or`, input: `or`,
expected: []item{{ItemLOR, 0, `or`}}, expected: []item{{LOR, 0, `or`}},
}, { }, {
input: `unless`, input: `unless`,
expected: []item{{ItemLUnless, 0, `unless`}}, expected: []item{{LUNLESS, 0, `unless`}},
}, },
}, },
}, },
@ -283,25 +283,25 @@ var tests = []struct {
tests: []testCase{ tests: []testCase{
{ {
input: `sum`, input: `sum`,
expected: []item{{ItemSum, 0, `sum`}}, expected: []item{{SUM, 0, `sum`}},
}, { }, {
input: `AVG`, input: `AVG`,
expected: []item{{ItemAvg, 0, `AVG`}}, expected: []item{{AVG, 0, `AVG`}},
}, { }, {
input: `MAX`, input: `MAX`,
expected: []item{{ItemMax, 0, `MAX`}}, expected: []item{{MAX, 0, `MAX`}},
}, { }, {
input: `min`, input: `min`,
expected: []item{{ItemMin, 0, `min`}}, expected: []item{{MIN, 0, `min`}},
}, { }, {
input: `count`, input: `count`,
expected: []item{{ItemCount, 0, `count`}}, expected: []item{{COUNT, 0, `count`}},
}, { }, {
input: `stdvar`, input: `stdvar`,
expected: []item{{ItemStdvar, 0, `stdvar`}}, expected: []item{{STDVAR, 0, `stdvar`}},
}, { }, {
input: `stddev`, input: `stddev`,
expected: []item{{ItemStddev, 0, `stddev`}}, expected: []item{{STDDEV, 0, `stddev`}},
}, },
}, },
}, },
@ -310,28 +310,28 @@ var tests = []struct {
tests: []testCase{ tests: []testCase{
{ {
input: "offset", input: "offset",
expected: []item{{ItemOffset, 0, "offset"}}, expected: []item{{OFFSET, 0, "offset"}},
}, { }, {
input: "by", input: "by",
expected: []item{{ItemBy, 0, "by"}}, expected: []item{{BY, 0, "by"}},
}, { }, {
input: "without", input: "without",
expected: []item{{ItemWithout, 0, "without"}}, expected: []item{{WITHOUT, 0, "without"}},
}, { }, {
input: "on", input: "on",
expected: []item{{ItemOn, 0, "on"}}, expected: []item{{ON, 0, "on"}},
}, { }, {
input: "ignoring", input: "ignoring",
expected: []item{{ItemIgnoring, 0, "ignoring"}}, expected: []item{{IGNORING, 0, "ignoring"}},
}, { }, {
input: "group_left", input: "group_left",
expected: []item{{ItemGroupLeft, 0, "group_left"}}, expected: []item{{GROUP_LEFT, 0, "group_left"}},
}, { }, {
input: "group_right", input: "group_right",
expected: []item{{ItemGroupRight, 0, "group_right"}}, expected: []item{{GROUP_RIGHT, 0, "group_right"}},
}, { }, {
input: "bool", input: "bool",
expected: []item{{ItemBool, 0, "bool"}}, expected: []item{{BOOL, 0, "bool"}},
}, },
}, },
}, },
@ -350,56 +350,56 @@ var tests = []struct {
}, { }, {
input: `{foo='bar'}`, input: `{foo='bar'}`,
expected: []item{ expected: []item{
{ItemLeftBrace, 0, `{`}, {LEFT_BRACE, 0, `{`},
{ItemIdentifier, 1, `foo`}, {IDENTIFIER, 1, `foo`},
{ItemEQL, 4, `=`}, {EQL, 4, `=`},
{ItemString, 5, `'bar'`}, {STRING, 5, `'bar'`},
{ItemRightBrace, 10, `}`}, {RIGHT_BRACE, 10, `}`},
}, },
}, { }, {
input: `{foo="bar"}`, input: `{foo="bar"}`,
expected: []item{ expected: []item{
{ItemLeftBrace, 0, `{`}, {LEFT_BRACE, 0, `{`},
{ItemIdentifier, 1, `foo`}, {IDENTIFIER, 1, `foo`},
{ItemEQL, 4, `=`}, {EQL, 4, `=`},
{ItemString, 5, `"bar"`}, {STRING, 5, `"bar"`},
{ItemRightBrace, 10, `}`}, {RIGHT_BRACE, 10, `}`},
}, },
}, { }, {
input: `{foo="bar\"bar"}`, input: `{foo="bar\"bar"}`,
expected: []item{ expected: []item{
{ItemLeftBrace, 0, `{`}, {LEFT_BRACE, 0, `{`},
{ItemIdentifier, 1, `foo`}, {IDENTIFIER, 1, `foo`},
{ItemEQL, 4, `=`}, {EQL, 4, `=`},
{ItemString, 5, `"bar\"bar"`}, {STRING, 5, `"bar\"bar"`},
{ItemRightBrace, 15, `}`}, {RIGHT_BRACE, 15, `}`},
}, },
}, { }, {
input: `{NaN != "bar" }`, input: `{NaN != "bar" }`,
expected: []item{ expected: []item{
{ItemLeftBrace, 0, `{`}, {LEFT_BRACE, 0, `{`},
{ItemIdentifier, 1, `NaN`}, {IDENTIFIER, 1, `NaN`},
{ItemNEQ, 5, `!=`}, {NEQ, 5, `!=`},
{ItemString, 8, `"bar"`}, {STRING, 8, `"bar"`},
{ItemRightBrace, 14, `}`}, {RIGHT_BRACE, 14, `}`},
}, },
}, { }, {
input: `{alert=~"bar" }`, input: `{alert=~"bar" }`,
expected: []item{ expected: []item{
{ItemLeftBrace, 0, `{`}, {LEFT_BRACE, 0, `{`},
{ItemIdentifier, 1, `alert`}, {IDENTIFIER, 1, `alert`},
{ItemEQLRegex, 6, `=~`}, {EQL_REGEX, 6, `=~`},
{ItemString, 8, `"bar"`}, {STRING, 8, `"bar"`},
{ItemRightBrace, 14, `}`}, {RIGHT_BRACE, 14, `}`},
}, },
}, { }, {
input: `{on!~"bar"}`, input: `{on!~"bar"}`,
expected: []item{ expected: []item{
{ItemLeftBrace, 0, `{`}, {LEFT_BRACE, 0, `{`},
{ItemIdentifier, 1, `on`}, {IDENTIFIER, 1, `on`},
{ItemNEQRegex, 3, `!~`}, {NEQ_REGEX, 3, `!~`},
{ItemString, 5, `"bar"`}, {STRING, 5, `"bar"`},
{ItemRightBrace, 10, `}`}, {RIGHT_BRACE, 10, `}`},
}, },
}, { }, {
input: `{alert!#"bar"}`, fail: true, input: `{alert!#"bar"}`, fail: true,
@ -469,43 +469,43 @@ var tests = []struct {
{ {
input: `{} _ 1 x .3`, input: `{} _ 1 x .3`,
expected: []item{ expected: []item{
{ItemLeftBrace, 0, `{`}, {LEFT_BRACE, 0, `{`},
{ItemRightBrace, 1, `}`}, {RIGHT_BRACE, 1, `}`},
{ItemSpace, 2, ` `}, {SPACE, 2, ` `},
{ItemBlank, 3, `_`}, {BLANK, 3, `_`},
{ItemSpace, 4, ` `}, {SPACE, 4, ` `},
{ItemNumber, 5, `1`}, {NUMBER, 5, `1`},
{ItemSpace, 6, ` `}, {SPACE, 6, ` `},
{ItemTimes, 7, `x`}, {TIMES, 7, `x`},
{ItemSpace, 8, ` `}, {SPACE, 8, ` `},
{ItemNumber, 9, `.3`}, {NUMBER, 9, `.3`},
}, },
seriesDesc: true, seriesDesc: true,
}, },
{ {
input: `metric +Inf Inf NaN`, input: `metric +Inf Inf NaN`,
expected: []item{ expected: []item{
{ItemIdentifier, 0, `metric`}, {IDENTIFIER, 0, `metric`},
{ItemSpace, 6, ` `}, {SPACE, 6, ` `},
{ItemADD, 7, `+`}, {ADD, 7, `+`},
{ItemNumber, 8, `Inf`}, {NUMBER, 8, `Inf`},
{ItemSpace, 11, ` `}, {SPACE, 11, ` `},
{ItemNumber, 12, `Inf`}, {NUMBER, 12, `Inf`},
{ItemSpace, 15, ` `}, {SPACE, 15, ` `},
{ItemNumber, 16, `NaN`}, {NUMBER, 16, `NaN`},
}, },
seriesDesc: true, seriesDesc: true,
}, },
{ {
input: `metric 1+1x4`, input: `metric 1+1x4`,
expected: []item{ expected: []item{
{ItemIdentifier, 0, `metric`}, {IDENTIFIER, 0, `metric`},
{ItemSpace, 6, ` `}, {SPACE, 6, ` `},
{ItemNumber, 7, `1`}, {NUMBER, 7, `1`},
{ItemADD, 8, `+`}, {ADD, 8, `+`},
{ItemNumber, 9, `1`}, {NUMBER, 9, `1`},
{ItemTimes, 10, `x`}, {TIMES, 10, `x`},
{ItemNumber, 11, `4`}, {NUMBER, 11, `4`},
}, },
seriesDesc: true, seriesDesc: true,
}, },
@ -517,150 +517,150 @@ var tests = []struct {
{ {
input: `test_name{on!~"bar"}[4m:4s]`, input: `test_name{on!~"bar"}[4m:4s]`,
expected: []item{ expected: []item{
{ItemIdentifier, 0, `test_name`}, {IDENTIFIER, 0, `test_name`},
{ItemLeftBrace, 9, `{`}, {LEFT_BRACE, 9, `{`},
{ItemIdentifier, 10, `on`}, {IDENTIFIER, 10, `on`},
{ItemNEQRegex, 12, `!~`}, {NEQ_REGEX, 12, `!~`},
{ItemString, 14, `"bar"`}, {STRING, 14, `"bar"`},
{ItemRightBrace, 19, `}`}, {RIGHT_BRACE, 19, `}`},
{ItemLeftBracket, 20, `[`}, {LEFT_BRACKET, 20, `[`},
{ItemDuration, 21, `4m`}, {DURATION, 21, `4m`},
{ItemColon, 23, `:`}, {COLON, 23, `:`},
{ItemDuration, 24, `4s`}, {DURATION, 24, `4s`},
{ItemRightBracket, 26, `]`}, {RIGHT_BRACKET, 26, `]`},
}, },
}, },
{ {
input: `test:name{on!~"bar"}[4m:4s]`, input: `test:name{on!~"bar"}[4m:4s]`,
expected: []item{ expected: []item{
{ItemMetricIdentifier, 0, `test:name`}, {METRIC_IDENTIFIER, 0, `test:name`},
{ItemLeftBrace, 9, `{`}, {LEFT_BRACE, 9, `{`},
{ItemIdentifier, 10, `on`}, {IDENTIFIER, 10, `on`},
{ItemNEQRegex, 12, `!~`}, {NEQ_REGEX, 12, `!~`},
{ItemString, 14, `"bar"`}, {STRING, 14, `"bar"`},
{ItemRightBrace, 19, `}`}, {RIGHT_BRACE, 19, `}`},
{ItemLeftBracket, 20, `[`}, {LEFT_BRACKET, 20, `[`},
{ItemDuration, 21, `4m`}, {DURATION, 21, `4m`},
{ItemColon, 23, `:`}, {COLON, 23, `:`},
{ItemDuration, 24, `4s`}, {DURATION, 24, `4s`},
{ItemRightBracket, 26, `]`}, {RIGHT_BRACKET, 26, `]`},
}, },
}, { }, {
input: `test:name{on!~"b:ar"}[4m:4s]`, input: `test:name{on!~"b:ar"}[4m:4s]`,
expected: []item{ expected: []item{
{ItemMetricIdentifier, 0, `test:name`}, {METRIC_IDENTIFIER, 0, `test:name`},
{ItemLeftBrace, 9, `{`}, {LEFT_BRACE, 9, `{`},
{ItemIdentifier, 10, `on`}, {IDENTIFIER, 10, `on`},
{ItemNEQRegex, 12, `!~`}, {NEQ_REGEX, 12, `!~`},
{ItemString, 14, `"b:ar"`}, {STRING, 14, `"b:ar"`},
{ItemRightBrace, 20, `}`}, {RIGHT_BRACE, 20, `}`},
{ItemLeftBracket, 21, `[`}, {LEFT_BRACKET, 21, `[`},
{ItemDuration, 22, `4m`}, {DURATION, 22, `4m`},
{ItemColon, 24, `:`}, {COLON, 24, `:`},
{ItemDuration, 25, `4s`}, {DURATION, 25, `4s`},
{ItemRightBracket, 27, `]`}, {RIGHT_BRACKET, 27, `]`},
}, },
}, { }, {
input: `test:name{on!~"b:ar"}[4m:]`, input: `test:name{on!~"b:ar"}[4m:]`,
expected: []item{ expected: []item{
{ItemMetricIdentifier, 0, `test:name`}, {METRIC_IDENTIFIER, 0, `test:name`},
{ItemLeftBrace, 9, `{`}, {LEFT_BRACE, 9, `{`},
{ItemIdentifier, 10, `on`}, {IDENTIFIER, 10, `on`},
{ItemNEQRegex, 12, `!~`}, {NEQ_REGEX, 12, `!~`},
{ItemString, 14, `"b:ar"`}, {STRING, 14, `"b:ar"`},
{ItemRightBrace, 20, `}`}, {RIGHT_BRACE, 20, `}`},
{ItemLeftBracket, 21, `[`}, {LEFT_BRACKET, 21, `[`},
{ItemDuration, 22, `4m`}, {DURATION, 22, `4m`},
{ItemColon, 24, `:`}, {COLON, 24, `:`},
{ItemRightBracket, 25, `]`}, {RIGHT_BRACKET, 25, `]`},
}, },
}, { // Nested Subquery. }, { // Nested Subquery.
input: `min_over_time(rate(foo{bar="baz"}[2s])[5m:])[4m:3s]`, input: `min_over_time(rate(foo{bar="baz"}[2s])[5m:])[4m:3s]`,
expected: []item{ expected: []item{
{ItemIdentifier, 0, `min_over_time`}, {IDENTIFIER, 0, `min_over_time`},
{ItemLeftParen, 13, `(`}, {LEFT_PAREN, 13, `(`},
{ItemIdentifier, 14, `rate`}, {IDENTIFIER, 14, `rate`},
{ItemLeftParen, 18, `(`}, {LEFT_PAREN, 18, `(`},
{ItemIdentifier, 19, `foo`}, {IDENTIFIER, 19, `foo`},
{ItemLeftBrace, 22, `{`}, {LEFT_BRACE, 22, `{`},
{ItemIdentifier, 23, `bar`}, {IDENTIFIER, 23, `bar`},
{ItemEQL, 26, `=`}, {EQL, 26, `=`},
{ItemString, 27, `"baz"`}, {STRING, 27, `"baz"`},
{ItemRightBrace, 32, `}`}, {RIGHT_BRACE, 32, `}`},
{ItemLeftBracket, 33, `[`}, {LEFT_BRACKET, 33, `[`},
{ItemDuration, 34, `2s`}, {DURATION, 34, `2s`},
{ItemRightBracket, 36, `]`}, {RIGHT_BRACKET, 36, `]`},
{ItemRightParen, 37, `)`}, {RIGHT_PAREN, 37, `)`},
{ItemLeftBracket, 38, `[`}, {LEFT_BRACKET, 38, `[`},
{ItemDuration, 39, `5m`}, {DURATION, 39, `5m`},
{ItemColon, 41, `:`}, {COLON, 41, `:`},
{ItemRightBracket, 42, `]`}, {RIGHT_BRACKET, 42, `]`},
{ItemRightParen, 43, `)`}, {RIGHT_PAREN, 43, `)`},
{ItemLeftBracket, 44, `[`}, {LEFT_BRACKET, 44, `[`},
{ItemDuration, 45, `4m`}, {DURATION, 45, `4m`},
{ItemColon, 47, `:`}, {COLON, 47, `:`},
{ItemDuration, 48, `3s`}, {DURATION, 48, `3s`},
{ItemRightBracket, 50, `]`}, {RIGHT_BRACKET, 50, `]`},
}, },
}, },
// Subquery with offset. // Subquery with offset.
{ {
input: `test:name{on!~"b:ar"}[4m:4s] offset 10m`, input: `test:name{on!~"b:ar"}[4m:4s] offset 10m`,
expected: []item{ expected: []item{
{ItemMetricIdentifier, 0, `test:name`}, {METRIC_IDENTIFIER, 0, `test:name`},
{ItemLeftBrace, 9, `{`}, {LEFT_BRACE, 9, `{`},
{ItemIdentifier, 10, `on`}, {IDENTIFIER, 10, `on`},
{ItemNEQRegex, 12, `!~`}, {NEQ_REGEX, 12, `!~`},
{ItemString, 14, `"b:ar"`}, {STRING, 14, `"b:ar"`},
{ItemRightBrace, 20, `}`}, {RIGHT_BRACE, 20, `}`},
{ItemLeftBracket, 21, `[`}, {LEFT_BRACKET, 21, `[`},
{ItemDuration, 22, `4m`}, {DURATION, 22, `4m`},
{ItemColon, 24, `:`}, {COLON, 24, `:`},
{ItemDuration, 25, `4s`}, {DURATION, 25, `4s`},
{ItemRightBracket, 27, `]`}, {RIGHT_BRACKET, 27, `]`},
{ItemOffset, 29, "offset"}, {OFFSET, 29, "offset"},
{ItemDuration, 36, "10m"}, {DURATION, 36, "10m"},
}, },
}, { }, {
input: `min_over_time(rate(foo{bar="baz"}[2s])[5m:] offset 6m)[4m:3s]`, input: `min_over_time(rate(foo{bar="baz"}[2s])[5m:] offset 6m)[4m:3s]`,
expected: []item{ expected: []item{
{ItemIdentifier, 0, `min_over_time`}, {IDENTIFIER, 0, `min_over_time`},
{ItemLeftParen, 13, `(`}, {LEFT_PAREN, 13, `(`},
{ItemIdentifier, 14, `rate`}, {IDENTIFIER, 14, `rate`},
{ItemLeftParen, 18, `(`}, {LEFT_PAREN, 18, `(`},
{ItemIdentifier, 19, `foo`}, {IDENTIFIER, 19, `foo`},
{ItemLeftBrace, 22, `{`}, {LEFT_BRACE, 22, `{`},
{ItemIdentifier, 23, `bar`}, {IDENTIFIER, 23, `bar`},
{ItemEQL, 26, `=`}, {EQL, 26, `=`},
{ItemString, 27, `"baz"`}, {STRING, 27, `"baz"`},
{ItemRightBrace, 32, `}`}, {RIGHT_BRACE, 32, `}`},
{ItemLeftBracket, 33, `[`}, {LEFT_BRACKET, 33, `[`},
{ItemDuration, 34, `2s`}, {DURATION, 34, `2s`},
{ItemRightBracket, 36, `]`}, {RIGHT_BRACKET, 36, `]`},
{ItemRightParen, 37, `)`}, {RIGHT_PAREN, 37, `)`},
{ItemLeftBracket, 38, `[`}, {LEFT_BRACKET, 38, `[`},
{ItemDuration, 39, `5m`}, {DURATION, 39, `5m`},
{ItemColon, 41, `:`}, {COLON, 41, `:`},
{ItemRightBracket, 42, `]`}, {RIGHT_BRACKET, 42, `]`},
{ItemOffset, 44, `offset`}, {OFFSET, 44, `offset`},
{ItemDuration, 51, `6m`}, {DURATION, 51, `6m`},
{ItemRightParen, 53, `)`}, {RIGHT_PAREN, 53, `)`},
{ItemLeftBracket, 54, `[`}, {LEFT_BRACKET, 54, `[`},
{ItemDuration, 55, `4m`}, {DURATION, 55, `4m`},
{ItemColon, 57, `:`}, {COLON, 57, `:`},
{ItemDuration, 58, `3s`}, {DURATION, 58, `3s`},
{ItemRightBracket, 60, `]`}, {RIGHT_BRACKET, 60, `]`},
}, },
}, },
{ {
input: `test:name[ 5m]`, input: `test:name[ 5m]`,
expected: []item{ expected: []item{
{ItemMetricIdentifier, 0, `test:name`}, {METRIC_IDENTIFIER, 0, `test:name`},
{ItemLeftBracket, 9, `[`}, {LEFT_BRACKET, 9, `[`},
{ItemDuration, 11, `5m`}, {DURATION, 11, `5m`},
{ItemRightBracket, 13, `]`}, {RIGHT_BRACKET, 13, `]`},
}, },
}, },
{ {
@ -703,18 +703,18 @@ func TestLexer(t *testing.T) {
lastItem := out[len(out)-1] lastItem := out[len(out)-1]
if test.fail { if test.fail {
if lastItem.typ != ItemError { if lastItem.typ != ERROR {
t.Logf("%d: input %q", i, test.input) t.Logf("%d: input %q", i, test.input)
t.Fatalf("expected lexing error but did not fail") t.Fatalf("expected lexing error but did not fail")
} }
continue continue
} }
if lastItem.typ == ItemError { if lastItem.typ == ERROR {
t.Logf("%d: input %q", i, test.input) t.Logf("%d: input %q", i, test.input)
t.Fatalf("unexpected lexing error at position %d: %s", lastItem.pos, lastItem) t.Fatalf("unexpected lexing error at position %d: %s", lastItem.pos, lastItem)
} }
eofItem := item{ItemEOF, Pos(len(test.input)), ""} eofItem := item{EOF, Pos(len(test.input)), ""}
testutil.Equals(t, lastItem, eofItem, "%d: input %q", i, test.input) testutil.Equals(t, lastItem, eofItem, "%d: input %q", i, test.input)
out = out[:len(out)-1] out = out[:len(out)-1]

View File

@ -70,7 +70,7 @@ func ParseMetric(input string) (m labels.Labels, err error) {
defer p.recover(&err) defer p.recover(&err)
m = p.metric() m = p.metric()
if p.peek().typ != ItemEOF { if p.peek().typ != EOF {
p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:]) p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
} }
return m, nil return m, nil
@ -83,11 +83,11 @@ func ParseMetricSelector(input string) (m []*labels.Matcher, err error) {
defer p.recover(&err) defer p.recover(&err)
name := "" name := ""
if t := p.peek().typ; t == ItemMetricIdentifier || t == ItemIdentifier { if t := p.peek().typ; t == METRIC_IDENTIFIER || t == IDENTIFIER {
name = p.next().val name = p.next().val
} }
vs := p.VectorSelector(name) vs := p.VectorSelector(name)
if p.peek().typ != ItemEOF { if p.peek().typ != EOF {
p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:]) p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
} }
return vs.LabelMatchers, nil return vs.LabelMatchers, nil
@ -105,7 +105,7 @@ func newParser(input string) *parser {
func (p *parser) parseExpr() (expr Expr, err error) { func (p *parser) parseExpr() (expr Expr, err error) {
defer p.recover(&err) defer p.recover(&err)
for p.peek().typ != ItemEOF { for p.peek().typ != EOF {
if expr != nil { if expr != nil {
p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:]) p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
} }
@ -147,20 +147,20 @@ func (p *parser) parseSeriesDesc() (m labels.Labels, vals []sequenceValue, err e
const ctx = "series values" const ctx = "series values"
for { for {
for p.peek().typ == ItemSpace { for p.peek().typ == SPACE {
p.next() p.next()
} }
if p.peek().typ == ItemEOF { if p.peek().typ == EOF {
break break
} }
// Extract blanks. // Extract blanks.
if p.peek().typ == ItemBlank { if p.peek().typ == BLANK {
p.next() p.next()
times := uint64(1) times := uint64(1)
if p.peek().typ == ItemTimes { if p.peek().typ == TIMES {
p.next() p.next()
times, err = strconv.ParseUint(p.expect(ItemNumber, ctx).val, 10, 64) times, err = strconv.ParseUint(p.expect(NUMBER, ctx).val, 10, 64)
if err != nil { if err != nil {
p.errorf("invalid repetition in %s: %s", ctx, err) p.errorf("invalid repetition in %s: %s", ctx, err)
} }
@ -170,7 +170,7 @@ func (p *parser) parseSeriesDesc() (m labels.Labels, vals []sequenceValue, err e
} }
// This is to ensure that there is a space between this and the next number. // This is to ensure that there is a space between this and the next number.
// This is especially required if the next number is negative. // This is especially required if the next number is negative.
if t := p.expectOneOf(ItemSpace, ItemEOF, ctx).typ; t == ItemEOF { if t := p.expectOneOf(SPACE, EOF, ctx).typ; t == EOF {
break break
} }
continue continue
@ -178,15 +178,15 @@ func (p *parser) parseSeriesDesc() (m labels.Labels, vals []sequenceValue, err e
// Extract values. // Extract values.
sign := 1.0 sign := 1.0
if t := p.peek().typ; t == ItemSUB || t == ItemADD { if t := p.peek().typ; t == SUB || t == ADD {
if p.next().typ == ItemSUB { if p.next().typ == SUB {
sign = -1 sign = -1
} }
} }
var k float64 var k float64
if t := p.peek().typ; t == ItemNumber { if t := p.peek().typ; t == NUMBER {
k = sign * p.number(p.expect(ItemNumber, ctx).val) k = sign * p.number(p.expect(NUMBER, ctx).val)
} else if t == ItemIdentifier && p.peek().val == "stale" { } else if t == IDENTIFIER && p.peek().val == "stale" {
p.next() p.next()
k = math.Float64frombits(value.StaleNaN) k = math.Float64frombits(value.StaleNaN)
} else { } else {
@ -197,24 +197,24 @@ func (p *parser) parseSeriesDesc() (m labels.Labels, vals []sequenceValue, err e
}) })
// If there are no offset repetitions specified, proceed with the next value. // If there are no offset repetitions specified, proceed with the next value.
if t := p.peek(); t.typ == ItemSpace { if t := p.peek(); t.typ == SPACE {
// This ensures there is a space between every value. // This ensures there is a space between every value.
continue continue
} else if t.typ == ItemEOF { } else if t.typ == EOF {
break break
} else if t.typ != ItemADD && t.typ != ItemSUB { } else if t.typ != ADD && t.typ != SUB {
p.errorf("expected next value or relative expansion in %s but got %s (value: %s)", ctx, t.desc(), p.peek()) p.errorf("expected next value or relative expansion in %s but got %s (value: %s)", ctx, t.desc(), p.peek())
} }
// Expand the repeated offsets into values. // Expand the repeated offsets into values.
sign = 1.0 sign = 1.0
if p.next().typ == ItemSUB { if p.next().typ == SUB {
sign = -1.0 sign = -1.0
} }
offset := sign * p.number(p.expect(ItemNumber, ctx).val) offset := sign * p.number(p.expect(NUMBER, ctx).val)
p.expect(ItemTimes, ctx) p.expect(TIMES, ctx)
times, err := strconv.ParseUint(p.expect(ItemNumber, ctx).val, 10, 64) times, err := strconv.ParseUint(p.expect(NUMBER, ctx).val, 10, 64)
if err != nil { if err != nil {
p.errorf("invalid repetition in %s: %s", ctx, err) p.errorf("invalid repetition in %s: %s", ctx, err)
} }
@ -228,7 +228,7 @@ func (p *parser) parseSeriesDesc() (m labels.Labels, vals []sequenceValue, err e
// This is to ensure that there is a space between this expanding notation // This is to ensure that there is a space between this expanding notation
// and the next number. This is especially required if the next number // and the next number. This is especially required if the next number
// is negative. // is negative.
if t := p.expectOneOf(ItemSpace, ItemEOF, ctx).typ; t == ItemEOF { if t := p.expectOneOf(SPACE, EOF, ctx).typ; t == EOF {
break break
} }
} }
@ -248,7 +248,7 @@ func (p *parser) next() item {
if !p.peeking { if !p.peeking {
t := p.lex.nextItem() t := p.lex.nextItem()
// Skip comments. // Skip comments.
for t.typ == ItemComment { for t.typ == COMMENT {
t = p.lex.nextItem() t = p.lex.nextItem()
} }
p.token = t p.token = t
@ -256,7 +256,7 @@ func (p *parser) next() item {
p.peeking = false p.peeking = false
if p.token.typ == ItemError { if p.token.typ == ERROR {
p.errorf("%s", p.token.val) p.errorf("%s", p.token.val)
} }
return p.token return p.token
@ -271,7 +271,7 @@ func (p *parser) peek() item {
t := p.lex.nextItem() t := p.lex.nextItem()
// Skip comments. // Skip comments.
for t.typ == ItemComment { for t.typ == COMMENT {
t = p.lex.nextItem() t = p.lex.nextItem()
} }
p.token = t p.token = t
@ -376,11 +376,11 @@ func (p *parser) expr() Expr {
op := p.peek().typ op := p.peek().typ
if !op.isOperator() { if !op.isOperator() {
// Check for subquery. // Check for subquery.
if op == ItemLeftBracket { if op == LEFT_BRACKET {
expr = p.subqueryOrRangeSelector(expr, false) expr = p.subqueryOrRangeSelector(expr, false)
if s, ok := expr.(*SubqueryExpr); ok { if s, ok := expr.(*SubqueryExpr); ok {
// Parse optional offset. // Parse optional offset.
if p.peek().typ == ItemOffset { if p.peek().typ == OFFSET {
offset := p.offset() offset := p.offset()
s.Offset = offset s.Offset = offset
} }
@ -401,7 +401,7 @@ func (p *parser) expr() Expr {
returnBool := false returnBool := false
// Parse bool modifier. // Parse bool modifier.
if p.peek().typ == ItemBool { if p.peek().typ == BOOL {
if !op.isComparisonOperator() { if !op.isComparisonOperator() {
p.errorf("bool modifier can only be used on comparison operators") p.errorf("bool modifier can only be used on comparison operators")
} }
@ -410,22 +410,22 @@ func (p *parser) expr() Expr {
} }
// Parse ON/IGNORING clause. // Parse ON/IGNORING clause.
if p.peek().typ == ItemOn || p.peek().typ == ItemIgnoring { if p.peek().typ == ON || p.peek().typ == IGNORING {
if p.peek().typ == ItemOn { if p.peek().typ == ON {
vecMatching.On = true vecMatching.On = true
} }
p.next() p.next()
vecMatching.MatchingLabels = p.labels() vecMatching.MatchingLabels = p.labels()
// Parse grouping. // Parse grouping.
if t := p.peek().typ; t == ItemGroupLeft || t == ItemGroupRight { if t := p.peek().typ; t == GROUP_LEFT || t == GROUP_RIGHT {
p.next() p.next()
if t == ItemGroupLeft { if t == GROUP_LEFT {
vecMatching.Card = CardManyToOne vecMatching.Card = CardManyToOne
} else { } else {
vecMatching.Card = CardOneToMany vecMatching.Card = CardOneToMany
} }
if p.peek().typ == ItemLeftParen { if p.peek().typ == LEFT_PAREN {
vecMatching.Include = p.labels() vecMatching.Include = p.labels()
} }
} }
@ -482,35 +482,35 @@ func (p *parser) balance(lhs Expr, op ItemType, rhs Expr, vecMatching *VectorMat
// //
func (p *parser) unaryExpr() Expr { func (p *parser) unaryExpr() Expr {
switch t := p.peek(); t.typ { switch t := p.peek(); t.typ {
case ItemADD, ItemSUB: case ADD, SUB:
p.next() p.next()
e := p.unaryExpr() e := p.unaryExpr()
// Simplify unary expressions for number literals. // Simplify unary expressions for number literals.
if nl, ok := e.(*NumberLiteral); ok { if nl, ok := e.(*NumberLiteral); ok {
if t.typ == ItemSUB { if t.typ == SUB {
nl.Val *= -1 nl.Val *= -1
} }
return nl return nl
} }
return &UnaryExpr{Op: t.typ, Expr: e} return &UnaryExpr{Op: t.typ, Expr: e}
case ItemLeftParen: case LEFT_PAREN:
p.next() p.next()
e := p.expr() e := p.expr()
p.expect(ItemRightParen, "paren expression") p.expect(RIGHT_PAREN, "paren expression")
return &ParenExpr{Expr: e} return &ParenExpr{Expr: e}
} }
e := p.primaryExpr() e := p.primaryExpr()
// Expression might be followed by a range selector. // Expression might be followed by a range selector.
if p.peek().typ == ItemLeftBracket { if p.peek().typ == LEFT_BRACKET {
e = p.subqueryOrRangeSelector(e, true) e = p.subqueryOrRangeSelector(e, true)
} }
// Parse optional offset. // Parse optional offset.
if p.peek().typ == ItemOffset { if p.peek().typ == OFFSET {
offset := p.offset() offset := p.offset()
switch s := e.(type) { switch s := e.(type) {
@ -544,7 +544,7 @@ func (p *parser) subqueryOrRangeSelector(expr Expr, checkRange bool) Expr {
var erange time.Duration var erange time.Duration
var err error var err error
erangeStr := p.expect(ItemDuration, ctx).val erangeStr := p.expect(DURATION, ctx).val
erange, err = parseDuration(erangeStr) erange, err = parseDuration(erangeStr)
if err != nil { if err != nil {
p.error(err) p.error(err)
@ -552,8 +552,8 @@ func (p *parser) subqueryOrRangeSelector(expr Expr, checkRange bool) Expr {
var itm item var itm item
if checkRange { if checkRange {
itm = p.expectOneOf(ItemRightBracket, ItemColon, ctx) itm = p.expectOneOf(RIGHT_BRACKET, COLON, ctx)
if itm.typ == ItemRightBracket { if itm.typ == RIGHT_BRACKET {
// Range selector. // Range selector.
vs, ok := expr.(*VectorSelector) vs, ok := expr.(*VectorSelector)
if !ok { if !ok {
@ -566,20 +566,20 @@ func (p *parser) subqueryOrRangeSelector(expr Expr, checkRange bool) Expr {
} }
} }
} else { } else {
itm = p.expect(ItemColon, ctx) itm = p.expect(COLON, ctx)
} }
// Subquery. // Subquery.
var estep time.Duration var estep time.Duration
itm = p.expectOneOf(ItemRightBracket, ItemDuration, ctx) itm = p.expectOneOf(RIGHT_BRACKET, DURATION, ctx)
if itm.typ == ItemDuration { if itm.typ == DURATION {
estepStr := itm.val estepStr := itm.val
estep, err = parseDuration(estepStr) estep, err = parseDuration(estepStr)
if err != nil { if err != nil {
p.error(err) p.error(err)
} }
p.expect(ItemRightBracket, ctx) p.expect(RIGHT_BRACKET, ctx)
} }
return &SubqueryExpr{ return &SubqueryExpr{
@ -608,26 +608,26 @@ func (p *parser) number(val string) float64 {
// //
func (p *parser) primaryExpr() Expr { func (p *parser) primaryExpr() Expr {
switch t := p.next(); { switch t := p.next(); {
case t.typ == ItemNumber: case t.typ == NUMBER:
f := p.number(t.val) f := p.number(t.val)
return &NumberLiteral{f} return &NumberLiteral{f}
case t.typ == ItemString: case t.typ == STRING:
return &StringLiteral{p.unquoteString(t.val)} return &StringLiteral{p.unquoteString(t.val)}
case t.typ == ItemLeftBrace: case t.typ == LEFT_BRACE:
// Metric selector without metric name. // Metric selector without metric name.
p.backup() p.backup()
return p.VectorSelector("") return p.VectorSelector("")
case t.typ == ItemIdentifier: case t.typ == IDENTIFIER:
// Check for function call. // Check for function call.
if p.peek().typ == ItemLeftParen { if p.peek().typ == LEFT_PAREN {
return p.call(t.val) return p.call(t.val)
} }
fallthrough // Else metric selector. fallthrough // Else metric selector.
case t.typ == ItemMetricIdentifier: case t.typ == METRIC_IDENTIFIER:
return p.VectorSelector(t.val) return p.VectorSelector(t.val)
case t.typ.isAggregator(): case t.typ.isAggregator():
@ -647,10 +647,10 @@ func (p *parser) primaryExpr() Expr {
func (p *parser) labels() []string { func (p *parser) labels() []string {
const ctx = "grouping opts" const ctx = "grouping opts"
p.expect(ItemLeftParen, ctx) p.expect(LEFT_PAREN, ctx)
labels := []string{} labels := []string{}
if p.peek().typ != ItemRightParen { if p.peek().typ != RIGHT_PAREN {
for { for {
id := p.next() id := p.next()
if !isLabel(id.val) { if !isLabel(id.val) {
@ -658,13 +658,13 @@ func (p *parser) labels() []string {
} }
labels = append(labels, id.val) labels = append(labels, id.val)
if p.peek().typ != ItemComma { if p.peek().typ != COMMA {
break break
} }
p.next() p.next()
} }
} }
p.expect(ItemRightParen, ctx) p.expect(RIGHT_PAREN, ctx)
return labels return labels
} }
@ -686,8 +686,8 @@ func (p *parser) aggrExpr() *AggregateExpr {
modifiersFirst := false modifiersFirst := false
if t := p.peek().typ; t == ItemBy || t == ItemWithout { if t := p.peek().typ; t == BY || t == WITHOUT {
if t == ItemWithout { if t == WITHOUT {
without = true without = true
} }
p.next() p.next()
@ -695,21 +695,21 @@ func (p *parser) aggrExpr() *AggregateExpr {
modifiersFirst = true modifiersFirst = true
} }
p.expect(ItemLeftParen, ctx) p.expect(LEFT_PAREN, ctx)
var param Expr var param Expr
if agop.typ.isAggregatorWithParam() { if agop.typ.isAggregatorWithParam() {
param = p.expr() param = p.expr()
p.expect(ItemComma, ctx) p.expect(COMMA, ctx)
} }
e := p.expr() e := p.expr()
p.expect(ItemRightParen, ctx) p.expect(RIGHT_PAREN, ctx)
if !modifiersFirst { if !modifiersFirst {
if t := p.peek().typ; t == ItemBy || t == ItemWithout { if t := p.peek().typ; t == BY || t == WITHOUT {
if len(grouping) > 0 { if len(grouping) > 0 {
p.errorf("aggregation must only contain one grouping clause") p.errorf("aggregation must only contain one grouping clause")
} }
if t == ItemWithout { if t == WITHOUT {
without = true without = true
} }
p.next() p.next()
@ -738,9 +738,9 @@ func (p *parser) call(name string) *Call {
p.errorf("unknown function with name %q", name) p.errorf("unknown function with name %q", name)
} }
p.expect(ItemLeftParen, ctx) p.expect(LEFT_PAREN, ctx)
// Might be call without args. // Might be call without args.
if p.peek().typ == ItemRightParen { if p.peek().typ == RIGHT_PAREN {
p.next() // Consume. p.next() // Consume.
return &Call{fn, nil} return &Call{fn, nil}
} }
@ -751,14 +751,14 @@ func (p *parser) call(name string) *Call {
args = append(args, e) args = append(args, e)
// Terminate if no more arguments. // Terminate if no more arguments.
if p.peek().typ != ItemComma { if p.peek().typ != COMMA {
break break
} }
p.next() p.next()
} }
// Call must be closed. // Call must be closed.
p.expect(ItemRightParen, ctx) p.expect(RIGHT_PAREN, ctx)
return &Call{Func: fn, Args: args} return &Call{Func: fn, Args: args}
} }
@ -769,7 +769,7 @@ func (p *parser) call(name string) *Call {
// //
func (p *parser) labelSet() labels.Labels { func (p *parser) labelSet() labels.Labels {
set := []labels.Label{} set := []labels.Label{}
for _, lm := range p.labelMatchers(ItemEQL) { for _, lm := range p.labelMatchers(EQL) {
set = append(set, labels.Label{Name: lm.Name, Value: lm.Value}) set = append(set, labels.Label{Name: lm.Name, Value: lm.Value})
} }
return labels.New(set...) return labels.New(set...)
@ -784,16 +784,16 @@ func (p *parser) labelMatchers(operators ...ItemType) []*labels.Matcher {
matchers := []*labels.Matcher{} matchers := []*labels.Matcher{}
p.expect(ItemLeftBrace, ctx) p.expect(LEFT_BRACE, ctx)
// Check if no matchers are provided. // Check if no matchers are provided.
if p.peek().typ == ItemRightBrace { if p.peek().typ == RIGHT_BRACE {
p.next() p.next()
return matchers return matchers
} }
for { for {
label := p.expect(ItemIdentifier, ctx) label := p.expect(IDENTIFIER, ctx)
op := p.next().typ op := p.next().typ
if !op.isOperator() { if !op.isOperator() {
@ -809,18 +809,18 @@ func (p *parser) labelMatchers(operators ...ItemType) []*labels.Matcher {
p.errorf("operator must be one of %q, is %q", operators, op) p.errorf("operator must be one of %q, is %q", operators, op)
} }
val := p.unquoteString(p.expect(ItemString, ctx).val) val := p.unquoteString(p.expect(STRING, ctx).val)
// Map the item to the respective match type. // Map the item to the respective match type.
var matchType labels.MatchType var matchType labels.MatchType
switch op { switch op {
case ItemEQL: case EQL:
matchType = labels.MatchEqual matchType = labels.MatchEqual
case ItemNEQ: case NEQ:
matchType = labels.MatchNotEqual matchType = labels.MatchNotEqual
case ItemEQLRegex: case EQL_REGEX:
matchType = labels.MatchRegexp matchType = labels.MatchRegexp
case ItemNEQRegex: case NEQ_REGEX:
matchType = labels.MatchNotRegexp matchType = labels.MatchNotRegexp
default: default:
p.errorf("item %q is not a metric match type", op) p.errorf("item %q is not a metric match type", op)
@ -833,23 +833,23 @@ func (p *parser) labelMatchers(operators ...ItemType) []*labels.Matcher {
matchers = append(matchers, m) matchers = append(matchers, m)
if p.peek().typ == ItemIdentifier { if p.peek().typ == IDENTIFIER {
p.errorf("missing comma before next identifier %q", p.peek().val) p.errorf("missing comma before next identifier %q", p.peek().val)
} }
// Terminate list if last matcher. // Terminate list if last matcher.
if p.peek().typ != ItemComma { if p.peek().typ != COMMA {
break break
} }
p.next() p.next()
// Allow comma after each item in a multi-line listing. // Allow comma after each item in a multi-line listing.
if p.peek().typ == ItemRightBrace { if p.peek().typ == RIGHT_BRACE {
break break
} }
} }
p.expect(ItemRightBrace, ctx) p.expect(RIGHT_BRACE, ctx)
return matchers return matchers
} }
@ -864,14 +864,14 @@ func (p *parser) metric() labels.Labels {
var m labels.Labels var m labels.Labels
t := p.peek().typ t := p.peek().typ
if t == ItemIdentifier || t == ItemMetricIdentifier { if t == IDENTIFIER || t == METRIC_IDENTIFIER {
name = p.next().val name = p.next().val
t = p.peek().typ t = p.peek().typ
} }
if t != ItemLeftBrace && name == "" { if t != LEFT_BRACE && name == "" {
p.errorf("missing metric name or metric selector") p.errorf("missing metric name or metric selector")
} }
if t == ItemLeftBrace { if t == LEFT_BRACE {
m = p.labelSet() m = p.labelSet()
} }
if name != "" { if name != "" {
@ -889,7 +889,7 @@ func (p *parser) offset() time.Duration {
const ctx = "offset" const ctx = "offset"
p.next() p.next()
offi := p.expect(ItemDuration, ctx) offi := p.expect(DURATION, ctx)
offset, err := parseDuration(offi.val) offset, err := parseDuration(offi.val)
if err != nil { if err != nil {
@ -907,8 +907,8 @@ func (p *parser) offset() time.Duration {
func (p *parser) VectorSelector(name string) *VectorSelector { func (p *parser) VectorSelector(name string) *VectorSelector {
var matchers []*labels.Matcher var matchers []*labels.Matcher
// Parse label matching if any. // Parse label matching if any.
if t := p.peek(); t.typ == ItemLeftBrace { if t := p.peek(); t.typ == LEFT_BRACE {
matchers = p.labelMatchers(ItemEQL, ItemNEQ, ItemEQLRegex, ItemNEQRegex) matchers = p.labelMatchers(EQL, NEQ, EQL_REGEX, NEQ_REGEX)
} }
// Metric name must not be set in the label matchers and before at the same time. // Metric name must not be set in the label matchers and before at the same time.
if name != "" { if name != "" {
@ -994,10 +994,10 @@ func (p *parser) checkType(node Node) (typ ValueType) {
p.errorf("aggregation operator expected in aggregation expression but got %q", n.Op) p.errorf("aggregation operator expected in aggregation expression but got %q", n.Op)
} }
p.expectType(n.Expr, ValueTypeVector, "aggregation expression") p.expectType(n.Expr, ValueTypeVector, "aggregation expression")
if n.Op == ItemTopK || n.Op == ItemBottomK || n.Op == ItemQuantile { if n.Op == TOPK || n.Op == BOTTOMK || n.Op == QUANTILE {
p.expectType(n.Param, ValueTypeScalar, "aggregation parameter") p.expectType(n.Param, ValueTypeScalar, "aggregation parameter")
} }
if n.Op == ItemCountValues { if n.Op == COUNT_VALUES {
p.expectType(n.Param, ValueTypeString, "aggregation parameter") p.expectType(n.Param, ValueTypeString, "aggregation parameter")
} }
@ -1059,7 +1059,7 @@ func (p *parser) checkType(node Node) (typ ValueType) {
p.checkType(n.Expr) p.checkType(n.Expr)
case *UnaryExpr: case *UnaryExpr:
if n.Op != ItemADD && n.Op != ItemSUB { if n.Op != ADD && n.Op != SUB {
p.errorf("only + and - operators allowed for unary expressions") p.errorf("only + and - operators allowed for unary expressions")
} }
if t := p.checkType(n.Expr); t != ValueTypeScalar && t != ValueTypeVector { if t := p.checkType(n.Expr); t != ValueTypeScalar && t != ValueTypeVector {

View File

@ -71,77 +71,77 @@ var testExpr = []struct {
expected: &NumberLiteral{-493}, expected: &NumberLiteral{-493},
}, { }, {
input: "1 + 1", input: "1 + 1",
expected: &BinaryExpr{ItemADD, &NumberLiteral{1}, &NumberLiteral{1}, nil, false}, expected: &BinaryExpr{ADD, &NumberLiteral{1}, &NumberLiteral{1}, nil, false},
}, { }, {
input: "1 - 1", input: "1 - 1",
expected: &BinaryExpr{ItemSUB, &NumberLiteral{1}, &NumberLiteral{1}, nil, false}, expected: &BinaryExpr{SUB, &NumberLiteral{1}, &NumberLiteral{1}, nil, false},
}, { }, {
input: "1 * 1", input: "1 * 1",
expected: &BinaryExpr{ItemMUL, &NumberLiteral{1}, &NumberLiteral{1}, nil, false}, expected: &BinaryExpr{MUL, &NumberLiteral{1}, &NumberLiteral{1}, nil, false},
}, { }, {
input: "1 % 1", input: "1 % 1",
expected: &BinaryExpr{ItemMOD, &NumberLiteral{1}, &NumberLiteral{1}, nil, false}, expected: &BinaryExpr{MOD, &NumberLiteral{1}, &NumberLiteral{1}, nil, false},
}, { }, {
input: "1 / 1", input: "1 / 1",
expected: &BinaryExpr{ItemDIV, &NumberLiteral{1}, &NumberLiteral{1}, nil, false}, expected: &BinaryExpr{DIV, &NumberLiteral{1}, &NumberLiteral{1}, nil, false},
}, { }, {
input: "1 == bool 1", input: "1 == bool 1",
expected: &BinaryExpr{ItemEQL, &NumberLiteral{1}, &NumberLiteral{1}, nil, true}, expected: &BinaryExpr{EQL, &NumberLiteral{1}, &NumberLiteral{1}, nil, true},
}, { }, {
input: "1 != bool 1", input: "1 != bool 1",
expected: &BinaryExpr{ItemNEQ, &NumberLiteral{1}, &NumberLiteral{1}, nil, true}, expected: &BinaryExpr{NEQ, &NumberLiteral{1}, &NumberLiteral{1}, nil, true},
}, { }, {
input: "1 > bool 1", input: "1 > bool 1",
expected: &BinaryExpr{ItemGTR, &NumberLiteral{1}, &NumberLiteral{1}, nil, true}, expected: &BinaryExpr{GTR, &NumberLiteral{1}, &NumberLiteral{1}, nil, true},
}, { }, {
input: "1 >= bool 1", input: "1 >= bool 1",
expected: &BinaryExpr{ItemGTE, &NumberLiteral{1}, &NumberLiteral{1}, nil, true}, expected: &BinaryExpr{GTE, &NumberLiteral{1}, &NumberLiteral{1}, nil, true},
}, { }, {
input: "1 < bool 1", input: "1 < bool 1",
expected: &BinaryExpr{ItemLSS, &NumberLiteral{1}, &NumberLiteral{1}, nil, true}, expected: &BinaryExpr{LSS, &NumberLiteral{1}, &NumberLiteral{1}, nil, true},
}, { }, {
input: "1 <= bool 1", input: "1 <= bool 1",
expected: &BinaryExpr{ItemLTE, &NumberLiteral{1}, &NumberLiteral{1}, nil, true}, expected: &BinaryExpr{LTE, &NumberLiteral{1}, &NumberLiteral{1}, nil, true},
}, { }, {
input: "+1 + -2 * 1", input: "+1 + -2 * 1",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemADD, Op: ADD,
LHS: &NumberLiteral{1}, LHS: &NumberLiteral{1},
RHS: &BinaryExpr{ RHS: &BinaryExpr{
Op: ItemMUL, LHS: &NumberLiteral{-2}, RHS: &NumberLiteral{1}, Op: MUL, LHS: &NumberLiteral{-2}, RHS: &NumberLiteral{1},
}, },
}, },
}, { }, {
input: "1 + 2/(3*1)", input: "1 + 2/(3*1)",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemADD, Op: ADD,
LHS: &NumberLiteral{1}, LHS: &NumberLiteral{1},
RHS: &BinaryExpr{ RHS: &BinaryExpr{
Op: ItemDIV, Op: DIV,
LHS: &NumberLiteral{2}, LHS: &NumberLiteral{2},
RHS: &ParenExpr{&BinaryExpr{ RHS: &ParenExpr{&BinaryExpr{
Op: ItemMUL, LHS: &NumberLiteral{3}, RHS: &NumberLiteral{1}, Op: MUL, LHS: &NumberLiteral{3}, RHS: &NumberLiteral{1},
}}, }},
}, },
}, },
}, { }, {
input: "1 < bool 2 - 1 * 2", input: "1 < bool 2 - 1 * 2",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLSS, Op: LSS,
ReturnBool: true, ReturnBool: true,
LHS: &NumberLiteral{1}, LHS: &NumberLiteral{1},
RHS: &BinaryExpr{ RHS: &BinaryExpr{
Op: ItemSUB, Op: SUB,
LHS: &NumberLiteral{2}, LHS: &NumberLiteral{2},
RHS: &BinaryExpr{ RHS: &BinaryExpr{
Op: ItemMUL, LHS: &NumberLiteral{1}, RHS: &NumberLiteral{2}, Op: MUL, LHS: &NumberLiteral{1}, RHS: &NumberLiteral{2},
}, },
}, },
}, },
}, { }, {
input: "-some_metric", input: "-some_metric",
expected: &UnaryExpr{ expected: &UnaryExpr{
Op: ItemSUB, Op: SUB,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -152,7 +152,7 @@ var testExpr = []struct {
}, { }, {
input: "+some_metric", input: "+some_metric",
expected: &UnaryExpr{ expected: &UnaryExpr{
Op: ItemADD, Op: ADD,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -261,7 +261,7 @@ var testExpr = []struct {
{ {
input: "foo * bar", input: "foo * bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemMUL, Op: MUL,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -279,7 +279,7 @@ var testExpr = []struct {
}, { }, {
input: "foo == 1", input: "foo == 1",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemEQL, Op: EQL,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -291,7 +291,7 @@ var testExpr = []struct {
}, { }, {
input: "foo == bool 1", input: "foo == bool 1",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemEQL, Op: EQL,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -304,7 +304,7 @@ var testExpr = []struct {
}, { }, {
input: "2.5 / bar", input: "2.5 / bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemDIV, Op: DIV,
LHS: &NumberLiteral{2.5}, LHS: &NumberLiteral{2.5},
RHS: &VectorSelector{ RHS: &VectorSelector{
Name: "bar", Name: "bar",
@ -316,7 +316,7 @@ var testExpr = []struct {
}, { }, {
input: "foo and bar", input: "foo and bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLAND, Op: LAND,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -334,7 +334,7 @@ var testExpr = []struct {
}, { }, {
input: "foo or bar", input: "foo or bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLOR, Op: LOR,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -352,7 +352,7 @@ var testExpr = []struct {
}, { }, {
input: "foo unless bar", input: "foo unless bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLUnless, Op: LUNLESS,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -371,9 +371,9 @@ var testExpr = []struct {
// Test and/or precedence and reassigning of operands. // Test and/or precedence and reassigning of operands.
input: "foo + bar or bla and blub", input: "foo + bar or bla and blub",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLOR, Op: LOR,
LHS: &BinaryExpr{ LHS: &BinaryExpr{
Op: ItemADD, Op: ADD,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -389,7 +389,7 @@ var testExpr = []struct {
VectorMatching: &VectorMatching{Card: CardOneToOne}, VectorMatching: &VectorMatching{Card: CardOneToOne},
}, },
RHS: &BinaryExpr{ RHS: &BinaryExpr{
Op: ItemLAND, Op: LAND,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "bla", Name: "bla",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -410,11 +410,11 @@ var testExpr = []struct {
// Test and/or/unless precedence. // Test and/or/unless precedence.
input: "foo and bar unless baz or qux", input: "foo and bar unless baz or qux",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLOR, Op: LOR,
LHS: &BinaryExpr{ LHS: &BinaryExpr{
Op: ItemLUnless, Op: LUNLESS,
LHS: &BinaryExpr{ LHS: &BinaryExpr{
Op: ItemLAND, Op: LAND,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -449,7 +449,7 @@ var testExpr = []struct {
// Test precedence and reassigning of operands. // Test precedence and reassigning of operands.
input: "bar + on(foo) bla / on(baz, buz) group_right(test) blub", input: "bar + on(foo) bla / on(baz, buz) group_right(test) blub",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemADD, Op: ADD,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "bar", Name: "bar",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -457,7 +457,7 @@ var testExpr = []struct {
}, },
}, },
RHS: &BinaryExpr{ RHS: &BinaryExpr{
Op: ItemDIV, Op: DIV,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "bla", Name: "bla",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -486,7 +486,7 @@ var testExpr = []struct {
}, { }, {
input: "foo * on(test,blub) bar", input: "foo * on(test,blub) bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemMUL, Op: MUL,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -508,7 +508,7 @@ var testExpr = []struct {
}, { }, {
input: "foo * on(test,blub) group_left bar", input: "foo * on(test,blub) group_left bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemMUL, Op: MUL,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -530,7 +530,7 @@ var testExpr = []struct {
}, { }, {
input: "foo and on(test,blub) bar", input: "foo and on(test,blub) bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLAND, Op: LAND,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -552,7 +552,7 @@ var testExpr = []struct {
}, { }, {
input: "foo and on() bar", input: "foo and on() bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLAND, Op: LAND,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -574,7 +574,7 @@ var testExpr = []struct {
}, { }, {
input: "foo and ignoring(test,blub) bar", input: "foo and ignoring(test,blub) bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLAND, Op: LAND,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -595,7 +595,7 @@ var testExpr = []struct {
}, { }, {
input: "foo and ignoring() bar", input: "foo and ignoring() bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLAND, Op: LAND,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -616,7 +616,7 @@ var testExpr = []struct {
}, { }, {
input: "foo unless on(bar) baz", input: "foo unless on(bar) baz",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemLUnless, Op: LUNLESS,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -638,7 +638,7 @@ var testExpr = []struct {
}, { }, {
input: "foo / on(test,blub) group_left(bar) bar", input: "foo / on(test,blub) group_left(bar) bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemDIV, Op: DIV,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -661,7 +661,7 @@ var testExpr = []struct {
}, { }, {
input: "foo / ignoring(test,blub) group_left(blub) bar", input: "foo / ignoring(test,blub) group_left(blub) bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemDIV, Op: DIV,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -683,7 +683,7 @@ var testExpr = []struct {
}, { }, {
input: "foo / ignoring(test,blub) group_left(bar) bar", input: "foo / ignoring(test,blub) group_left(bar) bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemDIV, Op: DIV,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -705,7 +705,7 @@ var testExpr = []struct {
}, { }, {
input: "foo - on(test,blub) group_right(bar,foo) bar", input: "foo - on(test,blub) group_right(bar,foo) bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemSUB, Op: SUB,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -728,7 +728,7 @@ var testExpr = []struct {
}, { }, {
input: "foo - ignoring(test,blub) group_right(bar,foo) bar", input: "foo - ignoring(test,blub) group_right(bar,foo) bar",
expected: &BinaryExpr{ expected: &BinaryExpr{
Op: ItemSUB, Op: SUB,
LHS: &VectorSelector{ LHS: &VectorSelector{
Name: "foo", Name: "foo",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -1057,7 +1057,7 @@ var testExpr = []struct {
{ {
input: "sum by (foo)(some_metric)", input: "sum by (foo)(some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemSum, Op: SUM,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -1069,7 +1069,7 @@ var testExpr = []struct {
}, { }, {
input: "avg by (foo)(some_metric)", input: "avg by (foo)(some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemAvg, Op: AVG,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -1081,7 +1081,7 @@ var testExpr = []struct {
}, { }, {
input: "max by (foo)(some_metric)", input: "max by (foo)(some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemMax, Op: MAX,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -1093,7 +1093,7 @@ var testExpr = []struct {
}, { }, {
input: "sum without (foo) (some_metric)", input: "sum without (foo) (some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemSum, Op: SUM,
Without: true, Without: true,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
@ -1106,7 +1106,7 @@ var testExpr = []struct {
}, { }, {
input: "sum (some_metric) without (foo)", input: "sum (some_metric) without (foo)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemSum, Op: SUM,
Without: true, Without: true,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
@ -1119,7 +1119,7 @@ var testExpr = []struct {
}, { }, {
input: "stddev(some_metric)", input: "stddev(some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemStddev, Op: STDDEV,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -1130,7 +1130,7 @@ var testExpr = []struct {
}, { }, {
input: "stdvar by (foo)(some_metric)", input: "stdvar by (foo)(some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemStdvar, Op: STDVAR,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -1142,7 +1142,7 @@ var testExpr = []struct {
}, { }, {
input: "sum by ()(some_metric)", input: "sum by ()(some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemSum, Op: SUM,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -1154,7 +1154,7 @@ var testExpr = []struct {
}, { }, {
input: "topk(5, some_metric)", input: "topk(5, some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemTopK, Op: TOPK,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -1166,7 +1166,7 @@ var testExpr = []struct {
}, { }, {
input: "count_values(\"value\", some_metric)", input: "count_values(\"value\", some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemCountValues, Op: COUNT_VALUES,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
LabelMatchers: []*labels.Matcher{ LabelMatchers: []*labels.Matcher{
@ -1179,7 +1179,7 @@ var testExpr = []struct {
// Test usage of keywords as label names. // Test usage of keywords as label names.
input: "sum without(and, by, avg, count, alert, annotations)(some_metric)", input: "sum without(and, by, avg, count, alert, annotations)(some_metric)",
expected: &AggregateExpr{ expected: &AggregateExpr{
Op: ItemSum, Op: SUM,
Without: true, Without: true,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
@ -1494,7 +1494,7 @@ var testExpr = []struct {
input: "sum without(and, by, avg, count, alert, annotations)(some_metric) [30m:10s]", input: "sum without(and, by, avg, count, alert, annotations)(some_metric) [30m:10s]",
expected: &SubqueryExpr{ expected: &SubqueryExpr{
Expr: &AggregateExpr{ Expr: &AggregateExpr{
Op: ItemSum, Op: SUM,
Without: true, Without: true,
Expr: &VectorSelector{ Expr: &VectorSelector{
Name: "some_metric", Name: "some_metric",
@ -1525,7 +1525,7 @@ var testExpr = []struct {
expected: &SubqueryExpr{ expected: &SubqueryExpr{
Expr: &ParenExpr{ Expr: &ParenExpr{
Expr: &BinaryExpr{ Expr: &BinaryExpr{
Op: ItemADD, Op: ADD,
VectorMatching: &VectorMatching{ VectorMatching: &VectorMatching{
Card: CardOneToOne, Card: CardOneToOne,
}, },
@ -1551,7 +1551,7 @@ var testExpr = []struct {
expected: &SubqueryExpr{ expected: &SubqueryExpr{
Expr: &ParenExpr{ Expr: &ParenExpr{
Expr: &BinaryExpr{ Expr: &BinaryExpr{
Op: ItemADD, Op: ADD,
VectorMatching: &VectorMatching{ VectorMatching: &VectorMatching{
Card: CardOneToOne, Card: CardOneToOne,
}, },