Dear Nigel and all,
I have written a recursive method to convert the parsed hash produced by
Parslet into RubyTree. In RubyTree, I use the offset interval of matches as the
node name (or key), and store the line range in node content (a hash). Here is
an example:
Input:
type, public, extends(list_elem_t<foo_t>) :: foo_t
integer i
contains
procedure :: init => foo_init
end type foo_t
Output (by changing 'print_tree' method in RubyTree):
* type_declaration ():
|---+ type_attributes ():
| |---+ type_attribute ():
| +---> attribute (): public
| +---+ type_attribute ():
|---> attribute (): extends
+---+ attribute_parameter ():
+---+ template_instance ():
|---> id (template_name): list_elem_t
+---+ template_arguments ():
+---+ expression ():
+---+ left_expression ():
+---+ id_with_scripts (left_item):
+---> id (): foo_t
|---> id (type_name): foo_t
|---+ declarations ():
| +---+ data_declaration ():
|---+ intrinsic_type (type):
| +---> type_name (): integer
+---+ data_list ():
+---+ data ():
+---+ id_with_scripts ():
+---> id (): i
|---+ tbp_declarations ():
| +---+ tbp_declaration ():
|---> id (tbp_name): init
+---> id (actual_tbp_name): foo_init
+---> id (end_name): foo_t
The texts in parens are the roles of the matches, and the texts after colon are
the contents.
The method can be found in
https://gist.github.com/dongli/5791976#file-fortran_parser-rb-L635-L680
This is my first try, maybe needs polish.
I have used the simple and stupid 'Mark' atom that I added to get the bounding
locations of the important matches, but it is not accepted (no offence~), so
the gist can not be run in your environment by far. I will move that atom into
my FortranParser later.
Cheers,
Li
在 2013-6-21,下午10:34,Nigel Thorne <[email protected]> 写道:
> Give an example of something you have generated from parslet, and what you
> want it to become.
>
> A failing unit test would be a good way to explain the problem you are
> having.
>
> Cheers
> Nigel
>
>
> ---
> "No man is an island... except Philip"
>
>
> On Fri, Jun 21, 2013 at 12:04 PM, Li Dong <[email protected]> wrote:
> Dear all,
>
> I would like to know how to turn the parsed hash tree into RubyTree[1]. By
> doing this, I can custom the behavior of each node. For example, check if
> there
> is any specific node in a subtree. Is this a practical way for doing
> post-processing on the parsed tree?
>
> How to do this through 'Transform'? Need I write a transforming rule for each
> parsing rule, which will be very cumbersome? I imagine this should be through
> a recursive method.
>
> Comment and advice is appreciated!
>
> Best,
>
> Li
>
> [1] http://rubytree.rubyforge.org
>
>
>
>