Discussion:
Ternary on multiple lines problem
Marc Palmer
2007-03-06 09:45:08 UTC
Permalink
Hi,

Is this an intentional break from Java?

def value = "hello"

def y = (value)
? value
: "no value"

This yields:

org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed, Script1: 4: unexpected token: ? @ line 4, column 3.
1 error


Now unless I've gone mad this is perfectly legal in Java. I am
guessing this is related to optional semicolons? Does this mean that
in Groovy all ternaries are supposed to be on one line?

def y = (value) ? value
: "no value";

results in:

org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed, Script5: 3: expecting ':', found '<newline>' @ line
3, column 24.
1 error


Thanks,
Marc


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

http://xircles.codehaus.org/manage_email
Jochen Theodorou
2007-03-06 09:54:16 UTC
Permalink
Post by Marc Palmer
Hi,
Is this an intentional break from Java?
def value = "hello"
def y = (value)
? value
: "no value"
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
1 error
Now unless I've gone mad this is perfectly legal in Java. I am guessing
this is related to optional semicolons? Does this mean that in Groovy
all ternaries are supposed to be on one line?
it means there are some NLS! tokens missing in the grammar. jez you
agree? I am not sure it introduces new problems if we add the NLS!

bye blackdrag
--
Jochen "blackdrag" Theodorou
Groovy Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

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

http://xircles.codehaus.org/manage_email
Barzilai Spinak
2007-03-06 19:19:50 UTC
Permalink
A little rewriting, fixes it:

def value = "hello"

def y = (value) ?
value :
"no value"

That is, put the ? and : symbols before the line break so the parser
"expects" something else on the next line.
I guess I never encounter this problem because I like the symbols at the
end of the line as a form of visual "continuation".
Of course, this is all a matter of style and taste :-)
Explicit obligatory semi-colons in Java make all these problems disappear.
At some point, during language fashion history someone decided
semi-colons are Evil and Ugly and Passé so all cool and groovy dynamic
languages don´t use semi-colons, or they are frowned upon.
Lately after using Groovy for some time I've been having semi-colon
identity crisis and I use them randomly in Groovy and Java, giving weird
compiler errors in the latter case :-)


Anyway, I haven't looked at Groovy's grammar so let's leave it to the
language experts to solve this.

BarZ
Post by Jochen Theodorou
Post by Marc Palmer
Hi,
Is this an intentional break from Java?
def value = "hello"
def y = (value)
? value
: "no value"
1 error
Now unless I've gone mad this is perfectly legal in Java. I am
guessing this is related to optional semicolons? Does this mean that
in Groovy all ternaries are supposed to be on one line?
it means there are some NLS! tokens missing in the grammar. jez you
agree? I am not sure it introduces new problems if we add the NLS!
bye blackdrag
---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
Alexandru Popescu
2007-03-07 01:32:11 UTC
Permalink
Post by Jochen Theodorou
Post by Marc Palmer
Hi,
Is this an intentional break from Java?
def value = "hello"
def y = (value)
? value
: "no value"
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
1 error
Now unless I've gone mad this is perfectly legal in Java. I am guessing
this is related to optional semicolons? Does this mean that in Groovy
all ternaries are supposed to be on one line?
it means there are some NLS! tokens missing in the grammar. jez you
agree? I am not sure it introduces new problems if we add the NLS!
A nls! in front of ? breaks the grammar, so I would say the change is
not so easy. But I am not an ANTLR expert. Here is the warning I am
getting from it:

[antlr] groovy.g:2216:9: warning:nondeterminism between alts 1 and
2 of block upon
[antlr] groovy.g:2216:9: k==1:NLS
[antlr] groovy.g:2216:9:
k==2:LBRACK,IDENT,LPAREN,QUESTION,"super","void","boolean","byte","char","short","int","float","long","double","any",LCURLY,NL
S,"this",STRING_LITERAL,PLUS,MINUS,INC,DEC,BNOT,LNOT,DOLLAR,STRING_CTOR_START,"new","true","false","null",NUM_INT,NUM_FL
OAT,NUM_LONG,NUM_DOUBLE,NUM_BIG_INT,NUM_BIG_DECIMAL
[antlr] groovy.g:2216:9:
k==3:"final","abstract","do","strictfp","import","static","def",LBRACK,RBRACK,IDENT,DOT,LPAREN,"class","interface","enum",AT,Q
UESTION,"super",LT,COMMA,GT,SR,BSR,"void","boolean","byte","char","short","int","float","long","double","any",STAR,"as",
"private","public","protected","transient","native","threadsafe","synchronized","volatile",ASSIGN,BAND,LCURLY,RCURLY,SEM
I,NLS,"this",STRING_LITERAL,TRIPLE_DOT,CLOSABLE_BLOCK_OP,COLON,"if","else","while","with","switch","for","in","return","
break","continue","throw","assert",PLUS,MINUS,"try","finally","catch",SPREAD_DOT,OPTIONAL_DOT,MEMBER_POINTER,PLUS_ASSIGN
,MINUS_ASSIGN,STAR_ASSIGN,DIV_ASSIGN,MOD_ASSIGN,SR_ASSIGN,BSR_ASSIGN,SL_ASSIGN,BAND_ASSIGN,BXOR_ASSIGN,BOR_ASSIGN,STAR_S
TAR_ASSIGN,LOR,LAND,BOR,BXOR,REGEX_FIND,REGEX_MATCH,NOT_EQUAL,EQUAL,COMPARE_TO,LE,GE,"instanceof",SL,RANGE_INCLUSIVE,RAN
GE_EXCLUSIVE,INC,DIV,MOD,DEC,STAR_STAR,BNOT,LNOT,DOLLAR,STRING_CTOR_START,"new","true","false","null",NUM_INT,NUM_FLOAT,
NUM_LONG,NUM_DOUBLE,NUM_BIG_INT,NUM_BIG_DECIMAL

./alex
--
.w( the_mindstorm )p.
_____________________________________
Alexandru Popescu, OSS Evangelist
TestNG/Groovy/AspectJ/WebWork/more...
Information Queue ~ www.InfoQ.com
Post by Jochen Theodorou
bye blackdrag
--
Jochen "blackdrag" Theodorou
Groovy Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
Jeremy Rayner
2007-03-07 09:23:49 UTC
Permalink
Hi Marc,
For the example
def y = (value) ? value : "no value"

The right hand side of that assignment in Java is defined like this:
(value) ? value : "no value"
logicalOrExpression (QUESTION^ assignmentExpression COLON!
conditionalExpression)?

Whereas in Groovy, due to semicolons not ending statements, we have to
be explicit in
where we allow the newlines to occur (using 'nls')
logicalOrExpression (QUESTION^ nls! assignmentExpression COLON! nls!
conditionalExpression)?

If this is amended to become:
logicalOrExpression (nls! QUESTION^ nls! assignmentExpression nls!
COLON! nls! conditionalExpression)?
i.e. to allow newlines before the '?' symbol

This would mean that the statement would look like this:

def y = (value)
? value : "no value"

As Alex noted, this leads to non-determinism in the grammar,
(i.e. which way should the parser turn)
compare
def y = (value)
def z = blah // a new statement
and
def y = (value)
? value : "no value" // an intended 'carry on' of the previous statement

As Barzilai correctly points out, you can currently give the parser
a hint, by having the '?' and ':' at the end of the line, rather
than on the newline. As you can see in the grammar above I've
put the 'nls' after QUESTION and COLON, to allow this.

def y = (value) ?
value : "no value" // previous statement was left hanging, so
this is a 'carry on'


Hope this helps

Jez.
--
Groovy Engineer
http://javanicus.com/blog2

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

http://xircles.codehaus.org/manage_email
Alexandru Popescu
2007-03-07 09:58:33 UTC
Permalink
Post by Jeremy Rayner
Hi Marc,
For the example
def y = (value) ? value : "no value"
(value) ? value : "no value"
logicalOrExpression (QUESTION^ assignmentExpression COLON!
conditionalExpression)?
Whereas in Groovy, due to semicolons not ending statements, we have to
be explicit in
where we allow the newlines to occur (using 'nls')
logicalOrExpression (QUESTION^ nls! assignmentExpression COLON! nls!
conditionalExpression)?
logicalOrExpression (nls! QUESTION^ nls! assignmentExpression nls!
COLON! nls! conditionalExpression)?
i.e. to allow newlines before the '?' symbol
def y = (value)
? value : "no value"
As Alex noted, this leads to non-determinism in the grammar,
(i.e. which way should the parser turn)
compare
def y = (value)
def z = blah // a new statement
and
def y = (value)
? value : "no value" // an intended 'carry on' of the previous statement
As Barzilai correctly points out, you can currently give the parser
a hint, by having the '?' and ':' at the end of the line, rather
than on the newline. As you can see in the grammar above I've
put the 'nls' after QUESTION and COLON, to allow this.
def y = (value) ?
value : "no value" // previous statement was left hanging, so
this is a 'carry on'
Hope this helps
Jez.
I am not sure if involving some advanced ANTLR techniques would not
make it possible, but for now I don't have a clue how to do it :-).

./alex
--
.w( the_mindstorm )p.
_____________________________________
Alexandru Popescu, OSS Evangelist
TestNG/Groovy/AspectJ/WebWork/more...
Information Queue ~ www.InfoQ.com
Post by Jeremy Rayner
--
Groovy Engineer
http://javanicus.com/blog2
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
Marc Palmer
2007-03-07 10:42:19 UTC
Permalink
Post by Jeremy Rayner
Hi Marc,
For the example
def y = (value) ? value : "no value"
(value) ? value : "no value"
logicalOrExpression (QUESTION^ assignmentExpression COLON!
conditionalExpression)?
Whereas in Groovy, due to semicolons not ending statements, we have to
be explicit in
where we allow the newlines to occur (using 'nls')
logicalOrExpression (QUESTION^ nls! assignmentExpression COLON! nls!
conditionalExpression)?
logicalOrExpression (nls! QUESTION^ nls! assignmentExpression nls!
COLON! nls! conditionalExpression)?
i.e. to allow newlines before the '?' symbol
def y = (value)
? value : "no value"
As Alex noted, this leads to non-determinism in the grammar,
(i.e. which way should the parser turn)
compare
def y = (value)
def z = blah // a new statement
and
def y = (value)
? value : "no value" // an intended 'carry on' of the previous statement
As Barzilai correctly points out, you can currently give the parser
a hint, by having the '?' and ':' at the end of the line, rather
than on the newline. As you can see in the grammar above I've
put the 'nls' after QUESTION and COLON, to allow this.
def y = (value) ?
value : "no value" // previous statement was left hanging, so
this is a 'carry on'
Hope this helps
Jez thanks for the clarification, I suspected as much. This is an
unfortunate departure from Java whitespace handling that I hand't
noticed before. I can't recall if we have a continuation character.
If we do/did surely that could solve it i.e:

def y = (value) \
? value
: "no value"
I'm assuming we wouldn't need a continuation char on the end of the ?
and : lines because surely the parse has context there, i.e. has a ?
and knows it needs an expr and then a :

I don't want to be a code style nazi but over the years I have
settled on this as the clearest way to break long ternary
expressions, or any kind of expression actually:

def y = someReallyLongValue
+ someOtherLongValue
- someThingElse

I imagine these are "broken" too in Groovy and I just hadn't noticed
yet. The rationale for this style is of course that:

def y = someReallyLongValue +
someOtherLongValue -
someThingElse

...means you have to scan back up a line to find out what is
happening to the bits that occur later, and that if you delete one of
the lines, the operations performed are altered - i.e:

def y = someReallyLongValue +
someThingElse

...adds someThingElse whereas previous it was subtracted.

Is a continuation character worthy of consideration for Groovy 2.0?

Marc


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

http://xircles.codehaus.org/manage_email
Jochen Theodorou
2007-03-07 12:35:19 UTC
Permalink
Marc Palmer schrieb:
[...]
Post by Marc Palmer
def y = (value) \
? value
: "no value"
I'm assuming we wouldn't need a continuation char on the end of the ?
and : lines because surely the parse has context there, i.e. has a ? and
"\" is the continuation character ;) This means we already have it and
your code above does compile. hehe

bye blackdrag
--
Jochen "blackdrag" Theodorou
Groovy Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

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

http://xircles.codehaus.org/manage_email
Marc Palmer
2007-03-07 12:58:57 UTC
Permalink
Post by Jochen Theodorou
[...]
Post by Marc Palmer
def y = (value) \
? value
: "no value"
I'm assuming we wouldn't need a continuation char on the end of
the ? and : lines because surely the parse has context there, i.e.
"\" is the continuation character ;) This means we already have it
and your code above does compile. hehe
Perfect... why didn't anybody say at the start ;-)

Marc


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

http://xircles.codehaus.org/manage_email
Alexandru Popescu
2007-03-07 13:05:11 UTC
Permalink
Post by Marc Palmer
Post by Jochen Theodorou
[...]
Post by Marc Palmer
def y = (value) \
? value
: "no value"
I'm assuming we wouldn't need a continuation char on the end of
the ? and : lines because surely the parse has context there, i.e.
"\" is the continuation character ;) This means we already have it
and your code above does compile. hehe
Perfect... why didn't anybody say at the start ;-)
I think only Jochen was aware of it. Not found it in GINA ;-).

./alex
--
.w( the_mindstorm )p.
_____________________________________
Alexandru Popescu, OSS Evangelist
TestNG/Groovy/AspectJ/WebWork/more...
Information Queue ~ www.InfoQ.com
Post by Marc Palmer
Marc
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email
Dierk Koenig
2007-03-07 13:29:48 UTC
Permalink
Post by Alexandru Popescu
I think only Jochen was aware of it. Not found it in GINA ;-).
Well, I at least also knew about it (once) :-) and I'm pretty sure
Jez and Mr.G did as well.
It's not in Gina, though. Added erratum
http://groovy.canoo.com/errata/erratum/show/14

ciao
Dierk

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

http://xircles.codehaus.org/manage_email
MK Tan
2007-03-08 06:38:18 UTC
Permalink
Hi Dierk,

Just a quick test

def value = "hello"

def y = (value) \
? value
: "no value"

will have the same compilation error, but if I do this

def value = "hello"

def y = (value) \
? value \
: "no value"

it will compile. But if I add a extra space after '\', I've this error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
failed, test.groovy: 3: unexpected char: '\' @ line 3, column 17.
def y = (value) \

Hope this help,
MK Tan
Post by Dierk Koenig
Post by Alexandru Popescu
I think only Jochen was aware of it. Not found it in GINA ;-).
Well, I at least also knew about it (once) :-) and I'm pretty sure
Jez and Mr.G did as well.
It's not in Gina, though. Added erratum
http://groovy.canoo.com/errata/erratum/show/14
ciao
Dierk
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
Dierk Koenig
2007-03-08 07:40:29 UTC
Permalink
yes, you have to put the \ on the very end of the line. It is escaping the
CR.

Dierk
-----Original Message-----
From: MK Tan [mailto:mktany2k-***@public.gmane.org]
Sent: Donnerstag, 8. März 2007 7:38
To: user-i9PBDF1N6cxnkHa44VUL00B+***@public.gmane.org
Subject: Re: [groovy-user] Ternary on multiple lines problem


Hi Dierk,

Just a quick test

def value = "hello"

def y = (value) \
? value
: "no value"

will have the same compilation error, but if I do this

def value = "hello"

def y = (value) \
? value \
: "no value"

it will compile. But if I add a extra space after '\', I've this error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
failed, test.groovy: 3: unexpected char: '\' @ line 3, column 17.
def y = (value) \

Hope this help,
MK Tan
Post by Alexandru Popescu
I think only Jochen was aware of it. Not found it in GINA ;-).
Well, I at least also knew about it (once) :-) and I'm pretty sure
Jez and Mr.G did as well.
It's not in Gina, though. Added erratum
http://groovy.canoo.com/errata/erratum/show/14

ciao
Dierk

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

http://xircles.codehaus.org/manage_email

Barzilai Spinak
2007-03-07 13:29:46 UTC
Permalink
Post by Marc Palmer
Post by Jochen Theodorou
[...]
Post by Marc Palmer
def y = (value) \
? value
: "no value"
I'm assuming we wouldn't need a continuation char on the end of the
? and : lines because surely the parse has context there, i.e. has a
"\" is the continuation character ;) This means we already have it
and your code above does compile. hehe
Perfect... why didn't anybody say at the start ;-)
Marc
Becoz it looks Uglee!!1!11!!!
hehehe... style, style... anyway, your argument about "deleting lines"
has some weight to it, I must say.

With respect to the \ continuation, if Java had it then we could have
the "here-documents" without needing the triple-quotes mechanism.
Still, we would need to append \ and possibly \n\ at the end of lines
to get the desired effect in Java.

I've wished the \ in Java many times in the past for this reason.

BarZ

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

http://xircles.codehaus.org/manage_email
Continue reading on narkive:
Loading...