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