[erlang-questions] prettypr

Ulf Wiger ulf@REDACTED
Fri May 11 23:03:10 CEST 2012


Hmm, so the code is in text form now?

E.g. Src = "foo() ->\n foo.\n"?

Well, you could do it that way:

Eshell V5.9  (abort with ^G)
1> Src = "add(X, Y) ->\n   X + Y.\n".
"add(X, Y) ->\n   X + Y.\n"
2> erl_scan:string(Src).
{ok,[{atom,1,add},...],3}
3> Ts = element(2,v(2)).
[{atom,1,add},
 {'(',1},... {dot,2}]
4> erl_parse:parse_form(Ts).
{ok,{function,1,add,2,
              [{clause,1,
                       [{var,1,'X'},{var,1,'Y'}],
                       [],
                       [{op,2,'+',{var,2,'X'},{var,2,'Y'}}]}]}}
5> io:fwrite("~s~n", [erl_pp:form(element(2,v(4)))]).
add(X, Y) ->
    X + Y.

ok

…but it will only work as long as you don't rely on the preprocessor for anything in the code, and only for one form at a time.

Full parsing, including preprocessing, from string input is not that easy. I did it in erlhive, but then used a ram_file_io_server (which I've long thought should be part of OTP):

http://erlhive.svn.sourceforge.net/viewvc/erlhive/trunk/lib/erlhive/src/erlhive_compile.erl?revision=70&view=markup (line 123)
http://erlhive.svn.sourceforge.net/viewvc/erlhive/trunk/lib/erlhive/src/erlhive_ram_file_io_server.erl?revision=57&view=markup

BR,
Ulf W

On 11 May 2012, at 22:28, Roberto Ostinelli wrote:

> Thank you Ulf.
> 
> So i first should erl_parse:tokens/1, then erl_parse:parse/form/1 then use what you suggested?
> 
> 
> On Fri, May 11, 2012 at 1:09 PM, Ulf Wiger <ulf@REDACTED> wrote:
> 
> You could take a look at how parse_trans does it:
> 
> https://github.com/esl/parse_trans/blob/master/src/parse_trans_pp.erl#L65
> 
> It uses erl_pp instead of erl_prettypr for reasons I have since forgotten
> (it was something that, at one time, erl_prettypr had problems with, but
> erl_pp was able to handle).
> 
> BR,
> Ulf W
> 
> On 11 May 2012, at 22:01, Roberto Ostinelli wrote:
> 
>> Dear All,
>> 
>> is there a way to use prettypr on a list()?
>> 
>> I want to pretty format some source code, all I found is a Trapexit post showing this:
>> 
>> F="tst.erl",
>> erl_prettypr:format(erl_recomment:recomment_forms(element(2,epp_dodger:parse_file(F)),erl_comment_scan:file(F))).
>> 
>> Unfortunately this is based on parsing a file, and epp_dodger needs an IoDevice to parse code.
>> 
>> Is there a way to do something similar but inputting the code as list() instead of parsing a file?
>> 
>> r.
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
> 
> Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
> http://feuerlabs.com
> 
> 
> 
> 

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120511/be67682c/attachment.htm>


More information about the erlang-questions mailing list