
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 both ImageJ and SCIFIO.
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2016 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* 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.log.LogService;
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;
@Parameter(required = false)
private LogService log;
private final PrintStream out;
private ScriptInterpreter interpreter;
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 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 of the REPL. */
public void initialize() {
out.println("Welcome to the SciJava REPL!");
out.println();
help();
out.println("Have fun!");
out.println();
lang(scriptService.getLanguages().get(0).getLanguageName());
populateBindings(interpreter.getBindings());
}
/** Outputs the prompt. */
public void prompt() {
out.print(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) {
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.startsWith(":lang ")) lang(line.substring(6).trim());
else if (line.trim().equals(":quit")) return false;
else {
// pass the input to the current interpreter for evaluation
try {
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 (log.isDebug()) 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(" :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() {
final List keys = new ArrayList();
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy