parseFunction parses a function call. This method assumes the function name and LPAREN have been consumed.
(name string)
| 492 | // parseFunction parses a function call. This method assumes |
| 493 | // the function name and LPAREN have been consumed. |
| 494 | func (p *Parser) parseFunction(name string) (*Function, error) { |
| 495 | name = strings.ToLower(name) |
| 496 | args := make([]Expr, 0) |
| 497 | |
| 498 | // If there's a right paren then just return immediately. |
| 499 | // This is the case for functions without arguments |
| 500 | if tok, _, _ := p.scan(); tok == Rparen { |
| 501 | fn := &Function{Name: name} |
| 502 | if err := fn.validate(); err != nil { |
| 503 | return nil, err |
| 504 | } |
| 505 | return fn, nil |
| 506 | } |
| 507 | p.unscan() |
| 508 | |
| 509 | arg, err := p.ParseExpr() |
| 510 | if err != nil { |
| 511 | return nil, err |
| 512 | } |
| 513 | args = append(args, arg) |
| 514 | |
| 515 | // Parse additional function arguments if there is a comma. |
| 516 | for { |
| 517 | // If there's not a comma, stop parsing arguments. |
| 518 | if tok, _, _ := p.scanIgnoreWhitespace(); tok != Comma { |
| 519 | p.unscan() |
| 520 | break |
| 521 | } |
| 522 | |
| 523 | // Parse an expression argument. |
| 524 | arg, err := p.ParseExpr() |
| 525 | if err != nil { |
| 526 | return nil, err |
| 527 | } |
| 528 | args = append(args, arg) |
| 529 | } |
| 530 | |
| 531 | // There should be a right parentheses at the end. |
| 532 | if tok, pos, lit := p.scan(); tok != Rparen { |
| 533 | return nil, newParseError(tokstr(tok, lit), []string{")"}, pos, p.expr) |
| 534 | } |
| 535 | |
| 536 | fn := &Function{Name: name, Args: args} |
| 537 | |
| 538 | if err := fn.validate(); err != nil { |
| 539 | return nil, err |
| 540 | } |
| 541 | |
| 542 | return fn, nil |
| 543 | } |
| 544 | |
| 545 | // parseDuration parses a string and returns a duration literal. |
| 546 | func (p *Parser) parseDuration() (time.Duration, error) { |
no test coverage detected