equal
deleted
inserted
replaced
1 /* |
1 /* |
2 * Copyright (c) 2014, 2016, 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 |
59 import java.util.stream.Stream; |
59 import java.util.stream.Stream; |
60 import javax.lang.model.util.Elements; |
60 import javax.lang.model.util.Elements; |
61 import javax.tools.FileObject; |
61 import javax.tools.FileObject; |
62 import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject; |
62 import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject; |
63 import java.lang.Runtime.Version; |
63 import java.lang.Runtime.Version; |
|
64 import com.sun.source.tree.Tree.Kind; |
64 |
65 |
65 /** |
66 /** |
66 * The primary interface to the compiler API. Parsing, analysis, and |
67 * The primary interface to the compiler API. Parsing, analysis, and |
67 * compilation to class files (in memory). |
68 * compilation to class files (in memory). |
68 * @author Robert Field |
69 * @author Robert Field |
98 |
99 |
99 MemoryFileManager fileManager() { |
100 MemoryFileManager fileManager() { |
100 return fileManager; |
101 return fileManager; |
101 } |
102 } |
102 |
103 |
|
104 // Parse a snippet and return our parse task handler |
|
105 ParseTask parse(final String source) { |
|
106 ParseTask pt = state.taskFactory.new ParseTask(source, false); |
|
107 if (!pt.units().isEmpty() |
|
108 && pt.units().get(0).getKind() == Kind.EXPRESSION_STATEMENT |
|
109 && pt.getDiagnostics().hasOtherThanNotStatementErrors()) { |
|
110 // It failed, it may be an expression being incorrectly |
|
111 // parsed as having a leading type variable, example: a < b |
|
112 // Try forcing interpretation as an expression |
|
113 ParseTask ept = state.taskFactory.new ParseTask(source, true); |
|
114 if (!ept.getDiagnostics().hasOtherThanNotStatementErrors()) { |
|
115 return ept; |
|
116 } |
|
117 } |
|
118 return pt; |
|
119 } |
|
120 |
103 private interface SourceHandler<T> { |
121 private interface SourceHandler<T> { |
104 |
122 |
105 JavaFileObject sourceToFileObject(MemoryFileManager fm, T t); |
123 JavaFileObject sourceToFileObject(MemoryFileManager fm, T t); |
106 |
124 |
107 Diag diag(Diagnostic<? extends JavaFileObject> d); |
125 Diag diag(Diagnostic<? extends JavaFileObject> d); |
177 class ParseTask extends BaseTask { |
195 class ParseTask extends BaseTask { |
178 |
196 |
179 private final Iterable<? extends CompilationUnitTree> cuts; |
197 private final Iterable<? extends CompilationUnitTree> cuts; |
180 private final List<? extends Tree> units; |
198 private final List<? extends Tree> units; |
181 |
199 |
182 ParseTask(final String source) { |
200 ParseTask(final String source, final boolean forceExpression) { |
183 super(Stream.of(source), |
201 super(Stream.of(source), |
184 new StringSourceHandler(), |
202 new StringSourceHandler(), |
185 "-XDallowStringFolding=false", "-proc:none"); |
203 "-XDallowStringFolding=false", "-proc:none"); |
186 ReplParserFactory.instance(getContext()); |
204 ReplParserFactory.preRegister(getContext(), forceExpression); |
187 cuts = parse(); |
205 cuts = parse(); |
188 units = Util.stream(cuts) |
206 units = Util.stream(cuts) |
189 .flatMap(cut -> { |
207 .flatMap(cut -> { |
190 List<? extends ImportTree> imps = cut.getImports(); |
208 List<? extends ImportTree> imps = cut.getImports(); |
191 return (!imps.isEmpty() ? imps : cut.getTypeDecls()).stream(); |
209 return (!imps.isEmpty() ? imps : cut.getTypeDecls()).stream(); |