github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/configs/legacy_promql/parse.go (about)

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