Better error messages for scripts with non-matching end statements
At the party at Speee yesterday, mame (Yusuke Endoh) explained that one of his contributions to Ruby 2.5 was to make available information about on which lines code blocks would start and end.
This reminded me of one (actually two) of what I think are the most unhelpful error messages from Ruby:
syntax error, unexpected end-of-input, expecting keyword_end
syntax error, unexpected keyword_end, expecting end-of-input
These two messages are unhelpful because they get created at the end of the input when the problem is often somewhere in the middle of a long program. They are a problem both for beginners (who often encounter them without knowing what to fix) and experts (for whom better error messages could lead to productivity gains).
I discussed this at the party with Yusuke and naruse (Yui NARUSE), which led to the following additional information:
- A strategy I use when I get such an error message is to randomly insert/delete some
endin my program and move it around until I find the correct place for it (with something like binary search). Anything faster would be better.
-wcan produce additional output. Trying this out today, I got a message for a missing
endkeyword, but not for a superfluous
endkeyword. (Of course, a better error message would be desirable for both cases.)
duerst@Arnisee /cygdrive/c/tmp $ ruby missing_ends.rb missing_ends.rb:9: syntax error, unexpected end-of-input, expecting keyword_end duerst@Arnisee /cygdrive/c/tmp $ ruby -w missing_ends.rb missing_ends.rb:9: warning: mismatched indentations at 'end' with 'def' at 2 missing_ends.rb:9: syntax error, unexpected end-of-input, expecting keyword_end
duerst@Arnisee /cygdrive/c/tmp $ ruby missing_ends.rb missing_ends.rb:10: syntax error, unexpected keyword_end, expecting end-of-input duerst@Arnisee /cygdrive/c/tmp $ ruby -w missing_ends.rb missing_ends.rb:10: syntax error, unexpected keyword_end, expecting end-of-input
- One strategy to produce better error messages might be to re-read the input with -w on, but that's difficult because the input may not be a file.
- The information that Yusuke made available is part of the syntax tree, which isn't really available when there's a syntax error, but it might be possible to reuse partially generated syntax tree fragments. nobu (Nobuyoshi Nakada) might be able to do this.
I have assigned this issue to mame (Yusuke Endoh) because he may know best what to do next. Please feel free to reassign it to somebody else.
#2 [ruby-core:84502] Updated by shevegen (Robert A. Heiler) 10 months ago
Would be nice.
My current "strategy" is to make mostly small changes and see what/if anything breaks.
This is not very sophisticated but it works, sort of.
Reminds me of tenderlove's blog entry about being a puts-debugger; in my case I
am a pp-debugger, aka using pp ... and really just the simplest way to find
problems ... :P
That's also why I liked the did-you-mean gem since it is simple but effective.
While I personally think that I do not ultimately need better problem-reporting
in regards to missing or erroneous end statements, newcomers to ruby may
definitely benefit from this, so +1.
It may also fit to the philosophy of focusing on the human side of programming.
#3 [ruby-core:84513] Updated by mame (Yusuke Endoh) 10 months ago
I couldn't find a bison API to get the detail of the syntax error, such as what kind of tokens was expected, and what token was actually occurred. Bison just provide us a string of the error message, like 'syntax error, unexpected end-of-input, expecting keyword_end'.
I think it would be possible to create a hint about broken indentation (actually it is done in verbose mode), but I'm unsure if it is possible to show the hint only when the error like 'syntax error, unexpected end-of-input, expecting keyword_end' occurs. Perhaps, it might be possible by tweaking syntax rules (such as abusing Bison's
error token cleverly), but I'm so not familiar with bison. nobu (Nobuyoshi Nakada) or yui-knk (Kaneko Yuichiro), what do you think?