oscript
Class Shell

java.lang.Object
  extended byoscript.Shell

public class Shell
extends java.lang.Object

An extensible read-eval-print shell implementation. Splits the basic process of read, eval, print and handle errors into individual methods which can be overriden and extended as needed.

NOTE this could probably be split into an abstract base class which does not implement read() and print(oscript.data.Value), and hence has no knowledge of input/output/error streams.

Version:
1.2
Author:
Rob Clark (rob@ti.com)

Constructor Summary
Shell(java.io.BufferedReader in, java.io.PrintWriter out)
          Class Constructor.
Shell(java.io.BufferedReader in, java.io.PrintWriter out, java.io.PrintWriter err)
          Class Constructor.
Shell(java.lang.String prompt, java.io.BufferedReader in, java.io.PrintWriter out, java.io.PrintWriter err)
          Class Constructor.
 
Method Summary
 void evalAndPrint(java.lang.String line)
          Evaluate and print a line.
 Value evalStr(java.lang.String line)
          Evaluate a string...
 void handleParseException(ParseException e)
          Called to handle syntax exceptions.
 void handleScriptException(PackagedScriptObjectException e)
          Called to handle script exceptions.
 void print(Value val)
          Print the result.
 void prompt()
          Print the prompt shown to the user to indicate that we are ready for more input.
 java.lang.String read()
          Read a line of input.
 void run()
          The entry point for the read-eval-print loop.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Shell

public Shell(java.io.BufferedReader in,
             java.io.PrintWriter out)
Class Constructor.

Parameters:
in - input
out - output

Shell

public Shell(java.io.BufferedReader in,
             java.io.PrintWriter out,
             java.io.PrintWriter err)
Class Constructor.

Parameters:
in - input
out - output
err - error output

Shell

public Shell(java.lang.String prompt,
             java.io.BufferedReader in,
             java.io.PrintWriter out,
             java.io.PrintWriter err)
Class Constructor.

Parameters:
prompt - the prompt to display before the read
in - input
out - output
err - error output
Method Detail

run

public void run()
         throws java.io.IOException
The entry point for the read-eval-print loop. This repeatedly calls prompt(), read(), and evalAndPrint(java.lang.String), which can each be overridden as needed.

Throws:
java.io.IOException

evalAndPrint

public void evalAndPrint(java.lang.String line)
Evaluate and print a line. This calls out to evalStr(java.lang.String), print(oscript.data.Value), handleScriptException(oscript.exceptions.PackagedScriptObjectException), and handleParseException(oscript.parser.ParseException), which can be overriden as needed.

Parameters:
line - the line of script

prompt

public void prompt()
Print the prompt shown to the user to indicate that we are ready for more input.


read

public java.lang.String read()
                      throws java.io.IOException
Read a line of input. This handles multi-line input (ie. lines ending with a \) by stripping out the backslash and reading the next line, until there is a line not terminated by a backslash. Also, as a convenience, this method appends a semicolon to the end of the input, if needed.

Returns:
the input
Throws:
java.io.IOException

evalStr

public Value evalStr(java.lang.String line)
              throws ParseException,
                     PackagedScriptObjectException
Evaluate a string... calls out to the interpreter to perform the evaluate, and throws exceptions of there is a syntax or logical error in the code evaluated.

Parameters:
line - the script to evaluate
Returns:
the result of evaluating the string
Throws:
ParseException
PackagedScriptObjectException

print

public void print(Value val)
Print the result. This is called to print a result after successfully evaluating an input.

Parameters:
val - the result to print

handleScriptException

public void handleScriptException(PackagedScriptObjectException e)
Called to handle script exceptions. A script exception may be a result of a logical error, in otherwise syntatically correct input.

Parameters:
e - the exception

handleParseException

public void handleParseException(ParseException e)
Called to handle syntax exceptions. A syntax exception occurs in the case where the input is not syntatically correct.

Parameters:
e - the exception