[erlang-questions] clarify: how to traverse AST with erl_syntax functions ?

Richard Carlsson richardc@REDACTED
Mon Dec 17 17:04:42 CET 2007


Roberto Saccon wrote:
> 2> {ok,{_,[{abstract_code,{_,Ac}}]}} = beam_lib:chunks(Beam,[abstract_code]).
> ...
> 3>T= erl_syntax:abstract(Ac).
> ...
> 4> erl_syntax:type(T).
> list
> 5> Ts=erl_syntax:list_elements(T).

Oops, I didn't see at first what you were doing. You should not use
the function erl_syntax:abstract/1 on the value Ac - that already is
a *list* of AST:s (albeit in the form that erl_parse returns), which
are acceptable to the erl_syntax functions. The list corresponds to
the sequence of declarations in the original program. If you convert
the list to a single AST, using erl_syntax:form_list/1, you can pass
this AST to erl_prettypr:format/1 to display it. (Or you can iterate
over the list and display the individual AST:s one at a time.)

The function erl_syntax:abstract/1 is for taking a concrete value,
as in erl_syntax:abstract(42), and returning an AST representing
the syntax for that value. (When you called abstract/1 on the value
Ac above, you created a representation of the representation.)

     /Richard





More information about the erlang-questions mailing list