[antlr-interest] Embedding expressions in plain text -- how to?
Gavin Lambert
antlr at mirality.co.nz
Sat Nov 10 13:06:03 PST 2007
At 08:59 11/11/2007, Rob Greene wrote:
>How should I go about embedding an expression language in plain
>text. The expression is wrapped in '${' and '}' but may occur
>zero to many times within plain text. When in plain text, I want
>to keep the whitespace, but within the expression, it should be
>ignored.
>
>I've currently placed a boolean into a ThreadLocal and check that
>before calling skip() in my whitespace rule. But, that causes
>issues with the actual expressions.
>
>For instance, "${true}" returns Boolean.TRUE while "${ true }"
>returns a null. If I take out the test in WS, I can have those
>spaces within an expression, but the plain text loses all white
>space.
The way I'd do it would be to create multiple lexers/parsers. (It
is possible to do it in a single one, but I think it's cleaner to
separate it out.)
At the first level, you just need a lexer-only grammar to separate
plain text and expressions. You need to make the whole ${...}
construct a single token (see the STRING rules in the wiki for an
example), and everything else a different token.
Once you've done that, in your app code loop through all the
returned expressions and then run another lexer/parser over them
to work out what they actually mean :)
(If you do want to combine it all into one operation, then look up
the "island grammar" examples. Same basic idea.)
More information about the antlr-interest
mailing list