There seems to be an initFromContext() call missing...
Here's what I do:
String s = ...; // "s" contains script source code
Context ctx = contextFactory.enterContext();
try
{
CompilerEnvirons compilerEnv = new CompilerEnvirons();
compilerEnv.initFromContext(ctx);
ErrorReporter compilationErrorReporter =
compilerEnv.getErrorReporter();
Parser p = new Parser(compilerEnv, compilationErrorReporter);
ScriptOrFnNode tree = p.parse(s, "", 1);
...
}
finally
{
Context.exit();
}
I definitely get non-null for "tree".
Attila.
On 2008.05.13., at 17:54, [EMAIL PROTECTED] wrote:
> On 14 Mar, 22:23, Glenn Boysko <[EMAIL PROTECTED]> wrote:
>> Here's what I've done (and members of the group may add more
>> insights/
>> better ways if they know of them).
>>
>> First, here's a method I use to parse a JavaScript file into a
>> ScriptOrFn node (which represents the root of the file contents):
>>
>> private ScriptOrFnNode parseJavascript(File file) throws
>> IOException {
>> // Try to open a reader to the file supplied...
>> Reader reader = new FileReader(file);
>>
>> // Setup the compiler environment, error reporter...
>> CompilerEnvirons compilerEnv = new CompilerEnvirons();
>> ErrorReporter errorReporter = compilerEnv.getErrorReporter();
>>
>> // Create an instance of theparser...
>> Parserparser= newParser(compilerEnv, errorReporter);
>>
>> String sourceURI;
>>
>> try {
>> sourceURI = file.getCanonicalPath();
>> } catch (IOException e) {
>> sourceURI = file.toString();
>> }
>>
>> // Try to parse the reader...
>> ScriptOrFnNode scriptOrFnNode =parser.parse(reader,
>> sourceURI, 1);
>>
>> return scriptOrFnNode;
>> }
>>
>> This top-level object represents the root "node" in this file. It
>> extends the org.mozilla.javascript.Node class.
>>
>> From there, you have a rough syntax tree that you can use for your
>> analysis of the JavaScript.
>>
>> To get a textual representation of this syntax tree, you would call
>> the toStringTree method (and pass in the scriptOrFnNode as its
>> parameter).
>>
>> Note that in the standard distribution, this returns null. To change
>> that, I had to change a constant in Token.java:
>>
>> public static final boolean printTrees = true; // was false
>>
>> Not sure if there are easier ways to do what I have done.
>>
>> Regards,
>> Glenn
>
> Hi guys,
>
> I am rather newbie with Rhino ( as well as with Java ;) ) and trying
> to use this nice piece of code above I always got the scriptOrFnNode
> == null returned (I switched 'printTrees' to true).
>
> The code I use:
>
> File file = new File("/user/public_html/test.js");
>
> if (file.exists()) {
> try {
> script = parseJavascript(file);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> System.out.println(script.toStringTree(script));
> }
> else
> System.out.println("File does not exist ");
>
>
> It looks as if the parser did nothing, it generates no warning/error
> output on stderr as well. Shall I play with the context for
> ComplierEnv? How to set the context so that it would neither compile
> nor execute the script (all I need is syntax analysis for the code) ?
>
> Thnx in advance & regards,
>
> Szafran
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino