(token Token)
| 722 | } |
| 723 | |
| 724 | func (p *Parser) parseMapExpression(token Token) Node { |
| 725 | p.expect(Bracket, "{") |
| 726 | |
| 727 | nodes := make([]Node, 0) |
| 728 | for !p.current.Is(Bracket, "}") && p.err == nil { |
| 729 | if len(nodes) > 0 { |
| 730 | p.expect(Operator, ",") |
| 731 | if p.current.Is(Bracket, "}") { |
| 732 | goto end |
| 733 | } |
| 734 | if p.current.Is(Operator, ",") { |
| 735 | p.error("unexpected token %v", p.current) |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | var key Node |
| 740 | // Map key can be one of: |
| 741 | // * number |
| 742 | // * string |
| 743 | // * identifier, which is equivalent to a string |
| 744 | // * expression, which must be enclosed in parentheses -- (1 + 2) |
| 745 | if p.current.Is(Number) || p.current.Is(String) || p.current.Is(Identifier) { |
| 746 | key = p.createNode(&StringNode{Value: p.current.Value}, p.current.Location) |
| 747 | if key == nil { |
| 748 | return nil |
| 749 | } |
| 750 | p.next() |
| 751 | } else if p.current.Is(Bracket, "(") { |
| 752 | key = p.parseExpression(0) |
| 753 | } else { |
| 754 | p.error("a map key must be a quoted string, a number, a identifier, or an expression enclosed in parentheses (unexpected token %v)", p.current) |
| 755 | } |
| 756 | |
| 757 | p.expect(Operator, ":") |
| 758 | |
| 759 | node := p.parseExpression(0) |
| 760 | pair := p.createNode(&PairNode{Key: key, Value: node}, token.Location) |
| 761 | if pair == nil { |
| 762 | return nil |
| 763 | } |
| 764 | nodes = append(nodes, pair) |
| 765 | } |
| 766 | |
| 767 | end: |
| 768 | p.expect(Bracket, "}") |
| 769 | |
| 770 | node := p.createNode(&MapNode{Pairs: nodes}, token.Location) |
| 771 | if node == nil { |
| 772 | return nil |
| 773 | } |
| 774 | return node |
| 775 | } |
| 776 | |
| 777 | func (p *Parser) parsePostfixExpression(node Node) Node { |
| 778 | postfixToken := p.current |
no test coverage detected