coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Lexer: Provide more helpful error message for trailing colon

Open joliss opened this issue 13 years ago • 4 comments

If you accidentally add a colon at the end of a line, like in Python, the CoffeeScript compiler (HEAD) produces rather unhelpful error messages. I ended up spending some time yesterday hunting down an extra colon in the middle of my source. ;-)

$ bin/coffee -sc
while 1:
  1
Error: Parse error on line 2: Unexpected 'TERMINATOR'

$ bin/coffee -sc
for i in [1]:
  1
Error: Parse error on line 1: Unexpected '{'

This is not really a bug, more like a nuisance -- just thought you might appreciate the report. Feel free to close it if there is nothing that can be done.

joliss avatar Nov 05 '11 16:11 joliss

related: #1358

michaelficarra avatar Nov 05 '11 17:11 michaelficarra

Notice that indentation isn't the issue here. Since

x:
  func()

is a legitimate way of writing {x: func()},

while x:
  func()

is erroneous only insofar as while {x: func()} is (and indeed, that line gives you the same Unexpected 'TERMINATOR' message).

Ideally, I'd want the compiler to say something like

Parse error on line 2: `while` requires a condition and an expression, but only a condition is given

TrevorBurnham avatar Nov 08 '11 09:11 TrevorBurnham

This would indeed be really helpful. Coming from Python I had class: somewhere in my code and had no idea that could be related to Unexpected 'TERMINATOR'.

niklasf avatar Oct 29 '12 14:10 niklasf

$ coffee -v
CoffeeScript version 1.9.2
$ coffee -e 'while 1:
>   1'
[stdin]:2:4: error: unexpected end of input
  1
   ^
$ coffee -e 'for i in [1]:
>   1'
[stdin]:1:10: error: unexpected [
for i in [1]:
         ^

Seems like the latter case has been fixed, while the first case has changed but still isn’t good.

lydell avatar May 01 '15 15:05 lydell