On Dec 9, 10:14 pm, [EMAIL PROTECTED] wrote:
> Hi!
>
> I need to be able to parse one 'token' at a time of a JavaScript
> source (fx. a java.lang.String) and build an AST tree out of it.
>
> The source string will not contain the complete JavaScript source when
> I start parsing it but it should always contain valid JavaScript from
> start to end of string at the time I parse.
>
> An example:
>
> I might start by having the following:
>
> String javascript =
>       "var x = 0;\n" +
>       "for (int i = 0; i < 5; i++) {";
>
> which is missing the ending "}" to have a valid for-loop.... but never
> the less this will be the case when I start building the AST tree, so
> I need to put each 'token' into the AST tree and keep checking for an
> end-of-string event.
>
> When I have parsed the above example I will add some more javascript,
> for example:
>
> javascript = javascript +
>         "    x = x + i;\n" +
>         "}\n" +
>         "x\n";
>
> and I should then be able to continue parsing from where I left off
> before and add additional tokens to the AST tree.

I think the closest thing Rhino supports is the
Context.stringIsCompilableUnit() method. This tries to compile a
string and returns false if an error occured and the string was fully
read, in other words if the error could be fixed by adding more code
to the source string.

The Rhino shell uses this for expressions spanning over multiple
lines.

Of course this doesn't give you access to the partial parse tree. I'm
not so familiar with the new parsing code, but maybe it would be
possible to attach the parse data to the exception in some way when an
error occurs.

Hannes

> I need to always be able to know if I'm inside a loop, a nested loop n
> times, a function or what-ever, and I think that AST is the way to
> go..... or can I keep this 'depth' by just reading tokens?
>
> Anyone have an example of how to do this?
> Also please do not hesitate to ask further questions if you didn't
> understand the above ;-)

_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to