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