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