ParseMatchExpr parses a complete match expression Grammar: 'match' scrutinee '{' arm* '}'
()
| 27 | // ParseMatchExpr parses a complete match expression |
| 28 | // Grammar: 'match' scrutinee '{' arm* '}' |
| 29 | func (p *MatchParser) ParseMatchExpr() (*ast.MatchExpr, error) { |
| 30 | matchTok, err := p.tok.Expect(tokenizer.MATCH) |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | |
| 35 | // Parse scrutinee expression (everything until '{') |
| 36 | scrutinee, err := p.parseScrutinee() |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | |
| 41 | openBrace, err := p.tok.Expect(tokenizer.LBRACE) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | // Parse arms |
| 47 | arms, comments, err := p.parseArms() |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | |
| 52 | closeBrace, err := p.tok.Expect(tokenizer.RBRACE) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | |
| 57 | p.matchIDGen++ |
| 58 | |
| 59 | return &ast.MatchExpr{ |
| 60 | Match: matchTok.Pos, |
| 61 | Scrutinee: scrutinee, |
| 62 | OpenBrace: openBrace.Pos, |
| 63 | Arms: arms, |
| 64 | CloseBrace: closeBrace.Pos, |
| 65 | MatchID: p.matchIDGen, |
| 66 | Comments: comments, |
| 67 | }, nil |
| 68 | } |
| 69 | |
| 70 | // parseScrutinee parses the expression being matched |
| 71 | func (p *MatchParser) parseScrutinee() (ast.Expr, error) { |