[erlang-questions] Help with erl_parse / epp

Jacob Vorreuter <>
Thu Dec 10 17:52:21 CET 2009


Yes, erl_pp:form/1 expects a single form as its input.  Once the source code
is tokenized it is easy to group the tokens into forms.  Every form ends
with the {dot, LINE} token.

Does that answer your question?

You can check out my EUC presentation if you're interested in this kind of
stuff: http://jacobvorreuter.com/hacking-erlang-at-euc-2009

Jake

On Thu, Dec 10, 2009 at 7:57 AM, Brian Candler <> wrote:

> I am trying to parse erlang into abstract form and then back into erlang
> source. Here's the code I'm trying to run:
>
> -module(rfe_cmd).
> -export([erl2erl/0]).
> 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),
>  io:put_chars(erl_pp:form(Form)),
>  ok.
>
> and the startup script 'erl2erl':
>
> #!/bin/sh
> 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?
>
> Thanks,
>
> Brian.
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>


More information about the erlang-questions mailing list