Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.javac.parser; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.*; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import com.sun.source.tree.MemberReferenceTree.ReferenceMode; |
aoqi@0 | 31 | |
aoqi@0 | 32 | import com.sun.tools.javac.code.*; |
aoqi@0 | 33 | import com.sun.tools.javac.parser.Tokens.*; |
aoqi@0 | 34 | import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle; |
aoqi@0 | 35 | import com.sun.tools.javac.tree.*; |
aoqi@0 | 36 | import com.sun.tools.javac.tree.JCTree.*; |
aoqi@0 | 37 | import com.sun.tools.javac.util.*; |
aoqi@0 | 38 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag; |
aoqi@0 | 39 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
aoqi@0 | 40 | import com.sun.tools.javac.util.List; |
aoqi@0 | 41 | |
aoqi@0 | 42 | import static com.sun.tools.javac.parser.Tokens.TokenKind.*; |
aoqi@0 | 43 | import static com.sun.tools.javac.parser.Tokens.TokenKind.ASSERT; |
aoqi@0 | 44 | import static com.sun.tools.javac.parser.Tokens.TokenKind.CASE; |
aoqi@0 | 45 | import static com.sun.tools.javac.parser.Tokens.TokenKind.CATCH; |
aoqi@0 | 46 | import static com.sun.tools.javac.parser.Tokens.TokenKind.EQ; |
aoqi@0 | 47 | import static com.sun.tools.javac.parser.Tokens.TokenKind.GT; |
aoqi@0 | 48 | import static com.sun.tools.javac.parser.Tokens.TokenKind.IMPORT; |
aoqi@0 | 49 | import static com.sun.tools.javac.parser.Tokens.TokenKind.LT; |
aoqi@0 | 50 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
aoqi@0 | 51 | |
aoqi@0 | 52 | /** The parser maps a token sequence into an abstract syntax |
aoqi@0 | 53 | * tree. It operates by recursive descent, with code derived |
aoqi@0 | 54 | * systematically from an LL(1) grammar. For efficiency reasons, an |
aoqi@0 | 55 | * operator precedence scheme is used for parsing binary operation |
aoqi@0 | 56 | * expressions. |
aoqi@0 | 57 | * |
aoqi@0 | 58 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 59 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 60 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 61 | * deletion without notice.</b> |
aoqi@0 | 62 | */ |
aoqi@0 | 63 | public class JavacParser implements Parser { |
aoqi@0 | 64 | |
aoqi@0 | 65 | /** The number of precedence levels of infix operators. |
aoqi@0 | 66 | */ |
aoqi@0 | 67 | private static final int infixPrecedenceLevels = 10; |
aoqi@0 | 68 | |
aoqi@0 | 69 | /** The scanner used for lexical analysis. |
aoqi@0 | 70 | */ |
aoqi@0 | 71 | protected Lexer S; |
aoqi@0 | 72 | |
aoqi@0 | 73 | /** The factory to be used for abstract syntax tree construction. |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | protected TreeMaker F; |
aoqi@0 | 76 | |
aoqi@0 | 77 | /** The log to be used for error diagnostics. |
aoqi@0 | 78 | */ |
aoqi@0 | 79 | private Log log; |
aoqi@0 | 80 | |
aoqi@0 | 81 | /** The Source language setting. */ |
aoqi@0 | 82 | private Source source; |
aoqi@0 | 83 | |
aoqi@0 | 84 | /** The name table. */ |
aoqi@0 | 85 | private Names names; |
aoqi@0 | 86 | |
aoqi@0 | 87 | /** End position mappings container */ |
aoqi@0 | 88 | private final AbstractEndPosTable endPosTable; |
aoqi@0 | 89 | |
aoqi@0 | 90 | // Because of javac's limited lookahead, some contexts are ambiguous in |
aoqi@0 | 91 | // the presence of type annotations even though they are not ambiguous |
aoqi@0 | 92 | // in the absence of type annotations. Consider this code: |
aoqi@0 | 93 | // void m(String [] m) { } |
aoqi@0 | 94 | // void m(String ... m) { } |
aoqi@0 | 95 | // After parsing "String", javac calls bracketsOpt which immediately |
aoqi@0 | 96 | // returns if the next character is not '['. Similarly, javac can see |
aoqi@0 | 97 | // if the next token is ... and in that case parse an ellipsis. But in |
aoqi@0 | 98 | // the presence of type annotations: |
aoqi@0 | 99 | // void m(String @A [] m) { } |
aoqi@0 | 100 | // void m(String @A ... m) { } |
aoqi@0 | 101 | // no finite lookahead is enough to determine whether to read array |
aoqi@0 | 102 | // levels or an ellipsis. Furthermore, if you call bracketsOpt, then |
aoqi@0 | 103 | // bracketsOpt first reads all the leading annotations and only then |
aoqi@0 | 104 | // discovers that it needs to fail. bracketsOpt needs a way to push |
aoqi@0 | 105 | // back the extra annotations that it read. (But, bracketsOpt should |
aoqi@0 | 106 | // not *always* be allowed to push back extra annotations that it finds |
aoqi@0 | 107 | // -- in most contexts, any such extra annotation is an error. |
aoqi@0 | 108 | // |
aoqi@0 | 109 | // The following two variables permit type annotations that have |
aoqi@0 | 110 | // already been read to be stored for later use. Alternate |
aoqi@0 | 111 | // implementations are possible but would cause much larger changes to |
aoqi@0 | 112 | // the parser. |
aoqi@0 | 113 | |
aoqi@0 | 114 | /** Type annotations that have already been read but have not yet been used. **/ |
aoqi@0 | 115 | private List<JCAnnotation> typeAnnotationsPushedBack = List.nil(); |
aoqi@0 | 116 | |
aoqi@0 | 117 | /** |
aoqi@0 | 118 | * If the parser notices extra annotations, then it either immediately |
aoqi@0 | 119 | * issues an error (if this variable is false) or places the extra |
aoqi@0 | 120 | * annotations in variable typeAnnotationsPushedBack (if this variable |
aoqi@0 | 121 | * is true). |
aoqi@0 | 122 | */ |
aoqi@0 | 123 | private boolean permitTypeAnnotationsPushBack = false; |
aoqi@0 | 124 | |
aoqi@0 | 125 | interface ErrorRecoveryAction { |
aoqi@0 | 126 | JCTree doRecover(JavacParser parser); |
aoqi@0 | 127 | } |
aoqi@0 | 128 | |
aoqi@0 | 129 | enum BasicErrorRecoveryAction implements ErrorRecoveryAction { |
aoqi@0 | 130 | BLOCK_STMT {public JCTree doRecover(JavacParser parser) { return parser.parseStatementAsBlock(); }}, |
aoqi@0 | 131 | CATCH_CLAUSE {public JCTree doRecover(JavacParser parser) { return parser.catchClause(); }} |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | /** Construct a parser from a given scanner, tree factory and log. |
aoqi@0 | 135 | */ |
aoqi@0 | 136 | protected JavacParser(ParserFactory fac, |
aoqi@0 | 137 | Lexer S, |
aoqi@0 | 138 | boolean keepDocComments, |
aoqi@0 | 139 | boolean keepLineMap, |
aoqi@0 | 140 | boolean keepEndPositions) { |
aoqi@0 | 141 | this.S = S; |
aoqi@0 | 142 | nextToken(); // prime the pump |
aoqi@0 | 143 | this.F = fac.F; |
aoqi@0 | 144 | this.log = fac.log; |
aoqi@0 | 145 | this.names = fac.names; |
aoqi@0 | 146 | this.source = fac.source; |
aoqi@0 | 147 | this.allowGenerics = source.allowGenerics(); |
aoqi@0 | 148 | this.allowVarargs = source.allowVarargs(); |
aoqi@0 | 149 | this.allowAsserts = source.allowAsserts(); |
aoqi@0 | 150 | this.allowEnums = source.allowEnums(); |
aoqi@0 | 151 | this.allowForeach = source.allowForeach(); |
aoqi@0 | 152 | this.allowStaticImport = source.allowStaticImport(); |
aoqi@0 | 153 | this.allowAnnotations = source.allowAnnotations(); |
aoqi@0 | 154 | this.allowTWR = source.allowTryWithResources(); |
aoqi@0 | 155 | this.allowDiamond = source.allowDiamond(); |
aoqi@0 | 156 | this.allowMulticatch = source.allowMulticatch(); |
aoqi@0 | 157 | this.allowStringFolding = fac.options.getBoolean("allowStringFolding", true); |
aoqi@0 | 158 | this.allowLambda = source.allowLambda(); |
aoqi@0 | 159 | this.allowMethodReferences = source.allowMethodReferences(); |
aoqi@0 | 160 | this.allowDefaultMethods = source.allowDefaultMethods(); |
aoqi@0 | 161 | this.allowStaticInterfaceMethods = source.allowStaticInterfaceMethods(); |
aoqi@0 | 162 | this.allowIntersectionTypesInCast = source.allowIntersectionTypesInCast(); |
aoqi@0 | 163 | this.allowTypeAnnotations = source.allowTypeAnnotations(); |
aoqi@0 | 164 | this.allowAnnotationsAfterTypeParams = source.allowAnnotationsAfterTypeParams(); |
aoqi@0 | 165 | this.keepDocComments = keepDocComments; |
aoqi@0 | 166 | docComments = newDocCommentTable(keepDocComments, fac); |
aoqi@0 | 167 | this.keepLineMap = keepLineMap; |
aoqi@0 | 168 | this.errorTree = F.Erroneous(); |
aoqi@0 | 169 | endPosTable = newEndPosTable(keepEndPositions); |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | protected AbstractEndPosTable newEndPosTable(boolean keepEndPositions) { |
aoqi@0 | 173 | return keepEndPositions |
aoqi@0 | 174 | ? new SimpleEndPosTable(this) |
aoqi@0 | 175 | : new EmptyEndPosTable(this); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | protected DocCommentTable newDocCommentTable(boolean keepDocComments, ParserFactory fac) { |
aoqi@0 | 179 | return keepDocComments ? new LazyDocCommentTable(fac) : null; |
aoqi@0 | 180 | } |
aoqi@0 | 181 | |
aoqi@0 | 182 | /** Switch: Should generics be recognized? |
aoqi@0 | 183 | */ |
aoqi@0 | 184 | boolean allowGenerics; |
aoqi@0 | 185 | |
aoqi@0 | 186 | /** Switch: Should diamond operator be recognized? |
aoqi@0 | 187 | */ |
aoqi@0 | 188 | boolean allowDiamond; |
aoqi@0 | 189 | |
aoqi@0 | 190 | /** Switch: Should multicatch clause be accepted? |
aoqi@0 | 191 | */ |
aoqi@0 | 192 | boolean allowMulticatch; |
aoqi@0 | 193 | |
aoqi@0 | 194 | /** Switch: Should varargs be recognized? |
aoqi@0 | 195 | */ |
aoqi@0 | 196 | boolean allowVarargs; |
aoqi@0 | 197 | |
aoqi@0 | 198 | /** Switch: should we recognize assert statements, or just give a warning? |
aoqi@0 | 199 | */ |
aoqi@0 | 200 | boolean allowAsserts; |
aoqi@0 | 201 | |
aoqi@0 | 202 | /** Switch: should we recognize enums, or just give a warning? |
aoqi@0 | 203 | */ |
aoqi@0 | 204 | boolean allowEnums; |
aoqi@0 | 205 | |
aoqi@0 | 206 | /** Switch: should we recognize foreach? |
aoqi@0 | 207 | */ |
aoqi@0 | 208 | boolean allowForeach; |
aoqi@0 | 209 | |
aoqi@0 | 210 | /** Switch: should we recognize foreach? |
aoqi@0 | 211 | */ |
aoqi@0 | 212 | boolean allowStaticImport; |
aoqi@0 | 213 | |
aoqi@0 | 214 | /** Switch: should we recognize annotations? |
aoqi@0 | 215 | */ |
aoqi@0 | 216 | boolean allowAnnotations; |
aoqi@0 | 217 | |
aoqi@0 | 218 | /** Switch: should we recognize try-with-resources? |
aoqi@0 | 219 | */ |
aoqi@0 | 220 | boolean allowTWR; |
aoqi@0 | 221 | |
aoqi@0 | 222 | /** Switch: should we fold strings? |
aoqi@0 | 223 | */ |
aoqi@0 | 224 | boolean allowStringFolding; |
aoqi@0 | 225 | |
aoqi@0 | 226 | /** Switch: should we recognize lambda expressions? |
aoqi@0 | 227 | */ |
aoqi@0 | 228 | boolean allowLambda; |
aoqi@0 | 229 | |
aoqi@0 | 230 | /** Switch: should we allow method/constructor references? |
aoqi@0 | 231 | */ |
aoqi@0 | 232 | boolean allowMethodReferences; |
aoqi@0 | 233 | |
aoqi@0 | 234 | /** Switch: should we allow default methods in interfaces? |
aoqi@0 | 235 | */ |
aoqi@0 | 236 | boolean allowDefaultMethods; |
aoqi@0 | 237 | |
aoqi@0 | 238 | /** Switch: should we allow static methods in interfaces? |
aoqi@0 | 239 | */ |
aoqi@0 | 240 | boolean allowStaticInterfaceMethods; |
aoqi@0 | 241 | |
aoqi@0 | 242 | /** Switch: should we allow intersection types in cast? |
aoqi@0 | 243 | */ |
aoqi@0 | 244 | boolean allowIntersectionTypesInCast; |
aoqi@0 | 245 | |
aoqi@0 | 246 | /** Switch: should we keep docComments? |
aoqi@0 | 247 | */ |
aoqi@0 | 248 | boolean keepDocComments; |
aoqi@0 | 249 | |
aoqi@0 | 250 | /** Switch: should we keep line table? |
aoqi@0 | 251 | */ |
aoqi@0 | 252 | boolean keepLineMap; |
aoqi@0 | 253 | |
aoqi@0 | 254 | /** Switch: should we recognize type annotations? |
aoqi@0 | 255 | */ |
aoqi@0 | 256 | boolean allowTypeAnnotations; |
aoqi@0 | 257 | |
aoqi@0 | 258 | /** Switch: should we allow annotations after the method type parameters? |
aoqi@0 | 259 | */ |
aoqi@0 | 260 | boolean allowAnnotationsAfterTypeParams; |
aoqi@0 | 261 | |
aoqi@0 | 262 | /** Switch: is "this" allowed as an identifier? |
aoqi@0 | 263 | * This is needed to parse receiver types. |
aoqi@0 | 264 | */ |
aoqi@0 | 265 | boolean allowThisIdent; |
aoqi@0 | 266 | |
aoqi@0 | 267 | /** The type of the method receiver, as specified by a first "this" parameter. |
aoqi@0 | 268 | */ |
aoqi@0 | 269 | JCVariableDecl receiverParam; |
aoqi@0 | 270 | |
aoqi@0 | 271 | |
aoqi@0 | 272 | /** When terms are parsed, the mode determines which is expected: |
aoqi@0 | 273 | * mode = EXPR : an expression |
aoqi@0 | 274 | * mode = TYPE : a type |
aoqi@0 | 275 | * mode = NOPARAMS : no parameters allowed for type |
aoqi@0 | 276 | * mode = TYPEARG : type argument |
aoqi@0 | 277 | */ |
aoqi@0 | 278 | static final int EXPR = 0x1; |
aoqi@0 | 279 | static final int TYPE = 0x2; |
aoqi@0 | 280 | static final int NOPARAMS = 0x4; |
aoqi@0 | 281 | static final int TYPEARG = 0x8; |
aoqi@0 | 282 | static final int DIAMOND = 0x10; |
aoqi@0 | 283 | |
aoqi@0 | 284 | /** The current mode. |
aoqi@0 | 285 | */ |
aoqi@0 | 286 | private int mode = 0; |
aoqi@0 | 287 | |
aoqi@0 | 288 | /** The mode of the term that was parsed last. |
aoqi@0 | 289 | */ |
aoqi@0 | 290 | private int lastmode = 0; |
aoqi@0 | 291 | |
aoqi@0 | 292 | /* ---------- token management -------------- */ |
aoqi@0 | 293 | |
aoqi@0 | 294 | protected Token token; |
aoqi@0 | 295 | |
aoqi@0 | 296 | public Token token() { |
aoqi@0 | 297 | return token; |
aoqi@0 | 298 | } |
aoqi@0 | 299 | |
aoqi@0 | 300 | public void nextToken() { |
aoqi@0 | 301 | S.nextToken(); |
aoqi@0 | 302 | token = S.token(); |
aoqi@0 | 303 | } |
aoqi@0 | 304 | |
aoqi@0 | 305 | protected boolean peekToken(Filter<TokenKind> tk) { |
aoqi@0 | 306 | return peekToken(0, tk); |
aoqi@0 | 307 | } |
aoqi@0 | 308 | |
aoqi@0 | 309 | protected boolean peekToken(int lookahead, Filter<TokenKind> tk) { |
aoqi@0 | 310 | return tk.accepts(S.token(lookahead + 1).kind); |
aoqi@0 | 311 | } |
aoqi@0 | 312 | |
aoqi@0 | 313 | protected boolean peekToken(Filter<TokenKind> tk1, Filter<TokenKind> tk2) { |
aoqi@0 | 314 | return peekToken(0, tk1, tk2); |
aoqi@0 | 315 | } |
aoqi@0 | 316 | |
aoqi@0 | 317 | protected boolean peekToken(int lookahead, Filter<TokenKind> tk1, Filter<TokenKind> tk2) { |
aoqi@0 | 318 | return tk1.accepts(S.token(lookahead + 1).kind) && |
aoqi@0 | 319 | tk2.accepts(S.token(lookahead + 2).kind); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | |
aoqi@0 | 322 | protected boolean peekToken(Filter<TokenKind> tk1, Filter<TokenKind> tk2, Filter<TokenKind> tk3) { |
aoqi@0 | 323 | return peekToken(0, tk1, tk2, tk3); |
aoqi@0 | 324 | } |
aoqi@0 | 325 | |
aoqi@0 | 326 | protected boolean peekToken(int lookahead, Filter<TokenKind> tk1, Filter<TokenKind> tk2, Filter<TokenKind> tk3) { |
aoqi@0 | 327 | return tk1.accepts(S.token(lookahead + 1).kind) && |
aoqi@0 | 328 | tk2.accepts(S.token(lookahead + 2).kind) && |
aoqi@0 | 329 | tk3.accepts(S.token(lookahead + 3).kind); |
aoqi@0 | 330 | } |
aoqi@0 | 331 | |
aoqi@0 | 332 | @SuppressWarnings("unchecked") |
aoqi@0 | 333 | protected boolean peekToken(Filter<TokenKind>... kinds) { |
aoqi@0 | 334 | return peekToken(0, kinds); |
aoqi@0 | 335 | } |
aoqi@0 | 336 | |
aoqi@0 | 337 | @SuppressWarnings("unchecked") |
aoqi@0 | 338 | protected boolean peekToken(int lookahead, Filter<TokenKind>... kinds) { |
aoqi@0 | 339 | for (; lookahead < kinds.length ; lookahead++) { |
aoqi@0 | 340 | if (!kinds[lookahead].accepts(S.token(lookahead + 1).kind)) { |
aoqi@0 | 341 | return false; |
aoqi@0 | 342 | } |
aoqi@0 | 343 | } |
aoqi@0 | 344 | return true; |
aoqi@0 | 345 | } |
aoqi@0 | 346 | |
aoqi@0 | 347 | /* ---------- error recovery -------------- */ |
aoqi@0 | 348 | |
aoqi@0 | 349 | private JCErroneous errorTree; |
aoqi@0 | 350 | |
aoqi@0 | 351 | /** Skip forward until a suitable stop token is found. |
aoqi@0 | 352 | */ |
aoqi@0 | 353 | private void skip(boolean stopAtImport, boolean stopAtMemberDecl, boolean stopAtIdentifier, boolean stopAtStatement) { |
aoqi@0 | 354 | while (true) { |
aoqi@0 | 355 | switch (token.kind) { |
aoqi@0 | 356 | case SEMI: |
aoqi@0 | 357 | nextToken(); |
aoqi@0 | 358 | return; |
aoqi@0 | 359 | case PUBLIC: |
aoqi@0 | 360 | case FINAL: |
aoqi@0 | 361 | case ABSTRACT: |
aoqi@0 | 362 | case MONKEYS_AT: |
aoqi@0 | 363 | case EOF: |
aoqi@0 | 364 | case CLASS: |
aoqi@0 | 365 | case INTERFACE: |
aoqi@0 | 366 | case ENUM: |
aoqi@0 | 367 | return; |
aoqi@0 | 368 | case IMPORT: |
aoqi@0 | 369 | if (stopAtImport) |
aoqi@0 | 370 | return; |
aoqi@0 | 371 | break; |
aoqi@0 | 372 | case LBRACE: |
aoqi@0 | 373 | case RBRACE: |
aoqi@0 | 374 | case PRIVATE: |
aoqi@0 | 375 | case PROTECTED: |
aoqi@0 | 376 | case STATIC: |
aoqi@0 | 377 | case TRANSIENT: |
aoqi@0 | 378 | case NATIVE: |
aoqi@0 | 379 | case VOLATILE: |
aoqi@0 | 380 | case SYNCHRONIZED: |
aoqi@0 | 381 | case STRICTFP: |
aoqi@0 | 382 | case LT: |
aoqi@0 | 383 | case BYTE: |
aoqi@0 | 384 | case SHORT: |
aoqi@0 | 385 | case CHAR: |
aoqi@0 | 386 | case INT: |
aoqi@0 | 387 | case LONG: |
aoqi@0 | 388 | case FLOAT: |
aoqi@0 | 389 | case DOUBLE: |
aoqi@0 | 390 | case BOOLEAN: |
aoqi@0 | 391 | case VOID: |
aoqi@0 | 392 | if (stopAtMemberDecl) |
aoqi@0 | 393 | return; |
aoqi@0 | 394 | break; |
aoqi@0 | 395 | case UNDERSCORE: |
aoqi@0 | 396 | case IDENTIFIER: |
aoqi@0 | 397 | if (stopAtIdentifier) |
aoqi@0 | 398 | return; |
aoqi@0 | 399 | break; |
aoqi@0 | 400 | case CASE: |
aoqi@0 | 401 | case DEFAULT: |
aoqi@0 | 402 | case IF: |
aoqi@0 | 403 | case FOR: |
aoqi@0 | 404 | case WHILE: |
aoqi@0 | 405 | case DO: |
aoqi@0 | 406 | case TRY: |
aoqi@0 | 407 | case SWITCH: |
aoqi@0 | 408 | case RETURN: |
aoqi@0 | 409 | case THROW: |
aoqi@0 | 410 | case BREAK: |
aoqi@0 | 411 | case CONTINUE: |
aoqi@0 | 412 | case ELSE: |
aoqi@0 | 413 | case FINALLY: |
aoqi@0 | 414 | case CATCH: |
aoqi@0 | 415 | if (stopAtStatement) |
aoqi@0 | 416 | return; |
aoqi@0 | 417 | break; |
aoqi@0 | 418 | } |
aoqi@0 | 419 | nextToken(); |
aoqi@0 | 420 | } |
aoqi@0 | 421 | } |
aoqi@0 | 422 | |
aoqi@0 | 423 | private JCErroneous syntaxError(int pos, String key, TokenKind... args) { |
aoqi@0 | 424 | return syntaxError(pos, List.<JCTree>nil(), key, args); |
aoqi@0 | 425 | } |
aoqi@0 | 426 | |
aoqi@0 | 427 | private JCErroneous syntaxError(int pos, List<JCTree> errs, String key, TokenKind... args) { |
aoqi@0 | 428 | setErrorEndPos(pos); |
aoqi@0 | 429 | JCErroneous err = F.at(pos).Erroneous(errs); |
aoqi@0 | 430 | reportSyntaxError(err, key, (Object[])args); |
aoqi@0 | 431 | if (errs != null) { |
aoqi@0 | 432 | JCTree last = errs.last(); |
aoqi@0 | 433 | if (last != null) |
aoqi@0 | 434 | storeEnd(last, pos); |
aoqi@0 | 435 | } |
aoqi@0 | 436 | return toP(err); |
aoqi@0 | 437 | } |
aoqi@0 | 438 | |
aoqi@0 | 439 | private int errorPos = Position.NOPOS; |
aoqi@0 | 440 | |
aoqi@0 | 441 | /** |
aoqi@0 | 442 | * Report a syntax using the given the position parameter and arguments, |
aoqi@0 | 443 | * unless one was already reported at the same position. |
aoqi@0 | 444 | */ |
aoqi@0 | 445 | private void reportSyntaxError(int pos, String key, Object... args) { |
aoqi@0 | 446 | JCDiagnostic.DiagnosticPosition diag = new JCDiagnostic.SimpleDiagnosticPosition(pos); |
aoqi@0 | 447 | reportSyntaxError(diag, key, args); |
aoqi@0 | 448 | } |
aoqi@0 | 449 | |
aoqi@0 | 450 | /** |
aoqi@0 | 451 | * Report a syntax error using the given DiagnosticPosition object and |
aoqi@0 | 452 | * arguments, unless one was already reported at the same position. |
aoqi@0 | 453 | */ |
aoqi@0 | 454 | private void reportSyntaxError(JCDiagnostic.DiagnosticPosition diagPos, String key, Object... args) { |
aoqi@0 | 455 | int pos = diagPos.getPreferredPosition(); |
aoqi@0 | 456 | if (pos > S.errPos() || pos == Position.NOPOS) { |
aoqi@0 | 457 | if (token.kind == EOF) { |
aoqi@0 | 458 | error(diagPos, "premature.eof"); |
aoqi@0 | 459 | } else { |
aoqi@0 | 460 | error(diagPos, key, args); |
aoqi@0 | 461 | } |
aoqi@0 | 462 | } |
aoqi@0 | 463 | S.errPos(pos); |
aoqi@0 | 464 | if (token.pos == errorPos) |
aoqi@0 | 465 | nextToken(); // guarantee progress |
aoqi@0 | 466 | errorPos = token.pos; |
aoqi@0 | 467 | } |
aoqi@0 | 468 | |
aoqi@0 | 469 | |
aoqi@0 | 470 | /** Generate a syntax error at current position unless one was already |
aoqi@0 | 471 | * reported at the same position. |
aoqi@0 | 472 | */ |
aoqi@0 | 473 | private JCErroneous syntaxError(String key) { |
aoqi@0 | 474 | return syntaxError(token.pos, key); |
aoqi@0 | 475 | } |
aoqi@0 | 476 | |
aoqi@0 | 477 | /** Generate a syntax error at current position unless one was |
aoqi@0 | 478 | * already reported at the same position. |
aoqi@0 | 479 | */ |
aoqi@0 | 480 | private JCErroneous syntaxError(String key, TokenKind arg) { |
aoqi@0 | 481 | return syntaxError(token.pos, key, arg); |
aoqi@0 | 482 | } |
aoqi@0 | 483 | |
aoqi@0 | 484 | /** If next input token matches given token, skip it, otherwise report |
aoqi@0 | 485 | * an error. |
aoqi@0 | 486 | */ |
aoqi@0 | 487 | public void accept(TokenKind tk) { |
aoqi@0 | 488 | if (token.kind == tk) { |
aoqi@0 | 489 | nextToken(); |
aoqi@0 | 490 | } else { |
aoqi@0 | 491 | setErrorEndPos(token.pos); |
aoqi@0 | 492 | reportSyntaxError(S.prevToken().endPos, "expected", tk); |
aoqi@0 | 493 | } |
aoqi@0 | 494 | } |
aoqi@0 | 495 | |
aoqi@0 | 496 | /** Report an illegal start of expression/type error at given position. |
aoqi@0 | 497 | */ |
aoqi@0 | 498 | JCExpression illegal(int pos) { |
aoqi@0 | 499 | setErrorEndPos(pos); |
aoqi@0 | 500 | if ((mode & EXPR) != 0) |
aoqi@0 | 501 | return syntaxError(pos, "illegal.start.of.expr"); |
aoqi@0 | 502 | else |
aoqi@0 | 503 | return syntaxError(pos, "illegal.start.of.type"); |
aoqi@0 | 504 | |
aoqi@0 | 505 | } |
aoqi@0 | 506 | |
aoqi@0 | 507 | /** Report an illegal start of expression/type error at current position. |
aoqi@0 | 508 | */ |
aoqi@0 | 509 | JCExpression illegal() { |
aoqi@0 | 510 | return illegal(token.pos); |
aoqi@0 | 511 | } |
aoqi@0 | 512 | |
aoqi@0 | 513 | /** Diagnose a modifier flag from the set, if any. */ |
aoqi@0 | 514 | void checkNoMods(long mods) { |
aoqi@0 | 515 | if (mods != 0) { |
aoqi@0 | 516 | long lowestMod = mods & -mods; |
aoqi@0 | 517 | error(token.pos, "mod.not.allowed.here", |
aoqi@0 | 518 | Flags.asFlagSet(lowestMod)); |
aoqi@0 | 519 | } |
aoqi@0 | 520 | } |
aoqi@0 | 521 | |
aoqi@0 | 522 | /* ---------- doc comments --------- */ |
aoqi@0 | 523 | |
aoqi@0 | 524 | /** A table to store all documentation comments |
aoqi@0 | 525 | * indexed by the tree nodes they refer to. |
aoqi@0 | 526 | * defined only if option flag keepDocComment is set. |
aoqi@0 | 527 | */ |
aoqi@0 | 528 | private final DocCommentTable docComments; |
aoqi@0 | 529 | |
aoqi@0 | 530 | /** Make an entry into docComments hashtable, |
aoqi@0 | 531 | * provided flag keepDocComments is set and given doc comment is non-null. |
aoqi@0 | 532 | * @param tree The tree to be used as index in the hashtable |
aoqi@0 | 533 | * @param dc The doc comment to associate with the tree, or null. |
aoqi@0 | 534 | */ |
aoqi@0 | 535 | void attach(JCTree tree, Comment dc) { |
aoqi@0 | 536 | if (keepDocComments && dc != null) { |
aoqi@0 | 537 | // System.out.println("doc comment = ");System.out.println(dc);//DEBUG |
aoqi@0 | 538 | docComments.putComment(tree, dc); |
aoqi@0 | 539 | } |
aoqi@0 | 540 | } |
aoqi@0 | 541 | |
aoqi@0 | 542 | /* -------- source positions ------- */ |
aoqi@0 | 543 | |
aoqi@0 | 544 | private void setErrorEndPos(int errPos) { |
aoqi@0 | 545 | endPosTable.setErrorEndPos(errPos); |
aoqi@0 | 546 | } |
aoqi@0 | 547 | |
aoqi@0 | 548 | private void storeEnd(JCTree tree, int endpos) { |
aoqi@0 | 549 | endPosTable.storeEnd(tree, endpos); |
aoqi@0 | 550 | } |
aoqi@0 | 551 | |
aoqi@0 | 552 | private <T extends JCTree> T to(T t) { |
aoqi@0 | 553 | return endPosTable.to(t); |
aoqi@0 | 554 | } |
aoqi@0 | 555 | |
aoqi@0 | 556 | private <T extends JCTree> T toP(T t) { |
aoqi@0 | 557 | return endPosTable.toP(t); |
aoqi@0 | 558 | } |
aoqi@0 | 559 | |
aoqi@0 | 560 | /** Get the start position for a tree node. The start position is |
aoqi@0 | 561 | * defined to be the position of the first character of the first |
aoqi@0 | 562 | * token of the node's source text. |
aoqi@0 | 563 | * @param tree The tree node |
aoqi@0 | 564 | */ |
aoqi@0 | 565 | public int getStartPos(JCTree tree) { |
aoqi@0 | 566 | return TreeInfo.getStartPos(tree); |
aoqi@0 | 567 | } |
aoqi@0 | 568 | |
aoqi@0 | 569 | /** |
aoqi@0 | 570 | * Get the end position for a tree node. The end position is |
aoqi@0 | 571 | * defined to be the position of the last character of the last |
aoqi@0 | 572 | * token of the node's source text. Returns Position.NOPOS if end |
aoqi@0 | 573 | * positions are not generated or the position is otherwise not |
aoqi@0 | 574 | * found. |
aoqi@0 | 575 | * @param tree The tree node |
aoqi@0 | 576 | */ |
aoqi@0 | 577 | public int getEndPos(JCTree tree) { |
aoqi@0 | 578 | return endPosTable.getEndPos(tree); |
aoqi@0 | 579 | } |
aoqi@0 | 580 | |
aoqi@0 | 581 | |
aoqi@0 | 582 | |
aoqi@0 | 583 | /* ---------- parsing -------------- */ |
aoqi@0 | 584 | |
aoqi@0 | 585 | /** |
aoqi@0 | 586 | * Ident = IDENTIFIER |
aoqi@0 | 587 | */ |
aoqi@0 | 588 | Name ident() { |
aoqi@0 | 589 | if (token.kind == IDENTIFIER) { |
aoqi@0 | 590 | Name name = token.name(); |
aoqi@0 | 591 | nextToken(); |
aoqi@0 | 592 | return name; |
aoqi@0 | 593 | } else if (token.kind == ASSERT) { |
aoqi@0 | 594 | if (allowAsserts) { |
aoqi@0 | 595 | error(token.pos, "assert.as.identifier"); |
aoqi@0 | 596 | nextToken(); |
aoqi@0 | 597 | return names.error; |
aoqi@0 | 598 | } else { |
aoqi@0 | 599 | warning(token.pos, "assert.as.identifier"); |
aoqi@0 | 600 | Name name = token.name(); |
aoqi@0 | 601 | nextToken(); |
aoqi@0 | 602 | return name; |
aoqi@0 | 603 | } |
aoqi@0 | 604 | } else if (token.kind == ENUM) { |
aoqi@0 | 605 | if (allowEnums) { |
aoqi@0 | 606 | error(token.pos, "enum.as.identifier"); |
aoqi@0 | 607 | nextToken(); |
aoqi@0 | 608 | return names.error; |
aoqi@0 | 609 | } else { |
aoqi@0 | 610 | warning(token.pos, "enum.as.identifier"); |
aoqi@0 | 611 | Name name = token.name(); |
aoqi@0 | 612 | nextToken(); |
aoqi@0 | 613 | return name; |
aoqi@0 | 614 | } |
aoqi@0 | 615 | } else if (token.kind == THIS) { |
aoqi@0 | 616 | if (allowThisIdent) { |
aoqi@0 | 617 | // Make sure we're using a supported source version. |
aoqi@0 | 618 | checkTypeAnnotations(); |
aoqi@0 | 619 | Name name = token.name(); |
aoqi@0 | 620 | nextToken(); |
aoqi@0 | 621 | return name; |
aoqi@0 | 622 | } else { |
aoqi@0 | 623 | error(token.pos, "this.as.identifier"); |
aoqi@0 | 624 | nextToken(); |
aoqi@0 | 625 | return names.error; |
aoqi@0 | 626 | } |
aoqi@0 | 627 | } else if (token.kind == UNDERSCORE) { |
aoqi@0 | 628 | warning(token.pos, "underscore.as.identifier"); |
aoqi@0 | 629 | Name name = token.name(); |
aoqi@0 | 630 | nextToken(); |
aoqi@0 | 631 | return name; |
aoqi@0 | 632 | } else { |
aoqi@0 | 633 | accept(IDENTIFIER); |
aoqi@0 | 634 | return names.error; |
aoqi@0 | 635 | } |
aoqi@0 | 636 | } |
aoqi@0 | 637 | |
aoqi@0 | 638 | /** |
aoqi@0 | 639 | * Qualident = Ident { DOT [Annotations] Ident } |
aoqi@0 | 640 | */ |
aoqi@0 | 641 | public JCExpression qualident(boolean allowAnnos) { |
aoqi@0 | 642 | JCExpression t = toP(F.at(token.pos).Ident(ident())); |
aoqi@0 | 643 | while (token.kind == DOT) { |
aoqi@0 | 644 | int pos = token.pos; |
aoqi@0 | 645 | nextToken(); |
aoqi@0 | 646 | List<JCAnnotation> tyannos = null; |
aoqi@0 | 647 | if (allowAnnos) { |
aoqi@0 | 648 | tyannos = typeAnnotationsOpt(); |
aoqi@0 | 649 | } |
aoqi@0 | 650 | t = toP(F.at(pos).Select(t, ident())); |
aoqi@0 | 651 | if (tyannos != null && tyannos.nonEmpty()) { |
aoqi@0 | 652 | t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t)); |
aoqi@0 | 653 | } |
aoqi@0 | 654 | } |
aoqi@0 | 655 | return t; |
aoqi@0 | 656 | } |
aoqi@0 | 657 | |
aoqi@0 | 658 | JCExpression literal(Name prefix) { |
aoqi@0 | 659 | return literal(prefix, token.pos); |
aoqi@0 | 660 | } |
aoqi@0 | 661 | |
aoqi@0 | 662 | /** |
aoqi@0 | 663 | * Literal = |
aoqi@0 | 664 | * INTLITERAL |
aoqi@0 | 665 | * | LONGLITERAL |
aoqi@0 | 666 | * | FLOATLITERAL |
aoqi@0 | 667 | * | DOUBLELITERAL |
aoqi@0 | 668 | * | CHARLITERAL |
aoqi@0 | 669 | * | STRINGLITERAL |
aoqi@0 | 670 | * | TRUE |
aoqi@0 | 671 | * | FALSE |
aoqi@0 | 672 | * | NULL |
aoqi@0 | 673 | */ |
aoqi@0 | 674 | JCExpression literal(Name prefix, int pos) { |
aoqi@0 | 675 | JCExpression t = errorTree; |
aoqi@0 | 676 | switch (token.kind) { |
aoqi@0 | 677 | case INTLITERAL: |
aoqi@0 | 678 | try { |
aoqi@0 | 679 | t = F.at(pos).Literal( |
aoqi@0 | 680 | TypeTag.INT, |
aoqi@0 | 681 | Convert.string2int(strval(prefix), token.radix())); |
aoqi@0 | 682 | } catch (NumberFormatException ex) { |
aoqi@0 | 683 | error(token.pos, "int.number.too.large", strval(prefix)); |
aoqi@0 | 684 | } |
aoqi@0 | 685 | break; |
aoqi@0 | 686 | case LONGLITERAL: |
aoqi@0 | 687 | try { |
aoqi@0 | 688 | t = F.at(pos).Literal( |
aoqi@0 | 689 | TypeTag.LONG, |
aoqi@0 | 690 | new Long(Convert.string2long(strval(prefix), token.radix()))); |
aoqi@0 | 691 | } catch (NumberFormatException ex) { |
aoqi@0 | 692 | error(token.pos, "int.number.too.large", strval(prefix)); |
aoqi@0 | 693 | } |
aoqi@0 | 694 | break; |
aoqi@0 | 695 | case FLOATLITERAL: { |
aoqi@0 | 696 | String proper = token.radix() == 16 ? |
aoqi@0 | 697 | ("0x"+ token.stringVal()) : |
aoqi@0 | 698 | token.stringVal(); |
aoqi@0 | 699 | Float n; |
aoqi@0 | 700 | try { |
aoqi@0 | 701 | n = Float.valueOf(proper); |
aoqi@0 | 702 | } catch (NumberFormatException ex) { |
aoqi@0 | 703 | // error already reported in scanner |
aoqi@0 | 704 | n = Float.NaN; |
aoqi@0 | 705 | } |
aoqi@0 | 706 | if (n.floatValue() == 0.0f && !isZero(proper)) |
aoqi@0 | 707 | error(token.pos, "fp.number.too.small"); |
aoqi@0 | 708 | else if (n.floatValue() == Float.POSITIVE_INFINITY) |
aoqi@0 | 709 | error(token.pos, "fp.number.too.large"); |
aoqi@0 | 710 | else |
aoqi@0 | 711 | t = F.at(pos).Literal(TypeTag.FLOAT, n); |
aoqi@0 | 712 | break; |
aoqi@0 | 713 | } |
aoqi@0 | 714 | case DOUBLELITERAL: { |
aoqi@0 | 715 | String proper = token.radix() == 16 ? |
aoqi@0 | 716 | ("0x"+ token.stringVal()) : |
aoqi@0 | 717 | token.stringVal(); |
aoqi@0 | 718 | Double n; |
aoqi@0 | 719 | try { |
aoqi@0 | 720 | n = Double.valueOf(proper); |
aoqi@0 | 721 | } catch (NumberFormatException ex) { |
aoqi@0 | 722 | // error already reported in scanner |
aoqi@0 | 723 | n = Double.NaN; |
aoqi@0 | 724 | } |
aoqi@0 | 725 | if (n.doubleValue() == 0.0d && !isZero(proper)) |
aoqi@0 | 726 | error(token.pos, "fp.number.too.small"); |
aoqi@0 | 727 | else if (n.doubleValue() == Double.POSITIVE_INFINITY) |
aoqi@0 | 728 | error(token.pos, "fp.number.too.large"); |
aoqi@0 | 729 | else |
aoqi@0 | 730 | t = F.at(pos).Literal(TypeTag.DOUBLE, n); |
aoqi@0 | 731 | break; |
aoqi@0 | 732 | } |
aoqi@0 | 733 | case CHARLITERAL: |
aoqi@0 | 734 | t = F.at(pos).Literal( |
aoqi@0 | 735 | TypeTag.CHAR, |
aoqi@0 | 736 | token.stringVal().charAt(0) + 0); |
aoqi@0 | 737 | break; |
aoqi@0 | 738 | case STRINGLITERAL: |
aoqi@0 | 739 | t = F.at(pos).Literal( |
aoqi@0 | 740 | TypeTag.CLASS, |
aoqi@0 | 741 | token.stringVal()); |
aoqi@0 | 742 | break; |
aoqi@0 | 743 | case TRUE: case FALSE: |
aoqi@0 | 744 | t = F.at(pos).Literal( |
aoqi@0 | 745 | TypeTag.BOOLEAN, |
aoqi@0 | 746 | (token.kind == TRUE ? 1 : 0)); |
aoqi@0 | 747 | break; |
aoqi@0 | 748 | case NULL: |
aoqi@0 | 749 | t = F.at(pos).Literal( |
aoqi@0 | 750 | TypeTag.BOT, |
aoqi@0 | 751 | null); |
aoqi@0 | 752 | break; |
aoqi@0 | 753 | default: |
aoqi@0 | 754 | Assert.error(); |
aoqi@0 | 755 | } |
aoqi@0 | 756 | if (t == errorTree) |
aoqi@0 | 757 | t = F.at(pos).Erroneous(); |
aoqi@0 | 758 | storeEnd(t, token.endPos); |
aoqi@0 | 759 | nextToken(); |
aoqi@0 | 760 | return t; |
aoqi@0 | 761 | } |
aoqi@0 | 762 | //where |
aoqi@0 | 763 | boolean isZero(String s) { |
aoqi@0 | 764 | char[] cs = s.toCharArray(); |
aoqi@0 | 765 | int base = ((cs.length > 1 && Character.toLowerCase(cs[1]) == 'x') ? 16 : 10); |
aoqi@0 | 766 | int i = ((base==16) ? 2 : 0); |
aoqi@0 | 767 | while (i < cs.length && (cs[i] == '0' || cs[i] == '.')) i++; |
aoqi@0 | 768 | return !(i < cs.length && (Character.digit(cs[i], base) > 0)); |
aoqi@0 | 769 | } |
aoqi@0 | 770 | |
aoqi@0 | 771 | String strval(Name prefix) { |
aoqi@0 | 772 | String s = token.stringVal(); |
aoqi@0 | 773 | return prefix.isEmpty() ? s : prefix + s; |
aoqi@0 | 774 | } |
aoqi@0 | 775 | |
aoqi@0 | 776 | /** terms can be either expressions or types. |
aoqi@0 | 777 | */ |
aoqi@0 | 778 | public JCExpression parseExpression() { |
aoqi@0 | 779 | return term(EXPR); |
aoqi@0 | 780 | } |
aoqi@0 | 781 | |
aoqi@0 | 782 | /** |
aoqi@0 | 783 | * parses (optional) type annotations followed by a type. If the |
aoqi@0 | 784 | * annotations are present before the type and are not consumed during array |
aoqi@0 | 785 | * parsing, this method returns a {@link JCAnnotatedType} consisting of |
aoqi@0 | 786 | * these annotations and the underlying type. Otherwise, it returns the |
aoqi@0 | 787 | * underlying type. |
aoqi@0 | 788 | * |
aoqi@0 | 789 | * <p> |
aoqi@0 | 790 | * |
aoqi@0 | 791 | * Note that this method sets {@code mode} to {@code TYPE} first, before |
aoqi@0 | 792 | * parsing annotations. |
aoqi@0 | 793 | */ |
aoqi@0 | 794 | public JCExpression parseType() { |
aoqi@0 | 795 | List<JCAnnotation> annotations = typeAnnotationsOpt(); |
aoqi@0 | 796 | return parseType(annotations); |
aoqi@0 | 797 | } |
aoqi@0 | 798 | |
aoqi@0 | 799 | public JCExpression parseType(List<JCAnnotation> annotations) { |
aoqi@0 | 800 | JCExpression result = unannotatedType(); |
aoqi@0 | 801 | |
aoqi@0 | 802 | if (annotations.nonEmpty()) { |
aoqi@0 | 803 | result = insertAnnotationsToMostInner(result, annotations, false); |
aoqi@0 | 804 | } |
aoqi@0 | 805 | |
aoqi@0 | 806 | return result; |
aoqi@0 | 807 | } |
aoqi@0 | 808 | |
aoqi@0 | 809 | public JCExpression unannotatedType() { |
aoqi@0 | 810 | return term(TYPE); |
aoqi@0 | 811 | } |
aoqi@0 | 812 | |
aoqi@0 | 813 | JCExpression term(int newmode) { |
aoqi@0 | 814 | int prevmode = mode; |
aoqi@0 | 815 | mode = newmode; |
aoqi@0 | 816 | JCExpression t = term(); |
aoqi@0 | 817 | lastmode = mode; |
aoqi@0 | 818 | mode = prevmode; |
aoqi@0 | 819 | return t; |
aoqi@0 | 820 | } |
aoqi@0 | 821 | |
aoqi@0 | 822 | /** |
aoqi@0 | 823 | * {@literal |
aoqi@0 | 824 | * Expression = Expression1 [ExpressionRest] |
aoqi@0 | 825 | * ExpressionRest = [AssignmentOperator Expression1] |
aoqi@0 | 826 | * AssignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" | |
aoqi@0 | 827 | * "&=" | "|=" | "^=" | |
aoqi@0 | 828 | * "%=" | "<<=" | ">>=" | ">>>=" |
aoqi@0 | 829 | * Type = Type1 |
aoqi@0 | 830 | * TypeNoParams = TypeNoParams1 |
aoqi@0 | 831 | * StatementExpression = Expression |
aoqi@0 | 832 | * ConstantExpression = Expression |
aoqi@0 | 833 | * } |
aoqi@0 | 834 | */ |
aoqi@0 | 835 | JCExpression term() { |
aoqi@0 | 836 | JCExpression t = term1(); |
aoqi@0 | 837 | if ((mode & EXPR) != 0 && |
aoqi@0 | 838 | token.kind == EQ || PLUSEQ.compareTo(token.kind) <= 0 && token.kind.compareTo(GTGTGTEQ) <= 0) |
aoqi@0 | 839 | return termRest(t); |
aoqi@0 | 840 | else |
aoqi@0 | 841 | return t; |
aoqi@0 | 842 | } |
aoqi@0 | 843 | |
aoqi@0 | 844 | JCExpression termRest(JCExpression t) { |
aoqi@0 | 845 | switch (token.kind) { |
aoqi@0 | 846 | case EQ: { |
aoqi@0 | 847 | int pos = token.pos; |
aoqi@0 | 848 | nextToken(); |
aoqi@0 | 849 | mode = EXPR; |
aoqi@0 | 850 | JCExpression t1 = term(); |
aoqi@0 | 851 | return toP(F.at(pos).Assign(t, t1)); |
aoqi@0 | 852 | } |
aoqi@0 | 853 | case PLUSEQ: |
aoqi@0 | 854 | case SUBEQ: |
aoqi@0 | 855 | case STAREQ: |
aoqi@0 | 856 | case SLASHEQ: |
aoqi@0 | 857 | case PERCENTEQ: |
aoqi@0 | 858 | case AMPEQ: |
aoqi@0 | 859 | case BAREQ: |
aoqi@0 | 860 | case CARETEQ: |
aoqi@0 | 861 | case LTLTEQ: |
aoqi@0 | 862 | case GTGTEQ: |
aoqi@0 | 863 | case GTGTGTEQ: |
aoqi@0 | 864 | int pos = token.pos; |
aoqi@0 | 865 | TokenKind tk = token.kind; |
aoqi@0 | 866 | nextToken(); |
aoqi@0 | 867 | mode = EXPR; |
aoqi@0 | 868 | JCExpression t1 = term(); |
aoqi@0 | 869 | return F.at(pos).Assignop(optag(tk), t, t1); |
aoqi@0 | 870 | default: |
aoqi@0 | 871 | return t; |
aoqi@0 | 872 | } |
aoqi@0 | 873 | } |
aoqi@0 | 874 | |
aoqi@0 | 875 | /** Expression1 = Expression2 [Expression1Rest] |
aoqi@0 | 876 | * Type1 = Type2 |
aoqi@0 | 877 | * TypeNoParams1 = TypeNoParams2 |
aoqi@0 | 878 | */ |
aoqi@0 | 879 | JCExpression term1() { |
aoqi@0 | 880 | JCExpression t = term2(); |
aoqi@0 | 881 | if ((mode & EXPR) != 0 && token.kind == QUES) { |
aoqi@0 | 882 | mode = EXPR; |
aoqi@0 | 883 | return term1Rest(t); |
aoqi@0 | 884 | } else { |
aoqi@0 | 885 | return t; |
aoqi@0 | 886 | } |
aoqi@0 | 887 | } |
aoqi@0 | 888 | |
aoqi@0 | 889 | /** Expression1Rest = ["?" Expression ":" Expression1] |
aoqi@0 | 890 | */ |
aoqi@0 | 891 | JCExpression term1Rest(JCExpression t) { |
aoqi@0 | 892 | if (token.kind == QUES) { |
aoqi@0 | 893 | int pos = token.pos; |
aoqi@0 | 894 | nextToken(); |
aoqi@0 | 895 | JCExpression t1 = term(); |
aoqi@0 | 896 | accept(COLON); |
aoqi@0 | 897 | JCExpression t2 = term1(); |
aoqi@0 | 898 | return F.at(pos).Conditional(t, t1, t2); |
aoqi@0 | 899 | } else { |
aoqi@0 | 900 | return t; |
aoqi@0 | 901 | } |
aoqi@0 | 902 | } |
aoqi@0 | 903 | |
aoqi@0 | 904 | /** Expression2 = Expression3 [Expression2Rest] |
aoqi@0 | 905 | * Type2 = Type3 |
aoqi@0 | 906 | * TypeNoParams2 = TypeNoParams3 |
aoqi@0 | 907 | */ |
aoqi@0 | 908 | JCExpression term2() { |
aoqi@0 | 909 | JCExpression t = term3(); |
aoqi@0 | 910 | if ((mode & EXPR) != 0 && prec(token.kind) >= TreeInfo.orPrec) { |
aoqi@0 | 911 | mode = EXPR; |
aoqi@0 | 912 | return term2Rest(t, TreeInfo.orPrec); |
aoqi@0 | 913 | } else { |
aoqi@0 | 914 | return t; |
aoqi@0 | 915 | } |
aoqi@0 | 916 | } |
aoqi@0 | 917 | |
aoqi@0 | 918 | /* Expression2Rest = {infixop Expression3} |
aoqi@0 | 919 | * | Expression3 instanceof Type |
aoqi@0 | 920 | * infixop = "||" |
aoqi@0 | 921 | * | "&&" |
aoqi@0 | 922 | * | "|" |
aoqi@0 | 923 | * | "^" |
aoqi@0 | 924 | * | "&" |
aoqi@0 | 925 | * | "==" | "!=" |
aoqi@0 | 926 | * | "<" | ">" | "<=" | ">=" |
aoqi@0 | 927 | * | "<<" | ">>" | ">>>" |
aoqi@0 | 928 | * | "+" | "-" |
aoqi@0 | 929 | * | "*" | "/" | "%" |
aoqi@0 | 930 | */ |
aoqi@0 | 931 | JCExpression term2Rest(JCExpression t, int minprec) { |
aoqi@0 | 932 | JCExpression[] odStack = newOdStack(); |
aoqi@0 | 933 | Token[] opStack = newOpStack(); |
aoqi@0 | 934 | |
aoqi@0 | 935 | // optimization, was odStack = new Tree[...]; opStack = new Tree[...]; |
aoqi@0 | 936 | int top = 0; |
aoqi@0 | 937 | odStack[0] = t; |
aoqi@0 | 938 | int startPos = token.pos; |
aoqi@0 | 939 | Token topOp = Tokens.DUMMY; |
aoqi@0 | 940 | while (prec(token.kind) >= minprec) { |
aoqi@0 | 941 | opStack[top] = topOp; |
aoqi@0 | 942 | top++; |
aoqi@0 | 943 | topOp = token; |
aoqi@0 | 944 | nextToken(); |
aoqi@0 | 945 | odStack[top] = (topOp.kind == INSTANCEOF) ? parseType() : term3(); |
aoqi@0 | 946 | while (top > 0 && prec(topOp.kind) >= prec(token.kind)) { |
aoqi@0 | 947 | odStack[top-1] = makeOp(topOp.pos, topOp.kind, odStack[top-1], |
aoqi@0 | 948 | odStack[top]); |
aoqi@0 | 949 | top--; |
aoqi@0 | 950 | topOp = opStack[top]; |
aoqi@0 | 951 | } |
aoqi@0 | 952 | } |
aoqi@0 | 953 | Assert.check(top == 0); |
aoqi@0 | 954 | t = odStack[0]; |
aoqi@0 | 955 | |
aoqi@0 | 956 | if (t.hasTag(JCTree.Tag.PLUS)) { |
aoqi@0 | 957 | StringBuilder buf = foldStrings(t); |
aoqi@0 | 958 | if (buf != null) { |
aoqi@0 | 959 | t = toP(F.at(startPos).Literal(TypeTag.CLASS, buf.toString())); |
aoqi@0 | 960 | } |
aoqi@0 | 961 | } |
aoqi@0 | 962 | |
aoqi@0 | 963 | odStackSupply.add(odStack); |
aoqi@0 | 964 | opStackSupply.add(opStack); |
aoqi@0 | 965 | return t; |
aoqi@0 | 966 | } |
aoqi@0 | 967 | //where |
aoqi@0 | 968 | /** Construct a binary or type test node. |
aoqi@0 | 969 | */ |
aoqi@0 | 970 | private JCExpression makeOp(int pos, |
aoqi@0 | 971 | TokenKind topOp, |
aoqi@0 | 972 | JCExpression od1, |
aoqi@0 | 973 | JCExpression od2) |
aoqi@0 | 974 | { |
aoqi@0 | 975 | if (topOp == INSTANCEOF) { |
aoqi@0 | 976 | return F.at(pos).TypeTest(od1, od2); |
aoqi@0 | 977 | } else { |
aoqi@0 | 978 | return F.at(pos).Binary(optag(topOp), od1, od2); |
aoqi@0 | 979 | } |
aoqi@0 | 980 | } |
aoqi@0 | 981 | /** If tree is a concatenation of string literals, replace it |
aoqi@0 | 982 | * by a single literal representing the concatenated string. |
aoqi@0 | 983 | */ |
aoqi@0 | 984 | protected StringBuilder foldStrings(JCTree tree) { |
aoqi@0 | 985 | if (!allowStringFolding) |
aoqi@0 | 986 | return null; |
aoqi@0 | 987 | List<String> buf = List.nil(); |
aoqi@0 | 988 | while (true) { |
aoqi@0 | 989 | if (tree.hasTag(LITERAL)) { |
aoqi@0 | 990 | JCLiteral lit = (JCLiteral) tree; |
aoqi@0 | 991 | if (lit.typetag == TypeTag.CLASS) { |
aoqi@0 | 992 | StringBuilder sbuf = |
aoqi@0 | 993 | new StringBuilder((String)lit.value); |
aoqi@0 | 994 | while (buf.nonEmpty()) { |
aoqi@0 | 995 | sbuf.append(buf.head); |
aoqi@0 | 996 | buf = buf.tail; |
aoqi@0 | 997 | } |
aoqi@0 | 998 | return sbuf; |
aoqi@0 | 999 | } |
aoqi@0 | 1000 | } else if (tree.hasTag(JCTree.Tag.PLUS)) { |
aoqi@0 | 1001 | JCBinary op = (JCBinary)tree; |
aoqi@0 | 1002 | if (op.rhs.hasTag(LITERAL)) { |
aoqi@0 | 1003 | JCLiteral lit = (JCLiteral) op.rhs; |
aoqi@0 | 1004 | if (lit.typetag == TypeTag.CLASS) { |
aoqi@0 | 1005 | buf = buf.prepend((String) lit.value); |
aoqi@0 | 1006 | tree = op.lhs; |
aoqi@0 | 1007 | continue; |
aoqi@0 | 1008 | } |
aoqi@0 | 1009 | } |
aoqi@0 | 1010 | } |
aoqi@0 | 1011 | return null; |
aoqi@0 | 1012 | } |
aoqi@0 | 1013 | } |
aoqi@0 | 1014 | |
aoqi@0 | 1015 | /** optimization: To save allocating a new operand/operator stack |
aoqi@0 | 1016 | * for every binary operation, we use supplys. |
aoqi@0 | 1017 | */ |
aoqi@0 | 1018 | ArrayList<JCExpression[]> odStackSupply = new ArrayList<JCExpression[]>(); |
aoqi@0 | 1019 | ArrayList<Token[]> opStackSupply = new ArrayList<Token[]>(); |
aoqi@0 | 1020 | |
aoqi@0 | 1021 | private JCExpression[] newOdStack() { |
aoqi@0 | 1022 | if (odStackSupply.isEmpty()) |
aoqi@0 | 1023 | return new JCExpression[infixPrecedenceLevels + 1]; |
aoqi@0 | 1024 | return odStackSupply.remove(odStackSupply.size() - 1); |
aoqi@0 | 1025 | } |
aoqi@0 | 1026 | |
aoqi@0 | 1027 | private Token[] newOpStack() { |
aoqi@0 | 1028 | if (opStackSupply.isEmpty()) |
aoqi@0 | 1029 | return new Token[infixPrecedenceLevels + 1]; |
aoqi@0 | 1030 | return opStackSupply.remove(opStackSupply.size() - 1); |
aoqi@0 | 1031 | } |
aoqi@0 | 1032 | |
aoqi@0 | 1033 | /** |
aoqi@0 | 1034 | * Expression3 = PrefixOp Expression3 |
aoqi@0 | 1035 | * | "(" Expr | TypeNoParams ")" Expression3 |
aoqi@0 | 1036 | * | Primary {Selector} {PostfixOp} |
aoqi@0 | 1037 | * |
aoqi@0 | 1038 | * {@literal |
aoqi@0 | 1039 | * Primary = "(" Expression ")" |
aoqi@0 | 1040 | * | Literal |
aoqi@0 | 1041 | * | [TypeArguments] THIS [Arguments] |
aoqi@0 | 1042 | * | [TypeArguments] SUPER SuperSuffix |
aoqi@0 | 1043 | * | NEW [TypeArguments] Creator |
aoqi@0 | 1044 | * | "(" Arguments ")" "->" ( Expression | Block ) |
aoqi@0 | 1045 | * | Ident "->" ( Expression | Block ) |
aoqi@0 | 1046 | * | [Annotations] Ident { "." [Annotations] Ident } |
aoqi@0 | 1047 | * | Expression3 MemberReferenceSuffix |
aoqi@0 | 1048 | * [ [Annotations] "[" ( "]" BracketsOpt "." CLASS | Expression "]" ) |
aoqi@0 | 1049 | * | Arguments |
aoqi@0 | 1050 | * | "." ( CLASS | THIS | [TypeArguments] SUPER Arguments | NEW [TypeArguments] InnerCreator ) |
aoqi@0 | 1051 | * ] |
aoqi@0 | 1052 | * | BasicType BracketsOpt "." CLASS |
aoqi@0 | 1053 | * } |
aoqi@0 | 1054 | * |
aoqi@0 | 1055 | * PrefixOp = "++" | "--" | "!" | "~" | "+" | "-" |
aoqi@0 | 1056 | * PostfixOp = "++" | "--" |
aoqi@0 | 1057 | * Type3 = Ident { "." Ident } [TypeArguments] {TypeSelector} BracketsOpt |
aoqi@0 | 1058 | * | BasicType |
aoqi@0 | 1059 | * TypeNoParams3 = Ident { "." Ident } BracketsOpt |
aoqi@0 | 1060 | * Selector = "." [TypeArguments] Ident [Arguments] |
aoqi@0 | 1061 | * | "." THIS |
aoqi@0 | 1062 | * | "." [TypeArguments] SUPER SuperSuffix |
aoqi@0 | 1063 | * | "." NEW [TypeArguments] InnerCreator |
aoqi@0 | 1064 | * | "[" Expression "]" |
aoqi@0 | 1065 | * TypeSelector = "." Ident [TypeArguments] |
aoqi@0 | 1066 | * SuperSuffix = Arguments | "." Ident [Arguments] |
aoqi@0 | 1067 | */ |
aoqi@0 | 1068 | protected JCExpression term3() { |
aoqi@0 | 1069 | int pos = token.pos; |
aoqi@0 | 1070 | JCExpression t; |
aoqi@0 | 1071 | List<JCExpression> typeArgs = typeArgumentsOpt(EXPR); |
aoqi@0 | 1072 | switch (token.kind) { |
aoqi@0 | 1073 | case QUES: |
aoqi@0 | 1074 | if ((mode & TYPE) != 0 && (mode & (TYPEARG|NOPARAMS)) == TYPEARG) { |
aoqi@0 | 1075 | mode = TYPE; |
aoqi@0 | 1076 | return typeArgument(); |
aoqi@0 | 1077 | } else |
aoqi@0 | 1078 | return illegal(); |
aoqi@0 | 1079 | case PLUSPLUS: case SUBSUB: case BANG: case TILDE: case PLUS: case SUB: |
aoqi@0 | 1080 | if (typeArgs == null && (mode & EXPR) != 0) { |
aoqi@0 | 1081 | TokenKind tk = token.kind; |
aoqi@0 | 1082 | nextToken(); |
aoqi@0 | 1083 | mode = EXPR; |
aoqi@0 | 1084 | if (tk == SUB && |
aoqi@0 | 1085 | (token.kind == INTLITERAL || token.kind == LONGLITERAL) && |
aoqi@0 | 1086 | token.radix() == 10) { |
aoqi@0 | 1087 | mode = EXPR; |
aoqi@0 | 1088 | t = literal(names.hyphen, pos); |
aoqi@0 | 1089 | } else { |
aoqi@0 | 1090 | t = term3(); |
aoqi@0 | 1091 | return F.at(pos).Unary(unoptag(tk), t); |
aoqi@0 | 1092 | } |
aoqi@0 | 1093 | } else return illegal(); |
aoqi@0 | 1094 | break; |
aoqi@0 | 1095 | case LPAREN: |
aoqi@0 | 1096 | if (typeArgs == null && (mode & EXPR) != 0) { |
aoqi@0 | 1097 | ParensResult pres = analyzeParens(); |
aoqi@0 | 1098 | switch (pres) { |
aoqi@0 | 1099 | case CAST: |
aoqi@0 | 1100 | accept(LPAREN); |
aoqi@0 | 1101 | mode = TYPE; |
aoqi@0 | 1102 | int pos1 = pos; |
aoqi@0 | 1103 | List<JCExpression> targets = List.of(t = term3()); |
aoqi@0 | 1104 | while (token.kind == AMP) { |
aoqi@0 | 1105 | checkIntersectionTypesInCast(); |
aoqi@0 | 1106 | accept(AMP); |
aoqi@0 | 1107 | targets = targets.prepend(term3()); |
aoqi@0 | 1108 | } |
aoqi@0 | 1109 | if (targets.length() > 1) { |
aoqi@0 | 1110 | t = toP(F.at(pos1).TypeIntersection(targets.reverse())); |
aoqi@0 | 1111 | } |
aoqi@0 | 1112 | accept(RPAREN); |
aoqi@0 | 1113 | mode = EXPR; |
aoqi@0 | 1114 | JCExpression t1 = term3(); |
aoqi@0 | 1115 | return F.at(pos).TypeCast(t, t1); |
aoqi@0 | 1116 | case IMPLICIT_LAMBDA: |
aoqi@0 | 1117 | case EXPLICIT_LAMBDA: |
aoqi@0 | 1118 | t = lambdaExpressionOrStatement(true, pres == ParensResult.EXPLICIT_LAMBDA, pos); |
aoqi@0 | 1119 | break; |
aoqi@0 | 1120 | default: //PARENS |
aoqi@0 | 1121 | accept(LPAREN); |
aoqi@0 | 1122 | mode = EXPR; |
aoqi@0 | 1123 | t = termRest(term1Rest(term2Rest(term3(), TreeInfo.orPrec))); |
aoqi@0 | 1124 | accept(RPAREN); |
aoqi@0 | 1125 | t = toP(F.at(pos).Parens(t)); |
aoqi@0 | 1126 | break; |
aoqi@0 | 1127 | } |
aoqi@0 | 1128 | } else { |
aoqi@0 | 1129 | return illegal(); |
aoqi@0 | 1130 | } |
aoqi@0 | 1131 | break; |
aoqi@0 | 1132 | case THIS: |
aoqi@0 | 1133 | if ((mode & EXPR) != 0) { |
aoqi@0 | 1134 | mode = EXPR; |
aoqi@0 | 1135 | t = to(F.at(pos).Ident(names._this)); |
aoqi@0 | 1136 | nextToken(); |
aoqi@0 | 1137 | if (typeArgs == null) |
aoqi@0 | 1138 | t = argumentsOpt(null, t); |
aoqi@0 | 1139 | else |
aoqi@0 | 1140 | t = arguments(typeArgs, t); |
aoqi@0 | 1141 | typeArgs = null; |
aoqi@0 | 1142 | } else return illegal(); |
aoqi@0 | 1143 | break; |
aoqi@0 | 1144 | case SUPER: |
aoqi@0 | 1145 | if ((mode & EXPR) != 0) { |
aoqi@0 | 1146 | mode = EXPR; |
aoqi@0 | 1147 | t = to(F.at(pos).Ident(names._super)); |
aoqi@0 | 1148 | t = superSuffix(typeArgs, t); |
aoqi@0 | 1149 | typeArgs = null; |
aoqi@0 | 1150 | } else return illegal(); |
aoqi@0 | 1151 | break; |
aoqi@0 | 1152 | case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: case DOUBLELITERAL: |
aoqi@0 | 1153 | case CHARLITERAL: case STRINGLITERAL: |
aoqi@0 | 1154 | case TRUE: case FALSE: case NULL: |
aoqi@0 | 1155 | if (typeArgs == null && (mode & EXPR) != 0) { |
aoqi@0 | 1156 | mode = EXPR; |
aoqi@0 | 1157 | t = literal(names.empty); |
aoqi@0 | 1158 | } else return illegal(); |
aoqi@0 | 1159 | break; |
aoqi@0 | 1160 | case NEW: |
aoqi@0 | 1161 | if (typeArgs != null) return illegal(); |
aoqi@0 | 1162 | if ((mode & EXPR) != 0) { |
aoqi@0 | 1163 | mode = EXPR; |
aoqi@0 | 1164 | nextToken(); |
aoqi@0 | 1165 | if (token.kind == LT) typeArgs = typeArguments(false); |
aoqi@0 | 1166 | t = creator(pos, typeArgs); |
aoqi@0 | 1167 | typeArgs = null; |
aoqi@0 | 1168 | } else return illegal(); |
aoqi@0 | 1169 | break; |
aoqi@0 | 1170 | case MONKEYS_AT: |
aoqi@0 | 1171 | // Only annotated cast types and method references are valid |
aoqi@0 | 1172 | List<JCAnnotation> typeAnnos = typeAnnotationsOpt(); |
aoqi@0 | 1173 | if (typeAnnos.isEmpty()) { |
aoqi@0 | 1174 | // else there would be no '@' |
aoqi@0 | 1175 | throw new AssertionError("Expected type annotations, but found none!"); |
aoqi@0 | 1176 | } |
aoqi@0 | 1177 | |
aoqi@0 | 1178 | JCExpression expr = term3(); |
aoqi@0 | 1179 | |
aoqi@0 | 1180 | if ((mode & TYPE) == 0) { |
aoqi@0 | 1181 | // Type annotations on class literals no longer legal |
aoqi@0 | 1182 | switch (expr.getTag()) { |
aoqi@0 | 1183 | case REFERENCE: { |
aoqi@0 | 1184 | JCMemberReference mref = (JCMemberReference) expr; |
aoqi@0 | 1185 | mref.expr = toP(F.at(pos).AnnotatedType(typeAnnos, mref.expr)); |
aoqi@0 | 1186 | t = mref; |
aoqi@0 | 1187 | break; |
aoqi@0 | 1188 | } |
aoqi@0 | 1189 | case SELECT: { |
aoqi@0 | 1190 | JCFieldAccess sel = (JCFieldAccess) expr; |
aoqi@0 | 1191 | |
aoqi@0 | 1192 | if (sel.name != names._class) { |
aoqi@0 | 1193 | return illegal(); |
aoqi@0 | 1194 | } else { |
aoqi@0 | 1195 | log.error(token.pos, "no.annotations.on.dot.class"); |
aoqi@0 | 1196 | return expr; |
aoqi@0 | 1197 | } |
aoqi@0 | 1198 | } |
aoqi@0 | 1199 | default: |
aoqi@0 | 1200 | return illegal(typeAnnos.head.pos); |
aoqi@0 | 1201 | } |
aoqi@0 | 1202 | |
aoqi@0 | 1203 | } else { |
aoqi@0 | 1204 | // Type annotations targeting a cast |
aoqi@0 | 1205 | t = insertAnnotationsToMostInner(expr, typeAnnos, false); |
aoqi@0 | 1206 | } |
aoqi@0 | 1207 | break; |
aoqi@0 | 1208 | case UNDERSCORE: case IDENTIFIER: case ASSERT: case ENUM: |
aoqi@0 | 1209 | if (typeArgs != null) return illegal(); |
aoqi@0 | 1210 | if ((mode & EXPR) != 0 && peekToken(ARROW)) { |
aoqi@0 | 1211 | t = lambdaExpressionOrStatement(false, false, pos); |
aoqi@0 | 1212 | } else { |
aoqi@0 | 1213 | t = toP(F.at(token.pos).Ident(ident())); |
aoqi@0 | 1214 | loop: while (true) { |
aoqi@0 | 1215 | pos = token.pos; |
aoqi@0 | 1216 | final List<JCAnnotation> annos = typeAnnotationsOpt(); |
aoqi@0 | 1217 | |
aoqi@0 | 1218 | // need to report an error later if LBRACKET is for array |
aoqi@0 | 1219 | // index access rather than array creation level |
aoqi@0 | 1220 | if (!annos.isEmpty() && token.kind != LBRACKET && token.kind != ELLIPSIS) |
aoqi@0 | 1221 | return illegal(annos.head.pos); |
aoqi@0 | 1222 | |
aoqi@0 | 1223 | switch (token.kind) { |
aoqi@0 | 1224 | case LBRACKET: |
aoqi@0 | 1225 | nextToken(); |
aoqi@0 | 1226 | if (token.kind == RBRACKET) { |
aoqi@0 | 1227 | nextToken(); |
aoqi@0 | 1228 | t = bracketsOpt(t); |
aoqi@0 | 1229 | t = toP(F.at(pos).TypeArray(t)); |
aoqi@0 | 1230 | if (annos.nonEmpty()) { |
aoqi@0 | 1231 | t = toP(F.at(pos).AnnotatedType(annos, t)); |
aoqi@0 | 1232 | } |
aoqi@0 | 1233 | // .class is only allowed if there were no annotations |
aoqi@0 | 1234 | JCExpression nt = bracketsSuffix(t); |
aoqi@0 | 1235 | if (nt != t && (annos.nonEmpty() || TreeInfo.containsTypeAnnotation(t))) { |
aoqi@0 | 1236 | // t and nt are different if bracketsSuffix parsed a .class. |
aoqi@0 | 1237 | // The check for nonEmpty covers the case when the whole array is annotated. |
aoqi@0 | 1238 | // Helper method isAnnotated looks for annos deeply within t. |
aoqi@0 | 1239 | syntaxError("no.annotations.on.dot.class"); |
aoqi@0 | 1240 | } |
aoqi@0 | 1241 | t = nt; |
aoqi@0 | 1242 | } else { |
aoqi@0 | 1243 | if ((mode & EXPR) != 0) { |
aoqi@0 | 1244 | mode = EXPR; |
aoqi@0 | 1245 | JCExpression t1 = term(); |
aoqi@0 | 1246 | if (!annos.isEmpty()) t = illegal(annos.head.pos); |
aoqi@0 | 1247 | t = to(F.at(pos).Indexed(t, t1)); |
aoqi@0 | 1248 | } |
aoqi@0 | 1249 | accept(RBRACKET); |
aoqi@0 | 1250 | } |
aoqi@0 | 1251 | break loop; |
aoqi@0 | 1252 | case LPAREN: |
aoqi@0 | 1253 | if ((mode & EXPR) != 0) { |
aoqi@0 | 1254 | mode = EXPR; |
aoqi@0 | 1255 | t = arguments(typeArgs, t); |
aoqi@0 | 1256 | if (!annos.isEmpty()) t = illegal(annos.head.pos); |
aoqi@0 | 1257 | typeArgs = null; |
aoqi@0 | 1258 | } |
aoqi@0 | 1259 | break loop; |
aoqi@0 | 1260 | case DOT: |
aoqi@0 | 1261 | nextToken(); |
aoqi@0 | 1262 | int oldmode = mode; |
aoqi@0 | 1263 | mode &= ~NOPARAMS; |
aoqi@0 | 1264 | typeArgs = typeArgumentsOpt(EXPR); |
aoqi@0 | 1265 | mode = oldmode; |
aoqi@0 | 1266 | if ((mode & EXPR) != 0) { |
aoqi@0 | 1267 | switch (token.kind) { |
aoqi@0 | 1268 | case CLASS: |
aoqi@0 | 1269 | if (typeArgs != null) return illegal(); |
aoqi@0 | 1270 | mode = EXPR; |
aoqi@0 | 1271 | t = to(F.at(pos).Select(t, names._class)); |
aoqi@0 | 1272 | nextToken(); |
aoqi@0 | 1273 | break loop; |
aoqi@0 | 1274 | case THIS: |
aoqi@0 | 1275 | if (typeArgs != null) return illegal(); |
aoqi@0 | 1276 | mode = EXPR; |
aoqi@0 | 1277 | t = to(F.at(pos).Select(t, names._this)); |
aoqi@0 | 1278 | nextToken(); |
aoqi@0 | 1279 | break loop; |
aoqi@0 | 1280 | case SUPER: |
aoqi@0 | 1281 | mode = EXPR; |
aoqi@0 | 1282 | t = to(F.at(pos).Select(t, names._super)); |
aoqi@0 | 1283 | t = superSuffix(typeArgs, t); |
aoqi@0 | 1284 | typeArgs = null; |
aoqi@0 | 1285 | break loop; |
aoqi@0 | 1286 | case NEW: |
aoqi@0 | 1287 | if (typeArgs != null) return illegal(); |
aoqi@0 | 1288 | mode = EXPR; |
aoqi@0 | 1289 | int pos1 = token.pos; |
aoqi@0 | 1290 | nextToken(); |
aoqi@0 | 1291 | if (token.kind == LT) typeArgs = typeArguments(false); |
aoqi@0 | 1292 | t = innerCreator(pos1, typeArgs, t); |
aoqi@0 | 1293 | typeArgs = null; |
aoqi@0 | 1294 | break loop; |
aoqi@0 | 1295 | } |
aoqi@0 | 1296 | } |
aoqi@0 | 1297 | |
aoqi@0 | 1298 | List<JCAnnotation> tyannos = null; |
aoqi@0 | 1299 | if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) { |
aoqi@0 | 1300 | tyannos = typeAnnotationsOpt(); |
aoqi@0 | 1301 | } |
aoqi@0 | 1302 | // typeArgs saved for next loop iteration. |
aoqi@0 | 1303 | t = toP(F.at(pos).Select(t, ident())); |
aoqi@0 | 1304 | if (tyannos != null && tyannos.nonEmpty()) { |
aoqi@0 | 1305 | t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t)); |
aoqi@0 | 1306 | } |
aoqi@0 | 1307 | break; |
aoqi@0 | 1308 | case ELLIPSIS: |
aoqi@0 | 1309 | if (this.permitTypeAnnotationsPushBack) { |
aoqi@0 | 1310 | this.typeAnnotationsPushedBack = annos; |
aoqi@0 | 1311 | } else if (annos.nonEmpty()) { |
aoqi@0 | 1312 | // Don't return here -- error recovery attempt |
aoqi@0 | 1313 | illegal(annos.head.pos); |
aoqi@0 | 1314 | } |
aoqi@0 | 1315 | break loop; |
aoqi@0 | 1316 | case LT: |
aoqi@0 | 1317 | if ((mode & TYPE) == 0 && isUnboundMemberRef()) { |
aoqi@0 | 1318 | //this is an unbound method reference whose qualifier |
aoqi@0 | 1319 | //is a generic type i.e. A<S>::m |
aoqi@0 | 1320 | int pos1 = token.pos; |
aoqi@0 | 1321 | accept(LT); |
aoqi@0 | 1322 | ListBuffer<JCExpression> args = new ListBuffer<JCExpression>(); |
aoqi@0 | 1323 | args.append(typeArgument()); |
aoqi@0 | 1324 | while (token.kind == COMMA) { |
aoqi@0 | 1325 | nextToken(); |
aoqi@0 | 1326 | args.append(typeArgument()); |
aoqi@0 | 1327 | } |
aoqi@0 | 1328 | accept(GT); |
aoqi@0 | 1329 | t = toP(F.at(pos1).TypeApply(t, args.toList())); |
aoqi@0 | 1330 | checkGenerics(); |
aoqi@0 | 1331 | while (token.kind == DOT) { |
aoqi@0 | 1332 | nextToken(); |
aoqi@0 | 1333 | mode = TYPE; |
aoqi@0 | 1334 | t = toP(F.at(token.pos).Select(t, ident())); |
aoqi@0 | 1335 | t = typeArgumentsOpt(t); |
aoqi@0 | 1336 | } |
aoqi@0 | 1337 | t = bracketsOpt(t); |
aoqi@0 | 1338 | if (token.kind != COLCOL) { |
aoqi@0 | 1339 | //method reference expected here |
aoqi@0 | 1340 | t = illegal(); |
aoqi@0 | 1341 | } |
aoqi@0 | 1342 | mode = EXPR; |
aoqi@0 | 1343 | return term3Rest(t, typeArgs); |
aoqi@0 | 1344 | } |
aoqi@0 | 1345 | break loop; |
aoqi@0 | 1346 | default: |
aoqi@0 | 1347 | break loop; |
aoqi@0 | 1348 | } |
aoqi@0 | 1349 | } |
aoqi@0 | 1350 | } |
aoqi@0 | 1351 | if (typeArgs != null) illegal(); |
aoqi@0 | 1352 | t = typeArgumentsOpt(t); |
aoqi@0 | 1353 | break; |
aoqi@0 | 1354 | case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT: |
aoqi@0 | 1355 | case DOUBLE: case BOOLEAN: |
aoqi@0 | 1356 | if (typeArgs != null) illegal(); |
aoqi@0 | 1357 | t = bracketsSuffix(bracketsOpt(basicType())); |
aoqi@0 | 1358 | break; |
aoqi@0 | 1359 | case VOID: |
aoqi@0 | 1360 | if (typeArgs != null) illegal(); |
aoqi@0 | 1361 | if ((mode & EXPR) != 0) { |
aoqi@0 | 1362 | nextToken(); |
aoqi@0 | 1363 | if (token.kind == DOT) { |
aoqi@0 | 1364 | JCPrimitiveTypeTree ti = toP(F.at(pos).TypeIdent(TypeTag.VOID)); |
aoqi@0 | 1365 | t = bracketsSuffix(ti); |
aoqi@0 | 1366 | } else { |
aoqi@0 | 1367 | return illegal(pos); |
aoqi@0 | 1368 | } |
aoqi@0 | 1369 | } else { |
aoqi@0 | 1370 | // Support the corner case of myMethodHandle.<void>invoke() by passing |
aoqi@0 | 1371 | // a void type (like other primitive types) to the next phase. |
aoqi@0 | 1372 | // The error will be reported in Attr.attribTypes or Attr.visitApply. |
aoqi@0 | 1373 | JCPrimitiveTypeTree ti = to(F.at(pos).TypeIdent(TypeTag.VOID)); |
aoqi@0 | 1374 | nextToken(); |
aoqi@0 | 1375 | return ti; |
aoqi@0 | 1376 | //return illegal(); |
aoqi@0 | 1377 | } |
aoqi@0 | 1378 | break; |
aoqi@0 | 1379 | default: |
aoqi@0 | 1380 | return illegal(); |
aoqi@0 | 1381 | } |
aoqi@0 | 1382 | return term3Rest(t, typeArgs); |
aoqi@0 | 1383 | } |
aoqi@0 | 1384 | |
aoqi@0 | 1385 | JCExpression term3Rest(JCExpression t, List<JCExpression> typeArgs) { |
aoqi@0 | 1386 | if (typeArgs != null) illegal(); |
aoqi@0 | 1387 | while (true) { |
aoqi@0 | 1388 | int pos1 = token.pos; |
aoqi@0 | 1389 | final List<JCAnnotation> annos = typeAnnotationsOpt(); |
aoqi@0 | 1390 | |
aoqi@0 | 1391 | if (token.kind == LBRACKET) { |
aoqi@0 | 1392 | nextToken(); |
aoqi@0 | 1393 | if ((mode & TYPE) != 0) { |
aoqi@0 | 1394 | int oldmode = mode; |
aoqi@0 | 1395 | mode = TYPE; |
aoqi@0 | 1396 | if (token.kind == RBRACKET) { |
aoqi@0 | 1397 | nextToken(); |
aoqi@0 | 1398 | t = bracketsOpt(t); |
aoqi@0 | 1399 | t = toP(F.at(pos1).TypeArray(t)); |
aoqi@0 | 1400 | if (token.kind == COLCOL) { |
aoqi@0 | 1401 | mode = EXPR; |
aoqi@0 | 1402 | continue; |
aoqi@0 | 1403 | } |
aoqi@0 | 1404 | if (annos.nonEmpty()) { |
aoqi@0 | 1405 | t = toP(F.at(pos1).AnnotatedType(annos, t)); |
aoqi@0 | 1406 | } |
aoqi@0 | 1407 | return t; |
aoqi@0 | 1408 | } |
aoqi@0 | 1409 | mode = oldmode; |
aoqi@0 | 1410 | } |
aoqi@0 | 1411 | if ((mode & EXPR) != 0) { |
aoqi@0 | 1412 | mode = EXPR; |
aoqi@0 | 1413 | JCExpression t1 = term(); |
aoqi@0 | 1414 | t = to(F.at(pos1).Indexed(t, t1)); |
aoqi@0 | 1415 | } |
aoqi@0 | 1416 | accept(RBRACKET); |
aoqi@0 | 1417 | } else if (token.kind == DOT) { |
aoqi@0 | 1418 | nextToken(); |
aoqi@0 | 1419 | typeArgs = typeArgumentsOpt(EXPR); |
aoqi@0 | 1420 | if (token.kind == SUPER && (mode & EXPR) != 0) { |
aoqi@0 | 1421 | mode = EXPR; |
aoqi@0 | 1422 | t = to(F.at(pos1).Select(t, names._super)); |
aoqi@0 | 1423 | nextToken(); |
aoqi@0 | 1424 | t = arguments(typeArgs, t); |
aoqi@0 | 1425 | typeArgs = null; |
aoqi@0 | 1426 | } else if (token.kind == NEW && (mode & EXPR) != 0) { |
aoqi@0 | 1427 | if (typeArgs != null) return illegal(); |
aoqi@0 | 1428 | mode = EXPR; |
aoqi@0 | 1429 | int pos2 = token.pos; |
aoqi@0 | 1430 | nextToken(); |
aoqi@0 | 1431 | if (token.kind == LT) typeArgs = typeArguments(false); |
aoqi@0 | 1432 | t = innerCreator(pos2, typeArgs, t); |
aoqi@0 | 1433 | typeArgs = null; |
aoqi@0 | 1434 | } else { |
aoqi@0 | 1435 | List<JCAnnotation> tyannos = null; |
aoqi@0 | 1436 | if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) { |
aoqi@0 | 1437 | // is the mode check needed? |
aoqi@0 | 1438 | tyannos = typeAnnotationsOpt(); |
aoqi@0 | 1439 | } |
aoqi@0 | 1440 | t = toP(F.at(pos1).Select(t, ident())); |
aoqi@0 | 1441 | if (tyannos != null && tyannos.nonEmpty()) { |
aoqi@0 | 1442 | t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t)); |
aoqi@0 | 1443 | } |
aoqi@0 | 1444 | t = argumentsOpt(typeArgs, typeArgumentsOpt(t)); |
aoqi@0 | 1445 | typeArgs = null; |
aoqi@0 | 1446 | } |
aoqi@0 | 1447 | } else if ((mode & EXPR) != 0 && token.kind == COLCOL) { |
aoqi@0 | 1448 | mode = EXPR; |
aoqi@0 | 1449 | if (typeArgs != null) return illegal(); |
aoqi@0 | 1450 | accept(COLCOL); |
aoqi@0 | 1451 | t = memberReferenceSuffix(pos1, t); |
aoqi@0 | 1452 | } else { |
aoqi@0 | 1453 | if (!annos.isEmpty()) { |
aoqi@0 | 1454 | if (permitTypeAnnotationsPushBack) |
aoqi@0 | 1455 | typeAnnotationsPushedBack = annos; |
aoqi@0 | 1456 | else |
aoqi@0 | 1457 | return illegal(annos.head.pos); |
aoqi@0 | 1458 | } |
aoqi@0 | 1459 | break; |
aoqi@0 | 1460 | } |
aoqi@0 | 1461 | } |
aoqi@0 | 1462 | while ((token.kind == PLUSPLUS || token.kind == SUBSUB) && (mode & EXPR) != 0) { |
aoqi@0 | 1463 | mode = EXPR; |
aoqi@0 | 1464 | t = to(F.at(token.pos).Unary( |
aoqi@0 | 1465 | token.kind == PLUSPLUS ? POSTINC : POSTDEC, t)); |
aoqi@0 | 1466 | nextToken(); |
aoqi@0 | 1467 | } |
aoqi@0 | 1468 | return toP(t); |
aoqi@0 | 1469 | } |
aoqi@0 | 1470 | |
aoqi@0 | 1471 | /** |
aoqi@0 | 1472 | * If we see an identifier followed by a '<' it could be an unbound |
aoqi@0 | 1473 | * method reference or a binary expression. To disambiguate, look for a |
aoqi@0 | 1474 | * matching '>' and see if the subsequent terminal is either '.' or '::'. |
aoqi@0 | 1475 | */ |
aoqi@0 | 1476 | @SuppressWarnings("fallthrough") |
aoqi@0 | 1477 | boolean isUnboundMemberRef() { |
aoqi@0 | 1478 | int pos = 0, depth = 0; |
aoqi@0 | 1479 | outer: for (Token t = S.token(pos) ; ; t = S.token(++pos)) { |
aoqi@0 | 1480 | switch (t.kind) { |
aoqi@0 | 1481 | case IDENTIFIER: case UNDERSCORE: case QUES: case EXTENDS: case SUPER: |
aoqi@0 | 1482 | case DOT: case RBRACKET: case LBRACKET: case COMMA: |
aoqi@0 | 1483 | case BYTE: case SHORT: case INT: case LONG: case FLOAT: |
aoqi@0 | 1484 | case DOUBLE: case BOOLEAN: case CHAR: |
aoqi@0 | 1485 | case MONKEYS_AT: |
aoqi@0 | 1486 | break; |
aoqi@0 | 1487 | |
aoqi@0 | 1488 | case LPAREN: |
aoqi@0 | 1489 | // skip annotation values |
aoqi@0 | 1490 | int nesting = 0; |
aoqi@0 | 1491 | for (; ; pos++) { |
aoqi@0 | 1492 | TokenKind tk2 = S.token(pos).kind; |
aoqi@0 | 1493 | switch (tk2) { |
aoqi@0 | 1494 | case EOF: |
aoqi@0 | 1495 | return false; |
aoqi@0 | 1496 | case LPAREN: |
aoqi@0 | 1497 | nesting++; |
aoqi@0 | 1498 | break; |
aoqi@0 | 1499 | case RPAREN: |
aoqi@0 | 1500 | nesting--; |
aoqi@0 | 1501 | if (nesting == 0) { |
aoqi@0 | 1502 | continue outer; |
aoqi@0 | 1503 | } |
aoqi@0 | 1504 | break; |
aoqi@0 | 1505 | } |
aoqi@0 | 1506 | } |
aoqi@0 | 1507 | |
aoqi@0 | 1508 | case LT: |
aoqi@0 | 1509 | depth++; break; |
aoqi@0 | 1510 | case GTGTGT: |
aoqi@0 | 1511 | depth--; |
aoqi@0 | 1512 | case GTGT: |
aoqi@0 | 1513 | depth--; |
aoqi@0 | 1514 | case GT: |
aoqi@0 | 1515 | depth--; |
aoqi@0 | 1516 | if (depth == 0) { |
aoqi@0 | 1517 | TokenKind nextKind = S.token(pos + 1).kind; |
aoqi@0 | 1518 | return |
aoqi@0 | 1519 | nextKind == TokenKind.DOT || |
aoqi@0 | 1520 | nextKind == TokenKind.LBRACKET || |
aoqi@0 | 1521 | nextKind == TokenKind.COLCOL; |
aoqi@0 | 1522 | } |
aoqi@0 | 1523 | break; |
aoqi@0 | 1524 | default: |
aoqi@0 | 1525 | return false; |
aoqi@0 | 1526 | } |
aoqi@0 | 1527 | } |
aoqi@0 | 1528 | } |
aoqi@0 | 1529 | |
aoqi@0 | 1530 | /** |
aoqi@0 | 1531 | * If we see an identifier followed by a '<' it could be an unbound |
aoqi@0 | 1532 | * method reference or a binary expression. To disambiguate, look for a |
aoqi@0 | 1533 | * matching '>' and see if the subsequent terminal is either '.' or '::'. |
aoqi@0 | 1534 | */ |
aoqi@0 | 1535 | @SuppressWarnings("fallthrough") |
aoqi@0 | 1536 | ParensResult analyzeParens() { |
aoqi@0 | 1537 | int depth = 0; |
aoqi@0 | 1538 | boolean type = false; |
aoqi@0 | 1539 | outer: for (int lookahead = 0 ; ; lookahead++) { |
aoqi@0 | 1540 | TokenKind tk = S.token(lookahead).kind; |
aoqi@0 | 1541 | switch (tk) { |
aoqi@0 | 1542 | case COMMA: |
aoqi@0 | 1543 | type = true; |
aoqi@0 | 1544 | case EXTENDS: case SUPER: case DOT: case AMP: |
aoqi@0 | 1545 | //skip |
aoqi@0 | 1546 | break; |
aoqi@0 | 1547 | case QUES: |
aoqi@0 | 1548 | if (peekToken(lookahead, EXTENDS) || |
aoqi@0 | 1549 | peekToken(lookahead, SUPER)) { |
aoqi@0 | 1550 | //wildcards |
aoqi@0 | 1551 | type = true; |
aoqi@0 | 1552 | } |
aoqi@0 | 1553 | break; |
aoqi@0 | 1554 | case BYTE: case SHORT: case INT: case LONG: case FLOAT: |
aoqi@0 | 1555 | case DOUBLE: case BOOLEAN: case CHAR: |
aoqi@0 | 1556 | if (peekToken(lookahead, RPAREN)) { |
aoqi@0 | 1557 | //Type, ')' -> cast |
aoqi@0 | 1558 | return ParensResult.CAST; |
aoqi@0 | 1559 | } else if (peekToken(lookahead, LAX_IDENTIFIER)) { |
aoqi@0 | 1560 | //Type, Identifier/'_'/'assert'/'enum' -> explicit lambda |
aoqi@0 | 1561 | return ParensResult.EXPLICIT_LAMBDA; |
aoqi@0 | 1562 | } |
aoqi@0 | 1563 | break; |
aoqi@0 | 1564 | case LPAREN: |
aoqi@0 | 1565 | if (lookahead != 0) { |
aoqi@0 | 1566 | // '(' in a non-starting position -> parens |
aoqi@0 | 1567 | return ParensResult.PARENS; |
aoqi@0 | 1568 | } else if (peekToken(lookahead, RPAREN)) { |
aoqi@0 | 1569 | // '(', ')' -> explicit lambda |
aoqi@0 | 1570 | return ParensResult.EXPLICIT_LAMBDA; |
aoqi@0 | 1571 | } |
aoqi@0 | 1572 | break; |
aoqi@0 | 1573 | case RPAREN: |
aoqi@0 | 1574 | // if we have seen something that looks like a type, |
aoqi@0 | 1575 | // then it's a cast expression |
aoqi@0 | 1576 | if (type) return ParensResult.CAST; |
aoqi@0 | 1577 | // otherwise, disambiguate cast vs. parenthesized expression |
aoqi@0 | 1578 | // based on subsequent token. |
aoqi@0 | 1579 | switch (S.token(lookahead + 1).kind) { |
aoqi@0 | 1580 | /*case PLUSPLUS: case SUBSUB: */ |
aoqi@0 | 1581 | case BANG: case TILDE: |
aoqi@0 | 1582 | case LPAREN: case THIS: case SUPER: |
aoqi@0 | 1583 | case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: |
aoqi@0 | 1584 | case DOUBLELITERAL: case CHARLITERAL: case STRINGLITERAL: |
aoqi@0 | 1585 | case TRUE: case FALSE: case NULL: |
aoqi@0 | 1586 | case NEW: case IDENTIFIER: case ASSERT: case ENUM: case UNDERSCORE: |
aoqi@0 | 1587 | case BYTE: case SHORT: case CHAR: case INT: |
aoqi@0 | 1588 | case LONG: case FLOAT: case DOUBLE: case BOOLEAN: case VOID: |
aoqi@0 | 1589 | return ParensResult.CAST; |
aoqi@0 | 1590 | default: |
aoqi@0 | 1591 | return ParensResult.PARENS; |
aoqi@0 | 1592 | } |
aoqi@0 | 1593 | case UNDERSCORE: |
aoqi@0 | 1594 | case ASSERT: |
aoqi@0 | 1595 | case ENUM: |
aoqi@0 | 1596 | case IDENTIFIER: |
aoqi@0 | 1597 | if (peekToken(lookahead, LAX_IDENTIFIER)) { |
aoqi@0 | 1598 | // Identifier, Identifier/'_'/'assert'/'enum' -> explicit lambda |
aoqi@0 | 1599 | return ParensResult.EXPLICIT_LAMBDA; |
aoqi@0 | 1600 | } else if (peekToken(lookahead, RPAREN, ARROW)) { |
aoqi@0 | 1601 | // Identifier, ')' '->' -> implicit lambda |
aoqi@0 | 1602 | return ParensResult.IMPLICIT_LAMBDA; |
aoqi@0 | 1603 | } |
aoqi@0 | 1604 | type = false; |
aoqi@0 | 1605 | break; |
aoqi@0 | 1606 | case FINAL: |
aoqi@0 | 1607 | case ELLIPSIS: |
aoqi@0 | 1608 | //those can only appear in explicit lambdas |
aoqi@0 | 1609 | return ParensResult.EXPLICIT_LAMBDA; |
aoqi@0 | 1610 | case MONKEYS_AT: |
aoqi@0 | 1611 | type = true; |
aoqi@0 | 1612 | lookahead += 1; //skip '@' |
aoqi@0 | 1613 | while (peekToken(lookahead, DOT)) { |
aoqi@0 | 1614 | lookahead += 2; |
aoqi@0 | 1615 | } |
aoqi@0 | 1616 | if (peekToken(lookahead, LPAREN)) { |
aoqi@0 | 1617 | lookahead++; |
aoqi@0 | 1618 | //skip annotation values |
aoqi@0 | 1619 | int nesting = 0; |
aoqi@0 | 1620 | for (; ; lookahead++) { |
aoqi@0 | 1621 | TokenKind tk2 = S.token(lookahead).kind; |
aoqi@0 | 1622 | switch (tk2) { |
aoqi@0 | 1623 | case EOF: |
aoqi@0 | 1624 | return ParensResult.PARENS; |
aoqi@0 | 1625 | case LPAREN: |
aoqi@0 | 1626 | nesting++; |
aoqi@0 | 1627 | break; |
aoqi@0 | 1628 | case RPAREN: |
aoqi@0 | 1629 | nesting--; |
aoqi@0 | 1630 | if (nesting == 0) { |
aoqi@0 | 1631 | continue outer; |
aoqi@0 | 1632 | } |
aoqi@0 | 1633 | break; |
aoqi@0 | 1634 | } |
aoqi@0 | 1635 | } |
aoqi@0 | 1636 | } |
aoqi@0 | 1637 | break; |
aoqi@0 | 1638 | case LBRACKET: |
aoqi@0 | 1639 | if (peekToken(lookahead, RBRACKET, LAX_IDENTIFIER)) { |
aoqi@0 | 1640 | // '[', ']', Identifier/'_'/'assert'/'enum' -> explicit lambda |
aoqi@0 | 1641 | return ParensResult.EXPLICIT_LAMBDA; |
aoqi@0 | 1642 | } else if (peekToken(lookahead, RBRACKET, RPAREN) || |
aoqi@0 | 1643 | peekToken(lookahead, RBRACKET, AMP)) { |
aoqi@0 | 1644 | // '[', ']', ')' -> cast |
aoqi@0 | 1645 | // '[', ']', '&' -> cast (intersection type) |
aoqi@0 | 1646 | return ParensResult.CAST; |
aoqi@0 | 1647 | } else if (peekToken(lookahead, RBRACKET)) { |
aoqi@0 | 1648 | //consume the ']' and skip |
aoqi@0 | 1649 | type = true; |
aoqi@0 | 1650 | lookahead++; |
aoqi@0 | 1651 | break; |
aoqi@0 | 1652 | } else { |
aoqi@0 | 1653 | return ParensResult.PARENS; |
aoqi@0 | 1654 | } |
aoqi@0 | 1655 | case LT: |
aoqi@0 | 1656 | depth++; break; |
aoqi@0 | 1657 | case GTGTGT: |
aoqi@0 | 1658 | depth--; |
aoqi@0 | 1659 | case GTGT: |
aoqi@0 | 1660 | depth--; |
aoqi@0 | 1661 | case GT: |
aoqi@0 | 1662 | depth--; |
aoqi@0 | 1663 | if (depth == 0) { |
aoqi@0 | 1664 | if (peekToken(lookahead, RPAREN) || |
aoqi@0 | 1665 | peekToken(lookahead, AMP)) { |
aoqi@0 | 1666 | // '>', ')' -> cast |
aoqi@0 | 1667 | // '>', '&' -> cast |
aoqi@0 | 1668 | return ParensResult.CAST; |
aoqi@0 | 1669 | } else if (peekToken(lookahead, LAX_IDENTIFIER, COMMA) || |
aoqi@0 | 1670 | peekToken(lookahead, LAX_IDENTIFIER, RPAREN, ARROW) || |
aoqi@0 | 1671 | peekToken(lookahead, ELLIPSIS)) { |
aoqi@0 | 1672 | // '>', Identifier/'_'/'assert'/'enum', ',' -> explicit lambda |
aoqi@0 | 1673 | // '>', Identifier/'_'/'assert'/'enum', ')', '->' -> explicit lambda |
aoqi@0 | 1674 | // '>', '...' -> explicit lambda |
aoqi@0 | 1675 | return ParensResult.EXPLICIT_LAMBDA; |
aoqi@0 | 1676 | } |
aoqi@0 | 1677 | //it looks a type, but could still be (i) a cast to generic type, |
aoqi@0 | 1678 | //(ii) an unbound method reference or (iii) an explicit lambda |
aoqi@0 | 1679 | type = true; |
aoqi@0 | 1680 | break; |
aoqi@0 | 1681 | } else if (depth < 0) { |
aoqi@0 | 1682 | //unbalanced '<', '>' - not a generic type |
aoqi@0 | 1683 | return ParensResult.PARENS; |
aoqi@0 | 1684 | } |
aoqi@0 | 1685 | break; |
aoqi@0 | 1686 | default: |
aoqi@0 | 1687 | //this includes EOF |
aoqi@0 | 1688 | return ParensResult.PARENS; |
aoqi@0 | 1689 | } |
aoqi@0 | 1690 | } |
aoqi@0 | 1691 | } |
aoqi@0 | 1692 | |
aoqi@0 | 1693 | /** Accepts all identifier-like tokens */ |
aoqi@0 | 1694 | Filter<TokenKind> LAX_IDENTIFIER = new Filter<TokenKind>() { |
aoqi@0 | 1695 | public boolean accepts(TokenKind t) { |
aoqi@0 | 1696 | return t == IDENTIFIER || t == UNDERSCORE || t == ASSERT || t == ENUM; |
aoqi@0 | 1697 | } |
aoqi@0 | 1698 | }; |
aoqi@0 | 1699 | |
aoqi@0 | 1700 | enum ParensResult { |
aoqi@0 | 1701 | CAST, |
aoqi@0 | 1702 | EXPLICIT_LAMBDA, |
aoqi@0 | 1703 | IMPLICIT_LAMBDA, |
aoqi@0 | 1704 | PARENS; |
aoqi@0 | 1705 | } |
aoqi@0 | 1706 | |
aoqi@0 | 1707 | JCExpression lambdaExpressionOrStatement(boolean hasParens, boolean explicitParams, int pos) { |
aoqi@0 | 1708 | List<JCVariableDecl> params = explicitParams ? |
aoqi@0 | 1709 | formalParameters(true) : |
aoqi@0 | 1710 | implicitParameters(hasParens); |
aoqi@0 | 1711 | |
aoqi@0 | 1712 | return lambdaExpressionOrStatementRest(params, pos); |
aoqi@0 | 1713 | } |
aoqi@0 | 1714 | |
aoqi@0 | 1715 | JCExpression lambdaExpressionOrStatementRest(List<JCVariableDecl> args, int pos) { |
aoqi@0 | 1716 | checkLambda(); |
aoqi@0 | 1717 | accept(ARROW); |
aoqi@0 | 1718 | |
aoqi@0 | 1719 | return token.kind == LBRACE ? |
aoqi@0 | 1720 | lambdaStatement(args, pos, pos) : |
aoqi@0 | 1721 | lambdaExpression(args, pos); |
aoqi@0 | 1722 | } |
aoqi@0 | 1723 | |
aoqi@0 | 1724 | JCExpression lambdaStatement(List<JCVariableDecl> args, int pos, int pos2) { |
aoqi@0 | 1725 | JCBlock block = block(pos2, 0); |
aoqi@0 | 1726 | return toP(F.at(pos).Lambda(args, block)); |
aoqi@0 | 1727 | } |
aoqi@0 | 1728 | |
aoqi@0 | 1729 | JCExpression lambdaExpression(List<JCVariableDecl> args, int pos) { |
aoqi@0 | 1730 | JCTree expr = parseExpression(); |
aoqi@0 | 1731 | return toP(F.at(pos).Lambda(args, expr)); |
aoqi@0 | 1732 | } |
aoqi@0 | 1733 | |
aoqi@0 | 1734 | /** SuperSuffix = Arguments | "." [TypeArguments] Ident [Arguments] |
aoqi@0 | 1735 | */ |
aoqi@0 | 1736 | JCExpression superSuffix(List<JCExpression> typeArgs, JCExpression t) { |
aoqi@0 | 1737 | nextToken(); |
aoqi@0 | 1738 | if (token.kind == LPAREN || typeArgs != null) { |
aoqi@0 | 1739 | t = arguments(typeArgs, t); |
aoqi@0 | 1740 | } else if (token.kind == COLCOL) { |
aoqi@0 | 1741 | if (typeArgs != null) return illegal(); |
aoqi@0 | 1742 | t = memberReferenceSuffix(t); |
aoqi@0 | 1743 | } else { |
aoqi@0 | 1744 | int pos = token.pos; |
aoqi@0 | 1745 | accept(DOT); |
aoqi@0 | 1746 | typeArgs = (token.kind == LT) ? typeArguments(false) : null; |
aoqi@0 | 1747 | t = toP(F.at(pos).Select(t, ident())); |
aoqi@0 | 1748 | t = argumentsOpt(typeArgs, t); |
aoqi@0 | 1749 | } |
aoqi@0 | 1750 | return t; |
aoqi@0 | 1751 | } |
aoqi@0 | 1752 | |
aoqi@0 | 1753 | /** BasicType = BYTE | SHORT | CHAR | INT | LONG | FLOAT | DOUBLE | BOOLEAN |
aoqi@0 | 1754 | */ |
aoqi@0 | 1755 | JCPrimitiveTypeTree basicType() { |
aoqi@0 | 1756 | JCPrimitiveTypeTree t = to(F.at(token.pos).TypeIdent(typetag(token.kind))); |
aoqi@0 | 1757 | nextToken(); |
aoqi@0 | 1758 | return t; |
aoqi@0 | 1759 | } |
aoqi@0 | 1760 | |
aoqi@0 | 1761 | /** ArgumentsOpt = [ Arguments ] |
aoqi@0 | 1762 | */ |
aoqi@0 | 1763 | JCExpression argumentsOpt(List<JCExpression> typeArgs, JCExpression t) { |
aoqi@0 | 1764 | if ((mode & EXPR) != 0 && token.kind == LPAREN || typeArgs != null) { |
aoqi@0 | 1765 | mode = EXPR; |
aoqi@0 | 1766 | return arguments(typeArgs, t); |
aoqi@0 | 1767 | } else { |
aoqi@0 | 1768 | return t; |
aoqi@0 | 1769 | } |
aoqi@0 | 1770 | } |
aoqi@0 | 1771 | |
aoqi@0 | 1772 | /** Arguments = "(" [Expression { COMMA Expression }] ")" |
aoqi@0 | 1773 | */ |
aoqi@0 | 1774 | List<JCExpression> arguments() { |
aoqi@0 | 1775 | ListBuffer<JCExpression> args = new ListBuffer<>(); |
aoqi@0 | 1776 | if (token.kind == LPAREN) { |
aoqi@0 | 1777 | nextToken(); |
aoqi@0 | 1778 | if (token.kind != RPAREN) { |
aoqi@0 | 1779 | args.append(parseExpression()); |
aoqi@0 | 1780 | while (token.kind == COMMA) { |
aoqi@0 | 1781 | nextToken(); |
aoqi@0 | 1782 | args.append(parseExpression()); |
aoqi@0 | 1783 | } |
aoqi@0 | 1784 | } |
aoqi@0 | 1785 | accept(RPAREN); |
aoqi@0 | 1786 | } else { |
aoqi@0 | 1787 | syntaxError(token.pos, "expected", LPAREN); |
aoqi@0 | 1788 | } |
aoqi@0 | 1789 | return args.toList(); |
aoqi@0 | 1790 | } |
aoqi@0 | 1791 | |
aoqi@0 | 1792 | JCMethodInvocation arguments(List<JCExpression> typeArgs, JCExpression t) { |
aoqi@0 | 1793 | int pos = token.pos; |
aoqi@0 | 1794 | List<JCExpression> args = arguments(); |
aoqi@0 | 1795 | return toP(F.at(pos).Apply(typeArgs, t, args)); |
aoqi@0 | 1796 | } |
aoqi@0 | 1797 | |
aoqi@0 | 1798 | /** TypeArgumentsOpt = [ TypeArguments ] |
aoqi@0 | 1799 | */ |
aoqi@0 | 1800 | JCExpression typeArgumentsOpt(JCExpression t) { |
aoqi@0 | 1801 | if (token.kind == LT && |
aoqi@0 | 1802 | (mode & TYPE) != 0 && |
aoqi@0 | 1803 | (mode & NOPARAMS) == 0) { |
aoqi@0 | 1804 | mode = TYPE; |
aoqi@0 | 1805 | checkGenerics(); |
aoqi@0 | 1806 | return typeArguments(t, false); |
aoqi@0 | 1807 | } else { |
aoqi@0 | 1808 | return t; |
aoqi@0 | 1809 | } |
aoqi@0 | 1810 | } |
aoqi@0 | 1811 | List<JCExpression> typeArgumentsOpt() { |
aoqi@0 | 1812 | return typeArgumentsOpt(TYPE); |
aoqi@0 | 1813 | } |
aoqi@0 | 1814 | |
aoqi@0 | 1815 | List<JCExpression> typeArgumentsOpt(int useMode) { |
aoqi@0 | 1816 | if (token.kind == LT) { |
aoqi@0 | 1817 | checkGenerics(); |
aoqi@0 | 1818 | if ((mode & useMode) == 0 || |
aoqi@0 | 1819 | (mode & NOPARAMS) != 0) { |
aoqi@0 | 1820 | illegal(); |
aoqi@0 | 1821 | } |
aoqi@0 | 1822 | mode = useMode; |
aoqi@0 | 1823 | return typeArguments(false); |
aoqi@0 | 1824 | } |
aoqi@0 | 1825 | return null; |
aoqi@0 | 1826 | } |
aoqi@0 | 1827 | |
aoqi@0 | 1828 | /** |
aoqi@0 | 1829 | * {@literal |
aoqi@0 | 1830 | * TypeArguments = "<" TypeArgument {"," TypeArgument} ">" |
aoqi@0 | 1831 | * } |
aoqi@0 | 1832 | */ |
aoqi@0 | 1833 | List<JCExpression> typeArguments(boolean diamondAllowed) { |
aoqi@0 | 1834 | if (token.kind == LT) { |
aoqi@0 | 1835 | nextToken(); |
aoqi@0 | 1836 | if (token.kind == GT && diamondAllowed) { |
aoqi@0 | 1837 | checkDiamond(); |
aoqi@0 | 1838 | mode |= DIAMOND; |
aoqi@0 | 1839 | nextToken(); |
aoqi@0 | 1840 | return List.nil(); |
aoqi@0 | 1841 | } else { |
aoqi@0 | 1842 | ListBuffer<JCExpression> args = new ListBuffer<>(); |
aoqi@0 | 1843 | args.append(((mode & EXPR) == 0) ? typeArgument() : parseType()); |
aoqi@0 | 1844 | while (token.kind == COMMA) { |
aoqi@0 | 1845 | nextToken(); |
aoqi@0 | 1846 | args.append(((mode & EXPR) == 0) ? typeArgument() : parseType()); |
aoqi@0 | 1847 | } |
aoqi@0 | 1848 | switch (token.kind) { |
aoqi@0 | 1849 | |
aoqi@0 | 1850 | case GTGTGTEQ: case GTGTEQ: case GTEQ: |
aoqi@0 | 1851 | case GTGTGT: case GTGT: |
aoqi@0 | 1852 | token = S.split(); |
aoqi@0 | 1853 | break; |
aoqi@0 | 1854 | case GT: |
aoqi@0 | 1855 | nextToken(); |
aoqi@0 | 1856 | break; |
aoqi@0 | 1857 | default: |
aoqi@0 | 1858 | args.append(syntaxError(token.pos, "expected", GT)); |
aoqi@0 | 1859 | break; |
aoqi@0 | 1860 | } |
aoqi@0 | 1861 | return args.toList(); |
aoqi@0 | 1862 | } |
aoqi@0 | 1863 | } else { |
aoqi@0 | 1864 | return List.<JCExpression>of(syntaxError(token.pos, "expected", LT)); |
aoqi@0 | 1865 | } |
aoqi@0 | 1866 | } |
aoqi@0 | 1867 | |
aoqi@0 | 1868 | /** |
aoqi@0 | 1869 | * {@literal |
aoqi@0 | 1870 | * TypeArgument = Type |
aoqi@0 | 1871 | * | [Annotations] "?" |
aoqi@0 | 1872 | * | [Annotations] "?" EXTENDS Type {"&" Type} |
aoqi@0 | 1873 | * | [Annotations] "?" SUPER Type |
aoqi@0 | 1874 | * } |
aoqi@0 | 1875 | */ |
aoqi@0 | 1876 | JCExpression typeArgument() { |
aoqi@0 | 1877 | List<JCAnnotation> annotations = typeAnnotationsOpt(); |
aoqi@0 | 1878 | if (token.kind != QUES) return parseType(annotations); |
aoqi@0 | 1879 | int pos = token.pos; |
aoqi@0 | 1880 | nextToken(); |
aoqi@0 | 1881 | JCExpression result; |
aoqi@0 | 1882 | if (token.kind == EXTENDS) { |
aoqi@0 | 1883 | TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.EXTENDS)); |
aoqi@0 | 1884 | nextToken(); |
aoqi@0 | 1885 | JCExpression bound = parseType(); |
aoqi@0 | 1886 | result = F.at(pos).Wildcard(t, bound); |
aoqi@0 | 1887 | } else if (token.kind == SUPER) { |
aoqi@0 | 1888 | TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.SUPER)); |
aoqi@0 | 1889 | nextToken(); |
aoqi@0 | 1890 | JCExpression bound = parseType(); |
aoqi@0 | 1891 | result = F.at(pos).Wildcard(t, bound); |
aoqi@0 | 1892 | } else if (LAX_IDENTIFIER.accepts(token.kind)) { |
aoqi@0 | 1893 | //error recovery |
aoqi@0 | 1894 | TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND); |
aoqi@0 | 1895 | JCExpression wc = toP(F.at(pos).Wildcard(t, null)); |
aoqi@0 | 1896 | JCIdent id = toP(F.at(token.pos).Ident(ident())); |
aoqi@0 | 1897 | JCErroneous err = F.at(pos).Erroneous(List.<JCTree>of(wc, id)); |
aoqi@0 | 1898 | reportSyntaxError(err, "expected3", GT, EXTENDS, SUPER); |
aoqi@0 | 1899 | result = err; |
aoqi@0 | 1900 | } else { |
aoqi@0 | 1901 | TypeBoundKind t = toP(F.at(pos).TypeBoundKind(BoundKind.UNBOUND)); |
aoqi@0 | 1902 | result = toP(F.at(pos).Wildcard(t, null)); |
aoqi@0 | 1903 | } |
aoqi@0 | 1904 | if (!annotations.isEmpty()) { |
aoqi@0 | 1905 | result = toP(F.at(annotations.head.pos).AnnotatedType(annotations,result)); |
aoqi@0 | 1906 | } |
aoqi@0 | 1907 | return result; |
aoqi@0 | 1908 | } |
aoqi@0 | 1909 | |
aoqi@0 | 1910 | JCTypeApply typeArguments(JCExpression t, boolean diamondAllowed) { |
aoqi@0 | 1911 | int pos = token.pos; |
aoqi@0 | 1912 | List<JCExpression> args = typeArguments(diamondAllowed); |
aoqi@0 | 1913 | return toP(F.at(pos).TypeApply(t, args)); |
aoqi@0 | 1914 | } |
aoqi@0 | 1915 | |
aoqi@0 | 1916 | /** |
aoqi@0 | 1917 | * BracketsOpt = { [Annotations] "[" "]" }* |
aoqi@0 | 1918 | * |
aoqi@0 | 1919 | * <p> |
aoqi@0 | 1920 | * |
aoqi@0 | 1921 | * <code>annotations</code> is the list of annotations targeting |
aoqi@0 | 1922 | * the expression <code>t</code>. |
aoqi@0 | 1923 | */ |
aoqi@0 | 1924 | private JCExpression bracketsOpt(JCExpression t, |
aoqi@0 | 1925 | List<JCAnnotation> annotations) { |
aoqi@0 | 1926 | List<JCAnnotation> nextLevelAnnotations = typeAnnotationsOpt(); |
aoqi@0 | 1927 | |
aoqi@0 | 1928 | if (token.kind == LBRACKET) { |
aoqi@0 | 1929 | int pos = token.pos; |
aoqi@0 | 1930 | nextToken(); |
aoqi@0 | 1931 | t = bracketsOptCont(t, pos, nextLevelAnnotations); |
aoqi@0 | 1932 | } else if (!nextLevelAnnotations.isEmpty()) { |
aoqi@0 | 1933 | if (permitTypeAnnotationsPushBack) { |
aoqi@0 | 1934 | this.typeAnnotationsPushedBack = nextLevelAnnotations; |
aoqi@0 | 1935 | } else { |
aoqi@0 | 1936 | return illegal(nextLevelAnnotations.head.pos); |
aoqi@0 | 1937 | } |
aoqi@0 | 1938 | } |
aoqi@0 | 1939 | |
aoqi@0 | 1940 | if (!annotations.isEmpty()) { |
aoqi@0 | 1941 | t = toP(F.at(token.pos).AnnotatedType(annotations, t)); |
aoqi@0 | 1942 | } |
aoqi@0 | 1943 | return t; |
aoqi@0 | 1944 | } |
aoqi@0 | 1945 | |
aoqi@0 | 1946 | /** BracketsOpt = [ "[" "]" { [Annotations] "[" "]"} ] |
aoqi@0 | 1947 | */ |
aoqi@0 | 1948 | private JCExpression bracketsOpt(JCExpression t) { |
aoqi@0 | 1949 | return bracketsOpt(t, List.<JCAnnotation>nil()); |
aoqi@0 | 1950 | } |
aoqi@0 | 1951 | |
aoqi@0 | 1952 | private JCExpression bracketsOptCont(JCExpression t, int pos, |
aoqi@0 | 1953 | List<JCAnnotation> annotations) { |
aoqi@0 | 1954 | accept(RBRACKET); |
aoqi@0 | 1955 | t = bracketsOpt(t); |
aoqi@0 | 1956 | t = toP(F.at(pos).TypeArray(t)); |
aoqi@0 | 1957 | if (annotations.nonEmpty()) { |
aoqi@0 | 1958 | t = toP(F.at(pos).AnnotatedType(annotations, t)); |
aoqi@0 | 1959 | } |
aoqi@0 | 1960 | return t; |
aoqi@0 | 1961 | } |
aoqi@0 | 1962 | |
aoqi@0 | 1963 | /** BracketsSuffixExpr = "." CLASS |
aoqi@0 | 1964 | * BracketsSuffixType = |
aoqi@0 | 1965 | */ |
aoqi@0 | 1966 | JCExpression bracketsSuffix(JCExpression t) { |
aoqi@0 | 1967 | if ((mode & EXPR) != 0 && token.kind == DOT) { |
aoqi@0 | 1968 | mode = EXPR; |
aoqi@0 | 1969 | int pos = token.pos; |
aoqi@0 | 1970 | nextToken(); |
aoqi@0 | 1971 | accept(CLASS); |
aoqi@0 | 1972 | if (token.pos == endPosTable.errorEndPos) { |
aoqi@0 | 1973 | // error recovery |
aoqi@0 | 1974 | Name name; |
aoqi@0 | 1975 | if (LAX_IDENTIFIER.accepts(token.kind)) { |
aoqi@0 | 1976 | name = token.name(); |
aoqi@0 | 1977 | nextToken(); |
aoqi@0 | 1978 | } else { |
aoqi@0 | 1979 | name = names.error; |
aoqi@0 | 1980 | } |
aoqi@0 | 1981 | t = F.at(pos).Erroneous(List.<JCTree>of(toP(F.at(pos).Select(t, name)))); |
aoqi@0 | 1982 | } else { |
aoqi@0 | 1983 | t = toP(F.at(pos).Select(t, names._class)); |
aoqi@0 | 1984 | } |
aoqi@0 | 1985 | } else if ((mode & TYPE) != 0) { |
aoqi@0 | 1986 | if (token.kind != COLCOL) { |
aoqi@0 | 1987 | mode = TYPE; |
aoqi@0 | 1988 | } |
aoqi@0 | 1989 | } else if (token.kind != COLCOL) { |
aoqi@0 | 1990 | syntaxError(token.pos, "dot.class.expected"); |
aoqi@0 | 1991 | } |
aoqi@0 | 1992 | return t; |
aoqi@0 | 1993 | } |
aoqi@0 | 1994 | |
aoqi@0 | 1995 | /** |
aoqi@0 | 1996 | * MemberReferenceSuffix = "::" [TypeArguments] Ident |
aoqi@0 | 1997 | * | "::" [TypeArguments] "new" |
aoqi@0 | 1998 | */ |
aoqi@0 | 1999 | JCExpression memberReferenceSuffix(JCExpression t) { |
aoqi@0 | 2000 | int pos1 = token.pos; |
aoqi@0 | 2001 | accept(COLCOL); |
aoqi@0 | 2002 | return memberReferenceSuffix(pos1, t); |
aoqi@0 | 2003 | } |
aoqi@0 | 2004 | |
aoqi@0 | 2005 | JCExpression memberReferenceSuffix(int pos1, JCExpression t) { |
aoqi@0 | 2006 | checkMethodReferences(); |
aoqi@0 | 2007 | mode = EXPR; |
aoqi@0 | 2008 | List<JCExpression> typeArgs = null; |
aoqi@0 | 2009 | if (token.kind == LT) { |
aoqi@0 | 2010 | typeArgs = typeArguments(false); |
aoqi@0 | 2011 | } |
aoqi@0 | 2012 | Name refName; |
aoqi@0 | 2013 | ReferenceMode refMode; |
aoqi@0 | 2014 | if (token.kind == NEW) { |
aoqi@0 | 2015 | refMode = ReferenceMode.NEW; |
aoqi@0 | 2016 | refName = names.init; |
aoqi@0 | 2017 | nextToken(); |
aoqi@0 | 2018 | } else { |
aoqi@0 | 2019 | refMode = ReferenceMode.INVOKE; |
aoqi@0 | 2020 | refName = ident(); |
aoqi@0 | 2021 | } |
aoqi@0 | 2022 | return toP(F.at(t.getStartPosition()).Reference(refMode, refName, t, typeArgs)); |
aoqi@0 | 2023 | } |
aoqi@0 | 2024 | |
aoqi@0 | 2025 | /** Creator = [Annotations] Qualident [TypeArguments] ( ArrayCreatorRest | ClassCreatorRest ) |
aoqi@0 | 2026 | */ |
aoqi@0 | 2027 | JCExpression creator(int newpos, List<JCExpression> typeArgs) { |
aoqi@0 | 2028 | List<JCAnnotation> newAnnotations = typeAnnotationsOpt(); |
aoqi@0 | 2029 | |
aoqi@0 | 2030 | switch (token.kind) { |
aoqi@0 | 2031 | case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT: |
aoqi@0 | 2032 | case DOUBLE: case BOOLEAN: |
aoqi@0 | 2033 | if (typeArgs == null) { |
aoqi@0 | 2034 | if (newAnnotations.isEmpty()) { |
aoqi@0 | 2035 | return arrayCreatorRest(newpos, basicType()); |
aoqi@0 | 2036 | } else { |
aoqi@0 | 2037 | return arrayCreatorRest(newpos, toP(F.at(newAnnotations.head.pos).AnnotatedType(newAnnotations, basicType()))); |
aoqi@0 | 2038 | } |
aoqi@0 | 2039 | } |
aoqi@0 | 2040 | break; |
aoqi@0 | 2041 | default: |
aoqi@0 | 2042 | } |
aoqi@0 | 2043 | JCExpression t = qualident(true); |
aoqi@0 | 2044 | |
aoqi@0 | 2045 | int oldmode = mode; |
aoqi@0 | 2046 | mode = TYPE; |
aoqi@0 | 2047 | boolean diamondFound = false; |
aoqi@0 | 2048 | int lastTypeargsPos = -1; |
aoqi@0 | 2049 | if (token.kind == LT) { |
aoqi@0 | 2050 | checkGenerics(); |
aoqi@0 | 2051 | lastTypeargsPos = token.pos; |
aoqi@0 | 2052 | t = typeArguments(t, true); |
aoqi@0 | 2053 | diamondFound = (mode & DIAMOND) != 0; |
aoqi@0 | 2054 | } |
aoqi@0 | 2055 | while (token.kind == DOT) { |
aoqi@0 | 2056 | if (diamondFound) { |
aoqi@0 | 2057 | //cannot select after a diamond |
aoqi@0 | 2058 | illegal(); |
aoqi@0 | 2059 | } |
aoqi@0 | 2060 | int pos = token.pos; |
aoqi@0 | 2061 | nextToken(); |
aoqi@0 | 2062 | List<JCAnnotation> tyannos = typeAnnotationsOpt(); |
aoqi@0 | 2063 | t = toP(F.at(pos).Select(t, ident())); |
aoqi@0 | 2064 | |
aoqi@0 | 2065 | if (tyannos != null && tyannos.nonEmpty()) { |
aoqi@0 | 2066 | t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t)); |
aoqi@0 | 2067 | } |
aoqi@0 | 2068 | |
aoqi@0 | 2069 | if (token.kind == LT) { |
aoqi@0 | 2070 | lastTypeargsPos = token.pos; |
aoqi@0 | 2071 | checkGenerics(); |
aoqi@0 | 2072 | t = typeArguments(t, true); |
aoqi@0 | 2073 | diamondFound = (mode & DIAMOND) != 0; |
aoqi@0 | 2074 | } |
aoqi@0 | 2075 | } |
aoqi@0 | 2076 | mode = oldmode; |
aoqi@0 | 2077 | if (token.kind == LBRACKET || token.kind == MONKEYS_AT) { |
aoqi@0 | 2078 | // handle type annotations for non primitive arrays |
aoqi@0 | 2079 | if (newAnnotations.nonEmpty()) { |
aoqi@0 | 2080 | t = insertAnnotationsToMostInner(t, newAnnotations, false); |
aoqi@0 | 2081 | } |
aoqi@0 | 2082 | |
aoqi@0 | 2083 | JCExpression e = arrayCreatorRest(newpos, t); |
aoqi@0 | 2084 | if (diamondFound) { |
aoqi@0 | 2085 | reportSyntaxError(lastTypeargsPos, "cannot.create.array.with.diamond"); |
aoqi@0 | 2086 | return toP(F.at(newpos).Erroneous(List.of(e))); |
aoqi@0 | 2087 | } |
aoqi@0 | 2088 | else if (typeArgs != null) { |
aoqi@0 | 2089 | int pos = newpos; |
aoqi@0 | 2090 | if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) { |
aoqi@0 | 2091 | // note: this should always happen but we should |
aoqi@0 | 2092 | // not rely on this as the parser is continuously |
aoqi@0 | 2093 | // modified to improve error recovery. |
aoqi@0 | 2094 | pos = typeArgs.head.pos; |
aoqi@0 | 2095 | } |
aoqi@0 | 2096 | setErrorEndPos(S.prevToken().endPos); |
aoqi@0 | 2097 | JCErroneous err = F.at(pos).Erroneous(typeArgs.prepend(e)); |
aoqi@0 | 2098 | reportSyntaxError(err, "cannot.create.array.with.type.arguments"); |
aoqi@0 | 2099 | return toP(err); |
aoqi@0 | 2100 | } |
aoqi@0 | 2101 | return e; |
aoqi@0 | 2102 | } else if (token.kind == LPAREN) { |
aoqi@0 | 2103 | JCNewClass newClass = classCreatorRest(newpos, null, typeArgs, t); |
aoqi@0 | 2104 | if (newClass.def != null) { |
aoqi@0 | 2105 | assert newClass.def.mods.annotations.isEmpty(); |
aoqi@0 | 2106 | if (newAnnotations.nonEmpty()) { |
aoqi@0 | 2107 | // Add type and declaration annotations to the new class; |
aoqi@0 | 2108 | // com.sun.tools.javac.code.TypeAnnotations.TypeAnnotationPositions.visitNewClass(JCNewClass) |
aoqi@0 | 2109 | // will later remove all type annotations and only leave the |
aoqi@0 | 2110 | // declaration annotations. |
aoqi@0 | 2111 | newClass.def.mods.pos = earlier(newClass.def.mods.pos, newAnnotations.head.pos); |
aoqi@0 | 2112 | newClass.def.mods.annotations = newAnnotations; |
aoqi@0 | 2113 | } |
aoqi@0 | 2114 | } else { |
aoqi@0 | 2115 | // handle type annotations for instantiations |
aoqi@0 | 2116 | if (newAnnotations.nonEmpty()) { |
aoqi@0 | 2117 | t = insertAnnotationsToMostInner(t, newAnnotations, false); |
aoqi@0 | 2118 | newClass.clazz = t; |
aoqi@0 | 2119 | } |
aoqi@0 | 2120 | } |
aoqi@0 | 2121 | return newClass; |
aoqi@0 | 2122 | } else { |
aoqi@0 | 2123 | setErrorEndPos(token.pos); |
aoqi@0 | 2124 | reportSyntaxError(token.pos, "expected2", LPAREN, LBRACKET); |
aoqi@0 | 2125 | t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.<JCExpression>nil(), null)); |
aoqi@0 | 2126 | return toP(F.at(newpos).Erroneous(List.<JCTree>of(t))); |
aoqi@0 | 2127 | } |
aoqi@0 | 2128 | } |
aoqi@0 | 2129 | |
aoqi@0 | 2130 | /** InnerCreator = [Annotations] Ident [TypeArguments] ClassCreatorRest |
aoqi@0 | 2131 | */ |
aoqi@0 | 2132 | JCExpression innerCreator(int newpos, List<JCExpression> typeArgs, JCExpression encl) { |
aoqi@0 | 2133 | List<JCAnnotation> newAnnotations = typeAnnotationsOpt(); |
aoqi@0 | 2134 | |
aoqi@0 | 2135 | JCExpression t = toP(F.at(token.pos).Ident(ident())); |
aoqi@0 | 2136 | |
aoqi@0 | 2137 | if (newAnnotations.nonEmpty()) { |
aoqi@0 | 2138 | t = toP(F.at(newAnnotations.head.pos).AnnotatedType(newAnnotations, t)); |
aoqi@0 | 2139 | } |
aoqi@0 | 2140 | |
aoqi@0 | 2141 | if (token.kind == LT) { |
aoqi@0 | 2142 | int oldmode = mode; |
aoqi@0 | 2143 | checkGenerics(); |
aoqi@0 | 2144 | t = typeArguments(t, true); |
aoqi@0 | 2145 | mode = oldmode; |
aoqi@0 | 2146 | } |
aoqi@0 | 2147 | return classCreatorRest(newpos, encl, typeArgs, t); |
aoqi@0 | 2148 | } |
aoqi@0 | 2149 | |
aoqi@0 | 2150 | /** ArrayCreatorRest = [Annotations] "[" ( "]" BracketsOpt ArrayInitializer |
aoqi@0 | 2151 | * | Expression "]" {[Annotations] "[" Expression "]"} BracketsOpt ) |
aoqi@0 | 2152 | */ |
aoqi@0 | 2153 | JCExpression arrayCreatorRest(int newpos, JCExpression elemtype) { |
aoqi@0 | 2154 | List<JCAnnotation> annos = typeAnnotationsOpt(); |
aoqi@0 | 2155 | |
aoqi@0 | 2156 | accept(LBRACKET); |
aoqi@0 | 2157 | if (token.kind == RBRACKET) { |
aoqi@0 | 2158 | accept(RBRACKET); |
aoqi@0 | 2159 | elemtype = bracketsOpt(elemtype, annos); |
aoqi@0 | 2160 | if (token.kind == LBRACE) { |
aoqi@0 | 2161 | JCNewArray na = (JCNewArray)arrayInitializer(newpos, elemtype); |
aoqi@0 | 2162 | if (annos.nonEmpty()) { |
aoqi@0 | 2163 | // when an array initializer is present then |
aoqi@0 | 2164 | // the parsed annotations should target the |
aoqi@0 | 2165 | // new array tree |
aoqi@0 | 2166 | // bracketsOpt inserts the annotation in |
aoqi@0 | 2167 | // elemtype, and it needs to be corrected |
aoqi@0 | 2168 | // |
aoqi@0 | 2169 | JCAnnotatedType annotated = (JCAnnotatedType)elemtype; |
aoqi@0 | 2170 | assert annotated.annotations == annos; |
aoqi@0 | 2171 | na.annotations = annotated.annotations; |
aoqi@0 | 2172 | na.elemtype = annotated.underlyingType; |
aoqi@0 | 2173 | } |
aoqi@0 | 2174 | return na; |
aoqi@0 | 2175 | } else { |
aoqi@0 | 2176 | JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.<JCExpression>nil(), null)); |
aoqi@0 | 2177 | return syntaxError(token.pos, List.<JCTree>of(t), "array.dimension.missing"); |
aoqi@0 | 2178 | } |
aoqi@0 | 2179 | } else { |
aoqi@0 | 2180 | ListBuffer<JCExpression> dims = new ListBuffer<JCExpression>(); |
aoqi@0 | 2181 | |
aoqi@0 | 2182 | // maintain array dimension type annotations |
aoqi@0 | 2183 | ListBuffer<List<JCAnnotation>> dimAnnotations = new ListBuffer<>(); |
aoqi@0 | 2184 | dimAnnotations.append(annos); |
aoqi@0 | 2185 | |
aoqi@0 | 2186 | dims.append(parseExpression()); |
aoqi@0 | 2187 | accept(RBRACKET); |
aoqi@0 | 2188 | while (token.kind == LBRACKET |
aoqi@0 | 2189 | || token.kind == MONKEYS_AT) { |
aoqi@0 | 2190 | List<JCAnnotation> maybeDimAnnos = typeAnnotationsOpt(); |
aoqi@0 | 2191 | int pos = token.pos; |
aoqi@0 | 2192 | nextToken(); |
aoqi@0 | 2193 | if (token.kind == RBRACKET) { |
aoqi@0 | 2194 | elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos); |
aoqi@0 | 2195 | } else { |
aoqi@0 | 2196 | if (token.kind == RBRACKET) { // no dimension |
aoqi@0 | 2197 | elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos); |
aoqi@0 | 2198 | } else { |
aoqi@0 | 2199 | dimAnnotations.append(maybeDimAnnos); |
aoqi@0 | 2200 | dims.append(parseExpression()); |
aoqi@0 | 2201 | accept(RBRACKET); |
aoqi@0 | 2202 | } |
aoqi@0 | 2203 | } |
aoqi@0 | 2204 | } |
aoqi@0 | 2205 | |
aoqi@0 | 2206 | JCNewArray na = toP(F.at(newpos).NewArray(elemtype, dims.toList(), null)); |
aoqi@0 | 2207 | na.dimAnnotations = dimAnnotations.toList(); |
aoqi@0 | 2208 | return na; |
aoqi@0 | 2209 | } |
aoqi@0 | 2210 | } |
aoqi@0 | 2211 | |
aoqi@0 | 2212 | /** ClassCreatorRest = Arguments [ClassBody] |
aoqi@0 | 2213 | */ |
aoqi@0 | 2214 | JCNewClass classCreatorRest(int newpos, |
aoqi@0 | 2215 | JCExpression encl, |
aoqi@0 | 2216 | List<JCExpression> typeArgs, |
aoqi@0 | 2217 | JCExpression t) |
aoqi@0 | 2218 | { |
aoqi@0 | 2219 | List<JCExpression> args = arguments(); |
aoqi@0 | 2220 | JCClassDecl body = null; |
aoqi@0 | 2221 | if (token.kind == LBRACE) { |
aoqi@0 | 2222 | int pos = token.pos; |
aoqi@0 | 2223 | List<JCTree> defs = classOrInterfaceBody(names.empty, false); |
aoqi@0 | 2224 | JCModifiers mods = F.at(Position.NOPOS).Modifiers(0); |
aoqi@0 | 2225 | body = toP(F.at(pos).AnonymousClassDef(mods, defs)); |
aoqi@0 | 2226 | } |
aoqi@0 | 2227 | return toP(F.at(newpos).NewClass(encl, typeArgs, t, args, body)); |
aoqi@0 | 2228 | } |
aoqi@0 | 2229 | |
aoqi@0 | 2230 | /** ArrayInitializer = "{" [VariableInitializer {"," VariableInitializer}] [","] "}" |
aoqi@0 | 2231 | */ |
aoqi@0 | 2232 | JCExpression arrayInitializer(int newpos, JCExpression t) { |
aoqi@0 | 2233 | accept(LBRACE); |
aoqi@0 | 2234 | ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>(); |
aoqi@0 | 2235 | if (token.kind == COMMA) { |
aoqi@0 | 2236 | nextToken(); |
aoqi@0 | 2237 | } else if (token.kind != RBRACE) { |
aoqi@0 | 2238 | elems.append(variableInitializer()); |
aoqi@0 | 2239 | while (token.kind == COMMA) { |
aoqi@0 | 2240 | nextToken(); |
aoqi@0 | 2241 | if (token.kind == RBRACE) break; |
aoqi@0 | 2242 | elems.append(variableInitializer()); |
aoqi@0 | 2243 | } |
aoqi@0 | 2244 | } |
aoqi@0 | 2245 | accept(RBRACE); |
aoqi@0 | 2246 | return toP(F.at(newpos).NewArray(t, List.<JCExpression>nil(), elems.toList())); |
aoqi@0 | 2247 | } |
aoqi@0 | 2248 | |
aoqi@0 | 2249 | /** VariableInitializer = ArrayInitializer | Expression |
aoqi@0 | 2250 | */ |
aoqi@0 | 2251 | public JCExpression variableInitializer() { |
aoqi@0 | 2252 | return token.kind == LBRACE ? arrayInitializer(token.pos, null) : parseExpression(); |
aoqi@0 | 2253 | } |
aoqi@0 | 2254 | |
aoqi@0 | 2255 | /** ParExpression = "(" Expression ")" |
aoqi@0 | 2256 | */ |
aoqi@0 | 2257 | JCExpression parExpression() { |
aoqi@0 | 2258 | int pos = token.pos; |
aoqi@0 | 2259 | accept(LPAREN); |
aoqi@0 | 2260 | JCExpression t = parseExpression(); |
aoqi@0 | 2261 | accept(RPAREN); |
aoqi@0 | 2262 | return toP(F.at(pos).Parens(t)); |
aoqi@0 | 2263 | } |
aoqi@0 | 2264 | |
aoqi@0 | 2265 | /** Block = "{" BlockStatements "}" |
aoqi@0 | 2266 | */ |
aoqi@0 | 2267 | JCBlock block(int pos, long flags) { |
aoqi@0 | 2268 | accept(LBRACE); |
aoqi@0 | 2269 | List<JCStatement> stats = blockStatements(); |
aoqi@0 | 2270 | JCBlock t = F.at(pos).Block(flags, stats); |
aoqi@0 | 2271 | while (token.kind == CASE || token.kind == DEFAULT) { |
aoqi@0 | 2272 | syntaxError("orphaned", token.kind); |
aoqi@0 | 2273 | switchBlockStatementGroups(); |
aoqi@0 | 2274 | } |
aoqi@0 | 2275 | // the Block node has a field "endpos" for first char of last token, which is |
aoqi@0 | 2276 | // usually but not necessarily the last char of the last token. |
aoqi@0 | 2277 | t.endpos = token.pos; |
aoqi@0 | 2278 | accept(RBRACE); |
aoqi@0 | 2279 | return toP(t); |
aoqi@0 | 2280 | } |
aoqi@0 | 2281 | |
aoqi@0 | 2282 | public JCBlock block() { |
aoqi@0 | 2283 | return block(token.pos, 0); |
aoqi@0 | 2284 | } |
aoqi@0 | 2285 | |
aoqi@0 | 2286 | /** BlockStatements = { BlockStatement } |
aoqi@0 | 2287 | * BlockStatement = LocalVariableDeclarationStatement |
aoqi@0 | 2288 | * | ClassOrInterfaceOrEnumDeclaration |
aoqi@0 | 2289 | * | [Ident ":"] Statement |
aoqi@0 | 2290 | * LocalVariableDeclarationStatement |
aoqi@0 | 2291 | * = { FINAL | '@' Annotation } Type VariableDeclarators ";" |
aoqi@0 | 2292 | */ |
aoqi@0 | 2293 | @SuppressWarnings("fallthrough") |
aoqi@0 | 2294 | List<JCStatement> blockStatements() { |
aoqi@0 | 2295 | //todo: skip to anchor on error(?) |
aoqi@0 | 2296 | ListBuffer<JCStatement> stats = new ListBuffer<JCStatement>(); |
aoqi@0 | 2297 | while (true) { |
aoqi@0 | 2298 | List<JCStatement> stat = blockStatement(); |
aoqi@0 | 2299 | if (stat.isEmpty()) { |
aoqi@0 | 2300 | return stats.toList(); |
aoqi@0 | 2301 | } else { |
aoqi@0 | 2302 | if (token.pos <= endPosTable.errorEndPos) { |
aoqi@0 | 2303 | skip(false, true, true, true); |
aoqi@0 | 2304 | } |
aoqi@0 | 2305 | stats.addAll(stat); |
aoqi@0 | 2306 | } |
aoqi@0 | 2307 | } |
aoqi@0 | 2308 | } |
aoqi@0 | 2309 | |
aoqi@0 | 2310 | /* |
aoqi@0 | 2311 | * This method parses a statement treating it as a block, relaxing the |
aoqi@0 | 2312 | * JLS restrictions, allows us to parse more faulty code, doing so |
aoqi@0 | 2313 | * enables us to provide better and accurate diagnostics to the user. |
aoqi@0 | 2314 | */ |
aoqi@0 | 2315 | JCStatement parseStatementAsBlock() { |
aoqi@0 | 2316 | int pos = token.pos; |
aoqi@0 | 2317 | List<JCStatement> stats = blockStatement(); |
aoqi@0 | 2318 | if (stats.isEmpty()) { |
aoqi@0 | 2319 | JCErroneous e = F.at(pos).Erroneous(); |
aoqi@0 | 2320 | error(e, "illegal.start.of.stmt"); |
aoqi@0 | 2321 | return F.at(pos).Exec(e); |
aoqi@0 | 2322 | } else { |
aoqi@0 | 2323 | JCStatement first = stats.head; |
aoqi@0 | 2324 | String error = null; |
aoqi@0 | 2325 | switch (first.getTag()) { |
aoqi@0 | 2326 | case CLASSDEF: |
aoqi@0 | 2327 | error = "class.not.allowed"; |
aoqi@0 | 2328 | break; |
aoqi@0 | 2329 | case VARDEF: |
aoqi@0 | 2330 | error = "variable.not.allowed"; |
aoqi@0 | 2331 | break; |
aoqi@0 | 2332 | } |
aoqi@0 | 2333 | if (error != null) { |
aoqi@0 | 2334 | error(first, error); |
aoqi@0 | 2335 | List<JCBlock> blist = List.of(F.at(first.pos).Block(0, stats)); |
aoqi@0 | 2336 | return toP(F.at(pos).Exec(F.at(first.pos).Erroneous(blist))); |
aoqi@0 | 2337 | } |
aoqi@0 | 2338 | return first; |
aoqi@0 | 2339 | } |
aoqi@0 | 2340 | } |
aoqi@0 | 2341 | |
aoqi@0 | 2342 | @SuppressWarnings("fallthrough") |
aoqi@0 | 2343 | List<JCStatement> blockStatement() { |
aoqi@0 | 2344 | //todo: skip to anchor on error(?) |
aoqi@0 | 2345 | int pos = token.pos; |
aoqi@0 | 2346 | switch (token.kind) { |
aoqi@0 | 2347 | case RBRACE: case CASE: case DEFAULT: case EOF: |
aoqi@0 | 2348 | return List.nil(); |
aoqi@0 | 2349 | case LBRACE: case IF: case FOR: case WHILE: case DO: case TRY: |
aoqi@0 | 2350 | case SWITCH: case SYNCHRONIZED: case RETURN: case THROW: case BREAK: |
aoqi@0 | 2351 | case CONTINUE: case SEMI: case ELSE: case FINALLY: case CATCH: |
aoqi@0 | 2352 | return List.of(parseStatement()); |
aoqi@0 | 2353 | case MONKEYS_AT: |
aoqi@0 | 2354 | case FINAL: { |
aoqi@0 | 2355 | Comment dc = token.comment(CommentStyle.JAVADOC); |
aoqi@0 | 2356 | JCModifiers mods = modifiersOpt(); |
aoqi@0 | 2357 | if (token.kind == INTERFACE || |
aoqi@0 | 2358 | token.kind == CLASS || |
aoqi@0 | 2359 | allowEnums && token.kind == ENUM) { |
aoqi@0 | 2360 | return List.of(classOrInterfaceOrEnumDeclaration(mods, dc)); |
aoqi@0 | 2361 | } else { |
aoqi@0 | 2362 | JCExpression t = parseType(); |
aoqi@0 | 2363 | ListBuffer<JCStatement> stats = |
aoqi@0 | 2364 | variableDeclarators(mods, t, new ListBuffer<JCStatement>()); |
aoqi@0 | 2365 | // A "LocalVariableDeclarationStatement" subsumes the terminating semicolon |
aoqi@0 | 2366 | storeEnd(stats.last(), token.endPos); |
aoqi@0 | 2367 | accept(SEMI); |
aoqi@0 | 2368 | return stats.toList(); |
aoqi@0 | 2369 | } |
aoqi@0 | 2370 | } |
aoqi@0 | 2371 | case ABSTRACT: case STRICTFP: { |
aoqi@0 | 2372 | Comment dc = token.comment(CommentStyle.JAVADOC); |
aoqi@0 | 2373 | JCModifiers mods = modifiersOpt(); |
aoqi@0 | 2374 | return List.of(classOrInterfaceOrEnumDeclaration(mods, dc)); |
aoqi@0 | 2375 | } |
aoqi@0 | 2376 | case INTERFACE: |
aoqi@0 | 2377 | case CLASS: |
aoqi@0 | 2378 | Comment dc = token.comment(CommentStyle.JAVADOC); |
aoqi@0 | 2379 | return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc)); |
aoqi@0 | 2380 | case ENUM: |
aoqi@0 | 2381 | case ASSERT: |
aoqi@0 | 2382 | if (allowEnums && token.kind == ENUM) { |
aoqi@0 | 2383 | error(token.pos, "local.enum"); |
aoqi@0 | 2384 | dc = token.comment(CommentStyle.JAVADOC); |
aoqi@0 | 2385 | return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc)); |
aoqi@0 | 2386 | } else if (allowAsserts && token.kind == ASSERT) { |
aoqi@0 | 2387 | return List.of(parseStatement()); |
aoqi@0 | 2388 | } |
aoqi@0 | 2389 | /* fall through to default */ |
aoqi@0 | 2390 | default: |
aoqi@0 | 2391 | Token prevToken = token; |
aoqi@0 | 2392 | JCExpression t = term(EXPR | TYPE); |
aoqi@0 | 2393 | if (token.kind == COLON && t.hasTag(IDENT)) { |
aoqi@0 | 2394 | nextToken(); |
aoqi@0 | 2395 | JCStatement stat = parseStatement(); |
aoqi@0 | 2396 | return List.<JCStatement>of(F.at(pos).Labelled(prevToken.name(), stat)); |
aoqi@0 | 2397 | } else if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) { |
aoqi@0 | 2398 | pos = token.pos; |
aoqi@0 | 2399 | JCModifiers mods = F.at(Position.NOPOS).Modifiers(0); |
aoqi@0 | 2400 | F.at(pos); |
aoqi@0 | 2401 | ListBuffer<JCStatement> stats = |
aoqi@0 | 2402 | variableDeclarators(mods, t, new ListBuffer<JCStatement>()); |
aoqi@0 | 2403 | // A "LocalVariableDeclarationStatement" subsumes the terminating semicolon |
aoqi@0 | 2404 | storeEnd(stats.last(), token.endPos); |
aoqi@0 | 2405 | accept(SEMI); |
aoqi@0 | 2406 | return stats.toList(); |
aoqi@0 | 2407 | } else { |
aoqi@0 | 2408 | // This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon |
aoqi@0 | 2409 | JCExpressionStatement expr = to(F.at(pos).Exec(checkExprStat(t))); |
aoqi@0 | 2410 | accept(SEMI); |
aoqi@0 | 2411 | return List.<JCStatement>of(expr); |
aoqi@0 | 2412 | } |
aoqi@0 | 2413 | } |
aoqi@0 | 2414 | } |
aoqi@0 | 2415 | |
aoqi@0 | 2416 | /** Statement = |
aoqi@0 | 2417 | * Block |
aoqi@0 | 2418 | * | IF ParExpression Statement [ELSE Statement] |
aoqi@0 | 2419 | * | FOR "(" ForInitOpt ";" [Expression] ";" ForUpdateOpt ")" Statement |
aoqi@0 | 2420 | * | FOR "(" FormalParameter : Expression ")" Statement |
aoqi@0 | 2421 | * | WHILE ParExpression Statement |
aoqi@0 | 2422 | * | DO Statement WHILE ParExpression ";" |
aoqi@0 | 2423 | * | TRY Block ( Catches | [Catches] FinallyPart ) |
aoqi@0 | 2424 | * | TRY "(" ResourceSpecification ";"opt ")" Block [Catches] [FinallyPart] |
aoqi@0 | 2425 | * | SWITCH ParExpression "{" SwitchBlockStatementGroups "}" |
aoqi@0 | 2426 | * | SYNCHRONIZED ParExpression Block |
aoqi@0 | 2427 | * | RETURN [Expression] ";" |
aoqi@0 | 2428 | * | THROW Expression ";" |
aoqi@0 | 2429 | * | BREAK [Ident] ";" |
aoqi@0 | 2430 | * | CONTINUE [Ident] ";" |
aoqi@0 | 2431 | * | ASSERT Expression [ ":" Expression ] ";" |
aoqi@0 | 2432 | * | ";" |
aoqi@0 | 2433 | * | ExpressionStatement |
aoqi@0 | 2434 | * | Ident ":" Statement |
aoqi@0 | 2435 | */ |
aoqi@0 | 2436 | @SuppressWarnings("fallthrough") |
aoqi@0 | 2437 | public JCStatement parseStatement() { |
aoqi@0 | 2438 | int pos = token.pos; |
aoqi@0 | 2439 | switch (token.kind) { |
aoqi@0 | 2440 | case LBRACE: |
aoqi@0 | 2441 | return block(); |
aoqi@0 | 2442 | case IF: { |
aoqi@0 | 2443 | nextToken(); |
aoqi@0 | 2444 | JCExpression cond = parExpression(); |
aoqi@0 | 2445 | JCStatement thenpart = parseStatementAsBlock(); |
aoqi@0 | 2446 | JCStatement elsepart = null; |
aoqi@0 | 2447 | if (token.kind == ELSE) { |
aoqi@0 | 2448 | nextToken(); |
aoqi@0 | 2449 | elsepart = parseStatementAsBlock(); |
aoqi@0 | 2450 | } |
aoqi@0 | 2451 | return F.at(pos).If(cond, thenpart, elsepart); |
aoqi@0 | 2452 | } |
aoqi@0 | 2453 | case FOR: { |
aoqi@0 | 2454 | nextToken(); |
aoqi@0 | 2455 | accept(LPAREN); |
aoqi@0 | 2456 | List<JCStatement> inits = token.kind == SEMI ? List.<JCStatement>nil() : forInit(); |
aoqi@0 | 2457 | if (inits.length() == 1 && |
aoqi@0 | 2458 | inits.head.hasTag(VARDEF) && |
aoqi@0 | 2459 | ((JCVariableDecl) inits.head).init == null && |
aoqi@0 | 2460 | token.kind == COLON) { |
aoqi@0 | 2461 | checkForeach(); |
aoqi@0 | 2462 | JCVariableDecl var = (JCVariableDecl)inits.head; |
aoqi@0 | 2463 | accept(COLON); |
aoqi@0 | 2464 | JCExpression expr = parseExpression(); |
aoqi@0 | 2465 | accept(RPAREN); |
aoqi@0 | 2466 | JCStatement body = parseStatementAsBlock(); |
aoqi@0 | 2467 | return F.at(pos).ForeachLoop(var, expr, body); |
aoqi@0 | 2468 | } else { |
aoqi@0 | 2469 | accept(SEMI); |
aoqi@0 | 2470 | JCExpression cond = token.kind == SEMI ? null : parseExpression(); |
aoqi@0 | 2471 | accept(SEMI); |
aoqi@0 | 2472 | List<JCExpressionStatement> steps = token.kind == RPAREN ? List.<JCExpressionStatement>nil() : forUpdate(); |
aoqi@0 | 2473 | accept(RPAREN); |
aoqi@0 | 2474 | JCStatement body = parseStatementAsBlock(); |
aoqi@0 | 2475 | return F.at(pos).ForLoop(inits, cond, steps, body); |
aoqi@0 | 2476 | } |
aoqi@0 | 2477 | } |
aoqi@0 | 2478 | case WHILE: { |
aoqi@0 | 2479 | nextToken(); |
aoqi@0 | 2480 | JCExpression cond = parExpression(); |
aoqi@0 | 2481 | JCStatement body = parseStatementAsBlock(); |
aoqi@0 | 2482 | return F.at(pos).WhileLoop(cond, body); |
aoqi@0 | 2483 | } |
aoqi@0 | 2484 | case DO: { |
aoqi@0 | 2485 | nextToken(); |
aoqi@0 | 2486 | JCStatement body = parseStatementAsBlock(); |
aoqi@0 | 2487 | accept(WHILE); |
aoqi@0 | 2488 | JCExpression cond = parExpression(); |
aoqi@0 | 2489 | JCDoWhileLoop t = to(F.at(pos).DoLoop(body, cond)); |
aoqi@0 | 2490 | accept(SEMI); |
aoqi@0 | 2491 | return t; |
aoqi@0 | 2492 | } |
aoqi@0 | 2493 | case TRY: { |
aoqi@0 | 2494 | nextToken(); |
aoqi@0 | 2495 | List<JCTree> resources = List.<JCTree>nil(); |
aoqi@0 | 2496 | if (token.kind == LPAREN) { |
aoqi@0 | 2497 | checkTryWithResources(); |
aoqi@0 | 2498 | nextToken(); |
aoqi@0 | 2499 | resources = resources(); |
aoqi@0 | 2500 | accept(RPAREN); |
aoqi@0 | 2501 | } |
aoqi@0 | 2502 | JCBlock body = block(); |
aoqi@0 | 2503 | ListBuffer<JCCatch> catchers = new ListBuffer<JCCatch>(); |
aoqi@0 | 2504 | JCBlock finalizer = null; |
aoqi@0 | 2505 | if (token.kind == CATCH || token.kind == FINALLY) { |
aoqi@0 | 2506 | while (token.kind == CATCH) catchers.append(catchClause()); |
aoqi@0 | 2507 | if (token.kind == FINALLY) { |
aoqi@0 | 2508 | nextToken(); |
aoqi@0 | 2509 | finalizer = block(); |
aoqi@0 | 2510 | } |
aoqi@0 | 2511 | } else { |
aoqi@0 | 2512 | if (allowTWR) { |
aoqi@0 | 2513 | if (resources.isEmpty()) |
aoqi@0 | 2514 | error(pos, "try.without.catch.finally.or.resource.decls"); |
aoqi@0 | 2515 | } else |
aoqi@0 | 2516 | error(pos, "try.without.catch.or.finally"); |
aoqi@0 | 2517 | } |
aoqi@0 | 2518 | return F.at(pos).Try(resources, body, catchers.toList(), finalizer); |
aoqi@0 | 2519 | } |
aoqi@0 | 2520 | case SWITCH: { |
aoqi@0 | 2521 | nextToken(); |
aoqi@0 | 2522 | JCExpression selector = parExpression(); |
aoqi@0 | 2523 | accept(LBRACE); |
aoqi@0 | 2524 | List<JCCase> cases = switchBlockStatementGroups(); |
aoqi@0 | 2525 | JCSwitch t = to(F.at(pos).Switch(selector, cases)); |
aoqi@0 | 2526 | accept(RBRACE); |
aoqi@0 | 2527 | return t; |
aoqi@0 | 2528 | } |
aoqi@0 | 2529 | case SYNCHRONIZED: { |
aoqi@0 | 2530 | nextToken(); |
aoqi@0 | 2531 | JCExpression lock = parExpression(); |
aoqi@0 | 2532 | JCBlock body = block(); |
aoqi@0 | 2533 | return F.at(pos).Synchronized(lock, body); |
aoqi@0 | 2534 | } |
aoqi@0 | 2535 | case RETURN: { |
aoqi@0 | 2536 | nextToken(); |
aoqi@0 | 2537 | JCExpression result = token.kind == SEMI ? null : parseExpression(); |
aoqi@0 | 2538 | JCReturn t = to(F.at(pos).Return(result)); |
aoqi@0 | 2539 | accept(SEMI); |
aoqi@0 | 2540 | return t; |
aoqi@0 | 2541 | } |
aoqi@0 | 2542 | case THROW: { |
aoqi@0 | 2543 | nextToken(); |
aoqi@0 | 2544 | JCExpression exc = parseExpression(); |
aoqi@0 | 2545 | JCThrow t = to(F.at(pos).Throw(exc)); |
aoqi@0 | 2546 | accept(SEMI); |
aoqi@0 | 2547 | return t; |
aoqi@0 | 2548 | } |
aoqi@0 | 2549 | case BREAK: { |
aoqi@0 | 2550 | nextToken(); |
aoqi@0 | 2551 | Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null; |
aoqi@0 | 2552 | JCBreak t = to(F.at(pos).Break(label)); |
aoqi@0 | 2553 | accept(SEMI); |
aoqi@0 | 2554 | return t; |
aoqi@0 | 2555 | } |
aoqi@0 | 2556 | case CONTINUE: { |
aoqi@0 | 2557 | nextToken(); |
aoqi@0 | 2558 | Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null; |
aoqi@0 | 2559 | JCContinue t = to(F.at(pos).Continue(label)); |
aoqi@0 | 2560 | accept(SEMI); |
aoqi@0 | 2561 | return t; |
aoqi@0 | 2562 | } |
aoqi@0 | 2563 | case SEMI: |
aoqi@0 | 2564 | nextToken(); |
aoqi@0 | 2565 | return toP(F.at(pos).Skip()); |
aoqi@0 | 2566 | case ELSE: |
aoqi@0 | 2567 | int elsePos = token.pos; |
aoqi@0 | 2568 | nextToken(); |
aoqi@0 | 2569 | return doRecover(elsePos, BasicErrorRecoveryAction.BLOCK_STMT, "else.without.if"); |
aoqi@0 | 2570 | case FINALLY: |
aoqi@0 | 2571 | int finallyPos = token.pos; |
aoqi@0 | 2572 | nextToken(); |
aoqi@0 | 2573 | return doRecover(finallyPos, BasicErrorRecoveryAction.BLOCK_STMT, "finally.without.try"); |
aoqi@0 | 2574 | case CATCH: |
aoqi@0 | 2575 | return doRecover(token.pos, BasicErrorRecoveryAction.CATCH_CLAUSE, "catch.without.try"); |
aoqi@0 | 2576 | case ASSERT: { |
aoqi@0 | 2577 | if (allowAsserts && token.kind == ASSERT) { |
aoqi@0 | 2578 | nextToken(); |
aoqi@0 | 2579 | JCExpression assertion = parseExpression(); |
aoqi@0 | 2580 | JCExpression message = null; |
aoqi@0 | 2581 | if (token.kind == COLON) { |
aoqi@0 | 2582 | nextToken(); |
aoqi@0 | 2583 | message = parseExpression(); |
aoqi@0 | 2584 | } |
aoqi@0 | 2585 | JCAssert t = to(F.at(pos).Assert(assertion, message)); |
aoqi@0 | 2586 | accept(SEMI); |
aoqi@0 | 2587 | return t; |
aoqi@0 | 2588 | } |
aoqi@0 | 2589 | /* else fall through to default case */ |
aoqi@0 | 2590 | } |
aoqi@0 | 2591 | case ENUM: |
aoqi@0 | 2592 | default: |
aoqi@0 | 2593 | Token prevToken = token; |
aoqi@0 | 2594 | JCExpression expr = parseExpression(); |
aoqi@0 | 2595 | if (token.kind == COLON && expr.hasTag(IDENT)) { |
aoqi@0 | 2596 | nextToken(); |
aoqi@0 | 2597 | JCStatement stat = parseStatement(); |
aoqi@0 | 2598 | return F.at(pos).Labelled(prevToken.name(), stat); |
aoqi@0 | 2599 | } else { |
aoqi@0 | 2600 | // This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon |
aoqi@0 | 2601 | JCExpressionStatement stat = to(F.at(pos).Exec(checkExprStat(expr))); |
aoqi@0 | 2602 | accept(SEMI); |
aoqi@0 | 2603 | return stat; |
aoqi@0 | 2604 | } |
aoqi@0 | 2605 | } |
aoqi@0 | 2606 | } |
aoqi@0 | 2607 | |
aoqi@0 | 2608 | private JCStatement doRecover(int startPos, ErrorRecoveryAction action, String key) { |
aoqi@0 | 2609 | int errPos = S.errPos(); |
aoqi@0 | 2610 | JCTree stm = action.doRecover(this); |
aoqi@0 | 2611 | S.errPos(errPos); |
aoqi@0 | 2612 | return toP(F.Exec(syntaxError(startPos, List.<JCTree>of(stm), key))); |
aoqi@0 | 2613 | } |
aoqi@0 | 2614 | |
aoqi@0 | 2615 | /** CatchClause = CATCH "(" FormalParameter ")" Block |
aoqi@0 | 2616 | * TODO: the "FormalParameter" is not correct, it uses the special "catchTypes" rule below. |
aoqi@0 | 2617 | */ |
aoqi@0 | 2618 | protected JCCatch catchClause() { |
aoqi@0 | 2619 | int pos = token.pos; |
aoqi@0 | 2620 | accept(CATCH); |
aoqi@0 | 2621 | accept(LPAREN); |
aoqi@0 | 2622 | JCModifiers mods = optFinal(Flags.PARAMETER); |
aoqi@0 | 2623 | List<JCExpression> catchTypes = catchTypes(); |
aoqi@0 | 2624 | JCExpression paramType = catchTypes.size() > 1 ? |
aoqi@0 | 2625 | toP(F.at(catchTypes.head.getStartPosition()).TypeUnion(catchTypes)) : |
aoqi@0 | 2626 | catchTypes.head; |
aoqi@0 | 2627 | JCVariableDecl formal = variableDeclaratorId(mods, paramType); |
aoqi@0 | 2628 | accept(RPAREN); |
aoqi@0 | 2629 | JCBlock body = block(); |
aoqi@0 | 2630 | return F.at(pos).Catch(formal, body); |
aoqi@0 | 2631 | } |
aoqi@0 | 2632 | |
aoqi@0 | 2633 | List<JCExpression> catchTypes() { |
aoqi@0 | 2634 | ListBuffer<JCExpression> catchTypes = new ListBuffer<>(); |
aoqi@0 | 2635 | catchTypes.add(parseType()); |
aoqi@0 | 2636 | while (token.kind == BAR) { |
aoqi@0 | 2637 | checkMulticatch(); |
aoqi@0 | 2638 | nextToken(); |
aoqi@0 | 2639 | // Instead of qualident this is now parseType. |
aoqi@0 | 2640 | // But would that allow too much, e.g. arrays or generics? |
aoqi@0 | 2641 | catchTypes.add(parseType()); |
aoqi@0 | 2642 | } |
aoqi@0 | 2643 | return catchTypes.toList(); |
aoqi@0 | 2644 | } |
aoqi@0 | 2645 | |
aoqi@0 | 2646 | /** SwitchBlockStatementGroups = { SwitchBlockStatementGroup } |
aoqi@0 | 2647 | * SwitchBlockStatementGroup = SwitchLabel BlockStatements |
aoqi@0 | 2648 | * SwitchLabel = CASE ConstantExpression ":" | DEFAULT ":" |
aoqi@0 | 2649 | */ |
aoqi@0 | 2650 | List<JCCase> switchBlockStatementGroups() { |
aoqi@0 | 2651 | ListBuffer<JCCase> cases = new ListBuffer<JCCase>(); |
aoqi@0 | 2652 | while (true) { |
aoqi@0 | 2653 | int pos = token.pos; |
aoqi@0 | 2654 | switch (token.kind) { |
aoqi@0 | 2655 | case CASE: |
aoqi@0 | 2656 | case DEFAULT: |
aoqi@0 | 2657 | cases.append(switchBlockStatementGroup()); |
aoqi@0 | 2658 | break; |
aoqi@0 | 2659 | case RBRACE: case EOF: |
aoqi@0 | 2660 | return cases.toList(); |
aoqi@0 | 2661 | default: |
aoqi@0 | 2662 | nextToken(); // to ensure progress |
aoqi@0 | 2663 | syntaxError(pos, "expected3", |
aoqi@0 | 2664 | CASE, DEFAULT, RBRACE); |
aoqi@0 | 2665 | } |
aoqi@0 | 2666 | } |
aoqi@0 | 2667 | } |
aoqi@0 | 2668 | |
aoqi@0 | 2669 | protected JCCase switchBlockStatementGroup() { |
aoqi@0 | 2670 | int pos = token.pos; |
aoqi@0 | 2671 | List<JCStatement> stats; |
aoqi@0 | 2672 | JCCase c; |
aoqi@0 | 2673 | switch (token.kind) { |
aoqi@0 | 2674 | case CASE: |
aoqi@0 | 2675 | nextToken(); |
aoqi@0 | 2676 | JCExpression pat = parseExpression(); |
aoqi@0 | 2677 | accept(COLON); |
aoqi@0 | 2678 | stats = blockStatements(); |
aoqi@0 | 2679 | c = F.at(pos).Case(pat, stats); |
aoqi@0 | 2680 | if (stats.isEmpty()) |
aoqi@0 | 2681 | storeEnd(c, S.prevToken().endPos); |
aoqi@0 | 2682 | return c; |
aoqi@0 | 2683 | case DEFAULT: |
aoqi@0 | 2684 | nextToken(); |
aoqi@0 | 2685 | accept(COLON); |
aoqi@0 | 2686 | stats = blockStatements(); |
aoqi@0 | 2687 | c = F.at(pos).Case(null, stats); |
aoqi@0 | 2688 | if (stats.isEmpty()) |
aoqi@0 | 2689 | storeEnd(c, S.prevToken().endPos); |
aoqi@0 | 2690 | return c; |
aoqi@0 | 2691 | } |
aoqi@0 | 2692 | throw new AssertionError("should not reach here"); |
aoqi@0 | 2693 | } |
aoqi@0 | 2694 | |
aoqi@0 | 2695 | /** MoreStatementExpressions = { COMMA StatementExpression } |
aoqi@0 | 2696 | */ |
aoqi@0 | 2697 | <T extends ListBuffer<? super JCExpressionStatement>> T moreStatementExpressions(int pos, |
aoqi@0 | 2698 | JCExpression first, |
aoqi@0 | 2699 | T stats) { |
aoqi@0 | 2700 | // This Exec is a "StatementExpression"; it subsumes no terminating token |
aoqi@0 | 2701 | stats.append(toP(F.at(pos).Exec(checkExprStat(first)))); |
aoqi@0 | 2702 | while (token.kind == COMMA) { |
aoqi@0 | 2703 | nextToken(); |
aoqi@0 | 2704 | pos = token.pos; |
aoqi@0 | 2705 | JCExpression t = parseExpression(); |
aoqi@0 | 2706 | // This Exec is a "StatementExpression"; it subsumes no terminating token |
aoqi@0 | 2707 | stats.append(toP(F.at(pos).Exec(checkExprStat(t)))); |
aoqi@0 | 2708 | } |
aoqi@0 | 2709 | return stats; |
aoqi@0 | 2710 | } |
aoqi@0 | 2711 | |
aoqi@0 | 2712 | /** ForInit = StatementExpression MoreStatementExpressions |
aoqi@0 | 2713 | * | { FINAL | '@' Annotation } Type VariableDeclarators |
aoqi@0 | 2714 | */ |
aoqi@0 | 2715 | List<JCStatement> forInit() { |
aoqi@0 | 2716 | ListBuffer<JCStatement> stats = new ListBuffer<>(); |
aoqi@0 | 2717 | int pos = token.pos; |
aoqi@0 | 2718 | if (token.kind == FINAL || token.kind == MONKEYS_AT) { |
aoqi@0 | 2719 | return variableDeclarators(optFinal(0), parseType(), stats).toList(); |
aoqi@0 | 2720 | } else { |
aoqi@0 | 2721 | JCExpression t = term(EXPR | TYPE); |
aoqi@0 | 2722 | if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) { |
aoqi@0 | 2723 | return variableDeclarators(modifiersOpt(), t, stats).toList(); |
aoqi@0 | 2724 | } else if ((lastmode & TYPE) != 0 && token.kind == COLON) { |
aoqi@0 | 2725 | error(pos, "bad.initializer", "for-loop"); |
aoqi@0 | 2726 | return List.of((JCStatement)F.at(pos).VarDef(null, null, t, null)); |
aoqi@0 | 2727 | } else { |
aoqi@0 | 2728 | return moreStatementExpressions(pos, t, stats).toList(); |
aoqi@0 | 2729 | } |
aoqi@0 | 2730 | } |
aoqi@0 | 2731 | } |
aoqi@0 | 2732 | |
aoqi@0 | 2733 | /** ForUpdate = StatementExpression MoreStatementExpressions |
aoqi@0 | 2734 | */ |
aoqi@0 | 2735 | List<JCExpressionStatement> forUpdate() { |
aoqi@0 | 2736 | return moreStatementExpressions(token.pos, |
aoqi@0 | 2737 | parseExpression(), |
aoqi@0 | 2738 | new ListBuffer<JCExpressionStatement>()).toList(); |
aoqi@0 | 2739 | } |
aoqi@0 | 2740 | |
aoqi@0 | 2741 | /** AnnotationsOpt = { '@' Annotation } |
aoqi@0 | 2742 | * |
aoqi@0 | 2743 | * @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION |
aoqi@0 | 2744 | */ |
aoqi@0 | 2745 | List<JCAnnotation> annotationsOpt(Tag kind) { |
aoqi@0 | 2746 | if (token.kind != MONKEYS_AT) return List.nil(); // optimization |
aoqi@0 | 2747 | ListBuffer<JCAnnotation> buf = new ListBuffer<JCAnnotation>(); |
aoqi@0 | 2748 | int prevmode = mode; |
aoqi@0 | 2749 | while (token.kind == MONKEYS_AT) { |
aoqi@0 | 2750 | int pos = token.pos; |
aoqi@0 | 2751 | nextToken(); |
aoqi@0 | 2752 | buf.append(annotation(pos, kind)); |
aoqi@0 | 2753 | } |
aoqi@0 | 2754 | lastmode = mode; |
aoqi@0 | 2755 | mode = prevmode; |
aoqi@0 | 2756 | List<JCAnnotation> annotations = buf.toList(); |
aoqi@0 | 2757 | |
aoqi@0 | 2758 | return annotations; |
aoqi@0 | 2759 | } |
aoqi@0 | 2760 | |
aoqi@0 | 2761 | List<JCAnnotation> typeAnnotationsOpt() { |
aoqi@0 | 2762 | List<JCAnnotation> annotations = annotationsOpt(Tag.TYPE_ANNOTATION); |
aoqi@0 | 2763 | return annotations; |
aoqi@0 | 2764 | } |
aoqi@0 | 2765 | |
aoqi@0 | 2766 | /** ModifiersOpt = { Modifier } |
aoqi@0 | 2767 | * Modifier = PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL |
aoqi@0 | 2768 | * | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE | "@" |
aoqi@0 | 2769 | * | "@" Annotation |
aoqi@0 | 2770 | */ |
aoqi@0 | 2771 | JCModifiers modifiersOpt() { |
aoqi@0 | 2772 | return modifiersOpt(null); |
aoqi@0 | 2773 | } |
aoqi@0 | 2774 | protected JCModifiers modifiersOpt(JCModifiers partial) { |
aoqi@0 | 2775 | long flags; |
aoqi@0 | 2776 | ListBuffer<JCAnnotation> annotations = new ListBuffer<JCAnnotation>(); |
aoqi@0 | 2777 | int pos; |
aoqi@0 | 2778 | if (partial == null) { |
aoqi@0 | 2779 | flags = 0; |
aoqi@0 | 2780 | pos = token.pos; |
aoqi@0 | 2781 | } else { |
aoqi@0 | 2782 | flags = partial.flags; |
aoqi@0 | 2783 | annotations.appendList(partial.annotations); |
aoqi@0 | 2784 | pos = partial.pos; |
aoqi@0 | 2785 | } |
aoqi@0 | 2786 | if (token.deprecatedFlag()) { |
aoqi@0 | 2787 | flags |= Flags.DEPRECATED; |
aoqi@0 | 2788 | } |
aoqi@0 | 2789 | int lastPos; |
aoqi@0 | 2790 | loop: |
aoqi@0 | 2791 | while (true) { |
aoqi@0 | 2792 | long flag; |
aoqi@0 | 2793 | switch (token.kind) { |
aoqi@0 | 2794 | case PRIVATE : flag = Flags.PRIVATE; break; |
aoqi@0 | 2795 | case PROTECTED : flag = Flags.PROTECTED; break; |
aoqi@0 | 2796 | case PUBLIC : flag = Flags.PUBLIC; break; |
aoqi@0 | 2797 | case STATIC : flag = Flags.STATIC; break; |
aoqi@0 | 2798 | case TRANSIENT : flag = Flags.TRANSIENT; break; |
aoqi@0 | 2799 | case FINAL : flag = Flags.FINAL; break; |
aoqi@0 | 2800 | case ABSTRACT : flag = Flags.ABSTRACT; break; |
aoqi@0 | 2801 | case NATIVE : flag = Flags.NATIVE; break; |
aoqi@0 | 2802 | case VOLATILE : flag = Flags.VOLATILE; break; |
aoqi@0 | 2803 | case SYNCHRONIZED: flag = Flags.SYNCHRONIZED; break; |
aoqi@0 | 2804 | case STRICTFP : flag = Flags.STRICTFP; break; |
aoqi@0 | 2805 | case MONKEYS_AT : flag = Flags.ANNOTATION; break; |
aoqi@0 | 2806 | case DEFAULT : checkDefaultMethods(); flag = Flags.DEFAULT; break; |
aoqi@0 | 2807 | case ERROR : flag = 0; nextToken(); break; |
aoqi@0 | 2808 | default: break loop; |
aoqi@0 | 2809 | } |
aoqi@0 | 2810 | if ((flags & flag) != 0) error(token.pos, "repeated.modifier"); |
aoqi@0 | 2811 | lastPos = token.pos; |
aoqi@0 | 2812 | nextToken(); |
aoqi@0 | 2813 | if (flag == Flags.ANNOTATION) { |
aoqi@0 | 2814 | checkAnnotations(); |
aoqi@0 | 2815 | if (token.kind != INTERFACE) { |
aoqi@0 | 2816 | JCAnnotation ann = annotation(lastPos, Tag.ANNOTATION); |
aoqi@0 | 2817 | // if first modifier is an annotation, set pos to annotation's. |
aoqi@0 | 2818 | if (flags == 0 && annotations.isEmpty()) |
aoqi@0 | 2819 | pos = ann.pos; |
aoqi@0 | 2820 | annotations.append(ann); |
aoqi@0 | 2821 | flag = 0; |
aoqi@0 | 2822 | } |
aoqi@0 | 2823 | } |
aoqi@0 | 2824 | flags |= flag; |
aoqi@0 | 2825 | } |
aoqi@0 | 2826 | switch (token.kind) { |
aoqi@0 | 2827 | case ENUM: flags |= Flags.ENUM; break; |
aoqi@0 | 2828 | case INTERFACE: flags |= Flags.INTERFACE; break; |
aoqi@0 | 2829 | default: break; |
aoqi@0 | 2830 | } |
aoqi@0 | 2831 | |
aoqi@0 | 2832 | /* A modifiers tree with no modifier tokens or annotations |
aoqi@0 | 2833 | * has no text position. */ |
aoqi@0 | 2834 | if ((flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0 && annotations.isEmpty()) |
aoqi@0 | 2835 | pos = Position.NOPOS; |
aoqi@0 | 2836 | |
aoqi@0 | 2837 | JCModifiers mods = F.at(pos).Modifiers(flags, annotations.toList()); |
aoqi@0 | 2838 | if (pos != Position.NOPOS) |
aoqi@0 | 2839 | storeEnd(mods, S.prevToken().endPos); |
aoqi@0 | 2840 | return mods; |
aoqi@0 | 2841 | } |
aoqi@0 | 2842 | |
aoqi@0 | 2843 | /** Annotation = "@" Qualident [ "(" AnnotationFieldValues ")" ] |
aoqi@0 | 2844 | * |
aoqi@0 | 2845 | * @param pos position of "@" token |
aoqi@0 | 2846 | * @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION |
aoqi@0 | 2847 | */ |
aoqi@0 | 2848 | JCAnnotation annotation(int pos, Tag kind) { |
aoqi@0 | 2849 | // accept(AT); // AT consumed by caller |
aoqi@0 | 2850 | checkAnnotations(); |
aoqi@0 | 2851 | if (kind == Tag.TYPE_ANNOTATION) { |
aoqi@0 | 2852 | checkTypeAnnotations(); |
aoqi@0 | 2853 | } |
aoqi@0 | 2854 | JCTree ident = qualident(false); |
aoqi@0 | 2855 | List<JCExpression> fieldValues = annotationFieldValuesOpt(); |
aoqi@0 | 2856 | JCAnnotation ann; |
aoqi@0 | 2857 | if (kind == Tag.ANNOTATION) { |
aoqi@0 | 2858 | ann = F.at(pos).Annotation(ident, fieldValues); |
aoqi@0 | 2859 | } else if (kind == Tag.TYPE_ANNOTATION) { |
aoqi@0 | 2860 | ann = F.at(pos).TypeAnnotation(ident, fieldValues); |
aoqi@0 | 2861 | } else { |
aoqi@0 | 2862 | throw new AssertionError("Unhandled annotation kind: " + kind); |
aoqi@0 | 2863 | } |
aoqi@0 | 2864 | |
aoqi@0 | 2865 | storeEnd(ann, S.prevToken().endPos); |
aoqi@0 | 2866 | return ann; |
aoqi@0 | 2867 | } |
aoqi@0 | 2868 | |
aoqi@0 | 2869 | List<JCExpression> annotationFieldValuesOpt() { |
aoqi@0 | 2870 | return (token.kind == LPAREN) ? annotationFieldValues() : List.<JCExpression>nil(); |
aoqi@0 | 2871 | } |
aoqi@0 | 2872 | |
aoqi@0 | 2873 | /** AnnotationFieldValues = "(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */ |
aoqi@0 | 2874 | List<JCExpression> annotationFieldValues() { |
aoqi@0 | 2875 | accept(LPAREN); |
aoqi@0 | 2876 | ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>(); |
aoqi@0 | 2877 | if (token.kind != RPAREN) { |
aoqi@0 | 2878 | buf.append(annotationFieldValue()); |
aoqi@0 | 2879 | while (token.kind == COMMA) { |
aoqi@0 | 2880 | nextToken(); |
aoqi@0 | 2881 | buf.append(annotationFieldValue()); |
aoqi@0 | 2882 | } |
aoqi@0 | 2883 | } |
aoqi@0 | 2884 | accept(RPAREN); |
aoqi@0 | 2885 | return buf.toList(); |
aoqi@0 | 2886 | } |
aoqi@0 | 2887 | |
aoqi@0 | 2888 | /** AnnotationFieldValue = AnnotationValue |
aoqi@0 | 2889 | * | Identifier "=" AnnotationValue |
aoqi@0 | 2890 | */ |
aoqi@0 | 2891 | JCExpression annotationFieldValue() { |
aoqi@0 | 2892 | if (LAX_IDENTIFIER.accepts(token.kind)) { |
aoqi@0 | 2893 | mode = EXPR; |
aoqi@0 | 2894 | JCExpression t1 = term1(); |
aoqi@0 | 2895 | if (t1.hasTag(IDENT) && token.kind == EQ) { |
aoqi@0 | 2896 | int pos = token.pos; |
aoqi@0 | 2897 | accept(EQ); |
aoqi@0 | 2898 | JCExpression v = annotationValue(); |
aoqi@0 | 2899 | return toP(F.at(pos).Assign(t1, v)); |
aoqi@0 | 2900 | } else { |
aoqi@0 | 2901 | return t1; |
aoqi@0 | 2902 | } |
aoqi@0 | 2903 | } |
aoqi@0 | 2904 | return annotationValue(); |
aoqi@0 | 2905 | } |
aoqi@0 | 2906 | |
aoqi@0 | 2907 | /* AnnotationValue = ConditionalExpression |
aoqi@0 | 2908 | * | Annotation |
aoqi@0 | 2909 | * | "{" [ AnnotationValue { "," AnnotationValue } ] [","] "}" |
aoqi@0 | 2910 | */ |
aoqi@0 | 2911 | JCExpression annotationValue() { |
aoqi@0 | 2912 | int pos; |
aoqi@0 | 2913 | switch (token.kind) { |
aoqi@0 | 2914 | case MONKEYS_AT: |
aoqi@0 | 2915 | pos = token.pos; |
aoqi@0 | 2916 | nextToken(); |
aoqi@0 | 2917 | return annotation(pos, Tag.ANNOTATION); |
aoqi@0 | 2918 | case LBRACE: |
aoqi@0 | 2919 | pos = token.pos; |
aoqi@0 | 2920 | accept(LBRACE); |
aoqi@0 | 2921 | ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>(); |
aoqi@0 | 2922 | if (token.kind == COMMA) { |
aoqi@0 | 2923 | nextToken(); |
aoqi@0 | 2924 | } else if (token.kind != RBRACE) { |
aoqi@0 | 2925 | buf.append(annotationValue()); |
aoqi@0 | 2926 | while (token.kind == COMMA) { |
aoqi@0 | 2927 | nextToken(); |
aoqi@0 | 2928 | if (token.kind == RBRACE) break; |
aoqi@0 | 2929 | buf.append(annotationValue()); |
aoqi@0 | 2930 | } |
aoqi@0 | 2931 | } |
aoqi@0 | 2932 | accept(RBRACE); |
aoqi@0 | 2933 | return toP(F.at(pos).NewArray(null, List.<JCExpression>nil(), buf.toList())); |
aoqi@0 | 2934 | default: |
aoqi@0 | 2935 | mode = EXPR; |
aoqi@0 | 2936 | return term1(); |
aoqi@0 | 2937 | } |
aoqi@0 | 2938 | } |
aoqi@0 | 2939 | |
aoqi@0 | 2940 | /** VariableDeclarators = VariableDeclarator { "," VariableDeclarator } |
aoqi@0 | 2941 | */ |
aoqi@0 | 2942 | public <T extends ListBuffer<? super JCVariableDecl>> T variableDeclarators(JCModifiers mods, |
aoqi@0 | 2943 | JCExpression type, |
aoqi@0 | 2944 | T vdefs) |
aoqi@0 | 2945 | { |
aoqi@0 | 2946 | return variableDeclaratorsRest(token.pos, mods, type, ident(), false, null, vdefs); |
aoqi@0 | 2947 | } |
aoqi@0 | 2948 | |
aoqi@0 | 2949 | /** VariableDeclaratorsRest = VariableDeclaratorRest { "," VariableDeclarator } |
aoqi@0 | 2950 | * ConstantDeclaratorsRest = ConstantDeclaratorRest { "," ConstantDeclarator } |
aoqi@0 | 2951 | * |
aoqi@0 | 2952 | * @param reqInit Is an initializer always required? |
aoqi@0 | 2953 | * @param dc The documentation comment for the variable declarations, or null. |
aoqi@0 | 2954 | */ |
aoqi@0 | 2955 | <T extends ListBuffer<? super JCVariableDecl>> T variableDeclaratorsRest(int pos, |
aoqi@0 | 2956 | JCModifiers mods, |
aoqi@0 | 2957 | JCExpression type, |
aoqi@0 | 2958 | Name name, |
aoqi@0 | 2959 | boolean reqInit, |
aoqi@0 | 2960 | Comment dc, |
aoqi@0 | 2961 | T vdefs) |
aoqi@0 | 2962 | { |
aoqi@0 | 2963 | vdefs.append(variableDeclaratorRest(pos, mods, type, name, reqInit, dc)); |
aoqi@0 | 2964 | while (token.kind == COMMA) { |
aoqi@0 | 2965 | // All but last of multiple declarators subsume a comma |
aoqi@0 | 2966 | storeEnd((JCTree)vdefs.last(), token.endPos); |
aoqi@0 | 2967 | nextToken(); |
aoqi@0 | 2968 | vdefs.append(variableDeclarator(mods, type, reqInit, dc)); |
aoqi@0 | 2969 | } |
aoqi@0 | 2970 | return vdefs; |
aoqi@0 | 2971 | } |
aoqi@0 | 2972 | |
aoqi@0 | 2973 | /** VariableDeclarator = Ident VariableDeclaratorRest |
aoqi@0 | 2974 | * ConstantDeclarator = Ident ConstantDeclaratorRest |
aoqi@0 | 2975 | */ |
aoqi@0 | 2976 | JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, Comment dc) { |
aoqi@0 | 2977 | return variableDeclaratorRest(token.pos, mods, type, ident(), reqInit, dc); |
aoqi@0 | 2978 | } |
aoqi@0 | 2979 | |
aoqi@0 | 2980 | /** VariableDeclaratorRest = BracketsOpt ["=" VariableInitializer] |
aoqi@0 | 2981 | * ConstantDeclaratorRest = BracketsOpt "=" VariableInitializer |
aoqi@0 | 2982 | * |
aoqi@0 | 2983 | * @param reqInit Is an initializer always required? |
aoqi@0 | 2984 | * @param dc The documentation comment for the variable declarations, or null. |
aoqi@0 | 2985 | */ |
aoqi@0 | 2986 | JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name, |
aoqi@0 | 2987 | boolean reqInit, Comment dc) { |
aoqi@0 | 2988 | type = bracketsOpt(type); |
aoqi@0 | 2989 | JCExpression init = null; |
aoqi@0 | 2990 | if (token.kind == EQ) { |
aoqi@0 | 2991 | nextToken(); |
aoqi@0 | 2992 | init = variableInitializer(); |
aoqi@0 | 2993 | } |
aoqi@0 | 2994 | else if (reqInit) syntaxError(token.pos, "expected", EQ); |
aoqi@0 | 2995 | JCVariableDecl result = |
aoqi@0 | 2996 | toP(F.at(pos).VarDef(mods, name, type, init)); |
aoqi@0 | 2997 | attach(result, dc); |
aoqi@0 | 2998 | return result; |
aoqi@0 | 2999 | } |
aoqi@0 | 3000 | |
aoqi@0 | 3001 | /** VariableDeclaratorId = Ident BracketsOpt |
aoqi@0 | 3002 | */ |
aoqi@0 | 3003 | JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type) { |
aoqi@0 | 3004 | return variableDeclaratorId(mods, type, false); |
aoqi@0 | 3005 | } |
aoqi@0 | 3006 | //where |
aoqi@0 | 3007 | JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type, boolean lambdaParameter) { |
aoqi@0 | 3008 | int pos = token.pos; |
aoqi@0 | 3009 | Name name; |
aoqi@0 | 3010 | if (lambdaParameter && token.kind == UNDERSCORE) { |
aoqi@0 | 3011 | log.error(pos, "underscore.as.identifier.in.lambda"); |
aoqi@0 | 3012 | name = token.name(); |
aoqi@0 | 3013 | nextToken(); |
aoqi@0 | 3014 | } else { |
aoqi@0 | 3015 | if (allowThisIdent) { |
aoqi@0 | 3016 | JCExpression pn = qualident(false); |
aoqi@0 | 3017 | if (pn.hasTag(Tag.IDENT) && ((JCIdent)pn).name != names._this) { |
aoqi@0 | 3018 | name = ((JCIdent)pn).name; |
aoqi@0 | 3019 | } else { |
aoqi@0 | 3020 | if ((mods.flags & Flags.VARARGS) != 0) { |
aoqi@0 | 3021 | log.error(token.pos, "varargs.and.receiver"); |
aoqi@0 | 3022 | } |
aoqi@0 | 3023 | if (token.kind == LBRACKET) { |
aoqi@0 | 3024 | log.error(token.pos, "array.and.receiver"); |
aoqi@0 | 3025 | } |
aoqi@0 | 3026 | return toP(F.at(pos).ReceiverVarDef(mods, pn, type)); |
aoqi@0 | 3027 | } |
aoqi@0 | 3028 | } else { |
aoqi@0 | 3029 | name = ident(); |
aoqi@0 | 3030 | } |
aoqi@0 | 3031 | } |
aoqi@0 | 3032 | if ((mods.flags & Flags.VARARGS) != 0 && |
aoqi@0 | 3033 | token.kind == LBRACKET) { |
aoqi@0 | 3034 | log.error(token.pos, "varargs.and.old.array.syntax"); |
aoqi@0 | 3035 | } |
aoqi@0 | 3036 | type = bracketsOpt(type); |
aoqi@0 | 3037 | return toP(F.at(pos).VarDef(mods, name, type, null)); |
aoqi@0 | 3038 | } |
aoqi@0 | 3039 | |
aoqi@0 | 3040 | /** Resources = Resource { ";" Resources } |
aoqi@0 | 3041 | */ |
aoqi@0 | 3042 | List<JCTree> resources() { |
aoqi@0 | 3043 | ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); |
aoqi@0 | 3044 | defs.append(resource()); |
aoqi@0 | 3045 | while (token.kind == SEMI) { |
aoqi@0 | 3046 | // All but last of multiple declarators must subsume a semicolon |
aoqi@0 | 3047 | storeEnd(defs.last(), token.endPos); |
aoqi@0 | 3048 | int semiColonPos = token.pos; |
aoqi@0 | 3049 | nextToken(); |
aoqi@0 | 3050 | if (token.kind == RPAREN) { // Optional trailing semicolon |
aoqi@0 | 3051 | // after last resource |
aoqi@0 | 3052 | break; |
aoqi@0 | 3053 | } |
aoqi@0 | 3054 | defs.append(resource()); |
aoqi@0 | 3055 | } |
aoqi@0 | 3056 | return defs.toList(); |
aoqi@0 | 3057 | } |
aoqi@0 | 3058 | |
aoqi@0 | 3059 | /** Resource = VariableModifiersOpt Type VariableDeclaratorId = Expression |
aoqi@0 | 3060 | */ |
aoqi@0 | 3061 | protected JCTree resource() { |
aoqi@0 | 3062 | JCModifiers optFinal = optFinal(Flags.FINAL); |
aoqi@0 | 3063 | JCExpression type = parseType(); |
aoqi@0 | 3064 | int pos = token.pos; |
aoqi@0 | 3065 | Name ident = ident(); |
aoqi@0 | 3066 | return variableDeclaratorRest(pos, optFinal, type, ident, true, null); |
aoqi@0 | 3067 | } |
aoqi@0 | 3068 | |
aoqi@0 | 3069 | /** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration} |
aoqi@0 | 3070 | */ |
aoqi@0 | 3071 | public JCTree.JCCompilationUnit parseCompilationUnit() { |
aoqi@0 | 3072 | Token firstToken = token; |
aoqi@0 | 3073 | JCExpression pid = null; |
aoqi@0 | 3074 | JCModifiers mods = null; |
aoqi@0 | 3075 | boolean consumedToplevelDoc = false; |
aoqi@0 | 3076 | boolean seenImport = false; |
aoqi@0 | 3077 | boolean seenPackage = false; |
aoqi@0 | 3078 | List<JCAnnotation> packageAnnotations = List.nil(); |
aoqi@0 | 3079 | if (token.kind == MONKEYS_AT) |
aoqi@0 | 3080 | mods = modifiersOpt(); |
aoqi@0 | 3081 | |
aoqi@0 | 3082 | if (token.kind == PACKAGE) { |
aoqi@0 | 3083 | seenPackage = true; |
aoqi@0 | 3084 | if (mods != null) { |
aoqi@0 | 3085 | checkNoMods(mods.flags); |
aoqi@0 | 3086 | packageAnnotations = mods.annotations; |
aoqi@0 | 3087 | mods = null; |
aoqi@0 | 3088 | } |
aoqi@0 | 3089 | nextToken(); |
aoqi@0 | 3090 | pid = qualident(false); |
aoqi@0 | 3091 | accept(SEMI); |
aoqi@0 | 3092 | } |
aoqi@0 | 3093 | ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); |
aoqi@0 | 3094 | boolean checkForImports = true; |
aoqi@0 | 3095 | boolean firstTypeDecl = true; |
aoqi@0 | 3096 | while (token.kind != EOF) { |
aoqi@0 | 3097 | if (token.pos > 0 && token.pos <= endPosTable.errorEndPos) { |
aoqi@0 | 3098 | // error recovery |
aoqi@0 | 3099 | skip(checkForImports, false, false, false); |
aoqi@0 | 3100 | if (token.kind == EOF) |
aoqi@0 | 3101 | break; |
aoqi@0 | 3102 | } |
aoqi@0 | 3103 | if (checkForImports && mods == null && token.kind == IMPORT) { |
aoqi@0 | 3104 | seenImport = true; |
aoqi@0 | 3105 | defs.append(importDeclaration()); |
aoqi@0 | 3106 | } else { |
aoqi@0 | 3107 | Comment docComment = token.comment(CommentStyle.JAVADOC); |
aoqi@0 | 3108 | if (firstTypeDecl && !seenImport && !seenPackage) { |
aoqi@0 | 3109 | docComment = firstToken.comment(CommentStyle.JAVADOC); |
aoqi@0 | 3110 | consumedToplevelDoc = true; |
aoqi@0 | 3111 | } |
aoqi@0 | 3112 | JCTree def = typeDeclaration(mods, docComment); |
aoqi@0 | 3113 | if (def instanceof JCExpressionStatement) |
aoqi@0 | 3114 | def = ((JCExpressionStatement)def).expr; |
aoqi@0 | 3115 | defs.append(def); |
aoqi@0 | 3116 | if (def instanceof JCClassDecl) |
aoqi@0 | 3117 | checkForImports = false; |
aoqi@0 | 3118 | mods = null; |
aoqi@0 | 3119 | firstTypeDecl = false; |
aoqi@0 | 3120 | } |
aoqi@0 | 3121 | } |
aoqi@0 | 3122 | JCTree.JCCompilationUnit toplevel = F.at(firstToken.pos).TopLevel(packageAnnotations, pid, defs.toList()); |
aoqi@0 | 3123 | if (!consumedToplevelDoc) |
aoqi@0 | 3124 | attach(toplevel, firstToken.comment(CommentStyle.JAVADOC)); |
aoqi@0 | 3125 | if (defs.isEmpty()) |
aoqi@0 | 3126 | storeEnd(toplevel, S.prevToken().endPos); |
aoqi@0 | 3127 | if (keepDocComments) |
aoqi@0 | 3128 | toplevel.docComments = docComments; |
aoqi@0 | 3129 | if (keepLineMap) |
aoqi@0 | 3130 | toplevel.lineMap = S.getLineMap(); |
aoqi@0 | 3131 | this.endPosTable.setParser(null); // remove reference to parser |
aoqi@0 | 3132 | toplevel.endPositions = this.endPosTable; |
aoqi@0 | 3133 | return toplevel; |
aoqi@0 | 3134 | } |
aoqi@0 | 3135 | |
aoqi@0 | 3136 | /** ImportDeclaration = IMPORT [ STATIC ] Ident { "." Ident } [ "." "*" ] ";" |
aoqi@0 | 3137 | */ |
aoqi@0 | 3138 | JCTree importDeclaration() { |
aoqi@0 | 3139 | int pos = token.pos; |
aoqi@0 | 3140 | nextToken(); |
aoqi@0 | 3141 | boolean importStatic = false; |
aoqi@0 | 3142 | if (token.kind == STATIC) { |
aoqi@0 | 3143 | checkStaticImports(); |
aoqi@0 | 3144 | importStatic = true; |
aoqi@0 | 3145 | nextToken(); |
aoqi@0 | 3146 | } |
aoqi@0 | 3147 | JCExpression pid = toP(F.at(token.pos).Ident(ident())); |
aoqi@0 | 3148 | do { |
aoqi@0 | 3149 | int pos1 = token.pos; |
aoqi@0 | 3150 | accept(DOT); |
aoqi@0 | 3151 | if (token.kind == STAR) { |
aoqi@0 | 3152 | pid = to(F.at(pos1).Select(pid, names.asterisk)); |
aoqi@0 | 3153 | nextToken(); |
aoqi@0 | 3154 | break; |
aoqi@0 | 3155 | } else { |
aoqi@0 | 3156 | pid = toP(F.at(pos1).Select(pid, ident())); |
aoqi@0 | 3157 | } |
aoqi@0 | 3158 | } while (token.kind == DOT); |
aoqi@0 | 3159 | accept(SEMI); |
aoqi@0 | 3160 | return toP(F.at(pos).Import(pid, importStatic)); |
aoqi@0 | 3161 | } |
aoqi@0 | 3162 | |
aoqi@0 | 3163 | /** TypeDeclaration = ClassOrInterfaceOrEnumDeclaration |
aoqi@0 | 3164 | * | ";" |
aoqi@0 | 3165 | */ |
aoqi@0 | 3166 | JCTree typeDeclaration(JCModifiers mods, Comment docComment) { |
aoqi@0 | 3167 | int pos = token.pos; |
aoqi@0 | 3168 | if (mods == null && token.kind == SEMI) { |
aoqi@0 | 3169 | nextToken(); |
aoqi@0 | 3170 | return toP(F.at(pos).Skip()); |
aoqi@0 | 3171 | } else { |
aoqi@0 | 3172 | return classOrInterfaceOrEnumDeclaration(modifiersOpt(mods), docComment); |
aoqi@0 | 3173 | } |
aoqi@0 | 3174 | } |
aoqi@0 | 3175 | |
aoqi@0 | 3176 | /** ClassOrInterfaceOrEnumDeclaration = ModifiersOpt |
aoqi@0 | 3177 | * (ClassDeclaration | InterfaceDeclaration | EnumDeclaration) |
aoqi@0 | 3178 | * @param mods Any modifiers starting the class or interface declaration |
aoqi@0 | 3179 | * @param dc The documentation comment for the class, or null. |
aoqi@0 | 3180 | */ |
aoqi@0 | 3181 | JCStatement classOrInterfaceOrEnumDeclaration(JCModifiers mods, Comment dc) { |
aoqi@0 | 3182 | if (token.kind == CLASS) { |
aoqi@0 | 3183 | return classDeclaration(mods, dc); |
aoqi@0 | 3184 | } else if (token.kind == INTERFACE) { |
aoqi@0 | 3185 | return interfaceDeclaration(mods, dc); |
aoqi@0 | 3186 | } else if (allowEnums) { |
aoqi@0 | 3187 | if (token.kind == ENUM) { |
aoqi@0 | 3188 | return enumDeclaration(mods, dc); |
aoqi@0 | 3189 | } else { |
aoqi@0 | 3190 | int pos = token.pos; |
aoqi@0 | 3191 | List<JCTree> errs; |
aoqi@0 | 3192 | if (LAX_IDENTIFIER.accepts(token.kind)) { |
aoqi@0 | 3193 | errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident()))); |
aoqi@0 | 3194 | setErrorEndPos(token.pos); |
aoqi@0 | 3195 | } else { |
aoqi@0 | 3196 | errs = List.<JCTree>of(mods); |
aoqi@0 | 3197 | } |
aoqi@0 | 3198 | return toP(F.Exec(syntaxError(pos, errs, "expected3", |
aoqi@0 | 3199 | CLASS, INTERFACE, ENUM))); |
aoqi@0 | 3200 | } |
aoqi@0 | 3201 | } else { |
aoqi@0 | 3202 | if (token.kind == ENUM) { |
aoqi@0 | 3203 | error(token.pos, "enums.not.supported.in.source", source.name); |
aoqi@0 | 3204 | allowEnums = true; |
aoqi@0 | 3205 | return enumDeclaration(mods, dc); |
aoqi@0 | 3206 | } |
aoqi@0 | 3207 | int pos = token.pos; |
aoqi@0 | 3208 | List<JCTree> errs; |
aoqi@0 | 3209 | if (LAX_IDENTIFIER.accepts(token.kind)) { |
aoqi@0 | 3210 | errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident()))); |
aoqi@0 | 3211 | setErrorEndPos(token.pos); |
aoqi@0 | 3212 | } else { |
aoqi@0 | 3213 | errs = List.<JCTree>of(mods); |
aoqi@0 | 3214 | } |
aoqi@0 | 3215 | return toP(F.Exec(syntaxError(pos, errs, "expected2", |
aoqi@0 | 3216 | CLASS, INTERFACE))); |
aoqi@0 | 3217 | } |
aoqi@0 | 3218 | } |
aoqi@0 | 3219 | |
aoqi@0 | 3220 | /** ClassDeclaration = CLASS Ident TypeParametersOpt [EXTENDS Type] |
aoqi@0 | 3221 | * [IMPLEMENTS TypeList] ClassBody |
aoqi@0 | 3222 | * @param mods The modifiers starting the class declaration |
aoqi@0 | 3223 | * @param dc The documentation comment for the class, or null. |
aoqi@0 | 3224 | */ |
aoqi@0 | 3225 | protected JCClassDecl classDeclaration(JCModifiers mods, Comment dc) { |
aoqi@0 | 3226 | int pos = token.pos; |
aoqi@0 | 3227 | accept(CLASS); |
aoqi@0 | 3228 | Name name = ident(); |
aoqi@0 | 3229 | |
aoqi@0 | 3230 | List<JCTypeParameter> typarams = typeParametersOpt(); |
aoqi@0 | 3231 | |
aoqi@0 | 3232 | JCExpression extending = null; |
aoqi@0 | 3233 | if (token.kind == EXTENDS) { |
aoqi@0 | 3234 | nextToken(); |
aoqi@0 | 3235 | extending = parseType(); |
aoqi@0 | 3236 | } |
aoqi@0 | 3237 | List<JCExpression> implementing = List.nil(); |
aoqi@0 | 3238 | if (token.kind == IMPLEMENTS) { |
aoqi@0 | 3239 | nextToken(); |
aoqi@0 | 3240 | implementing = typeList(); |
aoqi@0 | 3241 | } |
aoqi@0 | 3242 | List<JCTree> defs = classOrInterfaceBody(name, false); |
aoqi@0 | 3243 | JCClassDecl result = toP(F.at(pos).ClassDef( |
aoqi@0 | 3244 | mods, name, typarams, extending, implementing, defs)); |
aoqi@0 | 3245 | attach(result, dc); |
aoqi@0 | 3246 | return result; |
aoqi@0 | 3247 | } |
aoqi@0 | 3248 | |
aoqi@0 | 3249 | /** InterfaceDeclaration = INTERFACE Ident TypeParametersOpt |
aoqi@0 | 3250 | * [EXTENDS TypeList] InterfaceBody |
aoqi@0 | 3251 | * @param mods The modifiers starting the interface declaration |
aoqi@0 | 3252 | * @param dc The documentation comment for the interface, or null. |
aoqi@0 | 3253 | */ |
aoqi@0 | 3254 | protected JCClassDecl interfaceDeclaration(JCModifiers mods, Comment dc) { |
aoqi@0 | 3255 | int pos = token.pos; |
aoqi@0 | 3256 | accept(INTERFACE); |
aoqi@0 | 3257 | Name name = ident(); |
aoqi@0 | 3258 | |
aoqi@0 | 3259 | List<JCTypeParameter> typarams = typeParametersOpt(); |
aoqi@0 | 3260 | |
aoqi@0 | 3261 | List<JCExpression> extending = List.nil(); |
aoqi@0 | 3262 | if (token.kind == EXTENDS) { |
aoqi@0 | 3263 | nextToken(); |
aoqi@0 | 3264 | extending = typeList(); |
aoqi@0 | 3265 | } |
aoqi@0 | 3266 | List<JCTree> defs = classOrInterfaceBody(name, true); |
aoqi@0 | 3267 | JCClassDecl result = toP(F.at(pos).ClassDef( |
aoqi@0 | 3268 | mods, name, typarams, null, extending, defs)); |
aoqi@0 | 3269 | attach(result, dc); |
aoqi@0 | 3270 | return result; |
aoqi@0 | 3271 | } |
aoqi@0 | 3272 | |
aoqi@0 | 3273 | /** EnumDeclaration = ENUM Ident [IMPLEMENTS TypeList] EnumBody |
aoqi@0 | 3274 | * @param mods The modifiers starting the enum declaration |
aoqi@0 | 3275 | * @param dc The documentation comment for the enum, or null. |
aoqi@0 | 3276 | */ |
aoqi@0 | 3277 | protected JCClassDecl enumDeclaration(JCModifiers mods, Comment dc) { |
aoqi@0 | 3278 | int pos = token.pos; |
aoqi@0 | 3279 | accept(ENUM); |
aoqi@0 | 3280 | Name name = ident(); |
aoqi@0 | 3281 | |
aoqi@0 | 3282 | List<JCExpression> implementing = List.nil(); |
aoqi@0 | 3283 | if (token.kind == IMPLEMENTS) { |
aoqi@0 | 3284 | nextToken(); |
aoqi@0 | 3285 | implementing = typeList(); |
aoqi@0 | 3286 | } |
aoqi@0 | 3287 | |
aoqi@0 | 3288 | List<JCTree> defs = enumBody(name); |
aoqi@0 | 3289 | mods.flags |= Flags.ENUM; |
aoqi@0 | 3290 | JCClassDecl result = toP(F.at(pos). |
aoqi@0 | 3291 | ClassDef(mods, name, List.<JCTypeParameter>nil(), |
aoqi@0 | 3292 | null, implementing, defs)); |
aoqi@0 | 3293 | attach(result, dc); |
aoqi@0 | 3294 | return result; |
aoqi@0 | 3295 | } |
aoqi@0 | 3296 | |
aoqi@0 | 3297 | /** EnumBody = "{" { EnumeratorDeclarationList } [","] |
aoqi@0 | 3298 | * [ ";" {ClassBodyDeclaration} ] "}" |
aoqi@0 | 3299 | */ |
aoqi@0 | 3300 | List<JCTree> enumBody(Name enumName) { |
aoqi@0 | 3301 | accept(LBRACE); |
aoqi@0 | 3302 | ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); |
aoqi@0 | 3303 | if (token.kind == COMMA) { |
aoqi@0 | 3304 | nextToken(); |
aoqi@0 | 3305 | } else if (token.kind != RBRACE && token.kind != SEMI) { |
aoqi@0 | 3306 | defs.append(enumeratorDeclaration(enumName)); |
aoqi@0 | 3307 | while (token.kind == COMMA) { |
aoqi@0 | 3308 | nextToken(); |
aoqi@0 | 3309 | if (token.kind == RBRACE || token.kind == SEMI) break; |
aoqi@0 | 3310 | defs.append(enumeratorDeclaration(enumName)); |
aoqi@0 | 3311 | } |
aoqi@0 | 3312 | if (token.kind != SEMI && token.kind != RBRACE) { |
aoqi@0 | 3313 | defs.append(syntaxError(token.pos, "expected3", |
aoqi@0 | 3314 | COMMA, RBRACE, SEMI)); |
aoqi@0 | 3315 | nextToken(); |
aoqi@0 | 3316 | } |
aoqi@0 | 3317 | } |
aoqi@0 | 3318 | if (token.kind == SEMI) { |
aoqi@0 | 3319 | nextToken(); |
aoqi@0 | 3320 | while (token.kind != RBRACE && token.kind != EOF) { |
aoqi@0 | 3321 | defs.appendList(classOrInterfaceBodyDeclaration(enumName, |
aoqi@0 | 3322 | false)); |
aoqi@0 | 3323 | if (token.pos <= endPosTable.errorEndPos) { |
aoqi@0 | 3324 | // error recovery |
aoqi@0 | 3325 | skip(false, true, true, false); |
aoqi@0 | 3326 | } |
aoqi@0 | 3327 | } |
aoqi@0 | 3328 | } |
aoqi@0 | 3329 | accept(RBRACE); |
aoqi@0 | 3330 | return defs.toList(); |
aoqi@0 | 3331 | } |
aoqi@0 | 3332 | |
aoqi@0 | 3333 | /** EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ] |
aoqi@0 | 3334 | */ |
aoqi@0 | 3335 | JCTree enumeratorDeclaration(Name enumName) { |
aoqi@0 | 3336 | Comment dc = token.comment(CommentStyle.JAVADOC); |
aoqi@0 | 3337 | int flags = Flags.PUBLIC|Flags.STATIC|Flags.FINAL|Flags.ENUM; |
aoqi@0 | 3338 | if (token.deprecatedFlag()) { |
aoqi@0 | 3339 | flags |= Flags.DEPRECATED; |
aoqi@0 | 3340 | } |
aoqi@0 | 3341 | int pos = token.pos; |
aoqi@0 | 3342 | List<JCAnnotation> annotations = annotationsOpt(Tag.ANNOTATION); |
aoqi@0 | 3343 | JCModifiers mods = F.at(annotations.isEmpty() ? Position.NOPOS : pos).Modifiers(flags, annotations); |
aoqi@0 | 3344 | List<JCExpression> typeArgs = typeArgumentsOpt(); |
aoqi@0 | 3345 | int identPos = token.pos; |
aoqi@0 | 3346 | Name name = ident(); |
aoqi@0 | 3347 | int createPos = token.pos; |
aoqi@0 | 3348 | List<JCExpression> args = (token.kind == LPAREN) |
aoqi@0 | 3349 | ? arguments() : List.<JCExpression>nil(); |
aoqi@0 | 3350 | JCClassDecl body = null; |
aoqi@0 | 3351 | if (token.kind == LBRACE) { |
aoqi@0 | 3352 | JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM | Flags.STATIC); |
aoqi@0 | 3353 | List<JCTree> defs = classOrInterfaceBody(names.empty, false); |
aoqi@0 | 3354 | body = toP(F.at(identPos).AnonymousClassDef(mods1, defs)); |
aoqi@0 | 3355 | } |
aoqi@0 | 3356 | if (args.isEmpty() && body == null) |
aoqi@0 | 3357 | createPos = identPos; |
aoqi@0 | 3358 | JCIdent ident = F.at(identPos).Ident(enumName); |
aoqi@0 | 3359 | JCNewClass create = F.at(createPos).NewClass(null, typeArgs, ident, args, body); |
aoqi@0 | 3360 | if (createPos != identPos) |
aoqi@0 | 3361 | storeEnd(create, S.prevToken().endPos); |
aoqi@0 | 3362 | ident = F.at(identPos).Ident(enumName); |
aoqi@0 | 3363 | JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create)); |
aoqi@0 | 3364 | attach(result, dc); |
aoqi@0 | 3365 | return result; |
aoqi@0 | 3366 | } |
aoqi@0 | 3367 | |
aoqi@0 | 3368 | /** TypeList = Type {"," Type} |
aoqi@0 | 3369 | */ |
aoqi@0 | 3370 | List<JCExpression> typeList() { |
aoqi@0 | 3371 | ListBuffer<JCExpression> ts = new ListBuffer<JCExpression>(); |
aoqi@0 | 3372 | ts.append(parseType()); |
aoqi@0 | 3373 | while (token.kind == COMMA) { |
aoqi@0 | 3374 | nextToken(); |
aoqi@0 | 3375 | ts.append(parseType()); |
aoqi@0 | 3376 | } |
aoqi@0 | 3377 | return ts.toList(); |
aoqi@0 | 3378 | } |
aoqi@0 | 3379 | |
aoqi@0 | 3380 | /** ClassBody = "{" {ClassBodyDeclaration} "}" |
aoqi@0 | 3381 | * InterfaceBody = "{" {InterfaceBodyDeclaration} "}" |
aoqi@0 | 3382 | */ |
aoqi@0 | 3383 | List<JCTree> classOrInterfaceBody(Name className, boolean isInterface) { |
aoqi@0 | 3384 | accept(LBRACE); |
aoqi@0 | 3385 | if (token.pos <= endPosTable.errorEndPos) { |
aoqi@0 | 3386 | // error recovery |
aoqi@0 | 3387 | skip(false, true, false, false); |
aoqi@0 | 3388 | if (token.kind == LBRACE) |
aoqi@0 | 3389 | nextToken(); |
aoqi@0 | 3390 | } |
aoqi@0 | 3391 | ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); |
aoqi@0 | 3392 | while (token.kind != RBRACE && token.kind != EOF) { |
aoqi@0 | 3393 | defs.appendList(classOrInterfaceBodyDeclaration(className, isInterface)); |
aoqi@0 | 3394 | if (token.pos <= endPosTable.errorEndPos) { |
aoqi@0 | 3395 | // error recovery |
aoqi@0 | 3396 | skip(false, true, true, false); |
aoqi@0 | 3397 | } |
aoqi@0 | 3398 | } |
aoqi@0 | 3399 | accept(RBRACE); |
aoqi@0 | 3400 | return defs.toList(); |
aoqi@0 | 3401 | } |
aoqi@0 | 3402 | |
aoqi@0 | 3403 | /** ClassBodyDeclaration = |
aoqi@0 | 3404 | * ";" |
aoqi@0 | 3405 | * | [STATIC] Block |
aoqi@0 | 3406 | * | ModifiersOpt |
aoqi@0 | 3407 | * ( Type Ident |
aoqi@0 | 3408 | * ( VariableDeclaratorsRest ";" | MethodDeclaratorRest ) |
aoqi@0 | 3409 | * | VOID Ident VoidMethodDeclaratorRest |
aoqi@0 | 3410 | * | TypeParameters [Annotations] |
aoqi@0 | 3411 | * ( Type Ident MethodDeclaratorRest |
aoqi@0 | 3412 | * | VOID Ident VoidMethodDeclaratorRest |
aoqi@0 | 3413 | * ) |
aoqi@0 | 3414 | * | Ident ConstructorDeclaratorRest |
aoqi@0 | 3415 | * | TypeParameters Ident ConstructorDeclaratorRest |
aoqi@0 | 3416 | * | ClassOrInterfaceOrEnumDeclaration |
aoqi@0 | 3417 | * ) |
aoqi@0 | 3418 | * InterfaceBodyDeclaration = |
aoqi@0 | 3419 | * ";" |
aoqi@0 | 3420 | * | ModifiersOpt |
aoqi@0 | 3421 | * ( Type Ident |
aoqi@0 | 3422 | * ( ConstantDeclaratorsRest ";" | MethodDeclaratorRest ) |
aoqi@0 | 3423 | * | VOID Ident MethodDeclaratorRest |
aoqi@0 | 3424 | * | TypeParameters [Annotations] |
aoqi@0 | 3425 | * ( Type Ident MethodDeclaratorRest |
aoqi@0 | 3426 | * | VOID Ident VoidMethodDeclaratorRest |
aoqi@0 | 3427 | * ) |
aoqi@0 | 3428 | * | ClassOrInterfaceOrEnumDeclaration |
aoqi@0 | 3429 | * ) |
aoqi@0 | 3430 | * |
aoqi@0 | 3431 | */ |
aoqi@0 | 3432 | protected List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) { |
aoqi@0 | 3433 | if (token.kind == SEMI) { |
aoqi@0 | 3434 | nextToken(); |
aoqi@0 | 3435 | return List.<JCTree>nil(); |
aoqi@0 | 3436 | } else { |
aoqi@0 | 3437 | Comment dc = token.comment(CommentStyle.JAVADOC); |
aoqi@0 | 3438 | int pos = token.pos; |
aoqi@0 | 3439 | JCModifiers mods = modifiersOpt(); |
aoqi@0 | 3440 | if (token.kind == CLASS || |
aoqi@0 | 3441 | token.kind == INTERFACE || |
aoqi@0 | 3442 | allowEnums && token.kind == ENUM) { |
aoqi@0 | 3443 | return List.<JCTree>of(classOrInterfaceOrEnumDeclaration(mods, dc)); |
aoqi@0 | 3444 | } else if (token.kind == LBRACE && !isInterface && |
aoqi@0 | 3445 | (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 && |
aoqi@0 | 3446 | mods.annotations.isEmpty()) { |
aoqi@0 | 3447 | return List.<JCTree>of(block(pos, mods.flags)); |
aoqi@0 | 3448 | } else { |
aoqi@0 | 3449 | pos = token.pos; |
aoqi@0 | 3450 | List<JCTypeParameter> typarams = typeParametersOpt(); |
aoqi@0 | 3451 | // if there are type parameters but no modifiers, save the start |
aoqi@0 | 3452 | // position of the method in the modifiers. |
aoqi@0 | 3453 | if (typarams.nonEmpty() && mods.pos == Position.NOPOS) { |
aoqi@0 | 3454 | mods.pos = pos; |
aoqi@0 | 3455 | storeEnd(mods, pos); |
aoqi@0 | 3456 | } |
aoqi@0 | 3457 | List<JCAnnotation> annosAfterParams = annotationsOpt(Tag.ANNOTATION); |
aoqi@0 | 3458 | |
aoqi@0 | 3459 | if (annosAfterParams.nonEmpty()) { |
aoqi@0 | 3460 | checkAnnotationsAfterTypeParams(annosAfterParams.head.pos); |
aoqi@0 | 3461 | mods.annotations = mods.annotations.appendList(annosAfterParams); |
aoqi@0 | 3462 | if (mods.pos == Position.NOPOS) |
aoqi@0 | 3463 | mods.pos = mods.annotations.head.pos; |
aoqi@0 | 3464 | } |
aoqi@0 | 3465 | |
aoqi@0 | 3466 | Token tk = token; |
aoqi@0 | 3467 | pos = token.pos; |
aoqi@0 | 3468 | JCExpression type; |
aoqi@0 | 3469 | boolean isVoid = token.kind == VOID; |
aoqi@0 | 3470 | if (isVoid) { |
aoqi@0 | 3471 | type = to(F.at(pos).TypeIdent(TypeTag.VOID)); |
aoqi@0 | 3472 | nextToken(); |
aoqi@0 | 3473 | } else { |
aoqi@0 | 3474 | // method returns types are un-annotated types |
aoqi@0 | 3475 | type = unannotatedType(); |
aoqi@0 | 3476 | } |
aoqi@0 | 3477 | if (token.kind == LPAREN && !isInterface && type.hasTag(IDENT)) { |
aoqi@0 | 3478 | if (isInterface || tk.name() != className) |
aoqi@0 | 3479 | error(pos, "invalid.meth.decl.ret.type.req"); |
aoqi@0 | 3480 | else if (annosAfterParams.nonEmpty()) |
aoqi@0 | 3481 | illegal(annosAfterParams.head.pos); |
aoqi@0 | 3482 | return List.of(methodDeclaratorRest( |
aoqi@0 | 3483 | pos, mods, null, names.init, typarams, |
aoqi@0 | 3484 | isInterface, true, dc)); |
aoqi@0 | 3485 | } else { |
aoqi@0 | 3486 | pos = token.pos; |
aoqi@0 | 3487 | Name name = ident(); |
aoqi@0 | 3488 | if (token.kind == LPAREN) { |
aoqi@0 | 3489 | return List.of(methodDeclaratorRest( |
aoqi@0 | 3490 | pos, mods, type, name, typarams, |
aoqi@0 | 3491 | isInterface, isVoid, dc)); |
aoqi@0 | 3492 | } else if (!isVoid && typarams.isEmpty()) { |
aoqi@0 | 3493 | List<JCTree> defs = |
aoqi@0 | 3494 | variableDeclaratorsRest(pos, mods, type, name, isInterface, dc, |
aoqi@0 | 3495 | new ListBuffer<JCTree>()).toList(); |
aoqi@0 | 3496 | storeEnd(defs.last(), token.endPos); |
aoqi@0 | 3497 | accept(SEMI); |
aoqi@0 | 3498 | return defs; |
aoqi@0 | 3499 | } else { |
aoqi@0 | 3500 | pos = token.pos; |
aoqi@0 | 3501 | List<JCTree> err = isVoid |
aoqi@0 | 3502 | ? List.<JCTree>of(toP(F.at(pos).MethodDef(mods, name, type, typarams, |
aoqi@0 | 3503 | List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null))) |
aoqi@0 | 3504 | : null; |
aoqi@0 | 3505 | return List.<JCTree>of(syntaxError(token.pos, err, "expected", LPAREN)); |
aoqi@0 | 3506 | } |
aoqi@0 | 3507 | } |
aoqi@0 | 3508 | } |
aoqi@0 | 3509 | } |
aoqi@0 | 3510 | } |
aoqi@0 | 3511 | |
aoqi@0 | 3512 | /** MethodDeclaratorRest = |
aoqi@0 | 3513 | * FormalParameters BracketsOpt [THROWS TypeList] ( MethodBody | [DEFAULT AnnotationValue] ";") |
aoqi@0 | 3514 | * VoidMethodDeclaratorRest = |
aoqi@0 | 3515 | * FormalParameters [THROWS TypeList] ( MethodBody | ";") |
aoqi@0 | 3516 | * ConstructorDeclaratorRest = |
aoqi@0 | 3517 | * "(" FormalParameterListOpt ")" [THROWS TypeList] MethodBody |
aoqi@0 | 3518 | */ |
aoqi@0 | 3519 | protected JCTree methodDeclaratorRest(int pos, |
aoqi@0 | 3520 | JCModifiers mods, |
aoqi@0 | 3521 | JCExpression type, |
aoqi@0 | 3522 | Name name, |
aoqi@0 | 3523 | List<JCTypeParameter> typarams, |
aoqi@0 | 3524 | boolean isInterface, boolean isVoid, |
aoqi@0 | 3525 | Comment dc) { |
aoqi@0 | 3526 | if (isInterface && (mods.flags & Flags.STATIC) != 0) { |
aoqi@0 | 3527 | checkStaticInterfaceMethods(); |
aoqi@0 | 3528 | } |
aoqi@0 | 3529 | JCVariableDecl prevReceiverParam = this.receiverParam; |
aoqi@0 | 3530 | try { |
aoqi@0 | 3531 | this.receiverParam = null; |
aoqi@0 | 3532 | // Parsing formalParameters sets the receiverParam, if present |
aoqi@0 | 3533 | List<JCVariableDecl> params = formalParameters(); |
aoqi@0 | 3534 | if (!isVoid) type = bracketsOpt(type); |
aoqi@0 | 3535 | List<JCExpression> thrown = List.nil(); |
aoqi@0 | 3536 | if (token.kind == THROWS) { |
aoqi@0 | 3537 | nextToken(); |
aoqi@0 | 3538 | thrown = qualidentList(); |
aoqi@0 | 3539 | } |
aoqi@0 | 3540 | JCBlock body = null; |
aoqi@0 | 3541 | JCExpression defaultValue; |
aoqi@0 | 3542 | if (token.kind == LBRACE) { |
aoqi@0 | 3543 | body = block(); |
aoqi@0 | 3544 | defaultValue = null; |
aoqi@0 | 3545 | } else { |
aoqi@0 | 3546 | if (token.kind == DEFAULT) { |
aoqi@0 | 3547 | accept(DEFAULT); |
aoqi@0 | 3548 | defaultValue = annotationValue(); |
aoqi@0 | 3549 | } else { |
aoqi@0 | 3550 | defaultValue = null; |
aoqi@0 | 3551 | } |
aoqi@0 | 3552 | accept(SEMI); |
aoqi@0 | 3553 | if (token.pos <= endPosTable.errorEndPos) { |
aoqi@0 | 3554 | // error recovery |
aoqi@0 | 3555 | skip(false, true, false, false); |
aoqi@0 | 3556 | if (token.kind == LBRACE) { |
aoqi@0 | 3557 | body = block(); |
aoqi@0 | 3558 | } |
aoqi@0 | 3559 | } |
aoqi@0 | 3560 | } |
aoqi@0 | 3561 | |
aoqi@0 | 3562 | JCMethodDecl result = |
aoqi@0 | 3563 | toP(F.at(pos).MethodDef(mods, name, type, typarams, |
aoqi@0 | 3564 | receiverParam, params, thrown, |
aoqi@0 | 3565 | body, defaultValue)); |
aoqi@0 | 3566 | attach(result, dc); |
aoqi@0 | 3567 | return result; |
aoqi@0 | 3568 | } finally { |
aoqi@0 | 3569 | this.receiverParam = prevReceiverParam; |
aoqi@0 | 3570 | } |
aoqi@0 | 3571 | } |
aoqi@0 | 3572 | |
aoqi@0 | 3573 | /** QualidentList = [Annotations] Qualident {"," [Annotations] Qualident} |
aoqi@0 | 3574 | */ |
aoqi@0 | 3575 | List<JCExpression> qualidentList() { |
aoqi@0 | 3576 | ListBuffer<JCExpression> ts = new ListBuffer<JCExpression>(); |
aoqi@0 | 3577 | |
aoqi@0 | 3578 | List<JCAnnotation> typeAnnos = typeAnnotationsOpt(); |
aoqi@0 | 3579 | JCExpression qi = qualident(true); |
aoqi@0 | 3580 | if (!typeAnnos.isEmpty()) { |
aoqi@0 | 3581 | JCExpression at = insertAnnotationsToMostInner(qi, typeAnnos, false); |
aoqi@0 | 3582 | ts.append(at); |
aoqi@0 | 3583 | } else { |
aoqi@0 | 3584 | ts.append(qi); |
aoqi@0 | 3585 | } |
aoqi@0 | 3586 | while (token.kind == COMMA) { |
aoqi@0 | 3587 | nextToken(); |
aoqi@0 | 3588 | |
aoqi@0 | 3589 | typeAnnos = typeAnnotationsOpt(); |
aoqi@0 | 3590 | qi = qualident(true); |
aoqi@0 | 3591 | if (!typeAnnos.isEmpty()) { |
aoqi@0 | 3592 | JCExpression at = insertAnnotationsToMostInner(qi, typeAnnos, false); |
aoqi@0 | 3593 | ts.append(at); |
aoqi@0 | 3594 | } else { |
aoqi@0 | 3595 | ts.append(qi); |
aoqi@0 | 3596 | } |
aoqi@0 | 3597 | } |
aoqi@0 | 3598 | return ts.toList(); |
aoqi@0 | 3599 | } |
aoqi@0 | 3600 | |
aoqi@0 | 3601 | /** |
aoqi@0 | 3602 | * {@literal |
aoqi@0 | 3603 | * TypeParametersOpt = ["<" TypeParameter {"," TypeParameter} ">"] |
aoqi@0 | 3604 | * } |
aoqi@0 | 3605 | */ |
aoqi@0 | 3606 | List<JCTypeParameter> typeParametersOpt() { |
aoqi@0 | 3607 | if (token.kind == LT) { |
aoqi@0 | 3608 | checkGenerics(); |
aoqi@0 | 3609 | ListBuffer<JCTypeParameter> typarams = new ListBuffer<JCTypeParameter>(); |
aoqi@0 | 3610 | nextToken(); |
aoqi@0 | 3611 | typarams.append(typeParameter()); |
aoqi@0 | 3612 | while (token.kind == COMMA) { |
aoqi@0 | 3613 | nextToken(); |
aoqi@0 | 3614 | typarams.append(typeParameter()); |
aoqi@0 | 3615 | } |
aoqi@0 | 3616 | accept(GT); |
aoqi@0 | 3617 | return typarams.toList(); |
aoqi@0 | 3618 | } else { |
aoqi@0 | 3619 | return List.nil(); |
aoqi@0 | 3620 | } |
aoqi@0 | 3621 | } |
aoqi@0 | 3622 | |
aoqi@0 | 3623 | /** |
aoqi@0 | 3624 | * {@literal |
aoqi@0 | 3625 | * TypeParameter = [Annotations] TypeVariable [TypeParameterBound] |
aoqi@0 | 3626 | * TypeParameterBound = EXTENDS Type {"&" Type} |
aoqi@0 | 3627 | * TypeVariable = Ident |
aoqi@0 | 3628 | * } |
aoqi@0 | 3629 | */ |
aoqi@0 | 3630 | JCTypeParameter typeParameter() { |
aoqi@0 | 3631 | int pos = token.pos; |
aoqi@0 | 3632 | List<JCAnnotation> annos = typeAnnotationsOpt(); |
aoqi@0 | 3633 | Name name = ident(); |
aoqi@0 | 3634 | ListBuffer<JCExpression> bounds = new ListBuffer<JCExpression>(); |
aoqi@0 | 3635 | if (token.kind == EXTENDS) { |
aoqi@0 | 3636 | nextToken(); |
aoqi@0 | 3637 | bounds.append(parseType()); |
aoqi@0 | 3638 | while (token.kind == AMP) { |
aoqi@0 | 3639 | nextToken(); |
aoqi@0 | 3640 | bounds.append(parseType()); |
aoqi@0 | 3641 | } |
aoqi@0 | 3642 | } |
aoqi@0 | 3643 | return toP(F.at(pos).TypeParameter(name, bounds.toList(), annos)); |
aoqi@0 | 3644 | } |
aoqi@0 | 3645 | |
aoqi@0 | 3646 | /** FormalParameters = "(" [ FormalParameterList ] ")" |
aoqi@0 | 3647 | * FormalParameterList = [ FormalParameterListNovarargs , ] LastFormalParameter |
aoqi@0 | 3648 | * FormalParameterListNovarargs = [ FormalParameterListNovarargs , ] FormalParameter |
aoqi@0 | 3649 | */ |
aoqi@0 | 3650 | List<JCVariableDecl> formalParameters() { |
aoqi@0 | 3651 | return formalParameters(false); |
aoqi@0 | 3652 | } |
aoqi@0 | 3653 | List<JCVariableDecl> formalParameters(boolean lambdaParameters) { |
aoqi@0 | 3654 | ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>(); |
aoqi@0 | 3655 | JCVariableDecl lastParam; |
aoqi@0 | 3656 | accept(LPAREN); |
aoqi@0 | 3657 | if (token.kind != RPAREN) { |
aoqi@0 | 3658 | this.allowThisIdent = true; |
aoqi@0 | 3659 | lastParam = formalParameter(lambdaParameters); |
aoqi@0 | 3660 | if (lastParam.nameexpr != null) { |
aoqi@0 | 3661 | this.receiverParam = lastParam; |
aoqi@0 | 3662 | } else { |
aoqi@0 | 3663 | params.append(lastParam); |
aoqi@0 | 3664 | } |
aoqi@0 | 3665 | this.allowThisIdent = false; |
aoqi@0 | 3666 | while ((lastParam.mods.flags & Flags.VARARGS) == 0 && token.kind == COMMA) { |
aoqi@0 | 3667 | nextToken(); |
aoqi@0 | 3668 | params.append(lastParam = formalParameter(lambdaParameters)); |
aoqi@0 | 3669 | } |
aoqi@0 | 3670 | } |
aoqi@0 | 3671 | accept(RPAREN); |
aoqi@0 | 3672 | return params.toList(); |
aoqi@0 | 3673 | } |
aoqi@0 | 3674 | |
aoqi@0 | 3675 | List<JCVariableDecl> implicitParameters(boolean hasParens) { |
aoqi@0 | 3676 | if (hasParens) { |
aoqi@0 | 3677 | accept(LPAREN); |
aoqi@0 | 3678 | } |
aoqi@0 | 3679 | ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>(); |
aoqi@0 | 3680 | if (token.kind != RPAREN && token.kind != ARROW) { |
aoqi@0 | 3681 | params.append(implicitParameter()); |
aoqi@0 | 3682 | while (token.kind == COMMA) { |
aoqi@0 | 3683 | nextToken(); |
aoqi@0 | 3684 | params.append(implicitParameter()); |
aoqi@0 | 3685 | } |
aoqi@0 | 3686 | } |
aoqi@0 | 3687 | if (hasParens) { |
aoqi@0 | 3688 | accept(RPAREN); |
aoqi@0 | 3689 | } |
aoqi@0 | 3690 | return params.toList(); |
aoqi@0 | 3691 | } |
aoqi@0 | 3692 | |
aoqi@0 | 3693 | JCModifiers optFinal(long flags) { |
aoqi@0 | 3694 | JCModifiers mods = modifiersOpt(); |
aoqi@0 | 3695 | checkNoMods(mods.flags & ~(Flags.FINAL | Flags.DEPRECATED)); |
aoqi@0 | 3696 | mods.flags |= flags; |
aoqi@0 | 3697 | return mods; |
aoqi@0 | 3698 | } |
aoqi@0 | 3699 | |
aoqi@0 | 3700 | /** |
aoqi@0 | 3701 | * Inserts the annotations (and possibly a new array level) |
aoqi@0 | 3702 | * to the left-most type in an array or nested type. |
aoqi@0 | 3703 | * |
aoqi@0 | 3704 | * When parsing a type like {@code @B Outer.Inner @A []}, the |
aoqi@0 | 3705 | * {@code @A} annotation should target the array itself, while |
aoqi@0 | 3706 | * {@code @B} targets the nested type {@code Outer}. |
aoqi@0 | 3707 | * |
aoqi@0 | 3708 | * Currently the parser parses the annotation first, then |
aoqi@0 | 3709 | * the array, and then inserts the annotation to the left-most |
aoqi@0 | 3710 | * nested type. |
aoqi@0 | 3711 | * |
aoqi@0 | 3712 | * When {@code createNewLevel} is true, then a new array |
aoqi@0 | 3713 | * level is inserted as the most inner type, and have the |
aoqi@0 | 3714 | * annotations target it. This is useful in the case of |
aoqi@0 | 3715 | * varargs, e.g. {@code String @A [] @B ...}, as the parser |
aoqi@0 | 3716 | * first parses the type {@code String @A []} then inserts |
aoqi@0 | 3717 | * a new array level with {@code @B} annotation. |
aoqi@0 | 3718 | */ |
aoqi@0 | 3719 | private JCExpression insertAnnotationsToMostInner( |
aoqi@0 | 3720 | JCExpression type, List<JCAnnotation> annos, |
aoqi@0 | 3721 | boolean createNewLevel) { |
aoqi@0 | 3722 | int origEndPos = getEndPos(type); |
aoqi@0 | 3723 | JCExpression mostInnerType = type; |
aoqi@0 | 3724 | JCArrayTypeTree mostInnerArrayType = null; |
aoqi@0 | 3725 | while (TreeInfo.typeIn(mostInnerType).hasTag(TYPEARRAY)) { |
aoqi@0 | 3726 | mostInnerArrayType = (JCArrayTypeTree) TreeInfo.typeIn(mostInnerType); |
aoqi@0 | 3727 | mostInnerType = mostInnerArrayType.elemtype; |
aoqi@0 | 3728 | } |
aoqi@0 | 3729 | |
aoqi@0 | 3730 | if (createNewLevel) { |
aoqi@0 | 3731 | mostInnerType = to(F.at(token.pos).TypeArray(mostInnerType)); |
aoqi@0 | 3732 | } |
aoqi@0 | 3733 | |
aoqi@0 | 3734 | JCExpression mostInnerTypeToReturn = mostInnerType; |
aoqi@0 | 3735 | if (annos.nonEmpty()) { |
aoqi@0 | 3736 | JCExpression lastToModify = mostInnerType; |
aoqi@0 | 3737 | |
aoqi@0 | 3738 | while (TreeInfo.typeIn(mostInnerType).hasTag(SELECT) || |
aoqi@0 | 3739 | TreeInfo.typeIn(mostInnerType).hasTag(TYPEAPPLY)) { |
aoqi@0 | 3740 | while (TreeInfo.typeIn(mostInnerType).hasTag(SELECT)) { |
aoqi@0 | 3741 | lastToModify = mostInnerType; |
aoqi@0 | 3742 | mostInnerType = ((JCFieldAccess) TreeInfo.typeIn(mostInnerType)).getExpression(); |
aoqi@0 | 3743 | } |
aoqi@0 | 3744 | while (TreeInfo.typeIn(mostInnerType).hasTag(TYPEAPPLY)) { |
aoqi@0 | 3745 | lastToModify = mostInnerType; |
aoqi@0 | 3746 | mostInnerType = ((JCTypeApply) TreeInfo.typeIn(mostInnerType)).clazz; |
aoqi@0 | 3747 | } |
aoqi@0 | 3748 | } |
aoqi@0 | 3749 | |
aoqi@0 | 3750 | mostInnerType = F.at(annos.head.pos).AnnotatedType(annos, mostInnerType); |
aoqi@0 | 3751 | |
aoqi@0 | 3752 | if (TreeInfo.typeIn(lastToModify).hasTag(TYPEAPPLY)) { |
aoqi@0 | 3753 | ((JCTypeApply) TreeInfo.typeIn(lastToModify)).clazz = mostInnerType; |
aoqi@0 | 3754 | } else if (TreeInfo.typeIn(lastToModify).hasTag(SELECT)) { |
aoqi@0 | 3755 | ((JCFieldAccess) TreeInfo.typeIn(lastToModify)).selected = mostInnerType; |
aoqi@0 | 3756 | } else { |
aoqi@0 | 3757 | // We never saw a SELECT or TYPEAPPLY, return the annotated type. |
aoqi@0 | 3758 | mostInnerTypeToReturn = mostInnerType; |
aoqi@0 | 3759 | } |
aoqi@0 | 3760 | } |
aoqi@0 | 3761 | |
aoqi@0 | 3762 | if (mostInnerArrayType == null) { |
aoqi@0 | 3763 | return mostInnerTypeToReturn; |
aoqi@0 | 3764 | } else { |
aoqi@0 | 3765 | mostInnerArrayType.elemtype = mostInnerTypeToReturn; |
aoqi@0 | 3766 | storeEnd(type, origEndPos); |
aoqi@0 | 3767 | return type; |
aoqi@0 | 3768 | } |
aoqi@0 | 3769 | } |
aoqi@0 | 3770 | |
aoqi@0 | 3771 | /** FormalParameter = { FINAL | '@' Annotation } Type VariableDeclaratorId |
aoqi@0 | 3772 | * LastFormalParameter = { FINAL | '@' Annotation } Type '...' Ident | FormalParameter |
aoqi@0 | 3773 | */ |
aoqi@0 | 3774 | protected JCVariableDecl formalParameter() { |
aoqi@0 | 3775 | return formalParameter(false); |
aoqi@0 | 3776 | } |
aoqi@0 | 3777 | protected JCVariableDecl formalParameter(boolean lambdaParameter) { |
aoqi@0 | 3778 | JCModifiers mods = optFinal(Flags.PARAMETER); |
aoqi@0 | 3779 | // need to distinguish between vararg annos and array annos |
aoqi@0 | 3780 | // look at typeAnnotationsPushedBack comment |
aoqi@0 | 3781 | this.permitTypeAnnotationsPushBack = true; |
aoqi@0 | 3782 | JCExpression type = parseType(); |
aoqi@0 | 3783 | this.permitTypeAnnotationsPushBack = false; |
aoqi@0 | 3784 | |
aoqi@0 | 3785 | if (token.kind == ELLIPSIS) { |
aoqi@0 | 3786 | List<JCAnnotation> varargsAnnos = typeAnnotationsPushedBack; |
aoqi@0 | 3787 | typeAnnotationsPushedBack = List.nil(); |
aoqi@0 | 3788 | checkVarargs(); |
aoqi@0 | 3789 | mods.flags |= Flags.VARARGS; |
aoqi@0 | 3790 | // insert var arg type annotations |
aoqi@0 | 3791 | type = insertAnnotationsToMostInner(type, varargsAnnos, true); |
aoqi@0 | 3792 | nextToken(); |
aoqi@0 | 3793 | } else { |
aoqi@0 | 3794 | // if not a var arg, then typeAnnotationsPushedBack should be null |
aoqi@0 | 3795 | if (typeAnnotationsPushedBack.nonEmpty()) { |
aoqi@0 | 3796 | reportSyntaxError(typeAnnotationsPushedBack.head.pos, |
aoqi@0 | 3797 | "illegal.start.of.type"); |
aoqi@0 | 3798 | } |
aoqi@0 | 3799 | typeAnnotationsPushedBack = List.nil(); |
aoqi@0 | 3800 | } |
aoqi@0 | 3801 | return variableDeclaratorId(mods, type, lambdaParameter); |
aoqi@0 | 3802 | } |
aoqi@0 | 3803 | |
aoqi@0 | 3804 | protected JCVariableDecl implicitParameter() { |
aoqi@0 | 3805 | JCModifiers mods = F.at(token.pos).Modifiers(Flags.PARAMETER); |
aoqi@0 | 3806 | return variableDeclaratorId(mods, null, true); |
aoqi@0 | 3807 | } |
aoqi@0 | 3808 | |
aoqi@0 | 3809 | /* ---------- auxiliary methods -------------- */ |
aoqi@0 | 3810 | |
aoqi@0 | 3811 | void error(int pos, String key, Object ... args) { |
aoqi@0 | 3812 | log.error(DiagnosticFlag.SYNTAX, pos, key, args); |
aoqi@0 | 3813 | } |
aoqi@0 | 3814 | |
aoqi@0 | 3815 | void error(DiagnosticPosition pos, String key, Object ... args) { |
aoqi@0 | 3816 | log.error(DiagnosticFlag.SYNTAX, pos, key, args); |
aoqi@0 | 3817 | } |
aoqi@0 | 3818 | |
aoqi@0 | 3819 | void warning(int pos, String key, Object ... args) { |
aoqi@0 | 3820 | log.warning(pos, key, args); |
aoqi@0 | 3821 | } |
aoqi@0 | 3822 | |
aoqi@0 | 3823 | /** Check that given tree is a legal expression statement. |
aoqi@0 | 3824 | */ |
aoqi@0 | 3825 | protected JCExpression checkExprStat(JCExpression t) { |
aoqi@0 | 3826 | if (!TreeInfo.isExpressionStatement(t)) { |
aoqi@0 | 3827 | JCExpression ret = F.at(t.pos).Erroneous(List.<JCTree>of(t)); |
aoqi@0 | 3828 | error(ret, "not.stmt"); |
aoqi@0 | 3829 | return ret; |
aoqi@0 | 3830 | } else { |
aoqi@0 | 3831 | return t; |
aoqi@0 | 3832 | } |
aoqi@0 | 3833 | } |
aoqi@0 | 3834 | |
aoqi@0 | 3835 | /** Return precedence of operator represented by token, |
aoqi@0 | 3836 | * -1 if token is not a binary operator. @see TreeInfo.opPrec |
aoqi@0 | 3837 | */ |
aoqi@0 | 3838 | static int prec(TokenKind token) { |
aoqi@0 | 3839 | JCTree.Tag oc = optag(token); |
aoqi@0 | 3840 | return (oc != NO_TAG) ? TreeInfo.opPrec(oc) : -1; |
aoqi@0 | 3841 | } |
aoqi@0 | 3842 | |
aoqi@0 | 3843 | /** |
aoqi@0 | 3844 | * Return the lesser of two positions, making allowance for either one |
aoqi@0 | 3845 | * being unset. |
aoqi@0 | 3846 | */ |
aoqi@0 | 3847 | static int earlier(int pos1, int pos2) { |
aoqi@0 | 3848 | if (pos1 == Position.NOPOS) |
aoqi@0 | 3849 | return pos2; |
aoqi@0 | 3850 | if (pos2 == Position.NOPOS) |
aoqi@0 | 3851 | return pos1; |
aoqi@0 | 3852 | return (pos1 < pos2 ? pos1 : pos2); |
aoqi@0 | 3853 | } |
aoqi@0 | 3854 | |
aoqi@0 | 3855 | /** Return operation tag of binary operator represented by token, |
aoqi@0 | 3856 | * No_TAG if token is not a binary operator. |
aoqi@0 | 3857 | */ |
aoqi@0 | 3858 | static JCTree.Tag optag(TokenKind token) { |
aoqi@0 | 3859 | switch (token) { |
aoqi@0 | 3860 | case BARBAR: |
aoqi@0 | 3861 | return OR; |
aoqi@0 | 3862 | case AMPAMP: |
aoqi@0 | 3863 | return AND; |
aoqi@0 | 3864 | case BAR: |
aoqi@0 | 3865 | return BITOR; |
aoqi@0 | 3866 | case BAREQ: |
aoqi@0 | 3867 | return BITOR_ASG; |
aoqi@0 | 3868 | case CARET: |
aoqi@0 | 3869 | return BITXOR; |
aoqi@0 | 3870 | case CARETEQ: |
aoqi@0 | 3871 | return BITXOR_ASG; |
aoqi@0 | 3872 | case AMP: |
aoqi@0 | 3873 | return BITAND; |
aoqi@0 | 3874 | case AMPEQ: |
aoqi@0 | 3875 | return BITAND_ASG; |
aoqi@0 | 3876 | case EQEQ: |
aoqi@0 | 3877 | return JCTree.Tag.EQ; |
aoqi@0 | 3878 | case BANGEQ: |
aoqi@0 | 3879 | return NE; |
aoqi@0 | 3880 | case LT: |
aoqi@0 | 3881 | return JCTree.Tag.LT; |
aoqi@0 | 3882 | case GT: |
aoqi@0 | 3883 | return JCTree.Tag.GT; |
aoqi@0 | 3884 | case LTEQ: |
aoqi@0 | 3885 | return LE; |
aoqi@0 | 3886 | case GTEQ: |
aoqi@0 | 3887 | return GE; |
aoqi@0 | 3888 | case LTLT: |
aoqi@0 | 3889 | return SL; |
aoqi@0 | 3890 | case LTLTEQ: |
aoqi@0 | 3891 | return SL_ASG; |
aoqi@0 | 3892 | case GTGT: |
aoqi@0 | 3893 | return SR; |
aoqi@0 | 3894 | case GTGTEQ: |
aoqi@0 | 3895 | return SR_ASG; |
aoqi@0 | 3896 | case GTGTGT: |
aoqi@0 | 3897 | return USR; |
aoqi@0 | 3898 | case GTGTGTEQ: |
aoqi@0 | 3899 | return USR_ASG; |
aoqi@0 | 3900 | case PLUS: |
aoqi@0 | 3901 | return JCTree.Tag.PLUS; |
aoqi@0 | 3902 | case PLUSEQ: |
aoqi@0 | 3903 | return PLUS_ASG; |
aoqi@0 | 3904 | case SUB: |
aoqi@0 | 3905 | return MINUS; |
aoqi@0 | 3906 | case SUBEQ: |
aoqi@0 | 3907 | return MINUS_ASG; |
aoqi@0 | 3908 | case STAR: |
aoqi@0 | 3909 | return MUL; |
aoqi@0 | 3910 | case STAREQ: |
aoqi@0 | 3911 | return MUL_ASG; |
aoqi@0 | 3912 | case SLASH: |
aoqi@0 | 3913 | return DIV; |
aoqi@0 | 3914 | case SLASHEQ: |
aoqi@0 | 3915 | return DIV_ASG; |
aoqi@0 | 3916 | case PERCENT: |
aoqi@0 | 3917 | return MOD; |
aoqi@0 | 3918 | case PERCENTEQ: |
aoqi@0 | 3919 | return MOD_ASG; |
aoqi@0 | 3920 | case INSTANCEOF: |
aoqi@0 | 3921 | return TYPETEST; |
aoqi@0 | 3922 | default: |
aoqi@0 | 3923 | return NO_TAG; |
aoqi@0 | 3924 | } |
aoqi@0 | 3925 | } |
aoqi@0 | 3926 | |
aoqi@0 | 3927 | /** Return operation tag of unary operator represented by token, |
aoqi@0 | 3928 | * No_TAG if token is not a binary operator. |
aoqi@0 | 3929 | */ |
aoqi@0 | 3930 | static JCTree.Tag unoptag(TokenKind token) { |
aoqi@0 | 3931 | switch (token) { |
aoqi@0 | 3932 | case PLUS: |
aoqi@0 | 3933 | return POS; |
aoqi@0 | 3934 | case SUB: |
aoqi@0 | 3935 | return NEG; |
aoqi@0 | 3936 | case BANG: |
aoqi@0 | 3937 | return NOT; |
aoqi@0 | 3938 | case TILDE: |
aoqi@0 | 3939 | return COMPL; |
aoqi@0 | 3940 | case PLUSPLUS: |
aoqi@0 | 3941 | return PREINC; |
aoqi@0 | 3942 | case SUBSUB: |
aoqi@0 | 3943 | return PREDEC; |
aoqi@0 | 3944 | default: |
aoqi@0 | 3945 | return NO_TAG; |
aoqi@0 | 3946 | } |
aoqi@0 | 3947 | } |
aoqi@0 | 3948 | |
aoqi@0 | 3949 | /** Return type tag of basic type represented by token, |
aoqi@0 | 3950 | * NONE if token is not a basic type identifier. |
aoqi@0 | 3951 | */ |
aoqi@0 | 3952 | static TypeTag typetag(TokenKind token) { |
aoqi@0 | 3953 | switch (token) { |
aoqi@0 | 3954 | case BYTE: |
aoqi@0 | 3955 | return TypeTag.BYTE; |
aoqi@0 | 3956 | case CHAR: |
aoqi@0 | 3957 | return TypeTag.CHAR; |
aoqi@0 | 3958 | case SHORT: |
aoqi@0 | 3959 | return TypeTag.SHORT; |
aoqi@0 | 3960 | case INT: |
aoqi@0 | 3961 | return TypeTag.INT; |
aoqi@0 | 3962 | case LONG: |
aoqi@0 | 3963 | return TypeTag.LONG; |
aoqi@0 | 3964 | case FLOAT: |
aoqi@0 | 3965 | return TypeTag.FLOAT; |
aoqi@0 | 3966 | case DOUBLE: |
aoqi@0 | 3967 | return TypeTag.DOUBLE; |
aoqi@0 | 3968 | case BOOLEAN: |
aoqi@0 | 3969 | return TypeTag.BOOLEAN; |
aoqi@0 | 3970 | default: |
aoqi@0 | 3971 | return TypeTag.NONE; |
aoqi@0 | 3972 | } |
aoqi@0 | 3973 | } |
aoqi@0 | 3974 | |
aoqi@0 | 3975 | void checkGenerics() { |
aoqi@0 | 3976 | if (!allowGenerics) { |
aoqi@0 | 3977 | error(token.pos, "generics.not.supported.in.source", source.name); |
aoqi@0 | 3978 | allowGenerics = true; |
aoqi@0 | 3979 | } |
aoqi@0 | 3980 | } |
aoqi@0 | 3981 | void checkVarargs() { |
aoqi@0 | 3982 | if (!allowVarargs) { |
aoqi@0 | 3983 | error(token.pos, "varargs.not.supported.in.source", source.name); |
aoqi@0 | 3984 | allowVarargs = true; |
aoqi@0 | 3985 | } |
aoqi@0 | 3986 | } |
aoqi@0 | 3987 | void checkForeach() { |
aoqi@0 | 3988 | if (!allowForeach) { |
aoqi@0 | 3989 | error(token.pos, "foreach.not.supported.in.source", source.name); |
aoqi@0 | 3990 | allowForeach = true; |
aoqi@0 | 3991 | } |
aoqi@0 | 3992 | } |
aoqi@0 | 3993 | void checkStaticImports() { |
aoqi@0 | 3994 | if (!allowStaticImport) { |
aoqi@0 | 3995 | error(token.pos, "static.import.not.supported.in.source", source.name); |
aoqi@0 | 3996 | allowStaticImport = true; |
aoqi@0 | 3997 | } |
aoqi@0 | 3998 | } |
aoqi@0 | 3999 | void checkAnnotations() { |
aoqi@0 | 4000 | if (!allowAnnotations) { |
aoqi@0 | 4001 | error(token.pos, "annotations.not.supported.in.source", source.name); |
aoqi@0 | 4002 | allowAnnotations = true; |
aoqi@0 | 4003 | } |
aoqi@0 | 4004 | } |
aoqi@0 | 4005 | void checkDiamond() { |
aoqi@0 | 4006 | if (!allowDiamond) { |
aoqi@0 | 4007 | error(token.pos, "diamond.not.supported.in.source", source.name); |
aoqi@0 | 4008 | allowDiamond = true; |
aoqi@0 | 4009 | } |
aoqi@0 | 4010 | } |
aoqi@0 | 4011 | void checkMulticatch() { |
aoqi@0 | 4012 | if (!allowMulticatch) { |
aoqi@0 | 4013 | error(token.pos, "multicatch.not.supported.in.source", source.name); |
aoqi@0 | 4014 | allowMulticatch = true; |
aoqi@0 | 4015 | } |
aoqi@0 | 4016 | } |
aoqi@0 | 4017 | void checkTryWithResources() { |
aoqi@0 | 4018 | if (!allowTWR) { |
aoqi@0 | 4019 | error(token.pos, "try.with.resources.not.supported.in.source", source.name); |
aoqi@0 | 4020 | allowTWR = true; |
aoqi@0 | 4021 | } |
aoqi@0 | 4022 | } |
aoqi@0 | 4023 | void checkLambda() { |
aoqi@0 | 4024 | if (!allowLambda) { |
aoqi@0 | 4025 | log.error(token.pos, "lambda.not.supported.in.source", source.name); |
aoqi@0 | 4026 | allowLambda = true; |
aoqi@0 | 4027 | } |
aoqi@0 | 4028 | } |
aoqi@0 | 4029 | void checkMethodReferences() { |
aoqi@0 | 4030 | if (!allowMethodReferences) { |
aoqi@0 | 4031 | log.error(token.pos, "method.references.not.supported.in.source", source.name); |
aoqi@0 | 4032 | allowMethodReferences = true; |
aoqi@0 | 4033 | } |
aoqi@0 | 4034 | } |
aoqi@0 | 4035 | void checkDefaultMethods() { |
aoqi@0 | 4036 | if (!allowDefaultMethods) { |
aoqi@0 | 4037 | log.error(token.pos, "default.methods.not.supported.in.source", source.name); |
aoqi@0 | 4038 | allowDefaultMethods = true; |
aoqi@0 | 4039 | } |
aoqi@0 | 4040 | } |
aoqi@0 | 4041 | void checkIntersectionTypesInCast() { |
aoqi@0 | 4042 | if (!allowIntersectionTypesInCast) { |
aoqi@0 | 4043 | log.error(token.pos, "intersection.types.in.cast.not.supported.in.source", source.name); |
aoqi@0 | 4044 | allowIntersectionTypesInCast = true; |
aoqi@0 | 4045 | } |
aoqi@0 | 4046 | } |
aoqi@0 | 4047 | void checkStaticInterfaceMethods() { |
aoqi@0 | 4048 | if (!allowStaticInterfaceMethods) { |
aoqi@0 | 4049 | log.error(token.pos, "static.intf.methods.not.supported.in.source", source.name); |
aoqi@0 | 4050 | allowStaticInterfaceMethods = true; |
aoqi@0 | 4051 | } |
aoqi@0 | 4052 | } |
aoqi@0 | 4053 | void checkTypeAnnotations() { |
aoqi@0 | 4054 | if (!allowTypeAnnotations) { |
aoqi@0 | 4055 | log.error(token.pos, "type.annotations.not.supported.in.source", source.name); |
aoqi@0 | 4056 | allowTypeAnnotations = true; |
aoqi@0 | 4057 | } |
aoqi@0 | 4058 | } |
aoqi@0 | 4059 | void checkAnnotationsAfterTypeParams(int pos) { |
aoqi@0 | 4060 | if (!allowAnnotationsAfterTypeParams) { |
aoqi@0 | 4061 | log.error(pos, "annotations.after.type.params.not.supported.in.source", source.name); |
aoqi@0 | 4062 | allowAnnotationsAfterTypeParams = true; |
aoqi@0 | 4063 | } |
aoqi@0 | 4064 | } |
aoqi@0 | 4065 | |
aoqi@0 | 4066 | /* |
aoqi@0 | 4067 | * a functional source tree and end position mappings |
aoqi@0 | 4068 | */ |
aoqi@0 | 4069 | protected static class SimpleEndPosTable extends AbstractEndPosTable { |
aoqi@0 | 4070 | |
aoqi@0 | 4071 | private final IntHashTable endPosMap; |
aoqi@0 | 4072 | |
aoqi@0 | 4073 | SimpleEndPosTable(JavacParser parser) { |
aoqi@0 | 4074 | super(parser); |
aoqi@0 | 4075 | endPosMap = new IntHashTable(); |
aoqi@0 | 4076 | } |
aoqi@0 | 4077 | |
aoqi@0 | 4078 | public void storeEnd(JCTree tree, int endpos) { |
aoqi@0 | 4079 | endPosMap.putAtIndex(tree, errorEndPos > endpos ? errorEndPos : endpos, |
aoqi@0 | 4080 | endPosMap.lookup(tree)); |
aoqi@0 | 4081 | } |
aoqi@0 | 4082 | |
aoqi@0 | 4083 | protected <T extends JCTree> T to(T t) { |
aoqi@0 | 4084 | storeEnd(t, parser.token.endPos); |
aoqi@0 | 4085 | return t; |
aoqi@0 | 4086 | } |
aoqi@0 | 4087 | |
aoqi@0 | 4088 | protected <T extends JCTree> T toP(T t) { |
aoqi@0 | 4089 | storeEnd(t, parser.S.prevToken().endPos); |
aoqi@0 | 4090 | return t; |
aoqi@0 | 4091 | } |
aoqi@0 | 4092 | |
aoqi@0 | 4093 | public int getEndPos(JCTree tree) { |
aoqi@0 | 4094 | int value = endPosMap.getFromIndex(endPosMap.lookup(tree)); |
aoqi@0 | 4095 | // As long as Position.NOPOS==-1, this just returns value. |
aoqi@0 | 4096 | return (value == -1) ? Position.NOPOS : value; |
aoqi@0 | 4097 | } |
aoqi@0 | 4098 | |
aoqi@0 | 4099 | public int replaceTree(JCTree oldTree, JCTree newTree) { |
aoqi@0 | 4100 | int pos = endPosMap.remove(oldTree); |
aoqi@0 | 4101 | if (pos != -1) { |
aoqi@0 | 4102 | storeEnd(newTree, pos); |
aoqi@0 | 4103 | return pos; |
aoqi@0 | 4104 | } |
aoqi@0 | 4105 | return Position.NOPOS; |
aoqi@0 | 4106 | } |
aoqi@0 | 4107 | } |
aoqi@0 | 4108 | |
aoqi@0 | 4109 | /* |
aoqi@0 | 4110 | * a default skeletal implementation without any mapping overhead. |
aoqi@0 | 4111 | */ |
aoqi@0 | 4112 | protected static class EmptyEndPosTable extends AbstractEndPosTable { |
aoqi@0 | 4113 | |
aoqi@0 | 4114 | EmptyEndPosTable(JavacParser parser) { |
aoqi@0 | 4115 | super(parser); |
aoqi@0 | 4116 | } |
aoqi@0 | 4117 | |
aoqi@0 | 4118 | public void storeEnd(JCTree tree, int endpos) { /* empty */ } |
aoqi@0 | 4119 | |
aoqi@0 | 4120 | protected <T extends JCTree> T to(T t) { |
aoqi@0 | 4121 | return t; |
aoqi@0 | 4122 | } |
aoqi@0 | 4123 | |
aoqi@0 | 4124 | protected <T extends JCTree> T toP(T t) { |
aoqi@0 | 4125 | return t; |
aoqi@0 | 4126 | } |
aoqi@0 | 4127 | |
aoqi@0 | 4128 | public int getEndPos(JCTree tree) { |
aoqi@0 | 4129 | return Position.NOPOS; |
aoqi@0 | 4130 | } |
aoqi@0 | 4131 | |
aoqi@0 | 4132 | public int replaceTree(JCTree oldTree, JCTree newTree) { |
aoqi@0 | 4133 | return Position.NOPOS; |
aoqi@0 | 4134 | } |
aoqi@0 | 4135 | |
aoqi@0 | 4136 | } |
aoqi@0 | 4137 | |
aoqi@0 | 4138 | protected static abstract class AbstractEndPosTable implements EndPosTable { |
aoqi@0 | 4139 | /** |
aoqi@0 | 4140 | * The current parser. |
aoqi@0 | 4141 | */ |
aoqi@0 | 4142 | protected JavacParser parser; |
aoqi@0 | 4143 | |
aoqi@0 | 4144 | /** |
aoqi@0 | 4145 | * Store the last error position. |
aoqi@0 | 4146 | */ |
aoqi@0 | 4147 | protected int errorEndPos; |
aoqi@0 | 4148 | |
aoqi@0 | 4149 | public AbstractEndPosTable(JavacParser parser) { |
aoqi@0 | 4150 | this.parser = parser; |
aoqi@0 | 4151 | } |
aoqi@0 | 4152 | |
aoqi@0 | 4153 | /** |
aoqi@0 | 4154 | * Store current token's ending position for a tree, the value of which |
aoqi@0 | 4155 | * will be the greater of last error position and the ending position of |
aoqi@0 | 4156 | * the current token. |
aoqi@0 | 4157 | * @param t The tree. |
aoqi@0 | 4158 | */ |
aoqi@0 | 4159 | protected abstract <T extends JCTree> T to(T t); |
aoqi@0 | 4160 | |
aoqi@0 | 4161 | /** |
aoqi@0 | 4162 | * Store current token's ending position for a tree, the value of which |
aoqi@0 | 4163 | * will be the greater of last error position and the ending position of |
aoqi@0 | 4164 | * the previous token. |
aoqi@0 | 4165 | * @param t The tree. |
aoqi@0 | 4166 | */ |
aoqi@0 | 4167 | protected abstract <T extends JCTree> T toP(T t); |
aoqi@0 | 4168 | |
aoqi@0 | 4169 | /** |
aoqi@0 | 4170 | * Set the error position during the parsing phases, the value of which |
aoqi@0 | 4171 | * will be set only if it is greater than the last stored error position. |
aoqi@0 | 4172 | * @param errPos The error position |
aoqi@0 | 4173 | */ |
aoqi@0 | 4174 | protected void setErrorEndPos(int errPos) { |
aoqi@0 | 4175 | if (errPos > errorEndPos) { |
aoqi@0 | 4176 | errorEndPos = errPos; |
aoqi@0 | 4177 | } |
aoqi@0 | 4178 | } |
aoqi@0 | 4179 | |
aoqi@0 | 4180 | protected void setParser(JavacParser parser) { |
aoqi@0 | 4181 | this.parser = parser; |
aoqi@0 | 4182 | } |
aoqi@0 | 4183 | } |
aoqi@0 | 4184 | } |