parseUnaryExpr parses an non-binary expression. TODO: shz@ revisit inclusion parameter when open sourcing
(inclusion bool)
| 357 | // parseUnaryExpr parses an non-binary expression. |
| 358 | // TODO: shz@ revisit inclusion parameter when open sourcing |
| 359 | func (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 { |
no test coverage detected