When writing transformations, you need to think about rolling up the
information from the leaves towards the trunk. You also have to match a
whole hash at a time.

You would therefore match the {:variable_atom=>"item"@30} hash and replace
it with some ruby class that represents the construct you are representing.
I would then transform {:template_variable=>VariableNode} to something...
etc.

You can match subtrees to deal with a tree at once, but this is supported
to be the exception not the main approach.

I hope this helps.

Cheers
Nigel

---
"No man is an island... except Philip"


On Sat, Jun 29, 2013 at 6:16 PM, Eric Sagnes <[email protected]> wrote:

> Hello,
> I have some trouble in understanding how scopes can be used during the
> transform.
>
> I am trying to implement a stringtemplate <
> http://www.antlr.org/wiki/display/ST4/StringTemplate+cheat+sheet>
> interpreter.
> The parser parts works like a charm, but I am struggling with the
> transformation.
>
> In stringtemplate, there is something called anonymous templates that work
> in a similar fashion to the Array.map function in ruby.
>
> For example, the following stringtemplate code loops through "bar" values,
> assign each value to "item" in the {} context and display its value.
> ===
> $bar:{ item | $item$ }$
> ===
> equivalent ruby code:
> ===
> bar.map{ |item| p item }
> ===
>
> Using the following input string:
> =====
> $foo$
> $bar:{ item | $item$ }$
> =====
>
> My parser is generating the following tree:
> =====
> {:template_code=>
>   [{:expression=>
>      [{:variable=>{:template_variable=>{:variable_atom=>"foo"@1}}}]},
>    {:expression=>
>      [{:anonymous_template=>
>         {:variable=>{:template_variable=>{:variable_atom=>"bar"@16}},
>          :anonymous_variable=>"item"@22,
>          :template_code=>
>           [{:expression=>
>              [{:variable=>
>                 {:template_variable=>{:variable_atom=>"item"@30}}}]}]}}]}]}
> =====
>
> I noticed the transform will process the value "bar" before "item".
> But I am stuck on how to access the value of "bar" when processing "item"
> value, or in a more generic way in how to set/access scopes during
> transform.
> What would be a good solution to this problem?
>
> The current code of the parser and transform can be checked here <
> http://pastebin.com/3UB675f4 >.
>
> Any help will be greatly appreciated,
> Thanks in advance!
>
> Cheers,
> --
> Eric Sagnes
> サニエ エリック

Reply via email to