< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

Print this page




 876             return t;
 877         }
 878     }
 879 
 880     /** Expression2   = Expression3 [Expression2Rest]
 881      *  Type2         = Type3
 882      *  TypeNoParams2 = TypeNoParams3
 883      */
 884     JCExpression term2() {
 885         JCExpression t = term3();
 886         if ((mode & EXPR) != 0 && prec(token.kind) >= TreeInfo.orPrec) {
 887             selectExprMode();
 888             return term2Rest(t, TreeInfo.orPrec);
 889         } else {
 890             return t;
 891         }
 892     }
 893 
 894     /*  Expression2Rest = {infixop Expression3}
 895      *                  | Expression3 instanceof Type

 896      *  infixop         = "||"
 897      *                  | "&&"
 898      *                  | "|"
 899      *                  | "^"
 900      *                  | "&"
 901      *                  | "==" | "!="
 902      *                  | "<" | ">" | "<=" | ">="
 903      *                  | "<<" | ">>" | ">>>"
 904      *                  | "+" | "-"
 905      *                  | "*" | "/" | "%"
 906      */
 907     JCExpression term2Rest(JCExpression t, int minprec) {
 908         JCExpression[] odStack = newOdStack();
 909         Token[] opStack = newOpStack();
 910 
 911         // optimization, was odStack = new Tree[...]; opStack = new Tree[...];
 912         int top = 0;
 913         odStack[0] = t;
 914         int startPos = token.pos;
 915         Token topOp = Tokens.DUMMY;
 916         while (prec(token.kind) >= minprec) {
 917             opStack[top] = topOp;
 918             top++;
 919             topOp = token;
 920             nextToken();
 921             odStack[top] = (topOp.kind == INSTANCEOF) ? parseType() : term3();











 922             while (top > 0 && prec(topOp.kind) >= prec(token.kind)) {
 923                 odStack[top-1] = makeOp(topOp.pos, topOp.kind, odStack[top-1],
 924                                         odStack[top]);
 925                 top--;
 926                 topOp = opStack[top];
 927             }
 928         }
 929         Assert.check(top == 0);
 930         t = odStack[0];
 931 
 932         if (t.hasTag(JCTree.Tag.PLUS)) {
 933             t = foldStrings(t);
 934         }
 935 
 936         odStackSupply.add(odStack);
 937         opStackSupply.add(opStack);
 938         return t;
 939     }
 940     //where
 941         /** Construct a binary or type test node.
 942          */
 943         private JCExpression makeOp(int pos,
 944                                     TokenKind topOp,
 945                                     JCExpression od1,
 946                                     JCExpression od2)
 947         {
 948             if (topOp == INSTANCEOF) {
 949                 return F.at(pos).TypeTest(od1, od2);
 950             } else {
 951                 return F.at(pos).Binary(optag(topOp), od1, od2);
 952             }
 953         }
 954         /** If tree is a concatenation of string literals, replace it
 955          *  by a single literal representing the concatenated string.
 956          */
 957         protected JCExpression foldStrings(JCExpression tree) {
 958             if (!allowStringFolding)
 959                 return tree;
 960             ListBuffer<JCExpression> opStack = new ListBuffer<>();
 961             ListBuffer<JCLiteral> litBuf = new ListBuffer<>();
 962             boolean needsFolding = false;
 963             JCExpression curr = tree;
 964             while (true) {
 965                 if (curr.hasTag(JCTree.Tag.PLUS)) {
 966                     JCBinary op = (JCBinary)curr;
 967                     needsFolding |= foldIfNeeded(op.rhs, litBuf, opStack, false);
 968                     curr = op.lhs;
 969                 } else {
 970                     needsFolding |= foldIfNeeded(curr, litBuf, opStack, true);
 971                     break; //last one!
 972                 }
 973             }




 876             return t;
 877         }
 878     }
 879 
 880     /** Expression2   = Expression3 [Expression2Rest]
 881      *  Type2         = Type3
 882      *  TypeNoParams2 = TypeNoParams3
 883      */
 884     JCExpression term2() {
 885         JCExpression t = term3();
 886         if ((mode & EXPR) != 0 && prec(token.kind) >= TreeInfo.orPrec) {
 887             selectExprMode();
 888             return term2Rest(t, TreeInfo.orPrec);
 889         } else {
 890             return t;
 891         }
 892     }
 893 
 894     /*  Expression2Rest = {infixop Expression3}
 895      *                  | Expression3 instanceof Type
 896      *                  | Expression3 instanceof Pattern
 897      *  infixop         = "||"
 898      *                  | "&&"
 899      *                  | "|"
 900      *                  | "^"
 901      *                  | "&"
 902      *                  | "==" | "!="
 903      *                  | "<" | ">" | "<=" | ">="
 904      *                  | "<<" | ">>" | ">>>"
 905      *                  | "+" | "-"
 906      *                  | "*" | "/" | "%"
 907      */
 908     JCExpression term2Rest(JCExpression t, int minprec) {
 909         JCExpression[] odStack = newOdStack();
 910         Token[] opStack = newOpStack();
 911 
 912         // optimization, was odStack = new Tree[...]; opStack = new Tree[...];
 913         int top = 0;
 914         odStack[0] = t;
 915         int startPos = token.pos;
 916         Token topOp = Tokens.DUMMY;
 917         while (prec(token.kind) >= minprec) {
 918             opStack[top] = topOp;
 919 
 920             if (token.kind == INSTANCEOF) {
 921                 int pos = token.pos;
 922                 nextToken();
 923                 JCTree pattern = parseType();
 924                 if (token.kind == IDENTIFIER) {
 925                     pattern = toP(F.at(token.pos).BindingPattern(ident(), pattern));
 926                 }
 927                 odStack[top] = F.at(pos).TypeTest(odStack[top], pattern);
 928             } else {
 929                 topOp = token;
 930                 nextToken();
 931                 top++;
 932                 odStack[top] = term3();
 933             }
 934             while (top > 0 && prec(topOp.kind) >= prec(token.kind)) {
 935                 odStack[top - 1] = F.at(topOp.pos).Binary(optag(topOp.kind), odStack[top - 1], odStack[top]);

 936                 top--;
 937                 topOp = opStack[top];
 938             }
 939         }
 940         Assert.check(top == 0);
 941         t = odStack[0];
 942 
 943         if (t.hasTag(JCTree.Tag.PLUS)) {
 944             t = foldStrings(t);
 945         }
 946 
 947         odStackSupply.add(odStack);
 948         opStackSupply.add(opStack);
 949         return t;
 950     }
 951     //where













 952         /** If tree is a concatenation of string literals, replace it
 953          *  by a single literal representing the concatenated string.
 954          */
 955         protected JCExpression foldStrings(JCExpression tree) {
 956             if (!allowStringFolding)
 957                 return tree;
 958             ListBuffer<JCExpression> opStack = new ListBuffer<>();
 959             ListBuffer<JCLiteral> litBuf = new ListBuffer<>();
 960             boolean needsFolding = false;
 961             JCExpression curr = tree;
 962             while (true) {
 963                 if (curr.hasTag(JCTree.Tag.PLUS)) {
 964                     JCBinary op = (JCBinary)curr;
 965                     needsFolding |= foldIfNeeded(op.rhs, litBuf, opStack, false);
 966                     curr = op.lhs;
 967                 } else {
 968                     needsFolding |= foldIfNeeded(curr, litBuf, opStack, true);
 969                     break; //last one!
 970                 }
 971             }


< prev index next >