src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
changeset 48721 ef3557eb4306
parent 48551 9cf44c40aa35
child 48920 916690b5edc9
equal deleted inserted replaced
48720:290b480df13e 48721:ef3557eb4306
    24  */
    24  */
    25 
    25 
    26 package com.sun.tools.javac.parser;
    26 package com.sun.tools.javac.parser;
    27 
    27 
    28 import java.util.*;
    28 import java.util.*;
    29 import java.util.function.Function;
       
    30 import java.util.function.Predicate;
       
    31 import java.util.stream.Collectors;
    29 import java.util.stream.Collectors;
    32 
    30 
    33 import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
    31 import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
    34 import com.sun.source.tree.ModuleTree.ModuleKind;
    32 import com.sun.source.tree.ModuleTree.ModuleKind;
    35 
    33 
    37 import com.sun.tools.javac.code.Source.Feature;
    35 import com.sun.tools.javac.code.Source.Feature;
    38 import com.sun.tools.javac.parser.Tokens.*;
    36 import com.sun.tools.javac.parser.Tokens.*;
    39 import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
    37 import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
    40 import com.sun.tools.javac.resources.CompilerProperties;
    38 import com.sun.tools.javac.resources.CompilerProperties;
    41 import com.sun.tools.javac.resources.CompilerProperties.Errors;
    39 import com.sun.tools.javac.resources.CompilerProperties.Errors;
       
    40 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
       
    41 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
    42 import com.sun.tools.javac.tree.*;
    42 import com.sun.tools.javac.tree.*;
    43 import com.sun.tools.javac.tree.JCTree.*;
    43 import com.sun.tools.javac.tree.JCTree.*;
    44 import com.sun.tools.javac.util.*;
    44 import com.sun.tools.javac.util.*;
    45 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
    45 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
    46 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    46 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    47 import com.sun.tools.javac.util.JCDiagnostic.Error;
    47 import com.sun.tools.javac.util.JCDiagnostic.Error;
       
    48 import com.sun.tools.javac.util.JCDiagnostic.Warning;
       
    49 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
    48 import com.sun.tools.javac.util.List;
    50 import com.sun.tools.javac.util.List;
    49 
    51 
    50 import static com.sun.tools.javac.parser.Tokens.TokenKind.*;
    52 import static com.sun.tools.javac.parser.Tokens.TokenKind.*;
    51 import static com.sun.tools.javac.parser.Tokens.TokenKind.ASSERT;
    53 import static com.sun.tools.javac.parser.Tokens.TokenKind.ASSERT;
    52 import static com.sun.tools.javac.parser.Tokens.TokenKind.CASE;
    54 import static com.sun.tools.javac.parser.Tokens.TokenKind.CASE;
   363             }
   365             }
   364             nextToken();
   366             nextToken();
   365         }
   367         }
   366     }
   368     }
   367 
   369 
   368     protected JCErroneous syntaxError(int pos, String key, TokenKind... args) {
   370     protected JCErroneous syntaxError(int pos, Error errorKey) {
   369         return syntaxError(pos, List.nil(), key, args);
   371         return syntaxError(pos, List.nil(), errorKey);
   370     }
   372     }
   371 
   373 
   372     protected JCErroneous syntaxError(int pos, List<JCTree> errs, String key, TokenKind... args) {
   374     protected JCErroneous syntaxError(int pos, List<JCTree> errs, Error errorKey) {
   373         setErrorEndPos(pos);
   375         setErrorEndPos(pos);
   374         JCErroneous err = F.at(pos).Erroneous(errs);
   376         JCErroneous err = F.at(pos).Erroneous(errs);
   375         reportSyntaxError(err, key, (Object[])args);
   377         reportSyntaxError(err, errorKey);
   376         if (errs != null) {
   378         if (errs != null) {
   377             JCTree last = errs.last();
   379             JCTree last = errs.last();
   378             if (last != null)
   380             if (last != null)
   379                 storeEnd(last, pos);
   381                 storeEnd(last, pos);
   380         }
   382         }
   387 
   389 
   388     /**
   390     /**
   389      * Report a syntax using the given the position parameter and arguments,
   391      * Report a syntax using the given the position parameter and arguments,
   390      * unless one was already reported at the same position.
   392      * unless one was already reported at the same position.
   391      */
   393      */
   392     protected void reportSyntaxError(int pos, String key, Object... args) {
   394     protected void reportSyntaxError(int pos, Error errorKey) {
   393         JCDiagnostic.DiagnosticPosition diag = new JCDiagnostic.SimpleDiagnosticPosition(pos);
   395         JCDiagnostic.DiagnosticPosition diag = new JCDiagnostic.SimpleDiagnosticPosition(pos);
   394         reportSyntaxError(diag, key, args);
   396         reportSyntaxError(diag, errorKey);
   395     }
   397     }
   396 
   398 
   397     /**
   399     /**
   398      * Report a syntax error using the given DiagnosticPosition object and
   400      * Report a syntax error using the given DiagnosticPosition object and
   399      * arguments, unless one was already reported at the same position.
   401      * arguments, unless one was already reported at the same position.
   400      */
   402      */
   401     protected void reportSyntaxError(JCDiagnostic.DiagnosticPosition diagPos, String key, Object... args) {
   403     protected void reportSyntaxError(JCDiagnostic.DiagnosticPosition diagPos, Error errorKey) {
   402         int pos = diagPos.getPreferredPosition();
   404         int pos = diagPos.getPreferredPosition();
   403         if (pos > S.errPos() || pos == Position.NOPOS) {
   405         if (pos > S.errPos() || pos == Position.NOPOS) {
   404             if (token.kind == EOF) {
   406             if (token.kind == EOF) {
   405                 error(diagPos, "premature.eof");
   407                 log.error(DiagnosticFlag.SYNTAX, diagPos, Errors.PrematureEof);
   406             } else {
   408             } else {
   407                 error(diagPos, key, args);
   409                 log.error(DiagnosticFlag.SYNTAX, diagPos, errorKey);
   408             }
   410             }
   409         }
   411         }
   410         S.errPos(pos);
   412         S.errPos(pos);
   411         if (token.pos == errorPos) {
   413         if (token.pos == errorPos) {
   412             //check for a possible infinite loop in parsing:
   414             //check for a possible infinite loop in parsing:
   415             count = 0;
   417             count = 0;
   416             errorPos = token.pos;
   418             errorPos = token.pos;
   417         }
   419         }
   418     }
   420     }
   419 
   421 
   420 
       
   421     /** Generate a syntax error at current position unless one was already
       
   422      *  reported at the same position.
       
   423      */
       
   424     protected JCErroneous syntaxError(String key) {
       
   425         return syntaxError(token.pos, key);
       
   426     }
       
   427 
       
   428     /** Generate a syntax error at current position unless one was
       
   429      *  already reported at the same position.
       
   430      */
       
   431     protected JCErroneous syntaxError(String key, TokenKind arg) {
       
   432         return syntaxError(token.pos, key, arg);
       
   433     }
       
   434 
       
   435     /** If next input token matches given token, skip it, otherwise report
   422     /** If next input token matches given token, skip it, otherwise report
   436      *  an error.
   423      *  an error.
   437      */
   424      */
   438     public void accept(TokenKind tk) {
   425     public void accept(TokenKind tk) {
   439         if (token.kind == tk) {
   426         if (token.kind == tk) {
   440             nextToken();
   427             nextToken();
   441         } else {
   428         } else {
   442             setErrorEndPos(token.pos);
   429             setErrorEndPos(token.pos);
   443             reportSyntaxError(S.prevToken().endPos, "expected", tk);
   430             reportSyntaxError(S.prevToken().endPos, Errors.Expected(tk));
   444         }
   431         }
   445     }
   432     }
   446 
   433 
   447     /** Report an illegal start of expression/type error at given position.
   434     /** Report an illegal start of expression/type error at given position.
   448      */
   435      */
   449     JCExpression illegal(int pos) {
   436     JCExpression illegal(int pos) {
   450         setErrorEndPos(pos);
   437         setErrorEndPos(pos);
   451         if ((mode & EXPR) != 0)
   438         if ((mode & EXPR) != 0)
   452             return syntaxError(pos, "illegal.start.of.expr");
   439             return syntaxError(pos, Errors.IllegalStartOfExpr);
   453         else
   440         else
   454             return syntaxError(pos, "illegal.start.of.type");
   441             return syntaxError(pos, Errors.IllegalStartOfType);
   455 
   442 
   456     }
   443     }
   457 
   444 
   458     /** Report an illegal start of expression/type error at current position.
   445     /** Report an illegal start of expression/type error at current position.
   459      */
   446      */
   463 
   450 
   464     /** Diagnose a modifier flag from the set, if any. */
   451     /** Diagnose a modifier flag from the set, if any. */
   465     protected void checkNoMods(long mods) {
   452     protected void checkNoMods(long mods) {
   466         if (mods != 0) {
   453         if (mods != 0) {
   467             long lowestMod = mods & -mods;
   454             long lowestMod = mods & -mods;
   468             error(token.pos, "mod.not.allowed.here",
   455             log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.ModNotAllowedHere(Flags.asFlagSet(lowestMod)));
   469                     Flags.asFlagSet(lowestMod));
       
   470         }
   456         }
   471     }
   457     }
   472 
   458 
   473 /* ---------- doc comments --------- */
   459 /* ---------- doc comments --------- */
   474 
   460 
   544         if (token.kind == IDENTIFIER) {
   530         if (token.kind == IDENTIFIER) {
   545             Name name = token.name();
   531             Name name = token.name();
   546             nextToken();
   532             nextToken();
   547             return name;
   533             return name;
   548         } else if (token.kind == ASSERT) {
   534         } else if (token.kind == ASSERT) {
   549             error(token.pos, "assert.as.identifier");
   535             log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.AssertAsIdentifier);
   550             nextToken();
   536             nextToken();
   551             return names.error;
   537             return names.error;
   552         } else if (token.kind == ENUM) {
   538         } else if (token.kind == ENUM) {
   553             error(token.pos, "enum.as.identifier");
   539             log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.EnumAsIdentifier);
   554             nextToken();
   540             nextToken();
   555             return names.error;
   541             return names.error;
   556         } else if (token.kind == THIS) {
   542         } else if (token.kind == THIS) {
   557             if (allowThisIdent) {
   543             if (allowThisIdent) {
   558                 // Make sure we're using a supported source version.
   544                 // Make sure we're using a supported source version.
   559                 checkSourceLevel(Feature.TYPE_ANNOTATIONS);
   545                 checkSourceLevel(Feature.TYPE_ANNOTATIONS);
   560                 Name name = token.name();
   546                 Name name = token.name();
   561                 nextToken();
   547                 nextToken();
   562                 return name;
   548                 return name;
   563             } else {
   549             } else {
   564                 error(token.pos, "this.as.identifier");
   550                 log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.ThisAsIdentifier);
   565                 nextToken();
   551                 nextToken();
   566                 return names.error;
   552                 return names.error;
   567             }
   553             }
   568         } else if (token.kind == UNDERSCORE) {
   554         } else if (token.kind == UNDERSCORE) {
   569             if (Feature.UNDERSCORE_IDENTIFIER.allowedInSource(source)) {
   555             if (Feature.UNDERSCORE_IDENTIFIER.allowedInSource(source)) {
   570                 warning(token.pos, "underscore.as.identifier");
   556                 log.warning(token.pos, Warnings.UnderscoreAsIdentifier);
   571             } else {
   557             } else {
   572                 error(token.pos, "underscore.as.identifier");
   558                 log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.UnderscoreAsIdentifier);
   573             }
   559             }
   574             Name name = token.name();
   560             Name name = token.name();
   575             nextToken();
   561             nextToken();
   576             return name;
   562             return name;
   577         } else {
   563         } else {
   626             try {
   612             try {
   627                 t = F.at(pos).Literal(
   613                 t = F.at(pos).Literal(
   628                     TypeTag.INT,
   614                     TypeTag.INT,
   629                     Convert.string2int(strval(prefix), token.radix()));
   615                     Convert.string2int(strval(prefix), token.radix()));
   630             } catch (NumberFormatException ex) {
   616             } catch (NumberFormatException ex) {
   631                 error(token.pos, "int.number.too.large", strval(prefix));
   617                 log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.IntNumberTooLarge(strval(prefix)));
   632             }
   618             }
   633             break;
   619             break;
   634         case LONGLITERAL:
   620         case LONGLITERAL:
   635             try {
   621             try {
   636                 t = F.at(pos).Literal(
   622                 t = F.at(pos).Literal(
   637                     TypeTag.LONG,
   623                     TypeTag.LONG,
   638                     Long.valueOf(Convert.string2long(strval(prefix), token.radix())));
   624                     Long.valueOf(Convert.string2long(strval(prefix), token.radix())));
   639             } catch (NumberFormatException ex) {
   625             } catch (NumberFormatException ex) {
   640                 error(token.pos, "int.number.too.large", strval(prefix));
   626                 log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.IntNumberTooLarge(strval(prefix)));
   641             }
   627             }
   642             break;
   628             break;
   643         case FLOATLITERAL: {
   629         case FLOATLITERAL: {
   644             String proper = token.radix() == 16 ?
   630             String proper = token.radix() == 16 ?
   645                     ("0x"+ token.stringVal()) :
   631                     ("0x"+ token.stringVal()) :
   650             } catch (NumberFormatException ex) {
   636             } catch (NumberFormatException ex) {
   651                 // error already reported in scanner
   637                 // error already reported in scanner
   652                 n = Float.NaN;
   638                 n = Float.NaN;
   653             }
   639             }
   654             if (n.floatValue() == 0.0f && !isZero(proper))
   640             if (n.floatValue() == 0.0f && !isZero(proper))
   655                 error(token.pos, "fp.number.too.small");
   641                 log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.FpNumberTooSmall);
   656             else if (n.floatValue() == Float.POSITIVE_INFINITY)
   642             else if (n.floatValue() == Float.POSITIVE_INFINITY)
   657                 error(token.pos, "fp.number.too.large");
   643                 log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.FpNumberTooLarge);
   658             else
   644             else
   659                 t = F.at(pos).Literal(TypeTag.FLOAT, n);
   645                 t = F.at(pos).Literal(TypeTag.FLOAT, n);
   660             break;
   646             break;
   661         }
   647         }
   662         case DOUBLELITERAL: {
   648         case DOUBLELITERAL: {
   669             } catch (NumberFormatException ex) {
   655             } catch (NumberFormatException ex) {
   670                 // error already reported in scanner
   656                 // error already reported in scanner
   671                 n = Double.NaN;
   657                 n = Double.NaN;
   672             }
   658             }
   673             if (n.doubleValue() == 0.0d && !isZero(proper))
   659             if (n.doubleValue() == 0.0d && !isZero(proper))
   674                 error(token.pos, "fp.number.too.small");
   660                 log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.FpNumberTooSmall);
   675             else if (n.doubleValue() == Double.POSITIVE_INFINITY)
   661             else if (n.doubleValue() == Double.POSITIVE_INFINITY)
   676                 error(token.pos, "fp.number.too.large");
   662                 log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.FpNumberTooLarge);
   677             else
   663             else
   678                 t = F.at(pos).Literal(TypeTag.DOUBLE, n);
   664                 t = F.at(pos).Literal(TypeTag.DOUBLE, n);
   679             break;
   665             break;
   680         }
   666         }
   681         case CHARLITERAL:
   667         case CHARLITERAL:
   760 
   746 
   761     public JCExpression unannotatedType(boolean allowVar) {
   747     public JCExpression unannotatedType(boolean allowVar) {
   762         JCExpression result = term(TYPE);
   748         JCExpression result = term(TYPE);
   763 
   749 
   764         if (!allowVar && isRestrictedLocalVarTypeName(result)) {
   750         if (!allowVar && isRestrictedLocalVarTypeName(result)) {
   765             syntaxError(result.pos, "var.not.allowed.here");
   751             syntaxError(result.pos, Errors.VarNotAllowedHere);
   766         }
   752         }
   767 
   753 
   768         return result;
   754         return result;
   769     }
   755     }
   770 
   756 
  1768                     args.append(parseExpression());
  1754                     args.append(parseExpression());
  1769                 }
  1755                 }
  1770             }
  1756             }
  1771             accept(RPAREN);
  1757             accept(RPAREN);
  1772         } else {
  1758         } else {
  1773             syntaxError(token.pos, "expected", LPAREN);
  1759             syntaxError(token.pos, Errors.Expected(LPAREN));
  1774         }
  1760         }
  1775         return args.toList();
  1761         return args.toList();
  1776     }
  1762     }
  1777 
  1763 
  1778     JCMethodInvocation arguments(List<JCExpression> typeArgs, JCExpression t) {
  1764     JCMethodInvocation arguments(List<JCExpression> typeArgs, JCExpression t) {
  1837                     break;
  1823                     break;
  1838                 case GT:
  1824                 case GT:
  1839                     nextToken();
  1825                     nextToken();
  1840                     break;
  1826                     break;
  1841                 default:
  1827                 default:
  1842                     args.append(syntaxError(token.pos, "expected", GT));
  1828                     args.append(syntaxError(token.pos, Errors.Expected(GT)));
  1843                     break;
  1829                     break;
  1844                 }
  1830                 }
  1845                 return args.toList();
  1831                 return args.toList();
  1846             }
  1832             }
  1847         } else {
  1833         } else {
  1848             return List.of(syntaxError(token.pos, "expected", LT));
  1834             return List.of(syntaxError(token.pos, Errors.Expected(LT)));
  1849         }
  1835         }
  1850     }
  1836     }
  1851 
  1837 
  1852     /**
  1838     /**
  1853      *  {@literal
  1839      *  {@literal
  1877             //error recovery
  1863             //error recovery
  1878             TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND);
  1864             TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND);
  1879             JCExpression wc = toP(F.at(pos).Wildcard(t, null));
  1865             JCExpression wc = toP(F.at(pos).Wildcard(t, null));
  1880             JCIdent id = toP(F.at(token.pos).Ident(ident()));
  1866             JCIdent id = toP(F.at(token.pos).Ident(ident()));
  1881             JCErroneous err = F.at(pos).Erroneous(List.<JCTree>of(wc, id));
  1867             JCErroneous err = F.at(pos).Erroneous(List.<JCTree>of(wc, id));
  1882             reportSyntaxError(err, "expected3", GT, EXTENDS, SUPER);
  1868             reportSyntaxError(err, Errors.Expected3(GT, EXTENDS, SUPER));
  1883             result = err;
  1869             result = err;
  1884         } else {
  1870         } else {
  1885             TypeBoundKind t = toP(F.at(pos).TypeBoundKind(BoundKind.UNBOUND));
  1871             TypeBoundKind t = toP(F.at(pos).TypeBoundKind(BoundKind.UNBOUND));
  1886             result = toP(F.at(pos).Wildcard(t, null));
  1872             result = toP(F.at(pos).Wildcard(t, null));
  1887         }
  1873         }
  1967                 Tag tag = t.getTag();
  1953                 Tag tag = t.getTag();
  1968                 // Type annotations are illegal on class literals. Annotated non array class literals
  1954                 // Type annotations are illegal on class literals. Annotated non array class literals
  1969                 // are complained about directly in term3(), Here check for type annotations on dimensions
  1955                 // are complained about directly in term3(), Here check for type annotations on dimensions
  1970                 // taking care to handle some interior dimension(s) being annotated.
  1956                 // taking care to handle some interior dimension(s) being annotated.
  1971                 if ((tag == TYPEARRAY && TreeInfo.containsTypeAnnotation(t)) || tag == ANNOTATED_TYPE)
  1957                 if ((tag == TYPEARRAY && TreeInfo.containsTypeAnnotation(t)) || tag == ANNOTATED_TYPE)
  1972                     syntaxError("no.annotations.on.dot.class");
  1958                     syntaxError(token.pos, Errors.NoAnnotationsOnDotClass);
  1973                 t = toP(F.at(pos).Select(t, names._class));
  1959                 t = toP(F.at(pos).Select(t, names._class));
  1974             }
  1960             }
  1975         } else if ((mode & TYPE) != 0) {
  1961         } else if ((mode & TYPE) != 0) {
  1976             if (token.kind != COLCOL) {
  1962             if (token.kind != COLCOL) {
  1977                 mode = TYPE;
  1963                 mode = TYPE;
  1978             }
  1964             }
  1979         } else if (token.kind != COLCOL) {
  1965         } else if (token.kind != COLCOL) {
  1980             syntaxError(token.pos, "dot.class.expected");
  1966             syntaxError(token.pos, Errors.DotClassExpected);
  1981         }
  1967         }
  1982         return t;
  1968         return t;
  1983     }
  1969     }
  1984 
  1970 
  1985     /**
  1971     /**
  2068                 t = insertAnnotationsToMostInner(t, newAnnotations, false);
  2054                 t = insertAnnotationsToMostInner(t, newAnnotations, false);
  2069             }
  2055             }
  2070 
  2056 
  2071             JCExpression e = arrayCreatorRest(newpos, t);
  2057             JCExpression e = arrayCreatorRest(newpos, t);
  2072             if (diamondFound) {
  2058             if (diamondFound) {
  2073                 reportSyntaxError(lastTypeargsPos, "cannot.create.array.with.diamond");
  2059                 reportSyntaxError(lastTypeargsPos, Errors.CannotCreateArrayWithDiamond);
  2074                 return toP(F.at(newpos).Erroneous(List.of(e)));
  2060                 return toP(F.at(newpos).Erroneous(List.of(e)));
  2075             }
  2061             }
  2076             else if (typeArgs != null) {
  2062             else if (typeArgs != null) {
  2077                 int pos = newpos;
  2063                 int pos = newpos;
  2078                 if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) {
  2064                 if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) {
  2081                     // modified to improve error recovery.
  2067                     // modified to improve error recovery.
  2082                     pos = typeArgs.head.pos;
  2068                     pos = typeArgs.head.pos;
  2083                 }
  2069                 }
  2084                 setErrorEndPos(S.prevToken().endPos);
  2070                 setErrorEndPos(S.prevToken().endPos);
  2085                 JCErroneous err = F.at(pos).Erroneous(typeArgs.prepend(e));
  2071                 JCErroneous err = F.at(pos).Erroneous(typeArgs.prepend(e));
  2086                 reportSyntaxError(err, "cannot.create.array.with.type.arguments");
  2072                 reportSyntaxError(err, Errors.CannotCreateArrayWithTypeArguments);
  2087                 return toP(err);
  2073                 return toP(err);
  2088             }
  2074             }
  2089             return e;
  2075             return e;
  2090         } else if (token.kind == LPAREN) {
  2076         } else if (token.kind == LPAREN) {
  2091             JCNewClass newClass = classCreatorRest(newpos, null, typeArgs, t);
  2077             JCNewClass newClass = classCreatorRest(newpos, null, typeArgs, t);
  2107                 }
  2093                 }
  2108             }
  2094             }
  2109             return newClass;
  2095             return newClass;
  2110         } else {
  2096         } else {
  2111             setErrorEndPos(token.pos);
  2097             setErrorEndPos(token.pos);
  2112             reportSyntaxError(token.pos, "expected2", LPAREN, LBRACKET);
  2098             reportSyntaxError(token.pos, Errors.Expected2(LPAREN, LBRACKET));
  2113             t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.nil(), null));
  2099             t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.nil(), null));
  2114             return toP(F.at(newpos).Erroneous(List.<JCTree>of(t)));
  2100             return toP(F.at(newpos).Erroneous(List.<JCTree>of(t)));
  2115         }
  2101         }
  2116     }
  2102     }
  2117 
  2103 
  2159                     na.elemtype = annotated.underlyingType;
  2145                     na.elemtype = annotated.underlyingType;
  2160                 }
  2146                 }
  2161                 return na;
  2147                 return na;
  2162             } else {
  2148             } else {
  2163                 JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.nil(), null));
  2149                 JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.nil(), null));
  2164                 return syntaxError(token.pos, List.of(t), "array.dimension.missing");
  2150                 return syntaxError(token.pos, List.of(t), Errors.ArrayDimensionMissing);
  2165             }
  2151             }
  2166         } else {
  2152         } else {
  2167             ListBuffer<JCExpression> dims = new ListBuffer<>();
  2153             ListBuffer<JCExpression> dims = new ListBuffer<>();
  2168 
  2154 
  2169             // maintain array dimension type annotations
  2155             // maintain array dimension type annotations
  2199 
  2185 
  2200             JCNewArray na = toP(F.at(newpos).NewArray(elemtype, dims.toList(), elems));
  2186             JCNewArray na = toP(F.at(newpos).NewArray(elemtype, dims.toList(), elems));
  2201             na.dimAnnotations = dimAnnotations.toList();
  2187             na.dimAnnotations = dimAnnotations.toList();
  2202 
  2188 
  2203             if (elems != null) {
  2189             if (elems != null) {
  2204                 return syntaxError(errpos, List.of(na), "illegal.array.creation.both.dimension.and.initialization");
  2190                 return syntaxError(errpos, List.of(na), Errors.IllegalArrayCreationBothDimensionAndInitialization);
  2205             }
  2191             }
  2206 
  2192 
  2207             return na;
  2193             return na;
  2208         }
  2194         }
  2209     }
  2195     }
  2271     JCBlock block(int pos, long flags) {
  2257     JCBlock block(int pos, long flags) {
  2272         accept(LBRACE);
  2258         accept(LBRACE);
  2273         List<JCStatement> stats = blockStatements();
  2259         List<JCStatement> stats = blockStatements();
  2274         JCBlock t = F.at(pos).Block(flags, stats);
  2260         JCBlock t = F.at(pos).Block(flags, stats);
  2275         while (token.kind == CASE || token.kind == DEFAULT) {
  2261         while (token.kind == CASE || token.kind == DEFAULT) {
  2276             syntaxError("orphaned", token.kind);
  2262             syntaxError(token.pos, Errors.Orphaned(token.kind));
  2277             switchBlockStatementGroups();
  2263             switchBlockStatementGroups();
  2278         }
  2264         }
  2279         // the Block node has a field "endpos" for first char of last token, which is
  2265         // the Block node has a field "endpos" for first char of last token, which is
  2280         // usually but not necessarily the last char of the last token.
  2266         // usually but not necessarily the last char of the last token.
  2281         t.endpos = token.pos;
  2267         t.endpos = token.pos;
  2325      */
  2311      */
  2326     JCStatement parseStatementAsBlock() {
  2312     JCStatement parseStatementAsBlock() {
  2327         int pos = token.pos;
  2313         int pos = token.pos;
  2328         List<JCStatement> stats = blockStatement();
  2314         List<JCStatement> stats = blockStatement();
  2329         if (stats.isEmpty()) {
  2315         if (stats.isEmpty()) {
  2330             JCErroneous e = syntaxError(pos, "illegal.start.of.stmt");
  2316             JCErroneous e = syntaxError(pos, Errors.IllegalStartOfStmt);
  2331             return toP(F.at(pos).Exec(e));
  2317             return toP(F.at(pos).Exec(e));
  2332         } else {
  2318         } else {
  2333             JCStatement first = stats.head;
  2319             JCStatement first = stats.head;
  2334             String error = null;
  2320             Error error = null;
  2335             switch (first.getTag()) {
  2321             switch (first.getTag()) {
  2336             case CLASSDEF:
  2322             case CLASSDEF:
  2337                 error = "class.not.allowed";
  2323                 error = Errors.ClassNotAllowed;
  2338                 break;
  2324                 break;
  2339             case VARDEF:
  2325             case VARDEF:
  2340                 error = "variable.not.allowed";
  2326                 error = Errors.VariableNotAllowed;
  2341                 break;
  2327                 break;
  2342             }
  2328             }
  2343             if (error != null) {
  2329             if (error != null) {
  2344                 error(first, error);
  2330                 log.error(DiagnosticFlag.SYNTAX, first, error);
  2345                 List<JCBlock> blist = List.of(F.at(first.pos).Block(0, stats));
  2331                 List<JCBlock> blist = List.of(F.at(first.pos).Block(0, stats));
  2346                 return toP(F.at(pos).Exec(F.at(first.pos).Erroneous(blist)));
  2332                 return toP(F.at(pos).Exec(F.at(first.pos).Erroneous(blist)));
  2347             }
  2333             }
  2348             return first;
  2334             return first;
  2349         }
  2335         }
  2383         case INTERFACE:
  2369         case INTERFACE:
  2384         case CLASS:
  2370         case CLASS:
  2385             Comment dc = token.comment(CommentStyle.JAVADOC);
  2371             Comment dc = token.comment(CommentStyle.JAVADOC);
  2386             return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc));
  2372             return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc));
  2387         case ENUM:
  2373         case ENUM:
  2388             error(token.pos, "local.enum");
  2374             log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.LocalEnum);
  2389             dc = token.comment(CommentStyle.JAVADOC);
  2375             dc = token.comment(CommentStyle.JAVADOC);
  2390             return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc));
  2376             return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc));
  2391         default:
  2377         default:
  2392             Token prevToken = token;
  2378             Token prevToken = token;
  2393             JCExpression t = term(EXPR | TYPE);
  2379             JCExpression t = term(EXPR | TYPE);
  2511                     finalizer = block();
  2497                     finalizer = block();
  2512                 }
  2498                 }
  2513             } else {
  2499             } else {
  2514                 if (resources.isEmpty()) {
  2500                 if (resources.isEmpty()) {
  2515                     if (Feature.TRY_WITH_RESOURCES.allowedInSource(source)) {
  2501                     if (Feature.TRY_WITH_RESOURCES.allowedInSource(source)) {
  2516                         error(pos, "try.without.catch.finally.or.resource.decls");
  2502                         log.error(DiagnosticFlag.SYNTAX, pos, Errors.TryWithoutCatchFinallyOrResourceDecls);
  2517                     } else {
  2503                     } else {
  2518                         error(pos, "try.without.catch.or.finally");
  2504                         log.error(DiagnosticFlag.SYNTAX, pos, Errors.TryWithoutCatchOrFinally);
  2519                     }
  2505                     }
  2520                 }
  2506                 }
  2521             }
  2507             }
  2522             return F.at(pos).Try(resources, body, catchers.toList(), finalizer);
  2508             return F.at(pos).Try(resources, body, catchers.toList(), finalizer);
  2523         }
  2509         }
  2568             nextToken();
  2554             nextToken();
  2569             return toP(F.at(pos).Skip());
  2555             return toP(F.at(pos).Skip());
  2570         case ELSE:
  2556         case ELSE:
  2571             int elsePos = token.pos;
  2557             int elsePos = token.pos;
  2572             nextToken();
  2558             nextToken();
  2573             return doRecover(elsePos, BasicErrorRecoveryAction.BLOCK_STMT, "else.without.if");
  2559             return doRecover(elsePos, BasicErrorRecoveryAction.BLOCK_STMT, Errors.ElseWithoutIf);
  2574         case FINALLY:
  2560         case FINALLY:
  2575             int finallyPos = token.pos;
  2561             int finallyPos = token.pos;
  2576             nextToken();
  2562             nextToken();
  2577             return doRecover(finallyPos, BasicErrorRecoveryAction.BLOCK_STMT, "finally.without.try");
  2563             return doRecover(finallyPos, BasicErrorRecoveryAction.BLOCK_STMT, Errors.FinallyWithoutTry);
  2578         case CATCH:
  2564         case CATCH:
  2579             return doRecover(token.pos, BasicErrorRecoveryAction.CATCH_CLAUSE, "catch.without.try");
  2565             return doRecover(token.pos, BasicErrorRecoveryAction.CATCH_CLAUSE, Errors.CatchWithoutTry);
  2580         case ASSERT: {
  2566         case ASSERT: {
  2581             nextToken();
  2567             nextToken();
  2582             JCExpression assertion = parseExpression();
  2568             JCExpression assertion = parseExpression();
  2583             JCExpression message = null;
  2569             JCExpression message = null;
  2584             if (token.kind == COLON) {
  2570             if (token.kind == COLON) {
  2598     @Override
  2584     @Override
  2599     public JCStatement parseStatement() {
  2585     public JCStatement parseStatement() {
  2600         return parseStatementAsBlock();
  2586         return parseStatementAsBlock();
  2601     }
  2587     }
  2602 
  2588 
  2603     private JCStatement doRecover(int startPos, ErrorRecoveryAction action, String key) {
  2589     private JCStatement doRecover(int startPos, ErrorRecoveryAction action, Error errorKey) {
  2604         int errPos = S.errPos();
  2590         int errPos = S.errPos();
  2605         JCTree stm = action.doRecover(this);
  2591         JCTree stm = action.doRecover(this);
  2606         S.errPos(errPos);
  2592         S.errPos(errPos);
  2607         return toP(F.Exec(syntaxError(startPos, List.of(stm), key)));
  2593         return toP(F.Exec(syntaxError(startPos, List.of(stm), errorKey)));
  2608     }
  2594     }
  2609 
  2595 
  2610     /** CatchClause     = CATCH "(" FormalParameter ")" Block
  2596     /** CatchClause     = CATCH "(" FormalParameter ")" Block
  2611      * TODO: the "FormalParameter" is not correct, it uses the special "catchTypes" rule below.
  2597      * TODO: the "FormalParameter" is not correct, it uses the special "catchTypes" rule below.
  2612      */
  2598      */
  2653                 break;
  2639                 break;
  2654             case RBRACE: case EOF:
  2640             case RBRACE: case EOF:
  2655                 return cases.toList();
  2641                 return cases.toList();
  2656             default:
  2642             default:
  2657                 nextToken(); // to ensure progress
  2643                 nextToken(); // to ensure progress
  2658                 syntaxError(pos, "expected3",
  2644                 syntaxError(pos, Errors.Expected3(CASE, DEFAULT, RBRACE));
  2659                     CASE, DEFAULT, RBRACE);
       
  2660             }
  2645             }
  2661         }
  2646         }
  2662     }
  2647     }
  2663 
  2648 
  2664     protected JCCase switchBlockStatementGroup() {
  2649     protected JCCase switchBlockStatementGroup() {
  2715         } else {
  2700         } else {
  2716             JCExpression t = term(EXPR | TYPE);
  2701             JCExpression t = term(EXPR | TYPE);
  2717             if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
  2702             if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
  2718                 return variableDeclarators(modifiersOpt(), t, stats, true).toList();
  2703                 return variableDeclarators(modifiersOpt(), t, stats, true).toList();
  2719             } else if ((lastmode & TYPE) != 0 && token.kind == COLON) {
  2704             } else if ((lastmode & TYPE) != 0 && token.kind == COLON) {
  2720                 error(pos, "bad.initializer", "for-loop");
  2705                 log.error(DiagnosticFlag.SYNTAX, pos, Errors.BadInitializer("for-loop"));
  2721                 return List.of((JCStatement)F.at(pos).VarDef(null, null, t, null));
  2706                 return List.of((JCStatement)F.at(pos).VarDef(null, null, t, null));
  2722             } else {
  2707             } else {
  2723                 return moreStatementExpressions(pos, t, stats).toList();
  2708                 return moreStatementExpressions(pos, t, stats).toList();
  2724             }
  2709             }
  2725         }
  2710         }
  2800             case MONKEYS_AT  : flag = Flags.ANNOTATION; break;
  2785             case MONKEYS_AT  : flag = Flags.ANNOTATION; break;
  2801             case DEFAULT     : checkSourceLevel(Feature.DEFAULT_METHODS); flag = Flags.DEFAULT; break;
  2786             case DEFAULT     : checkSourceLevel(Feature.DEFAULT_METHODS); flag = Flags.DEFAULT; break;
  2802             case ERROR       : flag = 0; nextToken(); break;
  2787             case ERROR       : flag = 0; nextToken(); break;
  2803             default: break loop;
  2788             default: break loop;
  2804             }
  2789             }
  2805             if ((flags & flag) != 0) error(token.pos, "repeated.modifier");
  2790             if ((flags & flag) != 0) log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier);
  2806             lastPos = token.pos;
  2791             lastPos = token.pos;
  2807             nextToken();
  2792             nextToken();
  2808             if (flag == Flags.ANNOTATION) {
  2793             if (flag == Flags.ANNOTATION) {
  2809                 if (token.kind != INTERFACE) {
  2794                 if (token.kind != INTERFACE) {
  2810                     JCAnnotation ann = annotation(lastPos, Tag.ANNOTATION);
  2795                     JCAnnotation ann = annotation(lastPos, Tag.ANNOTATION);
  2958         JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl);
  2943         JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl);
  2959         boolean implicit = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && head.vartype == null;
  2944         boolean implicit = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && head.vartype == null;
  2960         vdefs.append(head);
  2945         vdefs.append(head);
  2961         while (token.kind == COMMA) {
  2946         while (token.kind == COMMA) {
  2962             if (implicit) {
  2947             if (implicit) {
  2963                 reportSyntaxError(pos, "var.not.allowed.compound");
  2948                 reportSyntaxError(pos, Errors.VarNotAllowedCompound);
  2964             }
  2949             }
  2965             // All but last of multiple declarators subsume a comma
  2950             // All but last of multiple declarators subsume a comma
  2966             storeEnd((JCTree)vdefs.last(), token.endPos);
  2951             storeEnd((JCTree)vdefs.last(), token.endPos);
  2967             nextToken();
  2952             nextToken();
  2968             vdefs.append(variableDeclarator(mods, type, reqInit, dc, localDecl));
  2953             vdefs.append(variableDeclarator(mods, type, reqInit, dc, localDecl));
  2989         JCExpression init = null;
  2974         JCExpression init = null;
  2990         if (token.kind == EQ) {
  2975         if (token.kind == EQ) {
  2991             nextToken();
  2976             nextToken();
  2992             init = variableInitializer();
  2977             init = variableInitializer();
  2993         }
  2978         }
  2994         else if (reqInit) syntaxError(token.pos, "expected", EQ);
  2979         else if (reqInit) syntaxError(token.pos, Errors.Expected(EQ));
  2995         JCTree elemType = TreeInfo.innermostType(type, true);
  2980         JCTree elemType = TreeInfo.innermostType(type, true);
  2996         int startPos = Position.NOPOS;
  2981         int startPos = Position.NOPOS;
  2997         if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && elemType.hasTag(IDENT)) {
  2982         if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source) && elemType.hasTag(IDENT)) {
  2998             Name typeName = ((JCIdent)elemType).name;
  2983             Name typeName = ((JCIdent)elemType).name;
  2999             if (isRestrictedLocalVarTypeName(typeName)) {
  2984             if (isRestrictedLocalVarTypeName(typeName)) {
  3000                 if (type.hasTag(TYPEARRAY)) {
  2985                 if (type.hasTag(TYPEARRAY)) {
  3001                     //error - 'var' and arrays
  2986                     //error - 'var' and arrays
  3002                     reportSyntaxError(pos, "var.not.allowed.array");
  2987                     reportSyntaxError(pos, Errors.VarNotAllowedArray);
  3003                 } else {
  2988                 } else {
  3004                     startPos = TreeInfo.getStartPos(mods);
  2989                     startPos = TreeInfo.getStartPos(mods);
  3005                     if (startPos == Position.NOPOS)
  2990                     if (startPos == Position.NOPOS)
  3006                         startPos = TreeInfo.getStartPos(type);
  2991                         startPos = TreeInfo.getStartPos(type);
  3007                     //implicit type
  2992                     //implicit type
  3175                         }
  3160                         }
  3176                         defs.append(moduleDecl(mods, kind, docComment));
  3161                         defs.append(moduleDecl(mods, kind, docComment));
  3177                         consumedToplevelDoc = true;
  3162                         consumedToplevelDoc = true;
  3178                         break;
  3163                         break;
  3179                     } else if (kind != ModuleKind.STRONG) {
  3164                     } else if (kind != ModuleKind.STRONG) {
  3180                         reportSyntaxError(token.pos, "expected.module");
  3165                         reportSyntaxError(token.pos, Errors.ExpectedModule);
  3181                     }
  3166                     }
  3182                 }
  3167                 }
  3183                 JCTree def = typeDeclaration(mods, docComment);
  3168                 JCTree def = typeDeclaration(mods, docComment);
  3184                 if (def instanceof JCExpressionStatement)
  3169                 if (def instanceof JCExpressionStatement)
  3185                     def = ((JCExpressionStatement)def).expr;
  3170                     def = ((JCExpressionStatement)def).expr;
  3244                             } else {
  3229                             } else {
  3245                                 break loop;
  3230                                 break loop;
  3246                             }
  3231                             }
  3247                         case STATIC:
  3232                         case STATIC:
  3248                             if (isStaticPhase) {
  3233                             if (isStaticPhase) {
  3249                                 error(token.pos, "repeated.modifier");
  3234                                 log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier);
  3250                             }
  3235                             }
  3251                             isStaticPhase = true;
  3236                             isStaticPhase = true;
  3252                             break;
  3237                             break;
  3253                         default:
  3238                         default:
  3254                             break loop;
  3239                             break loop;
  3282                     nextToken();
  3267                     nextToken();
  3283                     List<JCExpression> implNames = qualidentList(false);
  3268                     List<JCExpression> implNames = qualidentList(false);
  3284                     accept(SEMI);
  3269                     accept(SEMI);
  3285                     defs.append(toP(F.at(pos).Provides(serviceName, implNames)));
  3270                     defs.append(toP(F.at(pos).Provides(serviceName, implNames)));
  3286                 } else {
  3271                 } else {
  3287                     error(token.pos, "expected", "'" + names.with + "'");
  3272                     log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.ExpectedStr("'" + names.with + "'"));
  3288                     skip(false, false, false, false);
  3273                     skip(false, false, false, false);
  3289                 }
  3274                 }
  3290             } else if (token.name() == names.uses) {
  3275             } else if (token.name() == names.uses) {
  3291                 nextToken();
  3276                 nextToken();
  3292                 JCExpression service = qualident(false);
  3277                 JCExpression service = qualident(false);
  3293                 accept(SEMI);
  3278                 accept(SEMI);
  3294                 defs.append(toP(F.at(pos).Uses(service)));
  3279                 defs.append(toP(F.at(pos).Uses(service)));
  3295             } else {
  3280             } else {
  3296                 setErrorEndPos(pos);
  3281                 setErrorEndPos(pos);
  3297                 reportSyntaxError(pos, "invalid.module.directive");
  3282                 reportSyntaxError(pos, Errors.InvalidModuleDirective);
  3298                 break;
  3283                 break;
  3299             }
  3284             }
  3300         }
  3285         }
  3301         return defs.toList();
  3286         return defs.toList();
  3302     }
  3287     }
  3361             } else {
  3346             } else {
  3362                 errs = List.of(mods);
  3347                 errs = List.of(mods);
  3363             }
  3348             }
  3364             final JCErroneous erroneousTree;
  3349             final JCErroneous erroneousTree;
  3365             if (parseModuleInfo) {
  3350             if (parseModuleInfo) {
  3366                 erroneousTree = syntaxError(pos, errs, "expected.module.or.open");
  3351                 erroneousTree = syntaxError(pos, errs, Errors.ExpectedModuleOrOpen);
  3367             } else {
  3352             } else {
  3368                 erroneousTree = syntaxError(pos, errs, "expected3", CLASS, INTERFACE, ENUM);
  3353                 erroneousTree = syntaxError(pos, errs, Errors.Expected3(CLASS, INTERFACE, ENUM));
  3369             }
  3354             }
  3370             return toP(F.Exec(erroneousTree));
  3355             return toP(F.Exec(erroneousTree));
  3371         }
  3356         }
  3372     }
  3357     }
  3373 
  3358 
  3403     Name typeName() {
  3388     Name typeName() {
  3404         int pos = token.pos;
  3389         int pos = token.pos;
  3405         Name name = ident();
  3390         Name name = ident();
  3406         if (name == names.var) {
  3391         if (name == names.var) {
  3407             if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source)) {
  3392             if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source)) {
  3408                 reportSyntaxError(pos, "var.not.allowed", name);
  3393                 reportSyntaxError(pos, Errors.VarNotAllowed(name));
  3409             } else {
  3394             } else {
  3410                 warning(pos, "var.not.allowed");
  3395                 log.warning(pos, Warnings.VarNotAllowed);
  3411             }
  3396             }
  3412         }
  3397         }
  3413         return name;
  3398         return name;
  3414     }
  3399     }
  3415 
  3400 
  3477                 nextToken();
  3462                 nextToken();
  3478                 if (token.kind == RBRACE || token.kind == SEMI) break;
  3463                 if (token.kind == RBRACE || token.kind == SEMI) break;
  3479                 defs.append(enumeratorDeclaration(enumName));
  3464                 defs.append(enumeratorDeclaration(enumName));
  3480             }
  3465             }
  3481             if (token.kind != SEMI && token.kind != RBRACE) {
  3466             if (token.kind != SEMI && token.kind != RBRACE) {
  3482                 defs.append(syntaxError(token.pos, "expected3",
  3467                 defs.append(syntaxError(token.pos, Errors.Expected3(COMMA, RBRACE, SEMI)));
  3483                                 COMMA, RBRACE, SEMI));
       
  3484                 nextToken();
  3468                 nextToken();
  3485             }
  3469             }
  3486         }
  3470         }
  3487         if (token.kind == SEMI) {
  3471         if (token.kind == SEMI) {
  3488             nextToken();
  3472             nextToken();
  3612                 return List.of(classOrInterfaceOrEnumDeclaration(mods, dc));
  3596                 return List.of(classOrInterfaceOrEnumDeclaration(mods, dc));
  3613             } else if (token.kind == LBRACE &&
  3597             } else if (token.kind == LBRACE &&
  3614                        (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 &&
  3598                        (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 &&
  3615                        mods.annotations.isEmpty()) {
  3599                        mods.annotations.isEmpty()) {
  3616                 if (isInterface) {
  3600                 if (isInterface) {
  3617                     error(token.pos, "initializer.not.allowed");
  3601                     log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.InitializerNotAllowed);
  3618                 }
  3602                 }
  3619                 return List.of(block(pos, mods.flags));
  3603                 return List.of(block(pos, mods.flags));
  3620             } else {
  3604             } else {
  3621                 pos = token.pos;
  3605                 pos = token.pos;
  3622                 List<JCTypeParameter> typarams = typeParametersOpt();
  3606                 List<JCTypeParameter> typarams = typeParametersOpt();
  3646                     // method returns types are un-annotated types
  3630                     // method returns types are un-annotated types
  3647                     type = unannotatedType(false);
  3631                     type = unannotatedType(false);
  3648                 }
  3632                 }
  3649                 if (token.kind == LPAREN && !isInterface && type.hasTag(IDENT)) {
  3633                 if (token.kind == LPAREN && !isInterface && type.hasTag(IDENT)) {
  3650                     if (isInterface || tk.name() != className)
  3634                     if (isInterface || tk.name() != className)
  3651                         error(pos, "invalid.meth.decl.ret.type.req");
  3635                         log.error(DiagnosticFlag.SYNTAX, pos, Errors.InvalidMethDeclRetTypeReq);
  3652                     else if (annosAfterParams.nonEmpty())
  3636                     else if (annosAfterParams.nonEmpty())
  3653                         illegal(annosAfterParams.head.pos);
  3637                         illegal(annosAfterParams.head.pos);
  3654                     return List.of(methodDeclaratorRest(
  3638                     return List.of(methodDeclaratorRest(
  3655                         pos, mods, null, names.init, typarams,
  3639                         pos, mods, null, names.init, typarams,
  3656                         isInterface, true, dc));
  3640                         isInterface, true, dc));
  3672                         pos = token.pos;
  3656                         pos = token.pos;
  3673                         List<JCTree> err = isVoid
  3657                         List<JCTree> err = isVoid
  3674                             ? List.of(toP(F.at(pos).MethodDef(mods, name, type, typarams,
  3658                             ? List.of(toP(F.at(pos).MethodDef(mods, name, type, typarams,
  3675                                 List.nil(), List.nil(), null, null)))
  3659                                 List.nil(), List.nil(), null, null)))
  3676                             : null;
  3660                             : null;
  3677                         return List.of(syntaxError(token.pos, err, "expected", LPAREN));
  3661                         return List.of(syntaxError(token.pos, err, Errors.Expected(LPAREN)));
  3678                     }
  3662                     }
  3679                 }
  3663                 }
  3680             }
  3664             }
  3681         }
  3665         }
  3682     }
  3666     }
  3839                 params.append(lastParam);
  3823                 params.append(lastParam);
  3840             }
  3824             }
  3841             this.allowThisIdent = false;
  3825             this.allowThisIdent = false;
  3842             while (token.kind == COMMA) {
  3826             while (token.kind == COMMA) {
  3843                 if ((lastParam.mods.flags & Flags.VARARGS) != 0) {
  3827                 if ((lastParam.mods.flags & Flags.VARARGS) != 0) {
  3844                     error(lastParam, "varargs.must.be.last");
  3828                     log.error(DiagnosticFlag.SYNTAX, lastParam, Errors.VarargsMustBeLast);
  3845                 }
  3829                 }
  3846                 nextToken();
  3830                 nextToken();
  3847                 params.append(lastParam = formalParameter(lambdaParameters));
  3831                 params.append(lastParam = formalParameter(lambdaParameters));
  3848             }
  3832             }
  3849         }
  3833         }
  3850         if (token.kind == RPAREN) {
  3834         if (token.kind == RPAREN) {
  3851             nextToken();
  3835             nextToken();
  3852         } else {
  3836         } else {
  3853             setErrorEndPos(token.pos);
  3837             setErrorEndPos(token.pos);
  3854             reportSyntaxError(S.prevToken().endPos, "expected3", COMMA, RPAREN, LBRACKET);
  3838             reportSyntaxError(S.prevToken().endPos, Errors.Expected3(COMMA, RPAREN, LBRACKET));
  3855         }
  3839         }
  3856         return params.toList();
  3840         return params.toList();
  3857     }
  3841     }
  3858 
  3842 
  3859     List<JCVariableDecl> implicitParameters(boolean hasParens) {
  3843     List<JCVariableDecl> implicitParameters(boolean hasParens) {
  3974             type = insertAnnotationsToMostInner(type, varargsAnnos, true);
  3958             type = insertAnnotationsToMostInner(type, varargsAnnos, true);
  3975             nextToken();
  3959             nextToken();
  3976         } else {
  3960         } else {
  3977             // if not a var arg, then typeAnnotationsPushedBack should be null
  3961             // if not a var arg, then typeAnnotationsPushedBack should be null
  3978             if (typeAnnotationsPushedBack.nonEmpty()) {
  3962             if (typeAnnotationsPushedBack.nonEmpty()) {
  3979                 reportSyntaxError(typeAnnotationsPushedBack.head.pos,
  3963                 reportSyntaxError(typeAnnotationsPushedBack.head.pos, Errors.IllegalStartOfType);
  3980                         "illegal.start.of.type");
       
  3981             }
  3964             }
  3982             typeAnnotationsPushedBack = List.nil();
  3965             typeAnnotationsPushedBack = List.nil();
  3983         }
  3966         }
  3984         return variableDeclaratorId(mods, type, lambdaParameter);
  3967         return variableDeclaratorId(mods, type, lambdaParameter);
  3985     }
  3968     }
  3988         JCModifiers mods = F.at(token.pos).Modifiers(Flags.PARAMETER);
  3971         JCModifiers mods = F.at(token.pos).Modifiers(Flags.PARAMETER);
  3989         return variableDeclaratorId(mods, null, true);
  3972         return variableDeclaratorId(mods, null, true);
  3990     }
  3973     }
  3991 
  3974 
  3992 /* ---------- auxiliary methods -------------- */
  3975 /* ---------- auxiliary methods -------------- */
  3993 
       
  3994     void error(int pos, String key, Object ... args) {
       
  3995         log.error(DiagnosticFlag.SYNTAX, pos, key, args);
       
  3996     }
       
  3997 
       
  3998     void error(DiagnosticPosition pos, String key, Object ... args) {
       
  3999         log.error(DiagnosticFlag.SYNTAX, pos, key, args);
       
  4000     }
       
  4001 
       
  4002     void warning(int pos, String key, Object ... args) {
       
  4003         log.warning(pos, key, args);
       
  4004     }
       
  4005 
       
  4006     /** Check that given tree is a legal expression statement.
  3976     /** Check that given tree is a legal expression statement.
  4007      */
  3977      */
  4008     protected JCExpression checkExprStat(JCExpression t) {
  3978     protected JCExpression checkExprStat(JCExpression t) {
  4009         if (!TreeInfo.isExpressionStatement(t)) {
  3979         if (!TreeInfo.isExpressionStatement(t)) {
  4010             JCExpression ret = F.at(t.pos).Erroneous(List.<JCTree>of(t));
  3980             JCExpression ret = F.at(t.pos).Erroneous(List.<JCTree>of(t));
  4011             error(ret, "not.stmt");
  3981             log.error(DiagnosticFlag.SYNTAX, ret, Errors.NotStmt);
  4012             return ret;
  3982             return ret;
  4013         } else {
  3983         } else {
  4014             return t;
  3984             return t;
  4015         }
  3985         }
  4016     }
  3986     }