erl_prettypr

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Mon Jun 27 17:25:53 CEST 2005


I wrote my own layout engine, but cheated - and let the 
token scanner return column numbers rather than just line
numbers. The "layout engine" then laid things out exactly
as written.

In order to preserve comments, I made the token scanner 
save comments as well. The tricky part was knowing how
to pass them along without messing up the parser.

I finally settled on the perhaps inelegant strategy of 
making every other element in the list of tokens a 
"comment or whitespace" list.

Like so:

1> ccv_erl_scanner:file("ccv_stats.erl").
[[{comment,1,
           1,
           1,
           "%%% ----------------------------------------------------------"},
  {comment,2,1,1,"%%%"},
  {comment,3,
           1,
           1,
           "%%% The contents of this file are subject to the Erlang Public License,"},
...
  {comment,19,
           1,
           1,
           "%%% Contributor(s): ______________________________________."},
  {ws,20,1,1,"\n"},
  {...}|...],
 {symbol,31,1,2,'-'},
 [],
 {word,31,2,8,"module"},
 []|...]


One could of course use "annotated tokens" instead. Don't know 
what would be the most b/w incompatible change. Perhaps adding
column numbers to the scanner would involve reasonable effort.

/Uffe

> -----Original Message-----
> From: owner-erlang-questions@REDACTED
> [mailto:owner-erlang-questions@REDACTED]On Behalf Of 
> Richard Carlsson
> Sent: den 27 juni 2005 12:45
> To: Mats Cronqvist (AL/EAB); erlang-questions@REDACTED
> Subject: Re: erl_prettypr
> 
> 
> Mats Cronqvist wrote:
> >   i'm trying to make a erl-html filter using syntax-tools 
> (works so-so),
> > but i've found this annoying behaviour. if i have this in a 
> file (named
> > tst.erl);
> > 
> > foo(Bla,X)->%hello
> >     Bla.
> > 
> >   running this beauty;
> > 
> > 
> F="tst.erl",erl_prettypr:format(erl_recomment:recomment_forms(
> element(2,epp_dodger:parse_file(F)),erl_comment_scan:file(F))).
> > 
> > 
> >   will give this;
> > 
> > "foo(Bla,\n    X) ->%hello\n    Bla.
> > 
> >   there shouldn't be a line break between Bla and X. and 
> indeed, if the
> > comment is removed there isn't.
> >   anybody got any advice?
> 
> It's been a while since I wrote that stuff, so I had to try 
> it out to see why
> this happens. Although it might certainly be irritating, it's 
> not without logic
> (and I don't know if it can be fixed). The comment is 
> attached to the nearest
> construct in the parse tree, in this case the variable 'X' 
> (and not the
> function clause). Then, the formatting will not like that an 
> element in a
> parameter list has an attached comment (because comments must 
> end with a line
> break), and will lay out the sequence vertically. (After 
> that, the comment will
> be moved outside the ")->" part, but that is by another mechanism.)
> 
> Consider this slight modification, and you might see that 
> your example is
> just a special case:
> 
>   foo(Bla,
>       X, %hello
>       Bleh)->
>       Bla.
> 
> Of course, that doesn't make it less irritating, but now you 
> know what is
> causing it. Feel free to try to solve the problem. And 
> welcome to the wonderful
> world of layout engines.
> 
> 	/Richard
> 



More information about the erlang-questions mailing list