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