groovy.ui
Class InteractiveShell

java.lang.Object
  extended by groovy.ui.InteractiveShell
All Implemented Interfaces:
java.lang.Runnable

public class InteractiveShell
extends java.lang.Object
implements java.lang.Runnable

A simple interactive shell for evaluating groovy expressions on the command line (aka. groovysh).

Version:
$Revision: 12135 $
Author:
James Strachan, Chris Poirier, Yuri Schimke, Brian McCallistair, Guillaume Laforge, Dierk Koenig, include the inspect command, June 2005, Jason Dillon

Nested Class Summary
private  class InteractiveShell.CommandNameCompletor
           
 
Field Summary
private  java.lang.StringBuffer accepted
          The statement text accepted to date
private  Closure afterExecution
           
private  Closure beforeExecution
           
private static java.util.Map COMMAND_HELP
           
private static int COMMAND_ID_BINDING
           
private static int COMMAND_ID_DISCARD
           
private static int COMMAND_ID_DISCARD_LOADED_CLASSES
           
private static int COMMAND_ID_DISPLAY
           
private static int COMMAND_ID_EXECUTE
           
private static int COMMAND_ID_EXIT
           
private static int COMMAND_ID_EXPLAIN
           
private static int COMMAND_ID_HELP
           
private static int COMMAND_ID_INSPECT
           
private static java.util.Map COMMAND_MAPPINGS
           
private static java.lang.String[] COMMANDS
           
private  java.io.PrintStream err
           
private  java.lang.Exception error
          Any actual syntax error caught during parsing
private  java.io.InputStream in
           
private static int LAST_COMMAND_ID
           
private  java.lang.Object lastResult
           
private  int line
          The current line number
private static MessageSource MESSAGES
           
private static java.lang.String NEW_LINE
           
private  java.io.PrintStream out
           
private  SourceUnit parser
          A SourceUnit used to check the statement
private  java.lang.String pending
          A line of statement text not yet accepted
private  jline.ConsoleReader reader
           
private  GroovyShell shell
           
private  boolean stale
          Set to force clear of accepted
 
Constructor Summary
InteractiveShell()
          Default constructor, initializes uses new binding and system streams.
InteractiveShell(Binding binding, java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err)
          Constructs a new InteractiveShell instance
InteractiveShell(java.lang.ClassLoader parent, Binding binding, java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err)
          Constructs a new InteractiveShell instance
InteractiveShell(java.io.InputStream in, java.io.PrintStream out, java.io.PrintStream err)
          Constructs a new InteractiveShell instance
 
Method Summary
private  void accept()
          Accepts the pending text into the statement.
private  java.lang.String accepted(boolean complete)
          Returns the accepted statement as a string.
private  java.lang.String current()
          Returns the current statement, including pending text.
private  void displayBinding()
          Displays the current binding used when instanciating the shell.
private  void displayHelp()
          Displays help text about available commands.
private  void displayStatement()
          Displays the accepted statement.
private  void explainStatement()
          Attempts to parse the accepted statement and display the parse tree for it.
private  void filterAndPrintStackTrace(java.lang.Throwable cause)
          Filter stacktraces to show only relevant lines of the exception thrown.
private  void freshen()
          Clears accepted if stale.
private  void inspect()
           
static void main(java.lang.String[] args)
          Entry point when called directly.
private  boolean parse(java.lang.String code)
           
private  boolean parse(java.lang.String code, int tolerance)
          Attempts to parse the specified code with the specified tolerance.
private static void processCommandLineArguments(java.lang.String[] args)
          Process cli args when the shell is invoked via main().
protected  java.lang.String read()
          Reads a single statement from the command line.
private  void report()
          Reports the last parsing error to the user.
protected  void reset()
          Resets the command-line processing machinery after use.
private  void resetLoadedClasses()
           
 void run()
          Reads commands and statements from input stream and processes them.
 void setAfterExecution(Closure afterExecution)
          A closure that is executed after the execution of the last script.
 void setBeforeExecution(Closure beforeExecution)
          A closure that is executed before the exection of a given script
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NEW_LINE

private static final java.lang.String NEW_LINE

MESSAGES

private static final MessageSource MESSAGES

shell

private final GroovyShell shell

in

private final java.io.InputStream in

out

private final java.io.PrintStream out

err

private final java.io.PrintStream err

reader

private final jline.ConsoleReader reader

lastResult

private java.lang.Object lastResult

beforeExecution

private Closure beforeExecution

afterExecution

private Closure afterExecution

accepted

private java.lang.StringBuffer accepted
The statement text accepted to date


pending

private java.lang.String pending
A line of statement text not yet accepted


line

private int line
The current line number


stale

private boolean stale
Set to force clear of accepted


parser

private SourceUnit parser
A SourceUnit used to check the statement


error

private java.lang.Exception error
Any actual syntax error caught during parsing


COMMAND_ID_EXIT

private static final int COMMAND_ID_EXIT
See Also:
Constant Field Values

COMMAND_ID_HELP

private static final int COMMAND_ID_HELP
See Also:
Constant Field Values

COMMAND_ID_DISCARD

private static final int COMMAND_ID_DISCARD
See Also:
Constant Field Values

COMMAND_ID_DISPLAY

private static final int COMMAND_ID_DISPLAY
See Also:
Constant Field Values

COMMAND_ID_EXPLAIN

private static final int COMMAND_ID_EXPLAIN
See Also:
Constant Field Values

COMMAND_ID_EXECUTE

private static final int COMMAND_ID_EXECUTE
See Also:
Constant Field Values

COMMAND_ID_BINDING

private static final int COMMAND_ID_BINDING
See Also:
Constant Field Values

COMMAND_ID_DISCARD_LOADED_CLASSES

private static final int COMMAND_ID_DISCARD_LOADED_CLASSES
See Also:
Constant Field Values

COMMAND_ID_INSPECT

private static final int COMMAND_ID_INSPECT
See Also:
Constant Field Values

LAST_COMMAND_ID

private static final int LAST_COMMAND_ID
See Also:
Constant Field Values

COMMANDS

private static final java.lang.String[] COMMANDS

COMMAND_MAPPINGS

private static final java.util.Map COMMAND_MAPPINGS

COMMAND_HELP

private static final java.util.Map COMMAND_HELP
Constructor Detail

InteractiveShell

public InteractiveShell()
                 throws java.io.IOException
Default constructor, initializes uses new binding and system streams.

Throws:
java.io.IOException

InteractiveShell

public InteractiveShell(java.io.InputStream in,
                        java.io.PrintStream out,
                        java.io.PrintStream err)
                 throws java.io.IOException
Constructs a new InteractiveShell instance

Parameters:
in - The input stream to use
out - The output stream to use
err - The error stream to use
Throws:
java.io.IOException

InteractiveShell

public InteractiveShell(Binding binding,
                        java.io.InputStream in,
                        java.io.PrintStream out,
                        java.io.PrintStream err)
                 throws java.io.IOException
Constructs a new InteractiveShell instance

Parameters:
binding - The binding instance
in - The input stream to use
out - The output stream to use
err - The error stream to use
Throws:
java.io.IOException

InteractiveShell

public InteractiveShell(java.lang.ClassLoader parent,
                        Binding binding,
                        java.io.InputStream in,
                        java.io.PrintStream out,
                        java.io.PrintStream err)
                 throws java.io.IOException
Constructs a new InteractiveShell instance

Parameters:
parent - The parent ClassLoader
binding - The binding instance
in - The input stream to use
out - The output stream to use
err - The error stream to use
Throws:
java.io.IOException
Method Detail

main

public static void main(java.lang.String[] args)
Entry point when called directly.


processCommandLineArguments

private static void processCommandLineArguments(java.lang.String[] args)
                                         throws java.lang.Exception
Process cli args when the shell is invoked via main().

Throws:
java.lang.Exception

run

public void run()
Reads commands and statements from input stream and processes them.

Specified by:
run in interface java.lang.Runnable

setBeforeExecution

public void setBeforeExecution(Closure beforeExecution)
A closure that is executed before the exection of a given script

Parameters:
beforeExecution - The closure to execute

setAfterExecution

public void setAfterExecution(Closure afterExecution)
A closure that is executed after the execution of the last script. The result of the execution is passed as the first argument to the closure (the value of 'it')

Parameters:
afterExecution - The closure to execute

filterAndPrintStackTrace

private void filterAndPrintStackTrace(java.lang.Throwable cause)
Filter stacktraces to show only relevant lines of the exception thrown.

Parameters:
cause - the throwable whose stacktrace needs to be filtered

reset

protected void reset()
Resets the command-line processing machinery after use.


read

protected java.lang.String read()
Reads a single statement from the command line. Also identifies and processes command shell commands. Returns the command text on success, or null when command processing is complete. NOTE: Changed, for now, to read until 'execute' is issued. At 'execute', the statement must be complete.


inspect

private void inspect()

accepted

private java.lang.String accepted(boolean complete)
Returns the accepted statement as a string. If not complete, returns empty string.


current

private java.lang.String current()
Returns the current statement, including pending text.


accept

private void accept()
Accepts the pending text into the statement.


freshen

private void freshen()
Clears accepted if stale.


parse

private boolean parse(java.lang.String code,
                      int tolerance)
Attempts to parse the specified code with the specified tolerance. Updates the parser and error members appropriately. Returns true if the text parsed, false otherwise. The attempts to identify and suppress errors resulting from the unfinished source text.


parse

private boolean parse(java.lang.String code)

report

private void report()
Reports the last parsing error to the user.


displayHelp

private void displayHelp()
Displays help text about available commands.


displayStatement

private void displayStatement()
Displays the accepted statement.


displayBinding

private void displayBinding()
Displays the current binding used when instanciating the shell.


explainStatement

private void explainStatement()
Attempts to parse the accepted statement and display the parse tree for it.


resetLoadedClasses

private void resetLoadedClasses()


Copyright © ${year} The Codehaus. All Rights Reserved.