author | hseigel |
Wed, 01 Mar 2017 08:00:02 -0500 | |
changeset 46194 | 5596e6f63072 |
parent 43586 | cc7a4eb79b29 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
2 |
* Copyright (c) 2014, 2017, 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 |
*/ |
|
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
33362
diff
changeset
|
25 |
|
33362 | 26 |
package jdk.jshell; |
27 |
||
28 |
import com.sun.tools.javac.code.TypeTag; |
|
29 |
import com.sun.tools.javac.parser.JavacParser; |
|
30 |
import com.sun.tools.javac.parser.ParserFactory; |
|
31 |
import com.sun.tools.javac.parser.Tokens.Comment; |
|
32 |
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle; |
|
33 |
import com.sun.tools.javac.parser.Tokens.Token; |
|
34 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.CLASS; |
|
35 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.COLON; |
|
36 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.ENUM; |
|
37 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.EOF; |
|
38 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.IMPORT; |
|
39 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.INTERFACE; |
|
40 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.LPAREN; |
|
41 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.MONKEYS_AT; |
|
42 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.SEMI; |
|
43 |
import static com.sun.tools.javac.parser.Tokens.TokenKind.VOID; |
|
44 |
import com.sun.tools.javac.tree.JCTree; |
|
45 |
import com.sun.tools.javac.tree.JCTree.JCAnnotation; |
|
46 |
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
|
47 |
import com.sun.tools.javac.tree.JCTree.JCExpression; |
|
48 |
import com.sun.tools.javac.tree.JCTree.JCExpressionStatement; |
|
49 |
import com.sun.tools.javac.tree.JCTree.JCModifiers; |
|
50 |
import com.sun.tools.javac.tree.JCTree.JCStatement; |
|
51 |
import com.sun.tools.javac.tree.JCTree.JCTypeParameter; |
|
52 |
import com.sun.tools.javac.tree.JCTree.Tag; |
|
53 |
import static com.sun.tools.javac.tree.JCTree.Tag.IDENT; |
|
54 |
import com.sun.tools.javac.util.List; |
|
55 |
import com.sun.tools.javac.util.ListBuffer; |
|
56 |
import com.sun.tools.javac.util.Name; |
|
57 |
import com.sun.tools.javac.util.Position; |
|
58 |
||
59 |
/** |
|
60 |
* This is a subclass of JavacParser which overrides one method with a modified |
|
61 |
* verson of that method designed to allow parsing of one "snippet" of Java |
|
62 |
* code without the surrounding context of class, method, etc. |
|
63 |
* Accepts an expression, a statement, an import, or the declaration of a |
|
64 |
* method, variable, or type (class, interface, ...). |
|
65 |
*/ |
|
66 |
class ReplParser extends JavacParser { |
|
67 |
||
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
68 |
// force starting in expression mode |
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
69 |
private final boolean forceExpression; |
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
70 |
|
33362 | 71 |
public ReplParser(ParserFactory fac, |
72 |
com.sun.tools.javac.parser.Lexer S, |
|
73 |
boolean keepDocComments, |
|
74 |
boolean keepLineMap, |
|
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
75 |
boolean keepEndPositions, |
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
76 |
boolean forceExpression) { |
33362 | 77 |
super(fac, S, keepDocComments, keepLineMap, keepEndPositions); |
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
78 |
this.forceExpression = forceExpression; |
33362 | 79 |
} |
80 |
||
81 |
/** |
|
82 |
* As faithful a clone of the overridden method as possible while still |
|
83 |
* achieving the goal of allowing the parse of a stand-alone snippet. |
|
84 |
* As a result, some variables are assigned and never used, tests are |
|
85 |
* always true, loops don't, etc. This is to allow easy transition as the |
|
86 |
* underlying method changes. |
|
87 |
* @return a snippet wrapped in a compilation unit |
|
88 |
*/ |
|
89 |
@Override |
|
90 |
public JCCompilationUnit parseCompilationUnit() { |
|
91 |
Token firstToken = token; |
|
92 |
JCModifiers mods = null; |
|
93 |
boolean seenImport = false; |
|
94 |
boolean seenPackage = false; |
|
95 |
ListBuffer<JCTree> defs = new ListBuffer<>(); |
|
96 |
if (token.kind == MONKEYS_AT) { |
|
97 |
mods = modifiersOpt(); |
|
98 |
} |
|
99 |
||
100 |
boolean firstTypeDecl = true; |
|
101 |
while (token.kind != EOF) { |
|
102 |
if (token.pos > 0 && token.pos <= endPosTable.errorEndPos) { |
|
103 |
// error recovery |
|
104 |
skip(true, false, false, false); |
|
105 |
if (token.kind == EOF) { |
|
106 |
break; |
|
107 |
} |
|
108 |
} |
|
109 |
if (mods == null && token.kind == IMPORT) { |
|
110 |
seenImport = true; |
|
111 |
defs.append(importDeclaration()); |
|
112 |
} else { |
|
113 |
Comment docComment = token.comment(CommentStyle.JAVADOC); |
|
114 |
if (firstTypeDecl && !seenImport && !seenPackage) { |
|
115 |
docComment = firstToken.comment(CommentStyle.JAVADOC); |
|
116 |
} |
|
117 |
List<? extends JCTree> udefs = replUnit(mods, docComment); |
|
118 |
// if (def instanceof JCExpressionStatement) |
|
119 |
// def = ((JCExpressionStatement)def).expr; |
|
120 |
for (JCTree def : udefs) { |
|
121 |
defs.append(def); |
|
122 |
} |
|
123 |
mods = null; |
|
124 |
firstTypeDecl = false; |
|
125 |
} |
|
126 |
break; // Remove to process more than one snippet |
|
127 |
} |
|
128 |
List<JCTree> rdefs = defs.toList(); |
|
129 |
class ReplUnit extends JCCompilationUnit { |
|
130 |
||
131 |
public ReplUnit(List<JCTree> defs) { |
|
132 |
super(defs); |
|
133 |
} |
|
134 |
} |
|
135 |
JCCompilationUnit toplevel = new ReplUnit(rdefs); |
|
136 |
if (rdefs.isEmpty()) { |
|
137 |
storeEnd(toplevel, S.prevToken().endPos); |
|
138 |
} |
|
139 |
toplevel.lineMap = S.getLineMap(); |
|
140 |
this.endPosTable.setParser(null); // remove reference to parser |
|
141 |
toplevel.endPositions = this.endPosTable; |
|
142 |
return toplevel; |
|
143 |
} |
|
144 |
||
145 |
@SuppressWarnings("fallthrough") |
|
146 |
List<? extends JCTree> replUnit(JCModifiers pmods, Comment dc) { |
|
147 |
switch (token.kind) { |
|
148 |
case EOF: |
|
149 |
return List.nil(); |
|
150 |
case RBRACE: |
|
151 |
case CASE: |
|
152 |
case DEFAULT: |
|
153 |
// These are illegal, fall through to handle as illegal statement |
|
154 |
case LBRACE: |
|
155 |
case IF: |
|
156 |
case FOR: |
|
157 |
case WHILE: |
|
158 |
case DO: |
|
159 |
case TRY: |
|
160 |
case SWITCH: |
|
161 |
case RETURN: |
|
162 |
case THROW: |
|
163 |
case BREAK: |
|
164 |
case CONTINUE: |
|
165 |
case SEMI: |
|
166 |
case ELSE: |
|
167 |
case FINALLY: |
|
168 |
case CATCH: |
|
169 |
case ASSERT: |
|
170 |
return List.<JCTree>of(parseStatement()); |
|
171 |
case SYNCHRONIZED: |
|
172 |
if (peekToken(LPAREN)) { |
|
173 |
return List.<JCTree>of(parseStatement()); |
|
174 |
} |
|
175 |
//fall-through |
|
176 |
default: |
|
177 |
JCModifiers mods = modifiersOpt(pmods); |
|
178 |
if (token.kind == CLASS |
|
179 |
|| token.kind == INTERFACE |
|
180 |
|| token.kind == ENUM) { |
|
181 |
return List.<JCTree>of(classOrInterfaceOrEnumDeclaration(mods, dc)); |
|
182 |
} else { |
|
183 |
int pos = token.pos; |
|
184 |
List<JCTypeParameter> typarams = typeParametersOpt(); |
|
185 |
// if there are type parameters but no modifiers, save the start |
|
186 |
// position of the method in the modifiers. |
|
187 |
if (typarams.nonEmpty() && mods.pos == Position.NOPOS) { |
|
188 |
mods.pos = pos; |
|
189 |
storeEnd(mods, pos); |
|
190 |
} |
|
191 |
List<JCAnnotation> annosAfterParams = annotationsOpt(Tag.ANNOTATION); |
|
192 |
||
193 |
if (annosAfterParams.nonEmpty()) { |
|
194 |
checkAnnotationsAfterTypeParams(annosAfterParams.head.pos); |
|
195 |
mods.annotations = mods.annotations.appendList(annosAfterParams); |
|
196 |
if (mods.pos == Position.NOPOS) { |
|
197 |
mods.pos = mods.annotations.head.pos; |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
Token prevToken = token; |
|
202 |
pos = token.pos; |
|
203 |
JCExpression t; |
|
204 |
boolean isVoid = token.kind == VOID; |
|
205 |
if (isVoid) { |
|
206 |
t = to(F.at(pos).TypeIdent(TypeTag.VOID)); |
|
207 |
nextToken(); |
|
208 |
} else { |
|
209 |
// return type of method, declared type of variable, or an expression |
|
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
210 |
// unless expression is being forced |
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
211 |
t = term(forceExpression |
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
212 |
? EXPR |
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
42828
diff
changeset
|
213 |
: EXPR | TYPE); |
33362 | 214 |
} |
215 |
if (token.kind == COLON && t.hasTag(IDENT)) { |
|
216 |
// labelled statement |
|
217 |
nextToken(); |
|
218 |
JCStatement stat = parseStatement(); |
|
219 |
return List.<JCTree>of(F.at(pos).Labelled(prevToken.name(), stat)); |
|
220 |
} else if ((isVoid || (lastmode & TYPE) != 0) && LAX_IDENTIFIER.accepts(token.kind)) { |
|
221 |
// we have "Type Ident", so we can assume it is variable or method declaration |
|
222 |
pos = token.pos; |
|
223 |
Name name = ident(); |
|
224 |
if (token.kind == LPAREN) { |
|
225 |
// method declaration |
|
226 |
//mods.flags |= Flags.STATIC; |
|
227 |
return List.of(methodDeclaratorRest( |
|
228 |
pos, mods, t, name, typarams, |
|
229 |
false, isVoid, dc)); |
|
230 |
} else if (!isVoid && typarams.isEmpty()) { |
|
231 |
// variable declaration |
|
232 |
//mods.flags |= Flags.STATIC; |
|
233 |
List<JCTree> defs |
|
234 |
= variableDeclaratorsRest(pos, mods, t, name, false, dc, |
|
235 |
new ListBuffer<JCTree>()).toList(); |
|
236 |
accept(SEMI); |
|
237 |
storeEnd(defs.last(), S.prevToken().endPos); |
|
238 |
return defs; |
|
239 |
} else { |
|
240 |
// malformed declaration, return error |
|
241 |
pos = token.pos; |
|
242 |
List<JCTree> err = isVoid |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
40836
diff
changeset
|
243 |
? List.of(toP(F.at(pos).MethodDef(mods, name, t, typarams, |
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
40836
diff
changeset
|
244 |
List.nil(), List.nil(), null, null))) |
33362 | 245 |
: null; |
246 |
return List.<JCTree>of(syntaxError(token.pos, err, "expected", LPAREN)); |
|
247 |
} |
|
248 |
} else if (!typarams.isEmpty()) { |
|
249 |
// type parameters on non-variable non-method -- error |
|
250 |
return List.<JCTree>of(syntaxError(token.pos, "illegal.start.of.type")); |
|
251 |
} else { |
|
252 |
// expression-statement or expression to evaluate |
|
253 |
JCExpressionStatement expr = toP(F.at(pos).Exec(t)); |
|
254 |
return List.<JCTree>of(expr); |
|
255 |
} |
|
256 |
||
257 |
} |
|
258 |
} |
|
259 |
} |
|
260 |
} |