Improving Compiler Error Messages

Ary Borenszweig ary at esperanto.org.ar
Sun May 2 08:53:41 PDT 2010


bearophile wrote:
>> Yes, it's done in JavaScript. It doesn't work very well.<
> 
> JavaScript, like most languages, was designed too much quickly (even D, that is several years old, in its final rush has probably introduced several warts. I hope some of them will be fixed), and it contains some traps and design errors.
> 
> In JavaScript there is semicolon insertion:
>> It's implemented by the parser running along until it hits a syntax error, at which point it rewinds a little, inserts a semicolon in a likely place, and tries again.<
> 
> Here you can read more about this:
> http://www.mozilla.org/js/language/js20-2000-07/rationale/syntax.html
> 
> This code:
> 
> return {
> 	JavaScript : "fantastic"
> };
> 
> 
> Is different from:
> 
> return
> {
> 	JavaScript : "fantastic"
> };
> 
> 
> This is how JS reads that:
> 
> return; // Semicolon inserted, believing the statement has finished. Returns undefined
> { // Considered to be an anonymous block, doing nothing
> 	JavaScript : "fantastic"
> };// Semicolon interpreted as an empty dummy line and moved down
> 
> 
> So programming guides for JS suggest programmers to always use the semicolon...
(snip)
> In a program most lines of code end at the newlines, so to reduce typing and errors it's a good idea to swap the syntax: instead of adding an explicit syntax to end lines (semicolon) it's better to introduce a line continuation (like \ in Python).

In ruby the same thing is true. This (to return a hash):

return {
:a => 1
}

is different from

return
{
:a => 1
}

But it's not hard to see that the compiler cannot see what is what you 
intended: to return a hash or to return and then declare a hash. If you 
program just a little in ruby you understand this.

On the other hand, I just made a small test and I think Python can be 
improved.

$ python
 >>> 1 +
   File "<stdin>", line 1
     1 +
       ^
SyntaxError: invalid syntax
 >>> 2
2


$ irb
irb(main):001:0> 1 +
irb(main):002:0* 2
=> 3


As you can see, when python encounters an end of line after a + is 
chokes, gives an error. Ruby is smarter in that it knows that you intend 
to add another operand, be it in the current line or in another line. 
That's what I mean with understanding line endings in a smart way. The 
same goes in ruby with many other things:

$ irb
irb(main):001:0> def func(a, b); a + b; end;
irb(main):002:0* func 1,
irb(main):003:0* 2
=> 3

Since I put a comma and it's a function call, it doesn't matter if the 
next argument is in the current line or the next line(s). The compiler 
is smart to know that another argument will come. (oh, and parenthesis 
are optional :-D)

So as bearophile says, to implement this in D or another 
semicolon-language is much more than just assuming a line break is a 
semicolon.


More information about the Digitalmars-d mailing list