[groovy-user] How does groovy detect a line break

2,267 views
Skip to first unread message

jitendra...@gmail.com

unread,
Mar 2, 2015, 6:40:31 AM3/2/15
to us...@groovy.codehaus.org
Hi,
How does groovy detect a line break even without keeping a
semicolon--";".


Regards,
Jitendra Singh



--
View this message in context: http://groovy.329449.n5.nabble.com/How-does-groovy-detect-a-line-break-tp5722913.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Jochen Theodorou

unread,
Mar 2, 2015, 7:35:47 AM3/2/15
to us...@groovy.codehaus.org
Am 02.03.2015 12:38, schrieb jitendra...@gmail.com:
> Hi,
> How does groovy detect a line break even without keeping a
> semicolon--";".

in the parser you mean? "\n" becomes significant. So sometimes it is
just swallowed (like in 1+\n2) and sometimes not (like in 1\n+2). It
depends on the grammar rule

bye blackdrag

--
Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org

jitendra...@gmail.com

unread,
Mar 4, 2015, 11:39:08 AM3/4/15
to us...@groovy.codehaus.org
class GroovyMain { static void main(String... args) { new GroovyHello().sayHello() new JavaHello().sayHello() } } For example, in above line of code there is no semicolon placed, what is the grammer rule which force groovy to undertand that there is a line break and compile without any error. Thanks.

View this message in context: Re: How does groovy detect a line break

Owen Rubel

unread,
Mar 4, 2015, 12:30:39 PM3/4/15
to us...@groovy.codehaus.org
if there is output in the underlying method, you can detect off that. If it's a stream then pipe into log, pipe into buffer, etc and detect there.

I don't understand the problem

Keith Suderman

unread,
Mar 4, 2015, 4:07:59 PM3/4/15
to us...@groovy.codehaus.org
Did your code example get reformatted?  The code you posted doesn't compile (after defining the classes GroovyHello and JavaHello of course). There is no grammar rule -- that I know of -- that will accept that.  If you want to put multiple statements on one line they must be separated by semi-colons.  Otherwise Groovy is going to try and parse it all as one statement.

Cheers,
Keith
------------------------------
Research Associate
Department of Computer Science
Vassar College
Poughkeepsie, NY


Jitendra Singh

unread,
Mar 5, 2015, 1:36:29 AM3/5/15
to us...@groovy.codehaus.org
The code got reformatted while posting its actually line by line as corrected below with new exapmle>

class GroovyMain { 
static void main(String... args) { 
// Set email address sender
                         String s1 = "jeet...@gmail.com"
// Set password sender
String s2 = "jitu1234"
 println s1
                         println s2
 } 

My query will fit in now, you could see now none of the line is ended by a ;  . How does groovy detect this line break?
Thanks
--
Regards,
Jitendra Singh

Jim White

unread,
Mar 5, 2015, 2:19:54 AM3/5/15
to us...@groovy.codehaus.org
I haven't looked at Groovy's grammar (it's in ANTLR) but I'm pretty sure the rule is effectively that an EOL is treated as a ; if that would end a syntactically valid statement.

Therefore this is two statements:

1
+ 2

because this is valid:

1 ;
+ 2 

and this is one statement:

1 + 
2

because this is not:

1 + ;

A simple means to make an expression span multiple lines therefore is to embed the EOL within some parenthesis like this single statement:

(1
+ 2)


In Groovy semicolons at the end of the line can be omitted, if the line contains only a single statement.

Which is admittedly confusing because it isn't true.  We know that because this is valid and is parsed as three statements not two:

def a ; 1 
+ 2 

So I would go with a definition more like what I said above such as:

In Groovy a semicolon at the end of the line can be omitted if it would be valid for a statement to end with a semicolon there.

A bit stilted but more correct.

Jim

Jochen Theodorou

unread,
Mar 5, 2015, 11:35:36 AM3/5/15
to us...@groovy.codehaus.org
Am 05.03.2015 07:34, schrieb Jitendra Singh:
[...]
> My query will fit in now, you could see now none of the line is ended by
> a *; . How does groovy detect this line break?*


let's take this example and I will show you a little more detail than
Jim based on the grammar. Most important here is the nls rule in the
grammar:
https://github.com/groovy/groovy-core/blob/master/src/main/org/codehaus/groovy/antlr/groovy.g#L3347

newlines are significant, meaning we need to add this rule everywhere,
that this is supposed to not play a role. But nls is not exchangeable
with ";" For this we have the sep rule:
https://github.com/groovy/groovy-core/blob/master/src/main/org/codehaus/groovy/antlr/groovy.g#L3332
And statements are basically concatenated like this:
https://github.com/groovy/groovy-core/blob/master/src/main/org/codehaus/groovy/antlr/groovy.g#L493

So for a "new X()" for example
https://github.com/groovy/groovy-core/blob/master/src/main/org/codehaus/groovy/antlr/groovy.g#L3145

simplified:

"new" nls (typeArguments)? t:type (nls methodCallArgs classBlock)?

Meaning, there is a newline allowed after the "new" without terminating
the expression, you can have typeArguments for generics, but there is no
newline allowed before the type itself. The parameters to the new call
can be after a newline, the classblock not.... according to this rule at
least. If you ask now why there are not more nls in there... we would
have to check what the parser generator then says about that. Working
around the limitations of the generated parser is no easy task.
Reply all
Reply to author
Forward
0 new messages