[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Server Side Includes/HTML + scripting
You can do this buy having multiple lexer states - one for html
and one for stuff inside the tags.
I've implemented a PHP grammar for sablecc - you might
want to look at that for hints:
http://www.mare.ee/indrek/sablecc/#php4
http://www.mare.ee/indrek/sablecc/php4.sablecc3.txt
Just to warn I'm using an extended custom lexer here for
handling some peculiar cases with if's and strings so it
might not be all copy&paste for you.
Basically you declare a block of html to be one type of a stement,
and the entire grammar a set of statements (grammar = statement*).
Regards,
Indrek
On Thursday 03 June 2004 23:24, James Buchanan wrote:
> Hi,
>
> Can someone please give me some tips on how I might implement a parser
> for a program that processes server side includes (SSI) or a mini
> language that can be interspersed throughout an HTML file?
>
> E.g. for SSI:
>
> <!--#exec cmd="/bin/foo"-->
> <p>Hi. Document root is: <!--#var name="$DOCUMENT_ROOT"--></p>
>
> <!--#if expr="$foo == $bar" -->
> <h3>My homepage</h3>
> <!--#else -->
> <h3>My new homepage</h3>
> <!--#endif -->
>
> ... or very similar to this.
>
> For something like PHP or ASP:
>
> <%
> statments...
> %>
>
> <%= var %> ; Direct output
>
> <% if foo.length() > 0 then %>
> <b>foo's length is greater than 0!</b>
> <% endif %>
>
> The programming commands can mix in with the HTML by having the
> statments inside special tags the parser picks up. It would need to
> associate the next block of HTML with the tag if it's a conditional,
> otherwise it gets sent out after processing the command tag(s).
>
> I'm not exactly sure how I could do this. The nodes that get built after
> the parser builds the parse tree might have a link to a node that holds
> HTML to output. It would need to be able to find the extra nodes that
> it wants to end if statements and such, so it can skip elseif parts.
> Similar for looping constructs.
>
> Node: <% stuff %>
> HTMLNode (just print it out)
> Node: <%= varname %>
> HTMLNode (just print it out)
> Node: <% morestuff %>
>
> Any ideas for a grammar that I can make with SableCC?
>
> Thanks,
> James