On Jun 9, 6:06 am, [EMAIL PROTECTED] wrote:
> Maybe I should explain in more detail what I would like to achive.
>
> I'd like to parse a source-file (HTML file with JavaScript embedded in
> ASP-like fashion) with a custom HTML parser.
>
> I want to have a string (or Reader or something, really doesn't
> matter) that I can append JavaScript source to as I pick it up in my
> HTML source.
> I want to have a tree-representation of the JavaScript source (I
> believe this is already being build in Rhino parsing), but it should
> be build piece-by-piece as I append more and more JavaScript to the
> JavaScript string.
>
> As I parse my HTML, everytime I bump into a "<% ... %>" syntax I will
> get the content between <% and %> and append it to the string
> containing the previous <%...%> sections. This should also append the
> correct sub-tree to the tree-representation.
> Every time I get to a "%>" in the HTML source and after I've appended
> the content to the string and the tree has been updated I need to do
> the following:
>
> a) Check that the combined JavaScript - so far - is correct if you
> look at the syntax.
> b) Check if I'm now inside a function, a for-loop inside af while-loop
> inside a function etc.
> c) Check if any errors in the - so far - parsed JavaScript, and if it
> has then get line-number and possible column-number etc.
>
> As I parse the HTML and the JavaScript <%...%> sections and append to
> the string and the tree the JavaScript will eventually (if no errors
> of course) contain a complete and working JavaScript context.
>
> Please note that everytime I get the content inside <%...%> it could
> contain fx. "for (i = 0; i < 10; i++) {" with no ending "}", but as
> I'm not at the end of the HTML then this is - so far - correct syntax,
> so no errors should be shown here.
>
> Is the above duable with Rhino or do I have to build my own JavaScript
> parser - which I'd rather not :-D If it is not possible with Rhino,
> does someone know of a person that could build a custom parser for me
> (based on the Rhino, so that improvements could easily be added in the
> future)? Thanks!

Probably the easiest approach to this with Rhino is to use
org.mozilla.javascript.Context.stringIsCompilableUnit(String). This
method takes a string containing JavaScript source and returns true if
the source is compilable as it is. This method is used by the shell to
determine whether to prompt for more input when the user presses
enter. You could call stringIsCompilableUnit when you see %> and if it
returns false, assume that you're in the middle of a loop. In that
case you could then insert code into the JavaScript source that would
evaluate to the HTML text between the %> and the next <%. Then when
you evaluate the script when you've processed the entire file, it
should do what you want.

For example, the input might be

<html>
<body>
<%
   for (var i=0; i < 10; i++) {
%>
    Hello, world!<br>
<%
  }
%>
</body>
</html>

And the resulting script might be

writeToResponse(sourceSnippet[0]);
for (var i=0; i < 10; i++) {
  writeToResponse(sourceSnippet[1]);
}
writeToResponse(sourceSnippet[2]);

--N

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

Reply via email to