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