[antlr-interest] eating newlines

Terence Parr parrt at cs.usfca.edu
Mon Sep 27 18:34:35 PDT 2004


Howdy,

I've a proposal for consuming more newlines automatically.  Almost 
always you want <if> to work as it does:

<if(foo)>
this line
<else>
that line
<endif>

where you get exactly "this line\n" or "that line\n", but not "\nthis 
line\n" and "\nthat line\n" (note that there is in fact a \n after the 
<if(foo)> and before the "this line".  So, i automatically consume any 
\n immediately following an <if> or <else>.  Also, ignore \n after 
<endif> if on line by itself.  So, the following

hi <if(foo)>this<else>that</endif>
ho

gives you either

hi this
ho

or

hi that
ho

There are lots of cases, however, where one wants the \n to be killed 
before the <else> and <endif>.  So

<if(foo)>this<else>that</endif>

would be identical to

<if(foo)>
this
<else>
that
</endif>

which is way more readable for anything but small bits of text.  The 
problem is that sometimes you might want that extra newline as in the 
"this line\n" above.  Here is a real example from the ANTLR 3.0 code 
generator:

<if(LEXER)>
     System.err.println("<description>: no viable alt line "+getLine()+" 
c='"+(char)input.LA(1)+"'");
<else>
     System.err.println("<description>: no viable alt line 
token="+((TokenStream)input).LT(1));
<endif>

You can always get it with an extra line though like this i were to 
change it:

<if(LEXER)>
     System.err.println("<description>: no viable alt line "+getLine()+" 
c='"+(char)input.LA(1)+"'");

<else>
     System.err.println("<description>: no viable alt line 
token="+((TokenStream)input).LT(1));

<endif>

would yield "this\n" or "that\n".

The same goes for the new StringTemplateGroup group file format.  Often 
for clarity I want to put <<...>> template specs on separate lines:

test() ::= <<
fubar
 >>

Often I want "fubar", but I get "fubar\n".

My proposal is thus to be absolutely consistent: kill a single newline 
after <if>, <<, <else>, and <endif> (if on line by itself) as it does 
now.  Then kill newlines *before* <else> and <endif> and >>.  Anybody 
want to comment?

Thanks,
Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list