Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why unexpected token: * in when doing operation over two lines

Tags:

groovy

Running the following in a 1.8 console:

def accessories = null
final int prime = 31;
int result = 1;
result = prime
    * result
        + ((accessories == null) ? 0 : accessories
                .hashCode());

I get a compilation error stating:

unexpected token: * at line: 5, column: 13

Yet, when I move "* result" up to the previous line, it compiles and runs cleanly. I've searched to try to find an explanation, but have had no luck thus far. Can someone explain?

def accessories = null
final int prime = 31;
int result = 1;
result = prime * result
        + ((accessories == null) ? 0 : accessories
                .hashCode());
like image 815
Bill Turner Avatar asked Jun 10 '13 18:06

Bill Turner


People also ask

Why does the Bash unexpected token syntax error occur?

Why the Bash unexpected token syntax error occurs? As the error suggests this is a Bash syntax error, in other words it reports bad syntax somewhere in your script or command. There are many things that can go wrong in a Bash script and cause this error.

What are some examples of unexpected tokens in JavaScript?

Example 2: An unexpected token ‘, ‘occurs after i=0 which javascript cannot recognize.We can remove error here by removing extra. Example 3: An unexpected token ‘)’ occur after i++ which JavaScript cannot recognize.We can remove error here by removing extra ).

How to remove unnecessary use of any token in JavaScript?

Similarly, unnecessary use of any token will throw this type of error. We can remove this error by binding by following the programming rules of JavaScript.

What is an example of an unexpected error in JavaScript?

Example 1: It was either expecting a parameter in myFunc (mycar, ) or not, .So it was enable to execute this code. Example 2: An unexpected token ‘, ‘occurs after i=0 which javascript cannot recognize.We can remove error here by removing extra.


2 Answers

Because Groovy's statements are not delimited by ;, but rather by line break. It can't know the line below is a continuation of the statement on the line above. You can escape the line break:

int i = 10 \
    * 9
assert i == 90

Update:

Actually Groovy does identify some statements as from the above line. At least the dot is recognized:

assert [1, 2]
  .join("")
  .padLeft(4, "a") == "aa12"

And a statement with either +, - and ~ (and maybe more) could be methods:

def m = "aa"
  - m // fails with "No signature of method: java.lang.String.negative()"
like image 103
Will Avatar answered Oct 09 '22 10:10

Will


This is necessary because otherwise the parser for Groovy would have to do a lot more work.

There are lots of places ie:

String s = "tim"
             + "_yates"

Where it would be possible for the parser to work out what you meant, but in all of them, I believe it would involve backtracking (or implementing a two pass parse) which is slow

like image 30
tim_yates Avatar answered Oct 09 '22 10:10

tim_yates