(canBeStructName *bool)
| 810 | } |
| 811 | |
| 812 | func (p *prattParserWorker) parsePrimary(canBeStructName *bool) ast.Expr { |
| 813 | if canBeStructName != nil { |
| 814 | *canBeStructName = false |
| 815 | } |
| 816 | switch p.peekTok.kind { |
| 817 | case tokLeftParen: |
| 818 | if p.recursionLimitExceeded || p.isRecoveryLimitExceeded() { |
| 819 | return p.helper.newExpr(common.NoLocation) |
| 820 | } |
| 821 | // To avoid deep call-stack recursion on heavily nested parentheses (e.g. |
| 822 | // "((((a))))" or "((((a + 1) + 1) + 1))"), consume all consecutive |
| 823 | // leading '(' tokens upfront, parse the innermost expression once, and |
| 824 | // then iteratively unwind each enclosing '(' from innermost to outermost. |
| 825 | // After consuming each matching ')', if more enclosing '(' remain open |
| 826 | // and the next token is not another ')', continue parsing any trailing |
| 827 | // selectors or binary/ternary operators belonging to that enclosing |
| 828 | // parenthesized level using the already-parsed inner expression as the |
| 829 | // LHS. |
| 830 | openParens := 0 |
| 831 | for p.peekTok.kind == tokLeftParen { |
| 832 | openParens++ |
| 833 | p.nextToken() |
| 834 | } |
| 835 | // Every '(' nested inside the enclosing expression is a nesting level and |
| 836 | // costs one unit of recursion budget, so charge all of them here: the |
| 837 | // innermost of openParens parentheses sits at recursionDepth + openParens - 1. |
| 838 | // recursionDepth itself only advances by 1 because the parens are unwound |
| 839 | // iteratively and entering parseBinaryAndTernary(0) below adds just one |
| 840 | // stack frame. Parentheses do not add to the depth of the AST. |
| 841 | if p.checkRecursion(p.recursionDepth + openParens - 1) { |
| 842 | return p.helper.newExpr(common.NoLocation) |
| 843 | } |
| 844 | p.recursionDepth++ |
| 845 | expr := p.parseBinaryAndTernary(0) |
| 846 | depth := p.lastParsedDepth |
| 847 | for i := 0; i < openParens; i++ { |
| 848 | p.expect(tokRightParen, "expected ')'") |
| 849 | if i < openParens-1 && p.peekTok.kind != tokRightParen { |
| 850 | tok := p.peekTok.kind |
| 851 | if tok == tokDot || tok == tokLeftBracket || tok == tokLeftBrace { |
| 852 | expr = p.parseSelectorChainTail(expr, false, depth) |
| 853 | depth = p.lastParsedDepth |
| 854 | } |
| 855 | expr = p.parseBinaryAndTernaryFromLhs(expr, 0, depth) |
| 856 | depth = p.lastParsedDepth |
| 857 | } |
| 858 | } |
| 859 | p.recursionDepth-- |
| 860 | p.lastParsedDepth = depth |
| 861 | return expr |
| 862 | case tokNull: |
| 863 | return p.helper.exprFactory.NewLiteral(p.nextID(p.nextToken()), types.NullValue) |
| 864 | case tokTrue: |
| 865 | tok := p.nextToken() |
| 866 | return p.helper.newLiteralBool(p.nextID(tok), true) |
| 867 | case tokFalse: |
| 868 | tok := p.nextToken() |
| 869 | return p.helper.newLiteralBool(p.nextID(tok), false) |
no test coverage detected