[erlang-questions] prettypr

Ulf Wiger <>
Fri May 11 23:15:18 CEST 2012


If the code you want to pretty-print is generated from other code - and you don't want to create abstract forms by hand (most don't) - I can also recommend parse_trans_codegen:

https://github.com/esl/parse_trans/blob/master/doc/parse_trans_codegen.md

It's a fairly easy, and less error-prone, way to generate code.

If you look e.g. at https://github.com/esl/parse_trans/blob/master/examples/ex_codegen.erl

we can try the two first functions:

f(Name) ->
    codegen:gen_function(
      Name,
      fun(1,2,3) ->
              foo;
          (A,B,C) ->
              {A,B,C}
      end).

g(Name, V) ->
    codegen:gen_function(
      Name,
      fun(L) ->
              member({'$var',V}, L)
      end).


Eshell V5.9  (abort with ^G)
1> c(ex_codegen).
{ok,ex_codegen}
2> ex_codegen:f(foo).
{function,9,foo,3,
          [{clause,11,
                   [{integer,11,1},{integer,11,2},{integer,11,3}],
                   [],
                   [{atom,12,foo}]},
           {clause,13,
                   [{var,13,'A'},{var,13,'B'},{var,13,'C'}],
                   [],
                   [{tuple,14,[{var,14,'A'},{var,14,'B'},{var,14,'C'}]}]}]}
3> ex_codegen:g(is_in, foo).
{function,20,is_in,1,
          [{clause,22,
                   [{var,22,'L'}],
                   [],
                   [{call,23,{atom,23,member},[{atom,23,foo},{var,23,'L'}]}]}]}
4> io:fwrite("~s~n", [erl_pp:form(v(3))]).
is_in(L) ->
    member(foo, L).


…you get the idea.

Now, since I actually don't know exactly what you are trying to do, I will quit throwing out suggestions. :)

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 <> 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
>> 
>> 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/d06a8903/attachment.html>


More information about the erlang-questions mailing list