Hello Nigel, Thanks for the asnwer. Indeed, That make sense.
So to wrap it up, considering the following example:
A - Main scope - variables foo=42
- B - SubScope - getting A scope "foo" value and asign it to the variable "baz"
The transform should act as:
1/ every rule return a custom Ruby object encapsulating each of it sub-rules as
ruby object.
2/ all ruby objects should respond to a common "process" function that return
the transform result for this object
3/ the root rule calls a "process" function on each of its sequence result
passing the main scope variable dictionary
4/ the B subscope "process" function create the B scope variable dictionary and
pass it to the "process" function of each of his encapsulated subtrees object
Cheers,
Eric
On 2013年7月1日 月曜日 09:10:16 Nigel Thorne wrote:
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
サニエ エリック
--
Eric Sagnes
サニエ エリック
signature.asc
Description: This is a digitally signed message part.
