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