< prev index next >

src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java

Print this page

        

*** 234,246 **** } // Parse a snippet and return our parse task handler <Z> Z parse(final String source, Worker<ParseTask, Z> worker) { return parse(source, false, pt -> { ! if (!pt.units().isEmpty() ! && pt.units().get(0).getKind() == Kind.EXPRESSION_STATEMENT ! && pt.getDiagnostics().hasOtherThanNotStatementErrors()) { // It failed, it may be an expression being incorrectly // parsed as having a leading type variable, example: a < b // Try forcing interpretation as an expression return parse(source, true, ept -> { if (!ept.getDiagnostics().hasOtherThanNotStatementErrors()) { --- 234,244 ---- } // Parse a snippet and return our parse task handler <Z> Z parse(final String source, Worker<ParseTask, Z> worker) { return parse(source, false, pt -> { ! if (shouldParseAsExpression(pt)) { // It failed, it may be an expression being incorrectly // parsed as having a leading type variable, example: a < b // Try forcing interpretation as an expression return parse(source, true, ept -> { if (!ept.getDiagnostics().hasOtherThanNotStatementErrors()) {
*** 251,260 **** --- 249,272 ---- }); } return worker.withTask(pt); }); } + // where + private boolean shouldParseAsExpression(ParseTask pt) { + if (pt.units().isEmpty() || !pt.getDiagnostics().hasOtherThanNotStatementErrors()) + return false; + Tree tree = pt.units().get(0); + if (tree.getKind() == Kind.EXPRESSION_STATEMENT) + return true; + if (tree.getKind() == Kind.VARIABLE) { + JCVariableDecl varDecl = (JCVariableDecl) tree; + if (varDecl.vartype != null && varDecl.vartype.isQuestioned()) + return true; + } + return false; + } private interface SourceHandler<T> { JavaFileObject sourceToFileObject(MemoryFileManager fm, T t);
< prev index next >