Re: how to find out what variables an expression references?
"Jonathan Revusky" <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
On Tue, Oct 7, 2008 at 3:44 PM, Attila Szegedi <[email protected]> wrote: > This should work (code lifted from "?eval" built-in's code): > > SimpleCharStream scs = new SimpleCharStream(new > StringReader("(" + s + ")"), 1, 1, s.length() + 2); > FMParserTokenManager token_source = new > FMParserTokenManager(scs); > token_source.SwitchTo(FMParserConstants.FM_EXPRESSION); The above line is really the key. If you just instantiate an FMParser with something like new FMParser(new StringReader(myString)...) it isn't in the appropriate lexical state to parse an expression. Of course, a silly workaround would also be to parse the string "${" + myString + "}" and then drill down to get the expression iobject nside the interpolation object, but that's a little silly. But anyway, it switches into the FM_EXPRESSION lexical state after hitting the ${ token. Obviously it's better and more efficient to simply instantiate the parser in the appropriate lexical state as in the above snippet. Just providing some needless detail. :-) JR > FMParser parser = new FMParser(token_source); > parser.template = getTemplate(); token. > Expression exp = parser.Expression(); > > Attila. > > -- > weblog: http://constc.blogspot.com > twitter: http://twitter.com/szegedi > > On Oct 7, 2008, at 1:31 PM, conrad wrote: > >> Hi, >> >> I'm wondering if anyone can help me with following situation... >> >> I'm trying to figure out how I could parse a freemarker expression >> in order to >> get a list of the variables the expression (or template) references. >> >> I need this because I want to dynamically fetch the referenced >> variables from a >> user interface when the template is rendered. >> >> I tried parsing an expression using the FMParser, but I run into all >> kinds of >> trouble, so this does not seem the appropriate way to go about this. >> >> ... anyone know how to handle this? >> >> >> >> Thnx, >> Conrad. > > ------------------------------------------------------------------------- > This SF.Net email is sponsored by the Moblin Your Move Developer's challenge > Build the coolest Linux based applications with Moblin SDK & win great prizes > Grand prize is a trip for two to an Open Source event anywhere in the world > http://moblin-contest.org/redirect.php?banner_id=100&url=/ > _______________________________________________ > FreeMarker-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/freemarker-user > ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/