1 /* |
1 /* |
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
37 import static com.sun.tools.javac.parser.Tokens.TokenKind.EOF; |
37 import static com.sun.tools.javac.parser.Tokens.TokenKind.EOF; |
38 import static com.sun.tools.javac.parser.Tokens.TokenKind.IMPORT; |
38 import static com.sun.tools.javac.parser.Tokens.TokenKind.IMPORT; |
39 import static com.sun.tools.javac.parser.Tokens.TokenKind.INTERFACE; |
39 import static com.sun.tools.javac.parser.Tokens.TokenKind.INTERFACE; |
40 import static com.sun.tools.javac.parser.Tokens.TokenKind.LPAREN; |
40 import static com.sun.tools.javac.parser.Tokens.TokenKind.LPAREN; |
41 import static com.sun.tools.javac.parser.Tokens.TokenKind.MONKEYS_AT; |
41 import static com.sun.tools.javac.parser.Tokens.TokenKind.MONKEYS_AT; |
42 import static com.sun.tools.javac.parser.Tokens.TokenKind.PACKAGE; |
|
43 import static com.sun.tools.javac.parser.Tokens.TokenKind.SEMI; |
42 import static com.sun.tools.javac.parser.Tokens.TokenKind.SEMI; |
44 import static com.sun.tools.javac.parser.Tokens.TokenKind.VOID; |
43 import static com.sun.tools.javac.parser.Tokens.TokenKind.VOID; |
45 import com.sun.tools.javac.tree.JCTree; |
44 import com.sun.tools.javac.tree.JCTree; |
46 import com.sun.tools.javac.tree.JCTree.JCAnnotation; |
45 import com.sun.tools.javac.tree.JCTree.JCAnnotation; |
47 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
46 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
48 import com.sun.tools.javac.tree.JCTree.JCExpression; |
47 import com.sun.tools.javac.tree.JCTree.JCExpression; |
49 import com.sun.tools.javac.tree.JCTree.JCExpressionStatement; |
48 import com.sun.tools.javac.tree.JCTree.JCExpressionStatement; |
50 import com.sun.tools.javac.tree.JCTree.JCModifiers; |
49 import com.sun.tools.javac.tree.JCTree.JCModifiers; |
51 import com.sun.tools.javac.tree.JCTree.JCPackageDecl; |
|
52 import com.sun.tools.javac.tree.JCTree.JCStatement; |
50 import com.sun.tools.javac.tree.JCTree.JCStatement; |
53 import com.sun.tools.javac.tree.JCTree.JCTypeParameter; |
51 import com.sun.tools.javac.tree.JCTree.JCTypeParameter; |
54 import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
|
55 import com.sun.tools.javac.tree.JCTree.Tag; |
52 import com.sun.tools.javac.tree.JCTree.Tag; |
56 import static com.sun.tools.javac.tree.JCTree.Tag.IDENT; |
53 import static com.sun.tools.javac.tree.JCTree.Tag.IDENT; |
57 import com.sun.tools.javac.util.List; |
54 import com.sun.tools.javac.util.List; |
58 import com.sun.tools.javac.util.ListBuffer; |
55 import com.sun.tools.javac.util.ListBuffer; |
59 import com.sun.tools.javac.util.Name; |
56 import com.sun.tools.javac.util.Name; |
66 * Accepts an expression, a statement, an import, or the declaration of a |
63 * Accepts an expression, a statement, an import, or the declaration of a |
67 * method, variable, or type (class, interface, ...). |
64 * method, variable, or type (class, interface, ...). |
68 */ |
65 */ |
69 class ReplParser extends JavacParser { |
66 class ReplParser extends JavacParser { |
70 |
67 |
|
68 // force starting in expression mode |
|
69 private final boolean forceExpression; |
|
70 |
71 public ReplParser(ParserFactory fac, |
71 public ReplParser(ParserFactory fac, |
72 com.sun.tools.javac.parser.Lexer S, |
72 com.sun.tools.javac.parser.Lexer S, |
73 boolean keepDocComments, |
73 boolean keepDocComments, |
74 boolean keepLineMap, |
74 boolean keepLineMap, |
75 boolean keepEndPositions) { |
75 boolean keepEndPositions, |
|
76 boolean forceExpression) { |
76 super(fac, S, keepDocComments, keepLineMap, keepEndPositions); |
77 super(fac, S, keepDocComments, keepLineMap, keepEndPositions); |
|
78 this.forceExpression = forceExpression; |
77 } |
79 } |
78 |
80 |
79 /** |
81 /** |
80 * As faithful a clone of the overridden method as possible while still |
82 * As faithful a clone of the overridden method as possible while still |
81 * achieving the goal of allowing the parse of a stand-alone snippet. |
83 * achieving the goal of allowing the parse of a stand-alone snippet. |
203 if (isVoid) { |
205 if (isVoid) { |
204 t = to(F.at(pos).TypeIdent(TypeTag.VOID)); |
206 t = to(F.at(pos).TypeIdent(TypeTag.VOID)); |
205 nextToken(); |
207 nextToken(); |
206 } else { |
208 } else { |
207 // return type of method, declared type of variable, or an expression |
209 // return type of method, declared type of variable, or an expression |
208 t = term(EXPR | TYPE); |
210 // unless expression is being forced |
|
211 t = term(forceExpression |
|
212 ? EXPR |
|
213 : EXPR | TYPE); |
209 } |
214 } |
210 if (token.kind == COLON && t.hasTag(IDENT)) { |
215 if (token.kind == COLON && t.hasTag(IDENT)) { |
211 // labelled statement |
216 // labelled statement |
212 nextToken(); |
217 nextToken(); |
213 JCStatement stat = parseStatement(); |
218 JCStatement stat = parseStatement(); |