pnuts.lang
Class Pnuts

java.lang.Object
  extended by pnuts.lang.Pnuts
All Implemented Interfaces:
java.io.Serializable, Executable
Direct Known Subclasses:
CachedScript

public class Pnuts
extends java.lang.Object
implements Executable, java.io.Serializable

This class provides a set of static methods to parse/execute scripts.

This object also represents a parsed script.

This class is serializable. When a Pnuts object is serialized, the syntax tree is written to the object stream, along with the attributes such as line, column, and script source.

When the object is deserialized, the parsed script is restored using the information read from the object stream. If the script had been compiled, the script should be recompiled.

Serialized objects of this class will not be compatible with future releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing.

See Also:
Serialized Form

Field Summary
static java.lang.String pnuts_version
          The version number
static java.lang.String prompt
          "prompt" string for the command shell
protected  java.lang.Object scriptSource
          The script source, from where the script came.
protected  SimpleNode startNodes
          Parsed scripts
 
Constructor Summary
protected Pnuts()
           
 
Method Summary
protected  java.lang.Object accept(Context context)
          Executes the parsed script
 java.lang.Object accept(Visitor visitor, Context context)
          traverse the parsed tree with the specified Visitor and Context
static java.lang.ClassLoader createClassLoader(Context context)
          Create a classloader that can compile scripted classes with the current thread's context classloader as its parent classloader
static java.lang.ClassLoader createClassLoader(Context context, java.lang.ClassLoader parent)
          Create a classloader that can compile scripted classes
static java.lang.Object eval(java.lang.String expr, Context context)
          Evaluates "str" in "context"
static int evalDepth(Context context)
          Get the depth of evaluation.
static java.lang.String format(java.lang.Object obj)
          Returns the string representation of an object.
static java.lang.Object get(java.lang.String str)
          Deprecated. replaced by Context.getCurrentPackage().get(str.intern())
static java.lang.Object get(java.lang.String str, java.lang.String pkg)
          Deprecated. replaced by Package.getPackage(pkg, null).get(str)
static java.util.Properties getDefaults()
          Gets the properties previously set by setDefaults() method.
static java.net.URL getResource(java.lang.String s, Context context)
          Get the resource URL.
 java.lang.Object getScriptSource()
          Gets the script source associated with this parsed (compiled) expression
static boolean isJava2()
          Checks if the runtime environment supports J2SE.
static boolean isVerbose()
          Deprecated. replaced by Context.isVerbose()
static java.lang.Object load(java.io.InputStream in, boolean interactive, Context context)
          Load a script from an InputStream in the specified Context.
static java.lang.Object load(java.io.InputStream in, Context context)
          Loads a script from InputStream "in" in "context"
static java.lang.Object load(java.io.Reader reader, boolean interactive, Context context)
          This method loads a script
static java.lang.Object load(java.io.Reader reader, Context context)
          This method loads a script
static java.lang.Object load(java.lang.String name, Context context)
          Loads a script "file" in "context"
static java.lang.Object load(java.net.URL url, Context context)
          Loads a script specifed as a URL.
static java.lang.Class loadClass(java.lang.String name, Context context)
          Loads the class by the following order.
static java.lang.Object loadFile(java.lang.String file, Context context)
          Loads a local script "file" in "context"
static Pnuts parse(java.io.InputStream in)
          Deprecated. replaced by parse(Reader)
static Pnuts parse(java.io.Reader reader)
          parse a script from Reader and return a Pnuts object
static Pnuts parse(java.io.Reader reader, java.lang.Object scriptSource, Context context)
          parse a script from Reader and return a Pnuts object
static Pnuts parse(java.io.Reader reader, java.lang.Object scriptSource, Context context, ParseEnvironment env)
          parse a script from Reader and return a Pnuts object
static Pnuts parse(java.io.Reader reader, ParseEnvironment env)
          parse a script from Reader and return a Pnuts object
static Pnuts parse(java.lang.String expr)
          Parses a script and return a Pnuts object
static void require(java.lang.String file, Context context)
          Loads a script "file" only if the script has not been read.
static void require(java.lang.String file, Context context, boolean checkForUpdate)
           
 java.lang.Object run(Context context)
          Executes a Pnuts object with the specified Context
static void set(java.lang.String str, java.lang.Object val)
          Deprecated. replaced by context.getCurrentPackage().set(str.intern(), val)
static void set(java.lang.String str, java.lang.Object val, java.lang.String pkg)
          Deprecated. replaced by Package.getPackage(pkg, null).set(str, val)
static void setDefaults(java.util.Properties properties)
          Sets properties that affect the behavior of Pnuts interpreter/compiler.
static void setPrompt(java.lang.String str)
          Sets a "prompt" string for the command shell
 void setScriptSource(java.lang.Object src)
          Associates a script source with this parsed (compiled) expression.
static void setVerbose(boolean b)
          Deprecated. replaced by Context.setVerbose()
 java.lang.String unparse()
          Obtain the script code from a parsed object
 void unparse(java.io.Writer writer)
          Obtain the script code from a parsed object and write it to the specified Writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pnuts_version

public static final java.lang.String pnuts_version
The version number

See Also:
Constant Field Values

prompt

public static java.lang.String prompt
"prompt" string for the command shell


startNodes

protected SimpleNode startNodes
Parsed scripts


scriptSource

protected java.lang.Object scriptSource
The script source, from where the script came. It is usually a URL object, but not limitted to. If this variable is not null, error message would include the positional information such as the line number and the file name.

Constructor Detail

Pnuts

protected Pnuts()
Method Detail

isJava2

public static final boolean isJava2()
Checks if the runtime environment supports J2SE.

Returns:
true if the runtime environment supports J2SE

setDefaults

public static void setDefaults(java.util.Properties properties)
Sets properties that affect the behavior of Pnuts interpreter/compiler. This method should be called before the classes that read the default settings, such as pnuts.lang.Configuration and pnuts.lang.PnutsImpl. Once those classes are loaded, this method call has no effect.
 Pnuts.setDefaults(properties);
 Context c = new Context(); // this line should not precede setDefaults() call.
 
 

Parameters:
properties - the properties that override the system properties.

getDefaults

public static java.util.Properties getDefaults()
Gets the properties previously set by setDefaults() method.

Returns:
the default setting that affects the behavior of Pnuts interpreter/compiler.

loadClass

public static final java.lang.Class loadClass(java.lang.String name,
                                              Context context)
                                       throws java.lang.ClassNotFoundException
Loads the class by the following order.
  1. A class loader associated with Pnuts context.
  2. The class loader associated with the current Thread (J2SE).
  3. The class loader by which Pnuts classes are loaded.

Parameters:
name - the class name to be loaded
context - the context in which the class is loaded
Returns:
the loaded class. Note that it is not initialized.
Throws:
java.lang.ClassNotFoundException

getResource

public static final java.net.URL getResource(java.lang.String s,
                                             Context context)
Get the resource URL.

Parameters:
s - the resource name
context - the context in which the resource is read

setPrompt

public static void setPrompt(java.lang.String str)
Sets a "prompt" string for the command shell


setVerbose

public static void setVerbose(boolean b)
Deprecated. replaced by Context.setVerbose()

Sets the verbose mode


isVerbose

public static boolean isVerbose()
Deprecated. replaced by Context.isVerbose()

Check the current verbose mode

Returns:
the current verbose mode

format

public static java.lang.String format(java.lang.Object obj)
Returns the string representation of an object. When the object is a number, a character, a boolean, or a string, it can be reconstructed by eval() function.

Parameters:
obj - the object.
Returns:
the string representation of the object

get

public static java.lang.Object get(java.lang.String str)
Deprecated. replaced by Context.getCurrentPackage().get(str.intern())

Get the value of a global variable

Parameters:
str - the name of the global variable
Returns:
the value of the global variable str

get

public static java.lang.Object get(java.lang.String str,
                                   java.lang.String pkg)
Deprecated. replaced by Package.getPackage(pkg, null).get(str)

Gets a global variable.

Parameters:
str - the name of the variable
pkg - the package where the variable is defined
Returns:
the value of a variable "str" in the package "pkg"

set

public static void set(java.lang.String str,
                       java.lang.Object val)
Deprecated. replaced by context.getCurrentPackage().set(str.intern(), val)

set a value "val" to a global variable "str"


set

public static void set(java.lang.String str,
                       java.lang.Object val,
                       java.lang.String pkg)
Deprecated. replaced by Package.getPackage(pkg, null).set(str, val)

Set a value "val" to a variable "str" in package "pkg"

Parameters:
str -
val -
pkg -

eval

public static java.lang.Object eval(java.lang.String expr,
                                    Context context)
Evaluates "str" in "context"

Parameters:
expr - the expression to be evaluated
context - the context in which the expression is evaluated
Returns:
the result of the evaluation

loadFile

public static java.lang.Object loadFile(java.lang.String file,
                                        Context context)
                                 throws java.io.FileNotFoundException
Loads a local script "file" in "context"

Parameters:
file - the script file to be loaded.
context - the context in which the file is loaded.
Throws:
java.io.FileNotFoundException

load

public static java.lang.Object load(java.lang.String name,
                                    Context context)
                             throws java.io.FileNotFoundException
Loads a script "file" in "context"

Parameters:
name - the name of the script to be loaded
context - the context in which the script is loaded.
Throws:
java.io.FileNotFoundException

load

public static java.lang.Object load(java.net.URL url,
                                    Context context)
Loads a script specifed as a URL.

Parameters:
url - the URL
context - the context in which the script is loaded.

load

public static java.lang.Object load(java.io.InputStream in,
                                    Context context)
Loads a script from InputStream "in" in "context"

Parameters:
in - the input stream from which the script can be read.
context - the context in which the script is loaded.

load

public static java.lang.Object load(java.io.InputStream in,
                                    boolean interactive,
                                    Context context)
Load a script from an InputStream in the specified Context.

Parameters:
in - an InputStream from which the interpreter reads an input
interactive -
  • When "interactive" is true, the greeting message, the prompt, and the results of evaluations are displayed. When an exception is thrown and not caught by any exception handler, it is caught at the top level of the interpreter, display an error message, and resume the interactive session. If the exception is caught by a handler that is registered at the top level, the result of the handler becomes the return value of the last expression.
  • When "interactive" is false, exceptions are caught at the top level of the interpreter and exits this function. If the exception thrown is caught by a handler that is registered at the top level, the result of the handler becomes the return value of this method.
context - a Context in which the interpretation is taken place.
Returns:
the result of the last expression

load

public static java.lang.Object load(java.io.Reader reader,
                                    Context context)
This method loads a script

Parameters:
reader - the Reader from which the script is loaded
context - the context in which the script is loaded
Returns:
the result of the last expression

evalDepth

public static int evalDepth(Context context)
Get the depth of evaluation. This value increases when load(), loadFile(), or eval() is called.

Parameters:
context - the context of the evaluation.

load

public static java.lang.Object load(java.io.Reader reader,
                                    boolean interactive,
                                    Context context)
This method loads a script

Parameters:
reader - the Reader from which the script is loaded
interactive - specifies if the execution is in interactive mode.
context - the context in which the script is loaded
Returns:
the result of the last expression

parse

public static Pnuts parse(java.io.InputStream in)
                   throws ParseException
Deprecated. replaced by parse(Reader)

Parses a script from InputStream and return a Pnuts object

Parameters:
in - the InputStream
Returns:
the Pnuts object including a parsed syntax tree
Throws:
ParseException

parse

public static Pnuts parse(java.io.Reader reader)
                   throws ParseException
parse a script from Reader and return a Pnuts object

Parameters:
reader - the Reader
Returns:
the Pnuts object including a parsed syntax tree
Throws:
ParseException
Since:
Pnuts 1.0beta3

parse

public static Pnuts parse(java.io.Reader reader,
                          ParseEnvironment env)
                   throws ParseException
parse a script from Reader and return a Pnuts object

Throws:
ParseException

parse

public static Pnuts parse(java.io.Reader reader,
                          java.lang.Object scriptSource,
                          Context context)
parse a script from Reader and return a Pnuts object

Parameters:
reader - the Reader
scriptSource - the script source
Returns:
the Pnuts object including a parsed syntax tree

parse

public static Pnuts parse(java.io.Reader reader,
                          java.lang.Object scriptSource,
                          Context context,
                          ParseEnvironment env)
parse a script from Reader and return a Pnuts object

Parameters:
reader - the Reader
scriptSource - the script source
env -
Returns:
the Pnuts object including a parsed syntax tree

parse

public static Pnuts parse(java.lang.String expr)
                   throws ParseException
Parses a script and return a Pnuts object

Parameters:
expr - the script
Returns:
the Pnuts object including a parsed syntax tree
Throws:
ParseException

require

public static void require(java.lang.String file,
                           Context context)
                    throws java.io.FileNotFoundException
Loads a script "file" only if the script has not been read. It is guaranteed that the script runs at most once in this context.

Parameters:
file - the script file, which must be an intern'ed String.
context - the context in which the script is loaded
Throws:
java.io.FileNotFoundException

require

public static void require(java.lang.String file,
                           Context context,
                           boolean checkForUpdate)
                    throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException

createClassLoader

public static java.lang.ClassLoader createClassLoader(Context context)
Create a classloader that can compile scripted classes with the current thread's context classloader as its parent classloader

Parameters:
context - the context in which scripts are compiled
Returns:
the classloader

createClassLoader

public static java.lang.ClassLoader createClassLoader(Context context,
                                                      java.lang.ClassLoader parent)
Create a classloader that can compile scripted classes

Parameters:
context - the context in which scripts are compiled
parent - the parent classloader
Returns:
the classloader

run

public java.lang.Object run(Context context)
Executes a Pnuts object with the specified Context

Specified by:
run in interface Executable
Parameters:
context - the Context
Returns:
the result

setScriptSource

public void setScriptSource(java.lang.Object src)
Associates a script source with this parsed (compiled) expression.

Parameters:
src - the script source to be associated with.

getScriptSource

public java.lang.Object getScriptSource()
Gets the script source associated with this parsed (compiled) expression

Returns:
the script source to be associated with.

accept

public java.lang.Object accept(Visitor visitor,
                               Context context)
traverse the parsed tree with the specified Visitor and Context

Parameters:
context - the Context
Returns:
the result
Since:
Pnuts 1.0beta3

unparse

public java.lang.String unparse()
Obtain the script code from a parsed object

Returns:
the script code

unparse

public void unparse(java.io.Writer writer)
             throws java.io.IOException
Obtain the script code from a parsed object and write it to the specified Writer.

Parameters:
writer - the Writer to which the script code is written
Throws:
java.io.IOException

accept

protected java.lang.Object accept(Context context)
Executes the parsed script

Parameters:
context - the context in which the script is executed
Returns:
the result