1// Copyright 2015 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package promql
15
16import (
17	"fmt"
18	"math"
19	"os"
20	"runtime"
21	"sort"
22	"strconv"
23	"strings"
24	"time"
25
26	"github.com/prometheus/common/model"
27	"github.com/prometheus/prometheus/pkg/labels"
28	"github.com/prometheus/prometheus/pkg/value"
29
30	"github.com/prometheus/prometheus/util/strutil"
31)
32
33type parser struct {
34	lex       *lexer
35	token     [3]item
36	peekCount int
37}
38
39// ParseErr wraps a parsing error with line and position context.
40// If the parsing input was a single line, line will be 0 and omitted
41// from the error string.
42type ParseErr struct {
43	Line, Pos int
44	Err       error
45}
46
47func (e *ParseErr) Error() string {
48	if e.Line == 0 {
49		return fmt.Sprintf("parse error at char %d: %s", e.Pos, e.Err)
50	}
51	return fmt.Sprintf("parse error at line %d, char %d: %s", e.Line, e.Pos, e.Err)
52}
53
54// ParseExpr returns the expression parsed from the input.
55func ParseExpr(input string) (Expr, error) {
56	p := newParser(input)
57
58	expr, err := p.parseExpr()
59	if err != nil {
60		return nil, err
61	}
62	err = p.typecheck(expr)
63	return expr, err
64}
65
66// ParseMetric parses the input into a metric
67func ParseMetric(input string) (m labels.Labels, err error) {
68	p := newParser(input)
69	defer p.recover(&err)
70
71	m = p.metric()
72	if p.peek().typ != itemEOF {
73		p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
74	}
75	return m, nil
76}
77
78// ParseMetricSelector parses the provided textual metric selector into a list of
79// label matchers.
80func ParseMetricSelector(input string) (m []*labels.Matcher, err error) {
81	p := newParser(input)
82	defer p.recover(&err)
83
84	name := ""
85	if t := p.peek().typ; t == itemMetricIdentifier || t == itemIdentifier {
86		name = p.next().val
87	}
88	vs := p.VectorSelector(name)
89	if p.peek().typ != itemEOF {
90		p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
91	}
92	return vs.LabelMatchers, nil
93}
94
95// newParser returns a new parser.
96func newParser(input string) *parser {
97	p := &parser{
98		lex: lex(input),
99	}
100	return p
101}
102
103// parseExpr parses a single expression from the input.
104func (p *parser) parseExpr() (expr Expr, err error) {
105	defer p.recover(&err)
106
107	for p.peek().typ != itemEOF {
108		if p.peek().typ == itemComment {
109			continue
110		}
111		if expr != nil {
112			p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
113		}
114		expr = p.expr()
115	}
116
117	if expr == nil {
118		p.errorf("no expression found in input")
119	}
120	return
121}
122
123// sequenceValue is an omittable value in a sequence of time series values.
124type sequenceValue struct {
125	value   float64
126	omitted bool
127}
128
129func (v sequenceValue) String() string {
130	if v.omitted {
131		return "_"
132	}
133	return fmt.Sprintf("%f", v.value)
134}
135
136// parseSeriesDesc parses the description of a time series.
137func parseSeriesDesc(input string) (labels.Labels, []sequenceValue, error) {
138	p := newParser(input)
139	p.lex.seriesDesc = true
140
141	return p.parseSeriesDesc()
142}
143
144// parseSeriesDesc parses a description of a time series into its metric and value sequence.
145func (p *parser) parseSeriesDesc() (m labels.Labels, vals []sequenceValue, err error) {
146	defer p.recover(&err)
147
148	m = p.metric()
149
150	const ctx = "series values"
151	for {
152		for p.peek().typ == itemSpace {
153			p.next()
154		}
155		if p.peek().typ == itemEOF {
156			break
157		}
158
159		// Extract blanks.
160		if p.peek().typ == itemBlank {
161			p.next()
162			times := uint64(1)
163			if p.peek().typ == itemTimes {
164				p.next()
165				times, err = strconv.ParseUint(p.expect(itemNumber, ctx).val, 10, 64)
166				if err != nil {
167					p.errorf("invalid repetition in %s: %s", ctx, err)
168				}
169			}
170			for i := uint64(0); i < times; i++ {
171				vals = append(vals, sequenceValue{omitted: true})
172			}
173			// This is to ensure that there is a space between this and the next number.
174			// This is especially required if the next number is negative.
175			if t := p.expectOneOf(itemSpace, itemEOF, ctx).typ; t == itemEOF {
176				break
177			}
178			continue
179		}
180
181		// Extract values.
182		sign := 1.0
183		if t := p.peek().typ; t == itemSUB || t == itemADD {
184			if p.next().typ == itemSUB {
185				sign = -1
186			}
187		}
188		var k float64
189		if t := p.peek().typ; t == itemNumber {
190			k = sign * p.number(p.expect(itemNumber, ctx).val)
191		} else if t == itemIdentifier && p.peek().val == "stale" {
192			p.next()
193			k = math.Float64frombits(value.StaleNaN)
194		} else {
195			p.errorf("expected number or 'stale' in %s but got %s (value: %s)", ctx, t.desc(), p.peek())
196		}
197		vals = append(vals, sequenceValue{
198			value: k,
199		})
200
201		// If there are no offset repetitions specified, proceed with the next value.
202		if t := p.peek(); t.typ == itemSpace {
203			// This ensures there is a space between every value.
204			continue
205		} else if t.typ == itemEOF {
206			break
207		} else if t.typ != itemADD && t.typ != itemSUB {
208			p.errorf("expected next value or relative expansion in %s but got %s (value: %s)", ctx, t.desc(), p.peek())
209		}
210
211		// Expand the repeated offsets into values.
212		sign = 1.0
213		if p.next().typ == itemSUB {
214			sign = -1.0
215		}
216		offset := sign * p.number(p.expect(itemNumber, ctx).val)
217		p.expect(itemTimes, ctx)
218
219		times, err := strconv.ParseUint(p.expect(itemNumber, ctx).val, 10, 64)
220		if err != nil {
221			p.errorf("invalid repetition in %s: %s", ctx, err)
222		}
223
224		for i := uint64(0); i < times; i++ {
225			k += offset
226			vals = append(vals, sequenceValue{
227				value: k,
228			})
229		}
230		// This is to ensure that there is a space between this expanding notation
231		// and the next number. This is especially required if the next number
232		// is negative.
233		if t := p.expectOneOf(itemSpace, itemEOF, ctx).typ; t == itemEOF {
234			break
235		}
236	}
237	return m, vals, nil
238}
239
240// typecheck checks correct typing of the parsed statements or expression.
241func (p *parser) typecheck(node Node) (err error) {
242	defer p.recover(&err)
243
244	p.checkType(node)
245	return nil
246}
247
248// next returns the next token.
249func (p *parser) next() item {
250	if p.peekCount > 0 {
251		p.peekCount--
252	} else {
253		t := p.lex.nextItem()
254		// Skip comments.
255		for t.typ == itemComment {
256			t = p.lex.nextItem()
257		}
258		p.token[0] = t
259	}
260	if p.token[p.peekCount].typ == itemError {
261		p.errorf("%s", p.token[p.peekCount].val)
262	}
263	return p.token[p.peekCount]
264}
265
266// peek returns but does not consume the next token.
267func (p *parser) peek() item {
268	if p.peekCount > 0 {
269		return p.token[p.peekCount-1]
270	}
271	p.peekCount = 1
272
273	t := p.lex.nextItem()
274	// Skip comments.
275	for t.typ == itemComment {
276		t = p.lex.nextItem()
277	}
278	p.token[0] = t
279	return p.token[0]
280}
281
282// backup backs the input stream up one token.
283func (p *parser) backup() {
284	p.peekCount++
285}
286
287// errorf formats the error and terminates processing.
288func (p *parser) errorf(format string, args ...interface{}) {
289	p.error(fmt.Errorf(format, args...))
290}
291
292// error terminates processing.
293func (p *parser) error(err error) {
294	perr := &ParseErr{
295		Line: p.lex.lineNumber(),
296		Pos:  p.lex.linePosition(),
297		Err:  err,
298	}
299	if strings.Count(strings.TrimSpace(p.lex.input), "\n") == 0 {
300		perr.Line = 0
301	}
302	panic(perr)
303}
304
305// expect consumes the next token and guarantees it has the required type.
306func (p *parser) expect(exp ItemType, context string) item {
307	token := p.next()
308	if token.typ != exp {
309		p.errorf("unexpected %s in %s, expected %s", token.desc(), context, exp.desc())
310	}
311	return token
312}
313
314// expectOneOf consumes the next token and guarantees it has one of the required types.
315func (p *parser) expectOneOf(exp1, exp2 ItemType, context string) item {
316	token := p.next()
317	if token.typ != exp1 && token.typ != exp2 {
318		p.errorf("unexpected %s in %s, expected %s or %s", token.desc(), context, exp1.desc(), exp2.desc())
319	}
320	return token
321}
322
323var errUnexpected = fmt.Errorf("unexpected error")
324
325// recover is the handler that turns panics into returns from the top level of Parse.
326func (p *parser) recover(errp *error) {
327	e := recover()
328	if e != nil {
329		if _, ok := e.(runtime.Error); ok {
330			// Print the stack trace but do not inhibit the running application.
331			buf := make([]byte, 64<<10)
332			buf = buf[:runtime.Stack(buf, false)]
333
334			fmt.Fprintf(os.Stderr, "parser panic: %v\n%s", e, buf)
335			*errp = errUnexpected
336		} else {
337			*errp = e.(error)
338		}
339	}
340	p.lex.close()
341}
342
343// expr parses any expression.
344func (p *parser) expr() Expr {
345	// Parse the starting expression.
346	expr := p.unaryExpr()
347
348	// Loop through the operations and construct a binary operation tree based
349	// on the operators' precedence.
350	for {
351		// If the next token is not an operator the expression is done.
352		op := p.peek().typ
353		if !op.isOperator() {
354			return expr
355		}
356		p.next() // Consume operator.
357
358		// Parse optional operator matching options. Its validity
359		// is checked in the type-checking stage.
360		vecMatching := &VectorMatching{
361			Card: CardOneToOne,
362		}
363		if op.isSetOperator() {
364			vecMatching.Card = CardManyToMany
365		}
366
367		returnBool := false
368		// Parse bool modifier.
369		if p.peek().typ == itemBool {
370			if !op.isComparisonOperator() {
371				p.errorf("bool modifier can only be used on comparison operators")
372			}
373			p.next()
374			returnBool = true
375		}
376
377		// Parse ON/IGNORING clause.
378		if p.peek().typ == itemOn || p.peek().typ == itemIgnoring {
379			if p.peek().typ == itemOn {
380				vecMatching.On = true
381			}
382			p.next()
383			vecMatching.MatchingLabels = p.labels()
384
385			// Parse grouping.
386			if t := p.peek().typ; t == itemGroupLeft || t == itemGroupRight {
387				p.next()
388				if t == itemGroupLeft {
389					vecMatching.Card = CardManyToOne
390				} else {
391					vecMatching.Card = CardOneToMany
392				}
393				if p.peek().typ == itemLeftParen {
394					vecMatching.Include = p.labels()
395				}
396			}
397		}
398
399		for _, ln := range vecMatching.MatchingLabels {
400			for _, ln2 := range vecMatching.Include {
401				if ln == ln2 && vecMatching.On {
402					p.errorf("label %q must not occur in ON and GROUP clause at once", ln)
403				}
404			}
405		}
406
407		// Parse the next operand.
408		rhs := p.unaryExpr()
409
410		// Assign the new root based on the precedence of the LHS and RHS operators.
411		expr = p.balance(expr, op, rhs, vecMatching, returnBool)
412	}
413}
414
415func (p *parser) balance(lhs Expr, op ItemType, rhs Expr, vecMatching *VectorMatching, returnBool bool) *BinaryExpr {
416	if lhsBE, ok := lhs.(*BinaryExpr); ok {
417		precd := lhsBE.Op.precedence() - op.precedence()
418		if (precd < 0) || (precd == 0 && op.isRightAssociative()) {
419			balanced := p.balance(lhsBE.RHS, op, rhs, vecMatching, returnBool)
420			if lhsBE.Op.isComparisonOperator() && !lhsBE.ReturnBool && balanced.Type() == ValueTypeScalar && lhsBE.LHS.Type() == ValueTypeScalar {
421				p.errorf("comparisons between scalars must use BOOL modifier")
422			}
423			return &BinaryExpr{
424				Op:             lhsBE.Op,
425				LHS:            lhsBE.LHS,
426				RHS:            balanced,
427				VectorMatching: lhsBE.VectorMatching,
428				ReturnBool:     lhsBE.ReturnBool,
429			}
430		}
431	}
432	if op.isComparisonOperator() && !returnBool && rhs.Type() == ValueTypeScalar && lhs.Type() == ValueTypeScalar {
433		p.errorf("comparisons between scalars must use BOOL modifier")
434	}
435	return &BinaryExpr{
436		Op:             op,
437		LHS:            lhs,
438		RHS:            rhs,
439		VectorMatching: vecMatching,
440		ReturnBool:     returnBool,
441	}
442}
443
444// unaryExpr parses a unary expression.
445//
446//		<Vector_selector> | <Matrix_selector> | (+|-) <number_literal> | '(' <expr> ')'
447//
448func (p *parser) unaryExpr() Expr {
449	switch t := p.peek(); t.typ {
450	case itemADD, itemSUB:
451		p.next()
452		e := p.unaryExpr()
453
454		// Simplify unary expressions for number literals.
455		if nl, ok := e.(*NumberLiteral); ok {
456			if t.typ == itemSUB {
457				nl.Val *= -1
458			}
459			return nl
460		}
461		return &UnaryExpr{Op: t.typ, Expr: e}
462
463	case itemLeftParen:
464		p.next()
465		e := p.expr()
466		p.expect(itemRightParen, "paren expression")
467
468		return &ParenExpr{Expr: e}
469	}
470	e := p.primaryExpr()
471
472	// Expression might be followed by a range selector.
473	if p.peek().typ == itemLeftBracket {
474		vs, ok := e.(*VectorSelector)
475		if !ok {
476			p.errorf("range specification must be preceded by a metric selector, but follows a %T instead", e)
477		}
478		e = p.rangeSelector(vs)
479	}
480
481	// Parse optional offset.
482	if p.peek().typ == itemOffset {
483		offset := p.offset()
484
485		switch s := e.(type) {
486		case *VectorSelector:
487			s.Offset = offset
488		case *MatrixSelector:
489			s.Offset = offset
490		default:
491			p.errorf("offset modifier must be preceded by an instant or range selector, but follows a %T instead", e)
492		}
493	}
494
495	return e
496}
497
498// rangeSelector parses a Matrix (a.k.a. range) selector based on a given
499// Vector selector.
500//
501//		<Vector_selector> '[' <duration> ']'
502//
503func (p *parser) rangeSelector(vs *VectorSelector) *MatrixSelector {
504	const ctx = "range selector"
505	p.next()
506
507	var erange time.Duration
508	var err error
509
510	erangeStr := p.expect(itemDuration, ctx).val
511	erange, err = parseDuration(erangeStr)
512	if err != nil {
513		p.error(err)
514	}
515
516	p.expect(itemRightBracket, ctx)
517
518	e := &MatrixSelector{
519		Name:          vs.Name,
520		LabelMatchers: vs.LabelMatchers,
521		Range:         erange,
522	}
523	return e
524}
525
526// number parses a number.
527func (p *parser) number(val string) float64 {
528	n, err := strconv.ParseInt(val, 0, 64)
529	f := float64(n)
530	if err != nil {
531		f, err = strconv.ParseFloat(val, 64)
532	}
533	if err != nil {
534		p.errorf("error parsing number: %s", err)
535	}
536	return f
537}
538
539// primaryExpr parses a primary expression.
540//
541//		<metric_name> | <function_call> | <Vector_aggregation> | <literal>
542//
543func (p *parser) primaryExpr() Expr {
544	switch t := p.next(); {
545	case t.typ == itemNumber:
546		f := p.number(t.val)
547		return &NumberLiteral{f}
548
549	case t.typ == itemString:
550		return &StringLiteral{p.unquoteString(t.val)}
551
552	case t.typ == itemLeftBrace:
553		// Metric selector without metric name.
554		p.backup()
555		return p.VectorSelector("")
556
557	case t.typ == itemIdentifier:
558		// Check for function call.
559		if p.peek().typ == itemLeftParen {
560			return p.call(t.val)
561		}
562		fallthrough // Else metric selector.
563
564	case t.typ == itemMetricIdentifier:
565		return p.VectorSelector(t.val)
566
567	case t.typ.isAggregator():
568		p.backup()
569		return p.aggrExpr()
570
571	default:
572		p.errorf("no valid expression found")
573	}
574	return nil
575}
576
577// labels parses a list of labelnames.
578//
579//		'(' <label_name>, ... ')'
580//
581func (p *parser) labels() []string {
582	const ctx = "grouping opts"
583
584	p.expect(itemLeftParen, ctx)
585
586	labels := []string{}
587	if p.peek().typ != itemRightParen {
588		for {
589			id := p.next()
590			if !isLabel(id.val) {
591				p.errorf("unexpected %s in %s, expected label", id.desc(), ctx)
592			}
593			labels = append(labels, id.val)
594
595			if p.peek().typ != itemComma {
596				break
597			}
598			p.next()
599		}
600	}
601	p.expect(itemRightParen, ctx)
602
603	return labels
604}
605
606// aggrExpr parses an aggregation expression.
607//
608//		<aggr_op> (<Vector_expr>) [by|without <labels>]
609//		<aggr_op> [by|without <labels>] (<Vector_expr>)
610//
611func (p *parser) aggrExpr() *AggregateExpr {
612	const ctx = "aggregation"
613
614	agop := p.next()
615	if !agop.typ.isAggregator() {
616		p.errorf("expected aggregation operator but got %s", agop)
617	}
618	var grouping []string
619	var without bool
620
621	modifiersFirst := false
622
623	if t := p.peek().typ; t == itemBy || t == itemWithout {
624		if t == itemWithout {
625			without = true
626		}
627		p.next()
628		grouping = p.labels()
629		modifiersFirst = true
630	}
631
632	p.expect(itemLeftParen, ctx)
633	var param Expr
634	if agop.typ.isAggregatorWithParam() {
635		param = p.expr()
636		p.expect(itemComma, ctx)
637	}
638	e := p.expr()
639	p.expect(itemRightParen, ctx)
640
641	if !modifiersFirst {
642		if t := p.peek().typ; t == itemBy || t == itemWithout {
643			if len(grouping) > 0 {
644				p.errorf("aggregation must only contain one grouping clause")
645			}
646			if t == itemWithout {
647				without = true
648			}
649			p.next()
650			grouping = p.labels()
651		}
652	}
653
654	return &AggregateExpr{
655		Op:       agop.typ,
656		Expr:     e,
657		Param:    param,
658		Grouping: grouping,
659		Without:  without,
660	}
661}
662
663// call parses a function call.
664//
665//		<func_name> '(' [ <arg_expr>, ...] ')'
666//
667func (p *parser) call(name string) *Call {
668	const ctx = "function call"
669
670	fn, exist := getFunction(name)
671	if !exist {
672		p.errorf("unknown function with name %q", name)
673	}
674
675	p.expect(itemLeftParen, ctx)
676	// Might be call without args.
677	if p.peek().typ == itemRightParen {
678		p.next() // Consume.
679		return &Call{fn, nil}
680	}
681
682	var args []Expr
683	for {
684		e := p.expr()
685		args = append(args, e)
686
687		// Terminate if no more arguments.
688		if p.peek().typ != itemComma {
689			break
690		}
691		p.next()
692	}
693
694	// Call must be closed.
695	p.expect(itemRightParen, ctx)
696
697	return &Call{Func: fn, Args: args}
698}
699
700// labelSet parses a set of label matchers
701//
702//		'{' [ <labelname> '=' <match_string>, ... ] '}'
703//
704func (p *parser) labelSet() labels.Labels {
705	set := []labels.Label{}
706	for _, lm := range p.labelMatchers(itemEQL) {
707		set = append(set, labels.Label{Name: lm.Name, Value: lm.Value})
708	}
709	return labels.New(set...)
710}
711
712// labelMatchers parses a set of label matchers.
713//
714//		'{' [ <labelname> <match_op> <match_string>, ... ] '}'
715//
716func (p *parser) labelMatchers(operators ...ItemType) []*labels.Matcher {
717	const ctx = "label matching"
718
719	matchers := []*labels.Matcher{}
720
721	p.expect(itemLeftBrace, ctx)
722
723	// Check if no matchers are provided.
724	if p.peek().typ == itemRightBrace {
725		p.next()
726		return matchers
727	}
728
729	for {
730		label := p.expect(itemIdentifier, ctx)
731
732		op := p.next().typ
733		if !op.isOperator() {
734			p.errorf("expected label matching operator but got %s", op)
735		}
736		var validOp = false
737		for _, allowedOp := range operators {
738			if op == allowedOp {
739				validOp = true
740			}
741		}
742		if !validOp {
743			p.errorf("operator must be one of %q, is %q", operators, op)
744		}
745
746		val := p.unquoteString(p.expect(itemString, ctx).val)
747
748		// Map the item to the respective match type.
749		var matchType labels.MatchType
750		switch op {
751		case itemEQL:
752			matchType = labels.MatchEqual
753		case itemNEQ:
754			matchType = labels.MatchNotEqual
755		case itemEQLRegex:
756			matchType = labels.MatchRegexp
757		case itemNEQRegex:
758			matchType = labels.MatchNotRegexp
759		default:
760			p.errorf("item %q is not a metric match type", op)
761		}
762
763		m, err := labels.NewMatcher(matchType, label.val, val)
764		if err != nil {
765			p.error(err)
766		}
767
768		matchers = append(matchers, m)
769
770		if p.peek().typ == itemIdentifier {
771			p.errorf("missing comma before next identifier %q", p.peek().val)
772		}
773
774		// Terminate list if last matcher.
775		if p.peek().typ != itemComma {
776			break
777		}
778		p.next()
779
780		// Allow comma after each item in a multi-line listing.
781		if p.peek().typ == itemRightBrace {
782			break
783		}
784	}
785
786	p.expect(itemRightBrace, ctx)
787
788	return matchers
789}
790
791// metric parses a metric.
792//
793//		<label_set>
794//		<metric_identifier> [<label_set>]
795//
796func (p *parser) metric() labels.Labels {
797	name := ""
798	var m labels.Labels
799
800	t := p.peek().typ
801	if t == itemIdentifier || t == itemMetricIdentifier {
802		name = p.next().val
803		t = p.peek().typ
804	}
805	if t != itemLeftBrace && name == "" {
806		p.errorf("missing metric name or metric selector")
807	}
808	if t == itemLeftBrace {
809		m = p.labelSet()
810	}
811	if name != "" {
812		m = append(m, labels.Label{Name: labels.MetricName, Value: name})
813		sort.Sort(m)
814	}
815	return m
816}
817
818// offset parses an offset modifier.
819//
820//		offset <duration>
821//
822func (p *parser) offset() time.Duration {
823	const ctx = "offset"
824
825	p.next()
826	offi := p.expect(itemDuration, ctx)
827
828	offset, err := parseDuration(offi.val)
829	if err != nil {
830		p.error(err)
831	}
832
833	return offset
834}
835
836// VectorSelector parses a new (instant) vector selector.
837//
838//		<metric_identifier> [<label_matchers>]
839//		[<metric_identifier>] <label_matchers>
840//
841func (p *parser) VectorSelector(name string) *VectorSelector {
842	var matchers []*labels.Matcher
843	// Parse label matching if any.
844	if t := p.peek(); t.typ == itemLeftBrace {
845		matchers = p.labelMatchers(itemEQL, itemNEQ, itemEQLRegex, itemNEQRegex)
846	}
847	// Metric name must not be set in the label matchers and before at the same time.
848	if name != "" {
849		for _, m := range matchers {
850			if m.Name == labels.MetricName {
851				p.errorf("metric name must not be set twice: %q or %q", name, m.Value)
852			}
853		}
854		// Set name label matching.
855		m, err := labels.NewMatcher(labels.MatchEqual, labels.MetricName, name)
856		if err != nil {
857			panic(err) // Must not happen with metric.Equal.
858		}
859		matchers = append(matchers, m)
860	}
861
862	if len(matchers) == 0 {
863		p.errorf("vector selector must contain label matchers or metric name")
864	}
865	// A Vector selector must contain at least one non-empty matcher to prevent
866	// implicit selection of all metrics (e.g. by a typo).
867	notEmpty := false
868	for _, lm := range matchers {
869		if !lm.Matches("") {
870			notEmpty = true
871			break
872		}
873	}
874	if !notEmpty {
875		p.errorf("vector selector must contain at least one non-empty matcher")
876	}
877
878	return &VectorSelector{
879		Name:          name,
880		LabelMatchers: matchers,
881	}
882}
883
884// expectType checks the type of the node and raises an error if it
885// is not of the expected type.
886func (p *parser) expectType(node Node, want ValueType, context string) {
887	t := p.checkType(node)
888	if t != want {
889		p.errorf("expected type %s in %s, got %s", documentedType(want), context, documentedType(t))
890	}
891}
892
893// check the types of the children of each node and raise an error
894// if they do not form a valid node.
895//
896// Some of these checks are redundant as the the parsing stage does not allow
897// them, but the costs are small and might reveal errors when making changes.
898func (p *parser) checkType(node Node) (typ ValueType) {
899	// For expressions the type is determined by their Type function.
900	// Lists do not have a type but are not invalid either.
901	switch n := node.(type) {
902	case Expressions:
903		typ = ValueTypeNone
904	case Expr:
905		typ = n.Type()
906	default:
907		p.errorf("unknown node type: %T", node)
908	}
909
910	// Recursively check correct typing for child nodes and raise
911	// errors in case of bad typing.
912	switch n := node.(type) {
913	case *EvalStmt:
914		ty := p.checkType(n.Expr)
915		if ty == ValueTypeNone {
916			p.errorf("evaluation statement must have a valid expression type but got %s", documentedType(ty))
917		}
918
919	case Expressions:
920		for _, e := range n {
921			ty := p.checkType(e)
922			if ty == ValueTypeNone {
923				p.errorf("expression must have a valid expression type but got %s", documentedType(ty))
924			}
925		}
926	case *AggregateExpr:
927		if !n.Op.isAggregator() {
928			p.errorf("aggregation operator expected in aggregation expression but got %q", n.Op)
929		}
930		p.expectType(n.Expr, ValueTypeVector, "aggregation expression")
931		if n.Op == itemTopK || n.Op == itemBottomK || n.Op == itemQuantile {
932			p.expectType(n.Param, ValueTypeScalar, "aggregation parameter")
933		}
934		if n.Op == itemCountValues {
935			p.expectType(n.Param, ValueTypeString, "aggregation parameter")
936		}
937
938	case *BinaryExpr:
939		lt := p.checkType(n.LHS)
940		rt := p.checkType(n.RHS)
941
942		if !n.Op.isOperator() {
943			p.errorf("binary expression does not support operator %q", n.Op)
944		}
945		if (lt != ValueTypeScalar && lt != ValueTypeVector) || (rt != ValueTypeScalar && rt != ValueTypeVector) {
946			p.errorf("binary expression must contain only scalar and instant vector types")
947		}
948
949		if (lt != ValueTypeVector || rt != ValueTypeVector) && n.VectorMatching != nil {
950			if len(n.VectorMatching.MatchingLabels) > 0 {
951				p.errorf("vector matching only allowed between instant vectors")
952			}
953			n.VectorMatching = nil
954		} else {
955			// Both operands are Vectors.
956			if n.Op.isSetOperator() {
957				if n.VectorMatching.Card == CardOneToMany || n.VectorMatching.Card == CardManyToOne {
958					p.errorf("no grouping allowed for %q operation", n.Op)
959				}
960				if n.VectorMatching.Card != CardManyToMany {
961					p.errorf("set operations must always be many-to-many")
962				}
963			}
964		}
965
966		if (lt == ValueTypeScalar || rt == ValueTypeScalar) && n.Op.isSetOperator() {
967			p.errorf("set operator %q not allowed in binary scalar expression", n.Op)
968		}
969
970	case *Call:
971		nargs := len(n.Func.ArgTypes)
972		if n.Func.Variadic == 0 {
973			if nargs != len(n.Args) {
974				p.errorf("expected %d argument(s) in call to %q, got %d", nargs, n.Func.Name, len(n.Args))
975			}
976		} else {
977			na := nargs - 1
978			if na > len(n.Args) {
979				p.errorf("expected at least %d argument(s) in call to %q, got %d", na, n.Func.Name, len(n.Args))
980			} else if nargsmax := na + n.Func.Variadic; n.Func.Variadic > 0 && nargsmax < len(n.Args) {
981				p.errorf("expected at most %d argument(s) in call to %q, got %d", nargsmax, n.Func.Name, len(n.Args))
982			}
983		}
984
985		for i, arg := range n.Args {
986			if i >= len(n.Func.ArgTypes) {
987				i = len(n.Func.ArgTypes) - 1
988			}
989			p.expectType(arg, n.Func.ArgTypes[i], fmt.Sprintf("call to function %q", n.Func.Name))
990		}
991
992	case *ParenExpr:
993		p.checkType(n.Expr)
994
995	case *UnaryExpr:
996		if n.Op != itemADD && n.Op != itemSUB {
997			p.errorf("only + and - operators allowed for unary expressions")
998		}
999		if t := p.checkType(n.Expr); t != ValueTypeScalar && t != ValueTypeVector {
1000			p.errorf("unary expression only allowed on expressions of type scalar or instant vector, got %q", documentedType(t))
1001		}
1002
1003	case *NumberLiteral, *MatrixSelector, *StringLiteral, *VectorSelector:
1004		// Nothing to do for terminals.
1005
1006	default:
1007		p.errorf("unknown node type: %T", node)
1008	}
1009	return
1010}
1011
1012func (p *parser) unquoteString(s string) string {
1013	unquoted, err := strutil.Unquote(s)
1014	if err != nil {
1015		p.errorf("error unquoting string %q: %s", s, err)
1016	}
1017	return unquoted
1018}
1019
1020func parseDuration(ds string) (time.Duration, error) {
1021	dur, err := model.ParseDuration(ds)
1022	if err != nil {
1023		return 0, err
1024	}
1025	if dur == 0 {
1026		return 0, fmt.Errorf("duration must be greater than 0")
1027	}
1028	return time.Duration(dur), nil
1029}
1030