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 Wed, Oct 8, 2008 at 10:24 AM, conrad <[email protected]> wrote:
> Jonathan Revusky <revusky <at> gmail.com> writes:
>
>>
>> On Tue, Oct 7, 2008 at 3:44 PM, Attila Szegedi <szegedia <at> gmail.com> 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
>> >
>
>
> Many thanks for the feedback, especially for that bit of 'needless' detail... ;-)
>
> I tried using the FMParserTokenManager, but this type is not visible from
> outside of the freemarker.core package.

These things are more accessible in FreeMarker 2.4.


>
> Using the "${" + myString + "}" trick gets me one step further, but I don't see
> how I can walk the parser tree (parser.getNextToken()) to filter out any
> parameter tokens, since the Token type is also package local.

Using 2.3, it may be that you just have to make what you need publicly
visible and recompile the source.

JR


>
> ... it's a pity the parser implementation is not more accessible.
>
> I could of course create some types in the freemarker.core package to circumvent
> this, but I was kind of hoping there would be some higher level Freemarker API
> functions that allows me to parse an expression and extract the variable
> references (among other things) before actually evaluating the expression.
>
>
> 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=/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.