parseUnaryExpr parses an non-binary expression.
()
| 307 | |
| 308 | // parseUnaryExpr parses an non-binary expression. |
| 309 | func (p *Parser) parseUnaryExpr() (Expr, error) { |
| 310 | // If the first token is a LPAREN then parse it as its own grouped expression. |
| 311 | if tok, _, _ := p.scanIgnoreWhitespace(); tok == Lparen { |
| 312 | // parse a comma-separated list if this looks like a list |
| 313 | tagKeys, err := p.parseList() |
| 314 | if err != nil { |
| 315 | p.unscan() |
| 316 | // if it fails, try to parse the grouped expression |
| 317 | expr, err := p.ParseExpr() |
| 318 | if err != nil { |
| 319 | return nil, err |
| 320 | } |
| 321 | // Expect an RPAREN at the end. |
| 322 | if tok, pos, lit := p.scanIgnoreWhitespace(); tok != Rparen { |
| 323 | return nil, newParseError(tokstr(tok, lit), []string{"')'"}, pos, p.expr) |
| 324 | } |
| 325 | return &ParenExpr{Expr: expr}, nil |
| 326 | } |
| 327 | |
| 328 | // Expect an RPAREN at the end of list |
| 329 | if tok, pos, lit := p.scanIgnoreWhitespace(); tok != Rparen { |
| 330 | return nil, newParseError(tokstr(tok, lit), []string{"')'"}, pos, p.expr) |
| 331 | } |
| 332 | |
| 333 | return &ListLiteral{Values: tagKeys}, nil |
| 334 | } |
| 335 | |
| 336 | p.unscan() |
| 337 | |
| 338 | tok, pos, lit := p.scanIgnoreWhitespace() |
| 339 | switch tok { |
| 340 | case Ident: |
| 341 | if fields.IsField(lit) { |
| 342 | return p.parseField(lit) |
| 343 | } |
| 344 | |
| 345 | if tok0, _, _ := p.scan(); tok0 == Lparen { |
| 346 | return p.parseFunction(lit) |
| 347 | } |
| 348 | // unscan lparen token |
| 349 | p.unscan() |
| 350 | |
| 351 | // expand macros |
| 352 | if p.c != nil { |
| 353 | macro := p.c.GetMacro(lit) |
| 354 | if macro != nil { |
| 355 | if macro.Expr != "" { |
| 356 | p := NewParserWithConfig(macro.Expr, p.c) |
| 357 | expr, err := p.ParseExpr() |
| 358 | if err != nil { |
| 359 | return nil, multierror.WrapWithSeparator("\n", fmt.Errorf("syntax error in %q macro", lit), err) |
| 360 | } |
| 361 | return expr, nil |
| 362 | } |
| 363 | return &ListLiteral{Values: macro.List}, nil |
| 364 | } |
| 365 | // unscan ident |
| 366 | p.unscan() |