Class DefaultScriptInterpreter

    • Method Detail

      • walkHistory

        public String walkHistory​(String currentCommand,
                                  boolean forward)
        Description copied from interface: ScriptInterpreter
        Obtains the next/previous command in the command history.
        Specified by:
        walkHistory in interface ScriptInterpreter
        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
      • 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:
        interpret in interface ScriptInterpreter
        Parameters:
        line - line of code to interpret
        Returns:
        value of the line, or ScriptInterpreter.MORE_INPUT_PENDING if there is still pending input
        Throws:
        ScriptException - in case of an exception
      • isExpectingMoreInput

        public boolean isExpectingMoreInput()
        Specified by:
        isExpectingMoreInput in interface ScriptInterpreter
        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)