Help with erl_parse / epp

Brian Candler B.Candler@REDACTED
Thu Dec 10 16:57:08 CET 2009

I am trying to parse erlang into abstract form and then back into erlang
source. Here's the code I'm trying to run:

erl2erl() ->
  [Fname] = init:get_plain_arguments(),
  {ok, File} = file:read_file(Fname),
  {ok, Tokens, _} = erl_scan:string(binary_to_list(File)),
  {ok, Form} = erl_parse:parse_form(Tokens),

and the startup script 'erl2erl':

erl -pa ebin -pa ../ebin -noshell -s rfe_cmd erl2erl -s init stop -extra "$@"

It works when run on a simple input file, such as

f (A) -> A+B;  
f ([B]) -> length(B).

However it barfs when run against its own source.

{"init terminating in do_boot",{{badmatch,{error,{2,erl_parse,["syntax error
before: ","'-'"]}}},[{rfe_cmd,erl2erl,0},{init,start_it,1},{init,start_em,1}]}}

I think it's something to do with the -module / -export directives.

If I do

    {ok, Forms} = epp:parse_file(Fname, [], []),
    lists:foreach(fun(Z) -> io:put_chars(erl_pp:form(Z))  end, Forms).

then it seems to work. Is this the "right" way to handle it? That is,
erl_parse can only handle a single form rather than a complete source file?

If that's true, then how does epp know where the boundaries are between
the functions (forms) in a file, before it's actually parsed?



More information about the erlang-questions mailing list