MCPcopy
hub / github.com/uber/aresdb / parseUnaryExpr

Method parseUnaryExpr

query/expr/parser.go:359–473  ·  view source on GitHub ↗

parseUnaryExpr parses an non-binary expression. TODO: shz@ revisit inclusion parameter when open sourcing

(inclusion bool)

Source from the content-addressed store, hash-verified

357// parseUnaryExpr parses an non-binary expression.
358// TODO: shz@ revisit inclusion parameter when open sourcing
359func (p *Parser) parseUnaryExpr(inclusion bool) (Expr, error) {
360 // If the first token is a LPAREN then parse it as its own grouped expression.
361 if tok, _, _ := p.scanIgnoreWhitespace(); tok == LPAREN {
362 expr, err := p.ParseExpr(0)
363 if err != nil {
364 return nil, err
365 }
366 tok, pos, lit := p.scanIgnoreWhitespace()
367 if tok == RPAREN {
368 // Expect an RPAREN at the end.
369 if inclusion {
370 return &Call{Args: []Expr{expr}}, nil
371 }
372 return &ParenExpr{Expr: expr}, nil
373 } else if tok == COMMA {
374 // Parse a tuple as a function call with empty name.
375 var args []Expr
376 args = append(args, expr)
377
378 for {
379 // Parse an expression argument.
380 arg, err := p.ParseExpr(0)
381 if err != nil {
382 return nil, err
383 }
384 args = append(args, arg)
385
386 // If there's not a comma next then stop parsing arguments.
387 if tok, _, _ := p.scan(); tok != COMMA {
388 p.unscan()
389 break
390 }
391 }
392
393 // There should be a right parentheses at the end.
394 if tok, pos, lit := p.scan(); tok != RPAREN {
395 return nil, newParseError(tokstr(tok, lit), []string{")"}, pos)
396 }
397
398 return &Call{Args: args}, nil
399 } else {
400 return nil, newParseError(tokstr(tok, lit), []string{")"}, pos)
401 }
402
403 }
404 p.unscan()
405
406 // Read next token.
407 tok, pos, lit := p.scanIgnoreWhitespace()
408 if tok.isUnaryOperator() {
409 expr, err := p.ParseExpr(tok.Precedence())
410 if err != nil {
411 return nil, err
412 }
413 return &UnaryExpr{Op: tok, Expr: expr}, nil
414 }
415
416 switch tok {

Callers 1

ParseExprMethod · 0.95

Calls 11

scanIgnoreWhitespaceMethod · 0.95
ParseExprMethod · 0.95
scanMethod · 0.95
unscanMethod · 0.95
parseCaseMethod · 0.95
parseCallMethod · 0.95
parseVarRefMethod · 0.95
newParseErrorFunction · 0.85
tokstrFunction · 0.85
isUnaryOperatorMethod · 0.80
PrecedenceMethod · 0.80

Tested by

no test coverage detected