author | mcimadamore |
Mon, 04 Dec 2017 17:54:49 +0000 | |
changeset 48054 | 702043a4cdeb |
parent 47350 | d65c3b21081c |
child 48721 | ef3557eb4306 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
36494 | 2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jshell; |
|
27 |
||
28 |
import com.sun.tools.javac.code.Source; |
|
29 |
import com.sun.tools.javac.parser.Scanner; |
|
30 |
import com.sun.tools.javac.parser.ScannerFactory; |
|
31 |
import com.sun.tools.javac.parser.Tokens.Token; |
|
32 |
import com.sun.tools.javac.parser.Tokens.TokenKind; |
|
33 |
import com.sun.tools.javac.util.Context; |
|
34 |
import com.sun.tools.javac.util.JCDiagnostic; |
|
35 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag; |
|
36 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
48054
702043a4cdeb
8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents:
47350
diff
changeset
|
37 |
import com.sun.tools.javac.util.JCDiagnostic.Error; |
33362 | 38 |
import com.sun.tools.javac.util.Log; |
39 |
import java.io.PrintWriter; |
|
40 |
import java.io.StringWriter; |
|
41 |
import java.util.ArrayDeque; |
|
42 |
import java.util.Deque; |
|
43 |
import java.util.EnumMap; |
|
44 |
import java.util.Iterator; |
|
45 |
import jdk.jshell.SourceCodeAnalysis.Completeness; |
|
46 |
import com.sun.source.tree.Tree; |
|
47 |
import static jdk.jshell.CompletenessAnalyzer.TK.*; |
|
48 |
import jdk.jshell.TaskFactory.ParseTask; |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
49 |
import jdk.jshell.TaskFactory.Worker; |
33362 | 50 |
import java.util.List; |
40771 | 51 |
import java.util.function.Function; |
52 |
import java.util.function.Supplier; |
|
33362 | 53 |
|
54 |
/** |
|
55 |
* Low level scanner to determine completeness of input. |
|
56 |
* @author Robert Field |
|
57 |
*/ |
|
58 |
class CompletenessAnalyzer { |
|
59 |
||
60 |
private final ScannerFactory scannerFactory; |
|
61 |
private final JShell proc; |
|
62 |
||
63 |
private static Completeness error() { |
|
64 |
return Completeness.UNKNOWN; // For breakpointing |
|
65 |
} |
|
66 |
||
67 |
static class CaInfo { |
|
68 |
||
69 |
CaInfo(Completeness status, int unitEndPos) { |
|
70 |
this.status = status; |
|
71 |
this.unitEndPos = unitEndPos; |
|
72 |
} |
|
73 |
final int unitEndPos; |
|
74 |
final Completeness status; |
|
75 |
} |
|
76 |
||
77 |
CompletenessAnalyzer(JShell proc) { |
|
78 |
this.proc = proc; |
|
79 |
Context context = new Context(); |
|
80 |
Log log = CaLog.createLog(context); |
|
81 |
context.put(Log.class, log); |
|
48054
702043a4cdeb
8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents:
47350
diff
changeset
|
82 |
context.put(Source.class, Source.JDK9); |
33362 | 83 |
scannerFactory = ScannerFactory.instance(context); |
84 |
} |
|
85 |
||
86 |
CaInfo scan(String s) { |
|
87 |
try { |
|
40771 | 88 |
Parser parser = new Parser( |
89 |
() -> new Matched(scannerFactory.newScanner(s, false)), |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
90 |
worker -> proc.taskFactory.parse(s, worker)); |
33362 | 91 |
Completeness stat = parser.parseUnit(); |
92 |
int endPos = stat == Completeness.UNKNOWN |
|
93 |
? s.length() |
|
40771 | 94 |
: parser.endPos(); |
33362 | 95 |
return new CaInfo(stat, endPos); |
96 |
} catch (SyntaxException ex) { |
|
97 |
return new CaInfo(error(), s.length()); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
@SuppressWarnings("serial") // serialVersionUID intentionally omitted |
|
102 |
private static class SyntaxException extends RuntimeException { |
|
103 |
} |
|
104 |
||
105 |
private static void die() { |
|
106 |
throw new SyntaxException(); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Subclass of Log used by compiler API to die on error and ignore |
|
111 |
* other messages |
|
112 |
*/ |
|
113 |
private static class CaLog extends Log { |
|
114 |
||
115 |
private static CaLog createLog(Context context) { |
|
116 |
PrintWriter pw = new PrintWriter(new StringWriter()); |
|
40232
4995ab1a4558
8162359: javac should use stdout for --help and --version
jjg
parents:
36494
diff
changeset
|
117 |
CaLog log = new CaLog(context, pw); |
33362 | 118 |
context.put(logKey, log); |
119 |
return log; |
|
120 |
} |
|
121 |
||
40232
4995ab1a4558
8162359: javac should use stdout for --help and --version
jjg
parents:
36494
diff
changeset
|
122 |
private CaLog(Context context, PrintWriter pw) { |
4995ab1a4558
8162359: javac should use stdout for --help and --version
jjg
parents:
36494
diff
changeset
|
123 |
super(context, pw); |
33362 | 124 |
} |
125 |
||
126 |
@Override |
|
127 |
public void error(String key, Object... args) { |
|
128 |
die(); |
|
129 |
} |
|
130 |
||
131 |
@Override |
|
132 |
public void error(DiagnosticPosition pos, String key, Object... args) { |
|
133 |
die(); |
|
134 |
} |
|
135 |
||
136 |
@Override |
|
137 |
public void error(DiagnosticFlag flag, DiagnosticPosition pos, String key, Object... args) { |
|
138 |
die(); |
|
139 |
} |
|
140 |
||
141 |
@Override |
|
48054
702043a4cdeb
8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents:
47350
diff
changeset
|
142 |
public void error(int pos, Error errorKey) { |
702043a4cdeb
8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents:
47350
diff
changeset
|
143 |
die(); |
702043a4cdeb
8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents:
47350
diff
changeset
|
144 |
} |
702043a4cdeb
8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents:
47350
diff
changeset
|
145 |
|
702043a4cdeb
8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents:
47350
diff
changeset
|
146 |
@Override |
33362 | 147 |
public void error(int pos, String key, Object... args) { |
148 |
die(); |
|
149 |
} |
|
150 |
||
151 |
@Override |
|
152 |
public void error(DiagnosticFlag flag, int pos, String key, Object... args) { |
|
153 |
die(); |
|
154 |
} |
|
155 |
||
156 |
@Override |
|
157 |
public void report(JCDiagnostic diagnostic) { |
|
158 |
// Ignore |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
// Location position kinds -- a token is ... |
|
163 |
private static final int XEXPR = 0b1; // OK in expression (not first) |
|
164 |
private static final int XDECL = 0b10; // OK in declaration (not first) |
|
165 |
private static final int XSTMT = 0b100; // OK in statement framework (not first) |
|
166 |
private static final int XEXPR1o = 0b1000; // OK first in expression |
|
167 |
private static final int XDECL1o = 0b10000; // OK first in declaration |
|
168 |
private static final int XSTMT1o = 0b100000; // OK first or only in statement framework |
|
169 |
private static final int XEXPR1 = XEXPR1o | XEXPR; // OK in expression (anywhere) |
|
170 |
private static final int XDECL1 = XDECL1o | XDECL; // OK in declaration (anywhere) |
|
171 |
private static final int XSTMT1 = XSTMT1o | XSTMT; // OK in statement framework (anywhere) |
|
172 |
private static final int XANY1 = XEXPR1o | XDECL1o | XSTMT1o; // Mask: first in statement, declaration, or expression |
|
173 |
private static final int XTERM = 0b100000000; // Can terminate (last before EOF) |
|
174 |
private static final int XSTART = 0b1000000000; // Boundary, must be XTERM before |
|
175 |
private static final int XERRO = 0b10000000000; // Is an error |
|
41450
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
176 |
private static final int XBRACESNEEDED = 0b100000000000; // Expect {ANY} LBRACE |
33362 | 177 |
|
178 |
/** |
|
179 |
* An extension of the compiler's TokenKind which adds our combined/processed |
|
180 |
* kinds. Also associates each TK with a union of acceptable kinds of code |
|
181 |
* position it can occupy. For example: IDENTIFER is XEXPR1|XDECL1|XTERM, |
|
182 |
* meaning it can occur in expressions or declarations (but not in the |
|
183 |
* framework of a statement and that can be the final (terminating) token |
|
184 |
* in a snippet. |
|
185 |
* <P> |
|
186 |
* There must be a TK defined for each compiler TokenKind, an exception |
|
187 |
* will |
|
188 |
* be thrown if a TokenKind is defined and a corresponding TK is not. Add a |
|
189 |
* new TK in the appropriate category. If it is like an existing category |
|
190 |
* (e.g. a new modifier or type this may be all that is needed. If it |
|
191 |
* is bracketing or modifies the acceptable positions of other tokens, |
|
192 |
* please closely examine the needed changes to this scanner. |
|
193 |
*/ |
|
194 |
static enum TK { |
|
195 |
||
196 |
// Special |
|
197 |
EOF(TokenKind.EOF, 0), // |
|
198 |
ERROR(TokenKind.ERROR, XERRO), // |
|
199 |
IDENTIFIER(TokenKind.IDENTIFIER, XEXPR1|XDECL1|XTERM), // |
|
200 |
UNDERSCORE(TokenKind.UNDERSCORE, XERRO), // _ |
|
41450
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
201 |
CLASS(TokenKind.CLASS, XEXPR|XDECL1|XBRACESNEEDED), // class decl (MAPPED: DOTCLASS) |
33362 | 202 |
MONKEYS_AT(TokenKind.MONKEYS_AT, XEXPR|XDECL1), // @ |
203 |
IMPORT(TokenKind.IMPORT, XDECL1|XSTART), // import -- consider declaration |
|
204 |
SEMI(TokenKind.SEMI, XSTMT1|XTERM|XSTART), // ; |
|
205 |
||
206 |
// Shouldn't see -- error |
|
207 |
PACKAGE(TokenKind.PACKAGE, XERRO), // package |
|
208 |
CONST(TokenKind.CONST, XERRO), // reserved keyword -- const |
|
209 |
GOTO(TokenKind.GOTO, XERRO), // reserved keyword -- goto |
|
210 |
CUSTOM(TokenKind.CUSTOM, XERRO), // No uses |
|
211 |
||
212 |
// Declarations |
|
41450
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
213 |
ENUM(TokenKind.ENUM, XDECL1|XBRACESNEEDED), // enum |
33362 | 214 |
IMPLEMENTS(TokenKind.IMPLEMENTS, XDECL), // implements |
41450
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
215 |
INTERFACE(TokenKind.INTERFACE, XDECL1|XBRACESNEEDED), // interface |
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
216 |
THROWS(TokenKind.THROWS, XDECL|XBRACESNEEDED), // throws |
33362 | 217 |
|
218 |
// Primarive type names |
|
40771 | 219 |
BOOLEAN(TokenKind.BOOLEAN, XEXPR1|XDECL1), // boolean |
220 |
BYTE(TokenKind.BYTE, XEXPR1|XDECL1), // byte |
|
221 |
CHAR(TokenKind.CHAR, XEXPR1|XDECL1), // char |
|
222 |
DOUBLE(TokenKind.DOUBLE, XEXPR1|XDECL1), // double |
|
223 |
FLOAT(TokenKind.FLOAT, XEXPR1|XDECL1), // float |
|
224 |
INT(TokenKind.INT, XEXPR1|XDECL1), // int |
|
225 |
LONG(TokenKind.LONG, XEXPR1|XDECL1), // long |
|
226 |
SHORT(TokenKind.SHORT, XEXPR1|XDECL1), // short |
|
227 |
VOID(TokenKind.VOID, XEXPR1|XDECL1), // void |
|
33362 | 228 |
|
229 |
// Modifiers keywords |
|
230 |
ABSTRACT(TokenKind.ABSTRACT, XDECL1), // abstract |
|
231 |
FINAL(TokenKind.FINAL, XDECL1), // final |
|
232 |
NATIVE(TokenKind.NATIVE, XDECL1), // native |
|
233 |
STATIC(TokenKind.STATIC, XDECL1), // static |
|
234 |
STRICTFP(TokenKind.STRICTFP, XDECL1), // strictfp |
|
235 |
PRIVATE(TokenKind.PRIVATE, XDECL1), // private |
|
236 |
PROTECTED(TokenKind.PROTECTED, XDECL1), // protected |
|
237 |
PUBLIC(TokenKind.PUBLIC, XDECL1), // public |
|
238 |
TRANSIENT(TokenKind.TRANSIENT, XDECL1), // transient |
|
239 |
VOLATILE(TokenKind.VOLATILE, XDECL1), // volatile |
|
240 |
||
241 |
// Declarations and type parameters (thus expressions) |
|
242 |
EXTENDS(TokenKind.EXTENDS, XEXPR|XDECL), // extends |
|
42267
a0c712fc6575
8114842: JShell: SourceCodeAnalysis splits code with array initialiazer incorrectly
rfield
parents:
41450
diff
changeset
|
243 |
COMMA(TokenKind.COMMA, XEXPR|XDECL), // , |
33362 | 244 |
AMP(TokenKind.AMP, XEXPR|XDECL), // & |
245 |
GT(TokenKind.GT, XEXPR|XDECL), // > |
|
246 |
LT(TokenKind.LT, XEXPR|XDECL1), // < |
|
247 |
LTLT(TokenKind.LTLT, XEXPR|XDECL1), // << |
|
248 |
GTGT(TokenKind.GTGT, XEXPR|XDECL), // >> |
|
249 |
GTGTGT(TokenKind.GTGTGT, XEXPR|XDECL), // >>> |
|
250 |
QUES(TokenKind.QUES, XEXPR|XDECL), // ? |
|
251 |
DOT(TokenKind.DOT, XEXPR|XDECL), // . |
|
40771 | 252 |
STAR(TokenKind.STAR, XEXPR), // * (MAPPED: DOTSTAR) |
33362 | 253 |
|
254 |
// Statement keywords |
|
255 |
ASSERT(TokenKind.ASSERT, XSTMT1|XSTART), // assert |
|
256 |
BREAK(TokenKind.BREAK, XSTMT1|XTERM|XSTART), // break |
|
257 |
CATCH(TokenKind.CATCH, XSTMT1|XSTART), // catch |
|
258 |
CONTINUE(TokenKind.CONTINUE, XSTMT1|XTERM|XSTART), // continue |
|
259 |
DO(TokenKind.DO, XSTMT1|XSTART), // do |
|
260 |
ELSE(TokenKind.ELSE, XSTMT1|XTERM|XSTART), // else |
|
261 |
FINALLY(TokenKind.FINALLY, XSTMT1|XSTART), // finally |
|
262 |
FOR(TokenKind.FOR, XSTMT1|XSTART), // for |
|
263 |
IF(TokenKind.IF, XSTMT1|XSTART), // if |
|
264 |
RETURN(TokenKind.RETURN, XSTMT1|XTERM|XSTART), // return |
|
265 |
SWITCH(TokenKind.SWITCH, XSTMT1|XSTART), // switch |
|
266 |
SYNCHRONIZED(TokenKind.SYNCHRONIZED, XSTMT1|XDECL), // synchronized |
|
267 |
THROW(TokenKind.THROW, XSTMT1|XSTART), // throw |
|
268 |
TRY(TokenKind.TRY, XSTMT1|XSTART), // try |
|
269 |
WHILE(TokenKind.WHILE, XSTMT1|XSTART), // while |
|
270 |
||
271 |
// Statement keywords that we shouldn't see -- inside braces |
|
272 |
CASE(TokenKind.CASE, XSTMT|XSTART), // case |
|
273 |
DEFAULT(TokenKind.DEFAULT, XSTMT|XSTART), // default method, default case -- neither we should see |
|
274 |
||
275 |
// Expressions (can terminate) |
|
276 |
INTLITERAL(TokenKind.INTLITERAL, XEXPR1|XTERM), // |
|
277 |
LONGLITERAL(TokenKind.LONGLITERAL, XEXPR1|XTERM), // |
|
278 |
FLOATLITERAL(TokenKind.FLOATLITERAL, XEXPR1|XTERM), // |
|
279 |
DOUBLELITERAL(TokenKind.DOUBLELITERAL, XEXPR1|XTERM), // |
|
280 |
CHARLITERAL(TokenKind.CHARLITERAL, XEXPR1|XTERM), // |
|
281 |
STRINGLITERAL(TokenKind.STRINGLITERAL, XEXPR1|XTERM), // |
|
282 |
TRUE(TokenKind.TRUE, XEXPR1|XTERM), // true |
|
283 |
FALSE(TokenKind.FALSE, XEXPR1|XTERM), // false |
|
284 |
NULL(TokenKind.NULL, XEXPR1|XTERM), // null |
|
285 |
THIS(TokenKind.THIS, XEXPR1|XTERM), // this -- shouldn't see |
|
286 |
||
287 |
// Expressions maybe terminate //TODO handle these case separately |
|
288 |
PLUSPLUS(TokenKind.PLUSPLUS, XEXPR1|XTERM), // ++ |
|
289 |
SUBSUB(TokenKind.SUBSUB, XEXPR1|XTERM), // -- |
|
290 |
||
291 |
// Expressions cannot terminate |
|
292 |
INSTANCEOF(TokenKind.INSTANCEOF, XEXPR), // instanceof |
|
40771 | 293 |
NEW(TokenKind.NEW, XEXPR1), // new (MAPPED: COLCOLNEW) |
33362 | 294 |
SUPER(TokenKind.SUPER, XEXPR1|XDECL), // super -- shouldn't see as rec. But in type parameters |
295 |
ARROW(TokenKind.ARROW, XEXPR), // -> |
|
296 |
COLCOL(TokenKind.COLCOL, XEXPR), // :: |
|
297 |
LPAREN(TokenKind.LPAREN, XEXPR), // ( |
|
298 |
RPAREN(TokenKind.RPAREN, XEXPR), // ) |
|
299 |
LBRACE(TokenKind.LBRACE, XEXPR), // { |
|
300 |
RBRACE(TokenKind.RBRACE, XEXPR), // } |
|
301 |
LBRACKET(TokenKind.LBRACKET, XEXPR), // [ |
|
302 |
RBRACKET(TokenKind.RBRACKET, XEXPR), // ] |
|
303 |
ELLIPSIS(TokenKind.ELLIPSIS, XEXPR), // ... |
|
304 |
EQ(TokenKind.EQ, XEXPR), // = |
|
305 |
BANG(TokenKind.BANG, XEXPR1), // ! |
|
306 |
TILDE(TokenKind.TILDE, XEXPR1), // ~ |
|
307 |
COLON(TokenKind.COLON, XEXPR|XTERM), // : |
|
308 |
EQEQ(TokenKind.EQEQ, XEXPR), // == |
|
309 |
LTEQ(TokenKind.LTEQ, XEXPR), // <= |
|
310 |
GTEQ(TokenKind.GTEQ, XEXPR), // >= |
|
311 |
BANGEQ(TokenKind.BANGEQ, XEXPR), // != |
|
312 |
AMPAMP(TokenKind.AMPAMP, XEXPR), // && |
|
313 |
BARBAR(TokenKind.BARBAR, XEXPR), // || |
|
314 |
PLUS(TokenKind.PLUS, XEXPR1), // + |
|
315 |
SUB(TokenKind.SUB, XEXPR1), // - |
|
316 |
SLASH(TokenKind.SLASH, XEXPR), // / |
|
317 |
BAR(TokenKind.BAR, XEXPR), // | |
|
318 |
CARET(TokenKind.CARET, XEXPR), // ^ |
|
319 |
PERCENT(TokenKind.PERCENT, XEXPR), // % |
|
320 |
PLUSEQ(TokenKind.PLUSEQ, XEXPR), // += |
|
321 |
SUBEQ(TokenKind.SUBEQ, XEXPR), // -= |
|
322 |
STAREQ(TokenKind.STAREQ, XEXPR), // *= |
|
323 |
SLASHEQ(TokenKind.SLASHEQ, XEXPR), // /= |
|
324 |
AMPEQ(TokenKind.AMPEQ, XEXPR), // &= |
|
325 |
BAREQ(TokenKind.BAREQ, XEXPR), // |= |
|
326 |
CARETEQ(TokenKind.CARETEQ, XEXPR), // ^= |
|
327 |
PERCENTEQ(TokenKind.PERCENTEQ, XEXPR), // %= |
|
328 |
LTLTEQ(TokenKind.LTLTEQ, XEXPR), // <<= |
|
329 |
GTGTEQ(TokenKind.GTGTEQ, XEXPR), // >>= |
|
330 |
GTGTGTEQ(TokenKind.GTGTGTEQ, XEXPR), // >>>= |
|
331 |
||
332 |
// combined/processed kinds |
|
333 |
UNMATCHED(XERRO), |
|
334 |
PARENS(XEXPR1|XDECL|XSTMT|XTERM), |
|
335 |
BRACKETS(XEXPR|XDECL|XTERM), |
|
40771 | 336 |
BRACES(XSTMT1|XEXPR|XTERM), |
337 |
DOTSTAR(XDECL|XTERM), // import foo.* |
|
338 |
COLCOLNEW(XEXPR|XTERM), // :: new |
|
339 |
DOTCLASS(XEXPR|XTERM), // class decl and .class |
|
340 |
; |
|
33362 | 341 |
|
342 |
static final EnumMap<TokenKind,TK> tokenKindToTKMap = new EnumMap<>(TokenKind.class); |
|
343 |
||
344 |
final TokenKind tokenKind; |
|
345 |
final int belongs; |
|
40771 | 346 |
Function<TK,TK> mapping; |
33362 | 347 |
|
348 |
TK(int b) { |
|
40771 | 349 |
this(null, b); |
33362 | 350 |
} |
351 |
||
352 |
TK(TokenKind tokenKind, int b) { |
|
353 |
this.tokenKind = tokenKind; |
|
354 |
this.belongs = b; |
|
40771 | 355 |
this.mapping = null; |
33362 | 356 |
} |
357 |
||
40771 | 358 |
private static TK tokenKindToTK(TK prev, TokenKind kind) { |
33362 | 359 |
TK tk = tokenKindToTKMap.get(kind); |
360 |
if (tk == null) { |
|
361 |
System.err.printf("No corresponding %s for %s: %s\n", |
|
362 |
TK.class.getCanonicalName(), |
|
363 |
TokenKind.class.getCanonicalName(), |
|
364 |
kind); |
|
365 |
throw new InternalError("No corresponding TK for TokenKind: " + kind); |
|
366 |
} |
|
40771 | 367 |
return tk.mapping != null |
368 |
? tk.mapping.apply(prev) |
|
369 |
: tk; |
|
33362 | 370 |
} |
371 |
||
372 |
boolean isOkToTerminate() { |
|
373 |
return (belongs & XTERM) != 0; |
|
374 |
} |
|
375 |
||
376 |
boolean isExpression() { |
|
377 |
return (belongs & XEXPR) != 0; |
|
378 |
} |
|
379 |
||
380 |
boolean isDeclaration() { |
|
381 |
return (belongs & XDECL) != 0; |
|
382 |
} |
|
383 |
||
384 |
boolean isError() { |
|
385 |
return (belongs & XERRO) != 0; |
|
386 |
} |
|
387 |
||
388 |
boolean isStart() { |
|
389 |
return (belongs & XSTART) != 0; |
|
390 |
} |
|
391 |
||
41450
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
392 |
boolean isBracesNeeded() { |
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
393 |
return (belongs & XBRACESNEEDED) != 0; |
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
394 |
} |
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
395 |
|
33362 | 396 |
/** |
397 |
* After construction, check that all compiler TokenKind values have |
|
398 |
* corresponding TK values. |
|
399 |
*/ |
|
400 |
static { |
|
401 |
for (TK tk : TK.values()) { |
|
402 |
if (tk.tokenKind != null) { |
|
403 |
tokenKindToTKMap.put(tk.tokenKind, tk); |
|
404 |
} |
|
405 |
} |
|
406 |
for (TokenKind kind : TokenKind.values()) { |
|
40771 | 407 |
tokenKindToTK(null, kind); // assure they can be retrieved without error |
33362 | 408 |
} |
40771 | 409 |
// Mappings of disambiguated contexts |
410 |
STAR.mapping = prev -> prev == DOT ? DOTSTAR : STAR; |
|
411 |
NEW.mapping = prev -> prev == COLCOL ? COLCOLNEW : NEW; |
|
412 |
CLASS.mapping = prev -> prev == DOT ? DOTCLASS : CLASS; |
|
33362 | 413 |
} |
414 |
} |
|
415 |
||
416 |
/** |
|
417 |
* A completeness scanner token. |
|
418 |
*/ |
|
419 |
private static class CT { |
|
420 |
||
421 |
/** The token kind */ |
|
422 |
public final TK kind; |
|
423 |
||
424 |
/** The end position of this token */ |
|
425 |
public final int endPos; |
|
426 |
||
427 |
/** The error message **/ |
|
428 |
public final String message; |
|
429 |
||
430 |
private CT(TK tk, Token tok, String msg) { |
|
431 |
this.kind = tk; |
|
432 |
this.endPos = tok.endPos; |
|
433 |
this.message = msg; |
|
434 |
//throw new InternalError(msg); /* for debugging */ |
|
435 |
} |
|
436 |
||
437 |
private CT(TK tk, Token tok) { |
|
438 |
this.kind = tk; |
|
439 |
this.endPos = tok.endPos; |
|
440 |
this.message = null; |
|
441 |
} |
|
442 |
||
443 |
private CT(TK tk, int endPos) { |
|
444 |
this.kind = tk; |
|
445 |
this.endPos = endPos; |
|
446 |
this.message = null; |
|
447 |
} |
|
448 |
} |
|
449 |
||
450 |
/** |
|
451 |
* Look for matching tokens (like parens) and other special cases, like "new" |
|
452 |
*/ |
|
453 |
private static class Matched implements Iterator<CT> { |
|
454 |
||
455 |
private final Scanner scanner; |
|
456 |
private Token current; |
|
457 |
private CT prevCT; |
|
458 |
private CT currentCT; |
|
459 |
private final Deque<Token> stack = new ArrayDeque<>(); |
|
460 |
||
461 |
Matched(Scanner scanner) { |
|
462 |
this.scanner = scanner; |
|
463 |
advance(); |
|
464 |
prevCT = currentCT = new CT(SEMI, 0); // So is valid for testing |
|
465 |
} |
|
466 |
||
467 |
@Override |
|
468 |
public boolean hasNext() { |
|
469 |
return currentCT.kind != EOF; |
|
470 |
} |
|
471 |
||
472 |
private Token advance() { |
|
473 |
Token prev = current; |
|
474 |
scanner.nextToken(); |
|
475 |
current = scanner.token(); |
|
476 |
return prev; |
|
477 |
} |
|
478 |
||
479 |
@Override |
|
480 |
public CT next() { |
|
481 |
prevCT = currentCT; |
|
482 |
currentCT = nextCT(); |
|
483 |
return currentCT; |
|
484 |
} |
|
485 |
||
486 |
private CT match(TK tk, TokenKind open) { |
|
487 |
Token tok = advance(); |
|
488 |
db("match desired-tk=%s, open=%s, seen-tok=%s", tk, open, tok.kind); |
|
489 |
if (stack.isEmpty()) { |
|
490 |
return new CT(ERROR, tok, "Encountered '" + tok + "' with no opening '" + open + "'"); |
|
491 |
} |
|
492 |
Token p = stack.pop(); |
|
493 |
if (p.kind != open) { |
|
494 |
return new CT(ERROR, tok, "No match for '" + p + "' instead encountered '" + tok + "'"); |
|
495 |
} |
|
496 |
return new CT(tk, tok); |
|
497 |
} |
|
498 |
||
499 |
private void db(String format, Object ... args) { |
|
500 |
// System.err.printf(format, args); |
|
501 |
// System.err.printf(" -- stack("); |
|
502 |
// if (stack.isEmpty()) { |
|
503 |
// |
|
504 |
// } else { |
|
505 |
// for (Token tok : stack) { |
|
506 |
// System.err.printf("%s ", tok.kind); |
|
507 |
// } |
|
508 |
// } |
|
509 |
// System.err.printf(") current=%s / currentCT=%s\n", current.kind, currentCT.kind); |
|
510 |
} |
|
511 |
||
512 |
/** |
|
513 |
* @return the next scanner token |
|
514 |
*/ |
|
515 |
private CT nextCT() { |
|
516 |
// TODO Annotations? |
|
517 |
TK prevTK = currentCT.kind; |
|
518 |
while (true) { |
|
519 |
db("nextCT"); |
|
520 |
CT ct; |
|
521 |
switch (current.kind) { |
|
522 |
case EOF: |
|
523 |
db("eof"); |
|
524 |
if (stack.isEmpty()) { |
|
525 |
ct = new CT(EOF, current); |
|
526 |
} else { |
|
527 |
TokenKind unmatched = stack.pop().kind; |
|
528 |
stack.clear(); // So we will get EOF next time |
|
529 |
ct = new CT(UNMATCHED, current, "Unmatched " + unmatched); |
|
530 |
} |
|
531 |
break; |
|
532 |
case LPAREN: |
|
533 |
case LBRACE: |
|
534 |
case LBRACKET: |
|
535 |
stack.push(advance()); |
|
536 |
prevTK = SEMI; // new start |
|
537 |
continue; |
|
538 |
case RPAREN: |
|
539 |
ct = match(PARENS, TokenKind.LPAREN); |
|
540 |
break; |
|
541 |
case RBRACE: |
|
542 |
ct = match(BRACES, TokenKind.LBRACE); |
|
543 |
break; |
|
544 |
case RBRACKET: |
|
545 |
ct = match(BRACKETS, TokenKind.LBRACKET); |
|
546 |
break; |
|
547 |
default: |
|
40771 | 548 |
ct = new CT(TK.tokenKindToTK(prevTK, current.kind), advance()); |
33362 | 549 |
break; |
550 |
} |
|
42267
a0c712fc6575
8114842: JShell: SourceCodeAnalysis splits code with array initialiazer incorrectly
rfield
parents:
41450
diff
changeset
|
551 |
// Detect an error if we are at starting position and the last |
a0c712fc6575
8114842: JShell: SourceCodeAnalysis splits code with array initialiazer incorrectly
rfield
parents:
41450
diff
changeset
|
552 |
// token wasn't a terminating one. Special case: within braces, |
a0c712fc6575
8114842: JShell: SourceCodeAnalysis splits code with array initialiazer incorrectly
rfield
parents:
41450
diff
changeset
|
553 |
// comma can proceed semicolon, e.g. the values list in enum |
a0c712fc6575
8114842: JShell: SourceCodeAnalysis splits code with array initialiazer incorrectly
rfield
parents:
41450
diff
changeset
|
554 |
if (ct.kind.isStart() && !prevTK.isOkToTerminate() && prevTK != COMMA) { |
33362 | 555 |
return new CT(ERROR, current, "No '" + prevTK + "' before '" + ct.kind + "'"); |
556 |
} |
|
557 |
if (stack.isEmpty() || ct.kind.isError()) { |
|
558 |
return ct; |
|
559 |
} |
|
560 |
prevTK = ct.kind; |
|
561 |
} |
|
562 |
} |
|
563 |
} |
|
564 |
||
565 |
/** |
|
566 |
* Fuzzy parser based on token kinds |
|
567 |
*/ |
|
568 |
private static class Parser { |
|
569 |
||
40771 | 570 |
private final Supplier<Matched> matchedFactory; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
571 |
private final Function<Worker<ParseTask, Completeness>, Completeness> parseFactory; |
40771 | 572 |
private Matched in; |
573 |
private CT token; |
|
574 |
private Completeness checkResult; |
|
33362 | 575 |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
576 |
Parser(Supplier<Matched> matchedFactory, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
577 |
Function<Worker<ParseTask, Completeness>, Completeness> parseFactory) { |
40771 | 578 |
this.matchedFactory = matchedFactory; |
579 |
this.parseFactory = parseFactory; |
|
580 |
resetInput(); |
|
581 |
} |
|
33362 | 582 |
|
40771 | 583 |
final void resetInput() { |
584 |
this.in = matchedFactory.get(); |
|
33362 | 585 |
nextToken(); |
586 |
} |
|
587 |
||
588 |
final void nextToken() { |
|
589 |
in.next(); |
|
590 |
token = in.currentCT; |
|
591 |
} |
|
592 |
||
593 |
boolean shouldAbort(TK tk) { |
|
594 |
if (token.kind == tk) { |
|
595 |
nextToken(); |
|
596 |
return false; |
|
597 |
} |
|
598 |
switch (token.kind) { |
|
599 |
case EOF: |
|
600 |
checkResult = ((tk == SEMI) && in.prevCT.kind.isOkToTerminate()) |
|
601 |
? Completeness.COMPLETE_WITH_SEMI |
|
602 |
: Completeness.DEFINITELY_INCOMPLETE; |
|
603 |
return true; |
|
604 |
case UNMATCHED: |
|
605 |
checkResult = Completeness.DEFINITELY_INCOMPLETE; |
|
606 |
return true; |
|
607 |
default: |
|
608 |
checkResult = error(); |
|
609 |
return true; |
|
610 |
||
611 |
} |
|
612 |
} |
|
613 |
||
614 |
Completeness lastly(TK tk) { |
|
615 |
if (shouldAbort(tk)) return checkResult; |
|
616 |
return Completeness.COMPLETE; |
|
617 |
} |
|
618 |
||
619 |
Completeness optionalFinalSemi() { |
|
620 |
if (!shouldAbort(SEMI)) return Completeness.COMPLETE; |
|
621 |
if (checkResult == Completeness.COMPLETE_WITH_SEMI) return Completeness.COMPLETE; |
|
622 |
return checkResult; |
|
623 |
} |
|
624 |
||
625 |
boolean shouldAbort(Completeness flags) { |
|
626 |
checkResult = flags; |
|
627 |
return flags != Completeness.COMPLETE; |
|
628 |
} |
|
629 |
||
40771 | 630 |
public int endPos() { |
631 |
return in.prevCT.endPos; |
|
632 |
} |
|
633 |
||
33362 | 634 |
public Completeness parseUnit() { |
635 |
//System.err.printf("%s: belongs %o XANY1 %o\n", token.kind, token.kind.belongs, token.kind.belongs & XANY1); |
|
636 |
switch (token.kind.belongs & XANY1) { |
|
637 |
case XEXPR1o: |
|
638 |
return parseExpressionOptionalSemi(); |
|
639 |
case XSTMT1o: { |
|
640 |
Completeness stat = parseSimpleStatement(); |
|
641 |
return stat==null? error() : stat; |
|
642 |
} |
|
643 |
case XDECL1o: |
|
644 |
return parseDeclaration(); |
|
645 |
case XSTMT1o | XDECL1o: |
|
646 |
case XEXPR1o | XDECL1o: |
|
647 |
return disambiguateDeclarationVsExpression(); |
|
648 |
case 0: |
|
649 |
if ((token.kind.belongs & XERRO) != 0) { |
|
650 |
return parseExpressionStatement(); // Let this gen the status |
|
651 |
} |
|
652 |
return error(); |
|
653 |
default: |
|
654 |
throw new InternalError("Case not covered " + token.kind.belongs + " in " + token.kind); |
|
655 |
} |
|
656 |
} |
|
657 |
||
658 |
public Completeness parseDeclaration() { |
|
659 |
boolean isImport = token.kind == IMPORT; |
|
41450
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
660 |
boolean isBracesNeeded = false; |
33362 | 661 |
while (token.kind.isDeclaration()) { |
41450
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
662 |
isBracesNeeded |= token.kind.isBracesNeeded(); |
33362 | 663 |
nextToken(); |
664 |
} |
|
665 |
switch (token.kind) { |
|
666 |
case EQ: |
|
667 |
nextToken(); |
|
668 |
return parseExpressionStatement(); |
|
669 |
case BRACES: |
|
670 |
case SEMI: |
|
671 |
nextToken(); |
|
672 |
return Completeness.COMPLETE; |
|
673 |
case UNMATCHED: |
|
674 |
nextToken(); |
|
675 |
return Completeness.DEFINITELY_INCOMPLETE; |
|
676 |
case EOF: |
|
677 |
switch (in.prevCT.kind) { |
|
678 |
case BRACES: |
|
679 |
case SEMI: |
|
680 |
return Completeness.COMPLETE; |
|
681 |
case IDENTIFIER: |
|
41450
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
682 |
return isBracesNeeded |
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
683 |
? Completeness.DEFINITELY_INCOMPLETE |
83877f4dd010
8167343: JShell: Completeness analysis infers an incomplete declaration as COMPLETE_WITH_SEMI, which is a first line of Allman style
shinyafox
parents:
40771
diff
changeset
|
684 |
: Completeness.COMPLETE_WITH_SEMI; |
33362 | 685 |
case BRACKETS: |
686 |
return Completeness.COMPLETE_WITH_SEMI; |
|
40771 | 687 |
case DOTSTAR: |
33362 | 688 |
if (isImport) { |
689 |
return Completeness.COMPLETE_WITH_SEMI; |
|
690 |
} else { |
|
40771 | 691 |
return Completeness.UNKNOWN; |
33362 | 692 |
} |
693 |
default: |
|
694 |
return Completeness.DEFINITELY_INCOMPLETE; |
|
695 |
} |
|
696 |
default: |
|
697 |
return error(); |
|
698 |
} |
|
699 |
} |
|
700 |
||
701 |
public Completeness disambiguateDeclarationVsExpression() { |
|
702 |
// String folding messes up position information. |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
703 |
return parseFactory.apply(pt -> { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
704 |
List<? extends Tree> units = pt.units(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
705 |
if (units.isEmpty()) { |
33362 | 706 |
return error(); |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
707 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
708 |
Tree unitTree = units.get(0); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
709 |
switch (unitTree.getKind()) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
710 |
case EXPRESSION_STATEMENT: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
711 |
return parseExpressionOptionalSemi(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
712 |
case LABELED_STATEMENT: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
713 |
if (shouldAbort(IDENTIFIER)) return checkResult; |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
714 |
if (shouldAbort(COLON)) return checkResult; |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
715 |
return parseStatement(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
716 |
case VARIABLE: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
717 |
case IMPORT: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
718 |
case CLASS: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
719 |
case ENUM: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
720 |
case ANNOTATION_TYPE: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
721 |
case INTERFACE: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
722 |
case METHOD: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
723 |
return parseDeclaration(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
724 |
default: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
725 |
return error(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
726 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47216
diff
changeset
|
727 |
}); |
33362 | 728 |
} |
729 |
||
730 |
public Completeness parseExpressionStatement() { |
|
731 |
if (shouldAbort(parseExpression())) return checkResult; |
|
732 |
return lastly(SEMI); |
|
733 |
} |
|
734 |
||
735 |
public Completeness parseExpressionOptionalSemi() { |
|
736 |
if (shouldAbort(parseExpression())) return checkResult; |
|
737 |
return optionalFinalSemi(); |
|
738 |
} |
|
739 |
||
740 |
public Completeness parseExpression() { |
|
741 |
while (token.kind.isExpression()) |
|
742 |
nextToken(); |
|
743 |
return Completeness.COMPLETE; |
|
744 |
} |
|
745 |
||
746 |
public Completeness parseStatement() { |
|
747 |
Completeness stat = parseSimpleStatement(); |
|
748 |
if (stat == null) { |
|
749 |
return parseExpressionStatement(); |
|
750 |
} |
|
751 |
return stat; |
|
752 |
} |
|
753 |
||
754 |
/** |
|
755 |
* Statement = Block | IF ParExpression Statement [ELSE Statement] | FOR |
|
756 |
* "(" ForInitOpt ";" [Expression] ";" ForUpdateOpt ")" Statement | FOR |
|
757 |
* "(" FormalParameter : Expression ")" Statement | WHILE ParExpression |
|
758 |
* Statement | DO Statement WHILE ParExpression ";" | TRY Block ( |
|
759 |
* Catches | [Catches] FinallyPart ) | TRY "(" ResourceSpecification |
|
760 |
* ";"opt ")" Block [Catches] [FinallyPart] | SWITCH ParExpression "{" |
|
761 |
* SwitchBlockStatementGroups "}" | SYNCHRONIZED ParExpression Block | |
|
762 |
* RETURN [Expression] ";" | THROW Expression ";" | BREAK [Ident] ";" | |
|
763 |
* CONTINUE [Ident] ";" | ASSERT Expression [ ":" Expression ] ";" | ";" |
|
764 |
*/ |
|
765 |
public Completeness parseSimpleStatement() { |
|
766 |
switch (token.kind) { |
|
767 |
case BRACES: |
|
768 |
return lastly(BRACES); |
|
769 |
case IF: { |
|
770 |
nextToken(); |
|
771 |
if (shouldAbort(PARENS)) return checkResult; |
|
772 |
Completeness thenpart = parseStatement(); |
|
773 |
if (shouldAbort(thenpart)) return thenpart; |
|
774 |
if (token.kind == ELSE) { |
|
775 |
nextToken(); |
|
776 |
return parseStatement(); |
|
777 |
} |
|
778 |
return thenpart; |
|
779 |
||
780 |
} |
|
781 |
case FOR: { |
|
782 |
nextToken(); |
|
783 |
if (shouldAbort(PARENS)) return checkResult; |
|
784 |
if (shouldAbort(parseStatement())) return checkResult; |
|
785 |
return Completeness.COMPLETE; |
|
786 |
} |
|
787 |
case WHILE: { |
|
788 |
nextToken(); |
|
789 |
if (shouldAbort(PARENS)) return error(); |
|
790 |
return parseStatement(); |
|
791 |
} |
|
792 |
case DO: { |
|
793 |
nextToken(); |
|
794 |
switch (parseStatement()) { |
|
795 |
case DEFINITELY_INCOMPLETE: |
|
796 |
case CONSIDERED_INCOMPLETE: |
|
797 |
case COMPLETE_WITH_SEMI: |
|
798 |
return Completeness.DEFINITELY_INCOMPLETE; |
|
799 |
case UNKNOWN: |
|
800 |
return error(); |
|
801 |
case COMPLETE: |
|
802 |
break; |
|
803 |
} |
|
804 |
if (shouldAbort(WHILE)) return checkResult; |
|
805 |
if (shouldAbort(PARENS)) return checkResult; |
|
806 |
return lastly(SEMI); |
|
807 |
} |
|
808 |
case TRY: { |
|
809 |
boolean hasResources = false; |
|
810 |
nextToken(); |
|
811 |
if (token.kind == PARENS) { |
|
812 |
nextToken(); |
|
813 |
hasResources = true; |
|
814 |
} |
|
815 |
if (shouldAbort(BRACES)) return checkResult; |
|
816 |
if (token.kind == CATCH || token.kind == FINALLY) { |
|
817 |
while (token.kind == CATCH) { |
|
818 |
if (shouldAbort(CATCH)) return checkResult; |
|
819 |
if (shouldAbort(PARENS)) return checkResult; |
|
820 |
if (shouldAbort(BRACES)) return checkResult; |
|
821 |
} |
|
822 |
if (token.kind == FINALLY) { |
|
823 |
if (shouldAbort(FINALLY)) return checkResult; |
|
824 |
if (shouldAbort(BRACES)) return checkResult; |
|
825 |
} |
|
826 |
} else if (!hasResources) { |
|
827 |
if (token.kind == EOF) { |
|
828 |
return Completeness.DEFINITELY_INCOMPLETE; |
|
829 |
} else { |
|
830 |
return error(); |
|
831 |
} |
|
832 |
} |
|
833 |
return Completeness.COMPLETE; |
|
834 |
} |
|
835 |
case SWITCH: { |
|
836 |
nextToken(); |
|
837 |
if (shouldAbort(PARENS)) return checkResult; |
|
838 |
return lastly(BRACES); |
|
839 |
} |
|
840 |
case SYNCHRONIZED: { |
|
841 |
nextToken(); |
|
842 |
if (shouldAbort(PARENS)) return checkResult; |
|
843 |
return lastly(BRACES); |
|
844 |
} |
|
845 |
case THROW: { |
|
846 |
nextToken(); |
|
847 |
if (shouldAbort(parseExpression())) return checkResult; |
|
848 |
return lastly(SEMI); |
|
849 |
} |
|
850 |
case SEMI: |
|
851 |
return lastly(SEMI); |
|
852 |
case ASSERT: |
|
853 |
nextToken(); |
|
854 |
// Crude expression parsing just happily eats the optional colon |
|
855 |
return parseExpressionStatement(); |
|
856 |
case RETURN: |
|
857 |
case BREAK: |
|
858 |
case CONTINUE: |
|
859 |
nextToken(); |
|
860 |
return parseExpressionStatement(); |
|
861 |
// What are these doing here? |
|
862 |
case ELSE: |
|
863 |
case FINALLY: |
|
864 |
case CATCH: |
|
865 |
return error(); |
|
866 |
case EOF: |
|
867 |
return Completeness.CONSIDERED_INCOMPLETE; |
|
868 |
default: |
|
869 |
return null; |
|
870 |
} |
|
871 |
} |
|
872 |
} |
|
873 |
} |