public class DefaultScriptInterpreter extends Object implements ScriptInterpreter
ScriptInterpreter.
Credit to Jason Sachs for the multi-line evaluation (see his post on StackOverflow).
MORE_INPUT_PENDING| Constructor and Description |
|---|
DefaultScriptInterpreter(PrefService prefs,
ScriptService scriptService,
ScriptLanguage language)
Deprecated.
Use
DefaultScriptInterpreter(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.
|
| Modifier and Type | Method and Description |
|---|---|
Object |
eval(String command)
Evaluates a command.
|
Bindings |
getBindings()
|
ScriptEngine |
getEngine()
Returns the associated
ScriptEngine. |
ScriptLanguage |
getLanguage()
Returns the associated
ScriptLanguage. |
Object |
interpret(String line)
Interprets the given line of code, which might be part of a multi-line
statement.
|
boolean |
isExpectingMoreInput() |
boolean |
isReady() |
void |
readHistory()
Reads the persisted history of the current script interpreter.
|
void |
reset()
Clears the buffer of not-yet-evaluated lines of code, accumulated from
previous calls to
ScriptInterpreter.interpret(java.lang.String). |
String |
walkHistory(String currentCommand,
boolean forward)
Obtains the next/previous command in the command history.
|
void |
writeHistory()
Persists the history of the current script interpreter.
|
@Deprecated public DefaultScriptInterpreter(PrefService prefs, ScriptService scriptService, ScriptLanguage language)
DefaultScriptInterpreter(ScriptLanguage) instead.public DefaultScriptInterpreter(ScriptLanguage language)
language - ScriptLanguage of the interpreterpublic DefaultScriptInterpreter(ScriptLanguage language, ScriptEngine engine)
language - ScriptLanguage of the interpreterengine - ScriptEngine to use, or null for the specified
language's default enginepublic void readHistory()
ScriptInterpreterreadHistory in interface ScriptInterpreterpublic void writeHistory()
ScriptInterpreterwriteHistory in interface ScriptInterpreterpublic String walkHistory(String currentCommand, boolean forward)
ScriptInterpreterwalkHistory in interface ScriptInterpretercurrentCommand - the current command (will be stored in the history)forward - if true, the next history entry is returned (more recent),
if false, the previous onepublic Object eval(String command) throws ScriptException
ScriptInterpretereval in interface ScriptInterpretercommand - the command to evaluateScriptExceptionpublic Object interpret(String line) throws ScriptException
This implementation from Jason Sachs uses the following strategy:
This helps avoid certain problems:
For further details, see SO #5584674.
interpret in interface ScriptInterpreterline - line of code to interpretScriptInterpreter.MORE_INPUT_PENDING if there is still
pending inputScriptException - in case of an exceptionpublic void reset()
ScriptInterpreterScriptInterpreter.interpret(java.lang.String). In other words: start over with a new
(potentially multi-line) statement, discarding the current partial one.reset in interface ScriptInterpreterScriptInterpreter.interpret(java.lang.String)public ScriptLanguage getLanguage()
ScriptInterpreterScriptLanguage.getLanguage in interface ScriptInterpreterpublic ScriptEngine getEngine()
ScriptInterpreterScriptEngine.getEngine in interface ScriptInterpreterpublic Bindings getBindings()
ScriptInterpretergetBindings in interface ScriptInterpreterpublic boolean isReady()
isReady in interface ScriptInterpreterScriptInterpreter.interpret(String)public boolean isExpectingMoreInput()
isExpectingMoreInput in interface ScriptInterpreterScriptInterpreter.interpret(String)Copyright © 2015–2022 SciJava. All rights reserved.