[erlang-questions] How to get syntax tree out of abstract forms?

Justin Calleja calleja.justin@REDACTED
Thu Feb 7 15:30:38 CET 2013


Hi Richard,

Thanks for the reply :)

I had already tried erl_syntax:form_list/1 on the Forms. The output is:

21> erl_syntax:form_list(Forms).
{tree,form_list,
    {attr,0,[],none},
    [{attribute,1,file,{"../src/eaop_explore/hello.erl",1}},
     {attribute,1,module,hello},
     {attribute,2,export,[{send_hello,1},{receive_hello,0}]},
     {function,4,send_hello,1,
         [{clause,4,
              [{var,4,'To'}],
              [],
              [{op,5,'!',
                   {var,5,'To'},

{tuple,5,[{atom,5,hello},{call,5,{atom,5,self},[]}]}}]}]},
     {function,7,receive_hello,0,
         [{clause,7,[],[],
              [{'receive',8,
                   [{clause,9,
                        [{tuple,9,[{atom,9,hello},{var,9,'From'}]}],
                        [],
                        [{tuple,10,[{atom,10,ok},{var,10,'From'}]}]},
                    {clause,11,
                        [{var,11,'Msg'}],
                        [],
                        [{tuple,12,

[{atom,12,unknown_msg},{var,12,'Msg'}]}]}]}]}]},
     {eof,13}]}

But, when I did:

24> module_attr:postorder(module_attr:getAtomFun(), v(21)).
{tree,form_list,
    {attr,0,[],none},
    [{tree,attribute,
         {attr,1,[],none},
         {attribute,
             {tree,atom,{attr,0,[],none},a_file},
             [{tree,string,
                  {attr,1,[],none},
                  "../src/eaop_explore/hello.erl"},
              {tree,integer,{attr,1,[],none},1}]}},
     {tree,attribute,
         {attr,1,[],none},
         {attribute,
             {tree,atom,{attr,1,[],none},module},
             [{tree,atom,{attr,0,[],none},a_hello}]}},
     {tree,attribute,
         {attr,2,[],none},
         {attribute,
             {tree,atom,{attr,2,[],none},export},
             [{tree,list,
                  {attr,2,[],none},
                  {list,
                      [{tree,arity_qualifier,
                           {attr,2,[],none},
                           {arity_qualifier,{tree,...},{...}}},
                       {tree,arity_qualifier,
                           {attr,2,[],...},
                           {arity_qualifier,{...},...}}],
                      none}}]}},
     {tree,function,
         {attr,4,[],none},
         {function,
             {tree,atom,{attr,0,[],none},a_send_hello},
             [{tree,clause,
                  {attr,4,[],none},
                  {clause,
                      [{var,4,'To'}],
                      none,
                      [{tree,infix_expr,{attr,...},{...}}]}}]}},
     {tree,function,
         {attr,7,[],none},
         {function,
             {tree,atom,{attr,0,[],none},a_receive_hello},
             [{tree,clause,
                  {attr,7,[],none},
                  {clause,[],none,[{tree,receive_expr,{...},...}]}}]}},
     {eof,13}]}

and then:

25> erl_syntax:revert_forms(v(24)).
** exception error: {error,
                        {tree,attribute,
                            {attr,1,[],none},
                            {attribute,
                                {atom,0,a_file},
                                [{string,1,"../src/eaop_explore/hello.erl"},
                                 {integer,1,1}]}}}
     in function  erl_syntax:revert_forms/1 (erl_syntax.erl, line 6648)

lol, taking a look at it now I can see what I did... changed the 'file'
attribute to 'a_file'... it works if you remove the following (or at least
change 'a_file' to 'file')

{tree,attribute,
   {attr,1,[],none},
   {attribute,
    {tree,atom,{attr,0,[],none},a_file},
    [{tree,string,{attr,1,[],none},"../src/eaop_explore/hello.erl"},
     {tree,integer,{attr,1,[],none},1}]}},

How stupid of me ^^;

Already came across the same problem with the 'export' and 'module'
attributes just 30 mins or so ago. Thanks for the help!!

Regards,
Justin


On 7 February 2013 15:03, Richard Carlsson <carlsson.richard@REDACTED>wrote:

> On 02/07/2013 02:55 PM, Justin Calleja wrote:
>
>> I would like to use the postorder/2 function in the comments of
>> erl_syntax:
>>
>> postorder(F, Tree) ->
>>          F(case erl_syntax:subtrees(Tree) of
>>              [] -> Tree;
>>              List -> erl_syntax:update_tree(Tree,
>>                                  [[postorder(F, Subtree)
>>                                    || Subtree <- Group]
>>                                   || Group <- List])
>>            end).
>>
>> to traverse a syntax tree and do some kind of manipulation.
>>
>> The problems is that, although the documentation (or how I understood
>> the doc anyway) claims that abstract forms are a subset of syntax trees,
>>
>
> Yes, an abstract form is also an AST that can be used with e.g.
> erl_syntax:subtrees(). However, a *list* of abstract forms is not a tree.
> What you can do is to give your list to erl_syntax:form_list(Fs) to get a
> single AST that represents all the forms, and then pass that tree to your
> traversal function.
>
>    /Richard
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130207/03ee98e4/attachment.htm>


More information about the erlang-questions mailing list