| 1 | package parser |
| 2 | |
| 3 | import ( |
| 4 | "github.com/tomwright/dasel/v3/selector/ast" |
| 5 | "github.com/tomwright/dasel/v3/selector/lexer" |
| 6 | ) |
| 7 | |
| 8 | func parseTernary(p *Parser, cond ast.Expr) (ast.Expr, error) { |
| 9 | // consume '?' |
| 10 | if err := p.expect(lexer.QuestionMark); err != nil { |
| 11 | return nil, err |
| 12 | } |
| 13 | p.advance() |
| 14 | |
| 15 | // parse the "then" expression — stops at Colon since it has no binding power |
| 16 | then, err := p.parseExpression(bpDefault) |
| 17 | if err != nil { |
| 18 | return nil, err |
| 19 | } |
| 20 | if then == nil { |
| 21 | return nil, &UnexpectedTokenError{Token: p.current()} |
| 22 | } |
| 23 | |
| 24 | // expect and consume ':' |
| 25 | if err := p.expect(lexer.Colon); err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | p.advance() |
| 29 | |
| 30 | // parse the "else" expression |
| 31 | els, err := p.parseExpression(bpDefault) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | if els == nil { |
| 36 | return nil, &UnexpectedTokenError{Token: p.current()} |
| 37 | } |
| 38 | |
| 39 | return ast.ConditionalExpr{Cond: cond, Then: then, Else: els}, nil |
| 40 | } |