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

Mon, 21 Jan 2013 20:14:39 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:14:39 +0000
changeset 1511
c7c41a044e7c
parent 1503
2d2b2be57c78
child 1513
cf84b07a82db
permissions
-rw-r--r--

8006566: Remove transient lambda-related guards from JavacParser
Summary: Remove transitional internal flag for allowing intersection types in cast
Reviewed-by: jjg

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

mercurial