[erlang-questions] Analysing an Erlang file

Joe Armstrong <>
Wed Jun 17 16:10:15 CEST 2009


This should get you started ...

Use epp:file to parse the erlang, then a list comprehension to yank
out the stuff you
are interested in.

Like this:

test() ->
    {ok, L} = epp:parse_file("elib1_misc.erl",[],[]),
    [{{Name,Arity},length(Clauses)} || {function,_,Name,Arity,Clauses} <- L].

> test().
[{{test,0},1},
 {{added_files,2},1},
 {{complete,2},1},
 {{dos2unix,1},3},
 {{downcase_char,1},2},
 {{downcase_str,1},1},
 {{dump,2},1},
 {{duplicates,1},1},
 {{duplicates,2},4},
  ...

This is a bit "quick and dirty" since you need to know the internal
syntax of the abstract forms -

I suggest you define

dump(File, Term) ->
    Out = File ++ ".tmp",
    io:format("** dumping to ~s~n",[Out]),
    {ok, S} = file:open(Out, [write]),
    io:format(S, "~p.~n",[Term]),
    file:close(S).

then evaluate

dump("foo", epp:parse_file(File, [], [])).

Then stare hard at foo.tmp and all the secrets of the Erlang masters
will be revealed :-)

(note dump/2 is *very* useful for inspecting large data structures - I
use it all the time)



/Joe



On Wed, Jun 17, 2009 at 12:00 AM, Torben
Hoffmann<> wrote:
> Thanks to both Christian and Ricard for pointing out the epp_dodger module -
> it has exactly what I need!
>
> I have been reading the erl_syntax documentation and it is definitely what I
> need to use... I just couldn't get started.
>
> Cheers,
> Torben
>
> On Tue, Jun 16, 2009 at 11:02 PM, Richard Carlsson <>wrote:
>
>> Torben Hoffmann wrote:
>>
>>> Or should I approach this problem from a different angle?
>>>
>>
>> I suggest you read up on the syntax_tools application, mainly
>> the erl_syntax module and the epp_dodger module; the latter
>> lets you parse most source files, even those that contain include
>> directives, macros, and other preprocessor features. The former
>> (and the erl_syntax_lib companion module) make it a lot easier to
>> handle the parsed syntax trees.
>>
>>    /Richard
>>
>
>
>
> --
> http://www.linkedin.com/in/torbenhoffmann
>


More information about the erlang-questions mailing list