parseInfixExpression parses an infix expression that has a left operand and an operator followed by a right operand, such as "a or b" or "x and y". It takes the left operand as an argument, constructs an InfixExpression with the current operator and left operand, and parses the right operand with a
(left ast.Expression)
| 656 | // expression tree. |
| 657 | // It returns the resulting InfixExpression and any error encountered. |
| 658 | func (p *Parser) parseInfixExpression(left ast.Expression) (ast.Expression, error) { |
| 659 | // Ensure the current token is a valid infix operator before proceeding. |
| 660 | if !p.isInfixOperator(p.currentToken.Type) { |
| 661 | p.currentError(token.AND, token.OR, token.NOT) // Replace with your actual valid infix token types |
| 662 | return nil, p.Error() |
| 663 | } |
| 664 | |
| 665 | // Create a new InfixExpression with the left operand and the current operator. |
| 666 | expression := &ast.InfixExpression{ |
| 667 | Op: p.currentToken, |
| 668 | Left: left, |
| 669 | Operator: ast.Operator(p.currentToken.Literal), |
| 670 | } |
| 671 | |
| 672 | // Get the precedence of the current operator and consume the operator token. |
| 673 | precedence := p.currentPrecedence() |
| 674 | p.next() |
| 675 | |
| 676 | // Parse the right operand with a higher precedence to construct the final expression tree. |
| 677 | right, err := p.parseExpression(precedence) |
| 678 | if err != nil { |
| 679 | return nil, err |
| 680 | } |
| 681 | |
| 682 | // Ensure the right operand is not nil. |
| 683 | if right == nil { |
| 684 | p.currentError(token.IDENT, token.LP) // Replace with your actual valid right operand token types |
| 685 | return nil, p.Error() |
| 686 | } |
| 687 | |
| 688 | // Set the right operand of the InfixExpression and return it. |
| 689 | expression.Right = right |
| 690 | return expression, nil |
| 691 | } |
| 692 | |
| 693 | // parseIntegerLiteral parses an integer literal and returns the resulting IntegerLiteral expression. |
| 694 | func (p *Parser) isInfixOperator(tokenType token.Type) bool { |
nothing calls this directly
no test coverage detected