Package org.scijava.script
Class DefaultScriptInterpreter
- java.lang.Object
-
- org.scijava.script.DefaultScriptInterpreter
-
- All Implemented Interfaces:
ScriptInterpreter
public class DefaultScriptInterpreter extends Object implements ScriptInterpreter
The default implementation of aScriptInterpreter.Credit to Jason Sachs for the multi-line evaluation (see his post on StackOverflow).
- Author:
- Johannes Schindelin, Curtis Rueden
-
-
Field Summary
-
Fields inherited from interface org.scijava.script.ScriptInterpreter
MORE_INPUT_PENDING
-
-
Constructor Summary
Constructors Constructor Description DefaultScriptInterpreter(PrefService prefs, ScriptService scriptService, ScriptLanguage language)Deprecated.UseDefaultScriptInterpreter(ScriptLanguage)instead.DefaultScriptInterpreter(ScriptLanguage language)Creates a new script interpreter for the given script language.DefaultScriptInterpreter(ScriptLanguage language, ScriptEngine engine)Creates a new script interpreter for the given script language, using the specified script engine.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objecteval(String command)Evaluates a command.BindingsgetBindings()ScriptEnginegetEngine()Returns the associatedScriptEngine.ScriptLanguagegetLanguage()Returns the associatedScriptLanguage.Objectinterpret(String line)Interprets the given line of code, which might be part of a multi-line statement.booleanisExpectingMoreInput()booleanisReady()voidreadHistory()Reads the persisted history of the current script interpreter.voidreset()Clears the buffer of not-yet-evaluated lines of code, accumulated from previous calls toScriptInterpreter.interpret(java.lang.String).StringwalkHistory(String currentCommand, boolean forward)Obtains the next/previous command in the command history.voidwriteHistory()Persists the history of the current script interpreter.
-
-
-
Constructor Detail
-
DefaultScriptInterpreter
@Deprecated public DefaultScriptInterpreter(PrefService prefs, ScriptService scriptService, ScriptLanguage language)
Deprecated.UseDefaultScriptInterpreter(ScriptLanguage)instead.
-
DefaultScriptInterpreter
public DefaultScriptInterpreter(ScriptLanguage language)
Creates a new script interpreter for the given script language.- Parameters:
language-ScriptLanguageof the interpreter
-
DefaultScriptInterpreter
public DefaultScriptInterpreter(ScriptLanguage language, ScriptEngine engine)
Creates a new script interpreter for the given script language, using the specified script engine.- Parameters:
language-ScriptLanguageof the interpreterengine-ScriptEngineto use, or null for the specified language's default engine
-
-
Method Detail
-
readHistory
public void readHistory()
Description copied from interface:ScriptInterpreterReads the persisted history of the current script interpreter.- Specified by:
readHistoryin interfaceScriptInterpreter
-
writeHistory
public void writeHistory()
Description copied from interface:ScriptInterpreterPersists the history of the current script interpreter.- Specified by:
writeHistoryin interfaceScriptInterpreter
-
walkHistory
public String walkHistory(String currentCommand, boolean forward)
Description copied from interface:ScriptInterpreterObtains the next/previous command in the command history.- Specified by:
walkHistoryin interfaceScriptInterpreter- Parameters:
currentCommand- the current command (will be stored in the history)forward- if true, the next history entry is returned (more recent), if false, the previous one- Returns:
- the next/previous command
-
eval
public Object eval(String command) throws ScriptException
Description copied from interface:ScriptInterpreterEvaluates a command.- Specified by:
evalin interfaceScriptInterpreter- Parameters:
command- the command to evaluate- Returns:
- result of the evaluation
- Throws:
ScriptException
-
interpret
public Object interpret(String line) throws ScriptException
Interprets the given line of code, which might be part of a multi-line statement.This implementation from Jason Sachs uses the following strategy:
- Keep a pending list of input lines not yet evaluated.
- Try compiling (but not evaluating) the pending input lines.
- If the compilation is OK, we may be able to execute pending input lines.
- If the compilation throws an exception, and there is an indication of the position (line + column number) of the error, and this matches the end of the pending input, then that's a clue that we're expecting more input, so swallow the exception and wait for the next line.
- Otherwise, we either don't know where the error is, or it happened prior to the end of the pending input, so rethrow the exception.
- If we are not expecting any more input lines, and we only have one line of pending input, then evaluate it and restart.
- If we are not expecting any more input lines, and the last one is a blank one, and we have more than one line of pending input, then evaluate it and restart. Python's interactive shell seems to do this.
- Otherwise, keep reading input lines.
This helps avoid certain problems:
- users getting annoyed having to enter extra blank lines after single-line inputs
- users entering a long multi-line statement and only find out after the fact that there was a syntax error in the 2nd line.
For further details, see SO #5584674.
- Specified by:
interpretin interfaceScriptInterpreter- Parameters:
line- line of code to interpret- Returns:
- value of the line, or
ScriptInterpreter.MORE_INPUT_PENDINGif there is still pending input - Throws:
ScriptException- in case of an exception
-
reset
public void reset()
Description copied from interface:ScriptInterpreterClears the buffer of not-yet-evaluated lines of code, accumulated from previous calls toScriptInterpreter.interpret(java.lang.String). In other words: start over with a new (potentially multi-line) statement, discarding the current partial one.- Specified by:
resetin interfaceScriptInterpreter- See Also:
ScriptInterpreter.interpret(java.lang.String)
-
getLanguage
public ScriptLanguage getLanguage()
Description copied from interface:ScriptInterpreterReturns the associatedScriptLanguage.- Specified by:
getLanguagein interfaceScriptInterpreter
-
getEngine
public ScriptEngine getEngine()
Description copied from interface:ScriptInterpreterReturns the associatedScriptEngine.- Specified by:
getEnginein interfaceScriptInterpreter- Returns:
- the script engine
-
getBindings
public Bindings getBindings()
Description copied from interface:ScriptInterpreter- Specified by:
getBindingsin interfaceScriptInterpreter
-
isReady
public boolean isReady()
- Specified by:
isReadyin interfaceScriptInterpreter- Returns:
- whether the interpreter is ready for a brand new statement.
- See Also:
ScriptInterpreter.interpret(String)
-
isExpectingMoreInput
public boolean isExpectingMoreInput()
- Specified by:
isExpectingMoreInputin interfaceScriptInterpreter- Returns:
- whether the interpreter expects more input. A true value means there is definitely more input needed. A false value means no more input is needed, but it may not yet be appropriate to evaluate all the pending lines. (there's some ambiguity depending on the language)
- See Also:
ScriptInterpreter.interpret(String)
-
-