use of jline.internal.NonBlockingInputStream in project graal by oracle.
the class MultiLanguageShell method readEvalPrint.
public int readEvalPrint() throws IOException {
ConsoleReader console = new ConsoleReader(in, out);
console.setHandleUserInterrupt(true);
console.setExpandEvents(false);
console.setCopyPasteDetection(true);
console.println("GraalVM MultiLanguage Shell " + context.getEngine().getVersion());
console.println("Copyright (c) 2013-2018, Oracle and/or its affiliates");
List<Language> languages = new ArrayList<>();
Set<Language> uniqueValues = new HashSet<>();
for (Language language : context.getEngine().getLanguages().values()) {
if (language.isInteractive()) {
if (uniqueValues.add(language)) {
languages.add(language);
}
}
}
languages.sort(Comparator.comparing(Language::getName));
Map<String, Language> prompts = new HashMap<>();
StringBuilder promptsString = new StringBuilder();
for (Language language : languages) {
String prompt = createPrompt(language).trim();
promptsString.append(prompt).append(" ");
prompts.put(prompt, language);
console.println(" " + language.getName() + " version " + language.getVersion());
}
if (languages.isEmpty()) {
throw new Launcher.AbortException("Error: No Graal languages installed. Exiting shell.", 1);
}
printUsage(console, promptsString, false);
int maxNameLength = 0;
for (Language language : languages) {
maxNameLength = Math.max(maxNameLength, language.getName().length());
}
String startLanguage = defaultStartLanguage;
if (startLanguage == null) {
startLanguage = languages.get(0).getId();
}
Language currentLanguage = context.getEngine().getLanguages().get(startLanguage);
if (currentLanguage == null) {
throw new Launcher.AbortException("Error: could not find language '" + startLanguage + "'", 1);
}
assert languages.indexOf(currentLanguage) >= 0;
Source bufferSource = null;
String id = currentLanguage.getId();
// console.println("initialize time: " + (System.currentTimeMillis() - start));
String prompt = createPrompt(currentLanguage);
console.getKeys().bind(String.valueOf((char) 12), new ActionListener() {
public void actionPerformed(ActionEvent e) {
throw new ChangeLanguageException(null);
}
});
console.getKeys().bind(String.valueOf((char) 10), new ActionListener() {
public void actionPerformed(ActionEvent e) {
throw new RuntimeIncompleteSourceException();
}
});
// initializes the language
context.initialize(currentLanguage.getId());
boolean verboseErrors = false;
for (; ; ) {
String input = null;
Source source = null;
try {
input = console.readLine(bufferSource == null ? prompt : createBufferPrompt(prompt));
if (input == null) {
break;
} else if (input.trim().equals("")) {
continue;
}
Language switchedLanguage = null;
String trimmedInput = input.trim();
if (trimmedInput.equals("-usage")) {
printUsage(console, promptsString, true);
input = "";
} else if (trimmedInput.equals("-verboseErrors")) {
verboseErrors = !verboseErrors;
if (verboseErrors) {
console.println("Verbose errors is now on.");
} else {
console.println("Verbose errors is now off.");
}
input = "";
} else if (prompts.containsKey(trimmedInput)) {
switchedLanguage = prompts.get(input);
input = "";
}
NonBlockingInputStream nonBlockIn = ((NonBlockingInputStream) console.getInput());
while (nonBlockIn.isNonBlockingEnabled() && nonBlockIn.peek(10) != -2 && switchedLanguage == null) {
String line = console.readLine(createBufferPrompt(prompt));
String trimmedLine = line.trim();
if (prompts.containsKey(trimmedLine)) {
switchedLanguage = prompts.get(trimmedLine);
break;
} else {
input += "\n" + line;
}
}
if (!input.trim().equals("")) {
source = Source.newBuilder(currentLanguage.getId(), input, "<shell>").interactive(true).build();
context.eval(source);
bufferSource = null;
console.getHistory().replace(source.getCharacters());
}
if (switchedLanguage != null) {
throw new ChangeLanguageException(switchedLanguage);
}
} catch (UserInterruptException | EOFException e) {
// interrupted by ctrl-c
break;
} catch (ChangeLanguageException e) {
bufferSource = null;
histories.put(currentLanguage, console.getHistory());
currentLanguage = e.getLanguage() == null ? languages.get((languages.indexOf(currentLanguage) + 1) % languages.size()) : e.getLanguage();
History history = histories.computeIfAbsent(currentLanguage, k -> new MemoryHistory());
console.setHistory(history);
id = currentLanguage.getId();
prompt = createPrompt(currentLanguage);
console.resetPromptLine("", "", 0);
context.initialize(id);
} catch (ThreadDeath e) {
console.println("Execution killed!");
continue;
} catch (RuntimeIncompleteSourceException e) {
console.println();
input += "\n";
bufferSource = source;
} catch (PolyglotException e) {
input += "\n";
bufferSource = source;
if (e.isExit()) {
return e.getExitStatus();
} else if (e.isIncompleteSource()) {
input += "\n";
bufferSource = source;
} else if (!e.isInternalError()) {
if (e.getMessage() != null && e.getMessage().isEmpty()) {
console.println(e.toString());
} else {
if (verboseErrors) {
e.printStackTrace(new PrintWriter(console.getOutput()));
}
}
} else {
e.printStackTrace(new PrintWriter(console.getOutput()));
}
} catch (Throwable e) {
e.printStackTrace(new PrintWriter(console.getOutput()));
}
}
return 0;
}
Aggregations