org.scijava.script.ScriptREPL Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scijava-common Show documentation
Show all versions of scijava-common Show documentation
SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2017 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
* Institute of Molecular Cell Biology and Genetics, University of
* Konstanz, and KNIME GmbH.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.script;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import javax.script.Bindings;
import javax.script.ScriptException;
import org.scijava.Context;
import org.scijava.Gateway;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.PluginInfo;
import org.scijava.plugin.PluginService;
import org.scijava.service.Service;
/**
* A REPL for SciJava script engines, which allows dynamic language switching.
*
* @author Curtis Rueden
*/
public class ScriptREPL {
private static final String NULL = "";
@Parameter
private Context context;
@Parameter
private ScriptService scriptService;
@Parameter(required = false)
private PluginService pluginService;
private final PrintStream out;
/** List of interpreter-friendly script languages. */
private List languages;
/** The currently active interpreter. */
private ScriptInterpreter interpreter;
/** Flag for debug mode. */
private boolean debug;
public ScriptREPL(final Context context) {
this(context, System.out);
}
public ScriptREPL(final Context context, final OutputStream out) {
context.inject(this);
this.out = out instanceof PrintStream ?
(PrintStream) out : new PrintStream(out);
}
/**
* Gets the list of languages compatible with the REPL.
*
* This list will match those given by {@link ScriptService#getLanguages()},
* but filtered to exclude any who report {@code true} for
* {@link ScriptLanguage#isCompiledLanguage()}.
*
*/
public List getInterpretedLanguages() {
if (languages == null) initLanguages();
return languages;
}
/** Gets the script interpreter for the currently active language. */
public ScriptInterpreter getInterpreter() {
return interpreter;
}
/**
* Starts a Read-Eval-Print-Loop from the standard input stream, returning
* when the loop terminates.
*/
public void loop() throws IOException {
loop(System.in);
}
/**
* Starts a Read-Eval-Print-Loop from the given input stream, returning when
* the loop terminates.
*
* @param in Input stream from which commands are read.
*/
public void loop(final InputStream in) throws IOException {
initialize();
final BufferedReader bin = new BufferedReader(new InputStreamReader(in));
while (true) {
prompt();
final String line = bin.readLine();
if (line == null) break;
if (!evaluate(line)) return;
}
}
/**
* Outputs a greeting, and sets up the initial language and variables of the
* REPL.
*/
public void initialize() {
initialize(true);
}
/**
* Sets up the initial language and variables of the REPL.
*
* @param verbose Whether to output an initial greeting.
*/
public void initialize(final boolean verbose) {
if (verbose) {
out.println("Welcome to the SciJava REPL!");
out.println();
help();
}
final List langs = getInterpretedLanguages();
if (verbose) {
if (langs.isEmpty()) {
out.println("--------------------------------------------------------------");
out.println("Uh oh! There are no SciJava script languages available!");
out.println("Are any on your classpath? E.g.: org.scijava:scripting-groovy?");
out.println("--------------------------------------------------------------");
out.println();
return;
}
out.println("Have fun!");
out.println();
lang(langs.get(0).getLanguageName());
}
else if (!langs.isEmpty()) lang(langs.get(0));
populateBindings(interpreter.getBindings());
}
/** Outputs the prompt. */
public void prompt() {
out.print(interpreter == null || interpreter.isReady() ? "> " : "\\ ");
}
/**
* Evaluates the line, including handling of special colon-prefixed REPL
* commands.
*
* @param line The line to evaluate.
* @return False iff the REPL should exit.
*/
public boolean evaluate(final String line) {
try {
final String tLine = line.trim();
if (tLine.equals(":help")) help();
else if (tLine.equals(":vars")) vars();
else if (tLine.equals(":langs")) langs();
else if (tLine.equals(":debug")) debug();
else if (tLine.startsWith(":lang ")) lang(line.substring(6).trim());
else if (tLine.equals(":quit")) return false;
else {
// ensure that a script language is active
if (interpreter == null) return true;
// pass the input to the current interpreter for evaluation
final Object result = interpreter.interpret(line);
if (result != ScriptInterpreter.MORE_INPUT_PENDING) {
out.println(s(result));
}
}
}
catch (final ScriptException exc) {
// NB: Something went wrong interpreting the line of code.
// Let's just display the error message, unless we are in debug mode.
if (debug) exc.printStackTrace(out);
else {
final String msg = exc.getMessage();
out.println(msg == null ? exc.getClass().getName() : msg);
}
}
catch (final Throwable exc) {
// NB: Something unusual went wrong. Dump the whole exception always.
exc.printStackTrace(out);
}
return true;
}
// -- Commands --
/** Prints a usage guide. */
public void help() {
out.println("Available built-in commands:");
out.println();
out.println(" :help | this handy list of commands");
out.println(" :vars | dump a list of variables");
out.println(" :lang | switch the active language");
out.println(" :langs | list available languages");
out.println(" :debug | toggle full stack traces");
out.println(" :quit | exit the REPL");
out.println();
out.println("Or type a statement to evaluate it with the active language.");
out.println();
}
/** Lists variables in the script context. */
public void vars() {
if (interpreter == null) return; // no active script language
final List keys = new ArrayList<>();
final List