| java.lang.Object com.caucho.es.parser.Parser
Parser | public class Parser (Code) | | Parser is a factory for generating compiled Script objects.
Most applications will use the parse(String) interface
to parse JavaScript. That method will try to load a precompiled
script from the work directory before trying to parse it.
Applications will often set the script path a directory for
script and include the classpath in the path. Applications will
often override the work dir for a more appropriate work directory.
package com.caucho.vfs.*;
package com.caucho.es.*;
...
com.caucho.es.parser.Parser parser;
parser = new com.caucho.es.parser.Parser();
// configure the path to search for *.js files
MergePath scriptPath = new MergePath();
scriptPath.addMergePath(Vfs.lookup("/home/ferg/js"));
ClassLoader loader = Thread.currentThread().contextClassLoader();
scriptPath.addClassPath(loader);
parser.setScriptPath(scriptPath);
// configure the directory storing compiled scripts
Path workPath = Vfs.lookup("/tmp/caucho/work");
parser.setWorkDir(workPath);
Script script = parser.parse("test.js");
|
Constructor Summary | |
public | Parser() |
Method Summary | |
public void | addImport(String name) Adds a package/script to be automatically imported by the script. | ESException | error(String text) Creates an error message with the given text. | ClassLoader | getClassLoader() Returns the current class loader. | public String | getFilename() Returns the current filename being parsed. | public Path | getScriptPath() Returns the path to search for imported javascript. | public Path | getWorkDir() Returns the directory for generated *.java and *.class files. | public Script | parse(String name) Main application parsing method. | public Script | parse(ReadStream is) Alternative parsing method when the application only has an open
stream to the file. | public Script | parse(ReadStream is, String name, int line) An alternative parsing method given an open stream, a filename and
a line number.
Parameters: is - a stream to the javascript source. Parameters: name - filename to use for error messages. Parameters: line - initial line number. | public Script | parseEval(ReadStream is, String name, int line) Parses a script for the JavaScript "eval" expression. | public void | setClassLoader(ClassLoader loader) Internal method to set the actual class loader. | public void | setClassName(String name) Sets the name of the generated java class. | public void | setFast(boolean isFast) Sets "fast" mode, i.e. | public void | setLineMap(LineMap lineMap) Sets a line number map. | public void | setParentLoader(ClassLoader parentLoader) Sets the parent class loader. | public void | setScriptPath(Path scriptPath) Sets the path to search for imported javascript source files.
Normally, ScriptPath will be a MergePath. | public void | setWorkDir(Path path) Sets the directory for generated *.java and *.class files.
The parser will check this directory for any precompiled javascript
classes. |
PREC_AND | final static int PREC_AND(Code) | | |
PREC_ASSIGN | final static int PREC_ASSIGN(Code) | | |
PREC_BITAND | final static int PREC_BITAND(Code) | | |
PREC_BITOR | final static int PREC_BITOR(Code) | | |
PREC_BITXOR | final static int PREC_BITXOR(Code) | | |
PREC_CMP | final static int PREC_CMP(Code) | | |
PREC_COMMA | final static int PREC_COMMA(Code) | | |
PREC_COND | final static int PREC_COND(Code) | | |
PREC_DOT | final static int PREC_DOT(Code) | | |
PREC_EQ | final static int PREC_EQ(Code) | | |
PREC_FUN | final static int PREC_FUN(Code) | | |
PREC_MAX | final static int PREC_MAX(Code) | | |
PREC_OR | final static int PREC_OR(Code) | | |
PREC_PLUS | final static int PREC_PLUS(Code) | | |
PREC_POST | final static int PREC_POST(Code) | | |
PREC_SHIFT | final static int PREC_SHIFT(Code) | | |
PREC_TIMES | final static int PREC_TIMES(Code) | | |
PREC_UMINUS | final static int PREC_UMINUS(Code) | | |
scriptPath | Path scriptPath(Code) | | |
addImport | public void addImport(String name)(Code) | | Adds a package/script to be automatically imported by the script.
Each import is the equivalent of adding the following javascript:
package name;
Parameters: name - package or script name to be automatically imported. |
getClassLoader | ClassLoader getClassLoader()(Code) | | Returns the current class loader. If null, creates from the parent
loader and the work-dir.
|
getFilename | public String getFilename()(Code) | | Returns the current filename being parsed.
|
getScriptPath | public Path getScriptPath()(Code) | | Returns the path to search for imported javascript. Normally, scriptPath
will be a MergePath.
Parameters: scriptPath - path to search for imported scripts. |
getWorkDir | public Path getWorkDir()(Code) | | Returns the directory for generated *.java and *.class files.
|
parse | public Script parse(String name) throws ESException, IOException(Code) | | Main application parsing method. The parser will try to load
the compiled script. If the compiled script exists and the
source file has not changed, parse will return the old script.
Otherwise, it will parse and compile the javascript.
Parameters: name - the name of the javascript source. the parsed script |
parse | public Script parse(ReadStream is) throws ESException, IOException(Code) | | Alternative parsing method when the application only has an open
stream to the file. Since this method will always compile a new script,
it can be significantly slower than the parse(String)
method.
Parameters: is - a read stream to the javascript source. the parsed script. |
parse | public Script parse(ReadStream is, String name, int line) throws ESException, IOException(Code) | | An alternative parsing method given an open stream, a filename and
a line number.
Parameters: is - a stream to the javascript source. Parameters: name - filename to use for error messages. Parameters: line - initial line number. the compiled script |
parseEval | public Script parseEval(ReadStream is, String name, int line) throws ESException, IOException(Code) | | Parses a script for the JavaScript "eval" expression. The
semantics for "eval" are slightly different from standard
scripts.
Parameters: is - stream to the eval source. Parameters: name - filename to use for error messages Parameters: line - initial line number to use for error messages. the compiled script. |
setClassLoader | public void setClassLoader(ClassLoader loader)(Code) | | Internal method to set the actual class loader.
Normally, this should only be called from com.caucho.es functions.
|
setClassName | public void setClassName(String name)(Code) | | Sets the name of the generated java class. If unset, the parser will
mangle the input name.
|
setFast | public void setFast(boolean isFast)(Code) | | Sets "fast" mode, i.e. JavaScript 2.0. Fast mode lets the compiler
make assumptions about types and classes, e.g. that class methods
won't change dynamically. This lets the compiler generate more code
that directly translates to Java calls.
|
setLineMap | public void setLineMap(LineMap lineMap)(Code) | | Sets a line number map. For generated files like JSP or XTP,
the error messages need an extra translation to get to the original
line numbers.
|
setParentLoader | public void setParentLoader(ClassLoader parentLoader)(Code) | | Sets the parent class loader. If unspecified, defaults to the context
classloader. Most applications will just use the default.
Parameters: parentLoader - the classloader to be used for the script's parent. |
setScriptPath | public void setScriptPath(Path scriptPath)(Code) | | Sets the path to search for imported javascript source files.
Normally, ScriptPath will be a MergePath. If the MergePath
adds the classpath, then JavaScript files can be put in the
normal Java classpath.
If the ScriptPath is not specified, it will use the
current directory and the classpath from the context class loader.
MergePath scriptPath = new MergePath();
scriptPath.addMergePath(Vfs.lookup("/home/ferg/js"));
ClassLoader loader = Thread.currentThread().contextClassLoader();
scriptPath.addClassPath(loader);
parser.setScriptPath(scriptPath);
Parameters: scriptPath - path to search for imported scripts. |
setWorkDir | public void setWorkDir(Path path)(Code) | | Sets the directory for generated *.java and *.class files.
The parser will check this directory for any precompiled javascript
classes. The default work-dir is /tmp/caucho on unix and
c:\temp\caucho on windows.
Parameters: path - the work directory. |
|
|