Project

General

Profile

Feature #14244

Better error messages for scripts with non-matching end statements

Added by duerst (Martin Dürst) over 2 years ago. Updated over 1 year ago.

Status:
Open
Priority:
Normal
Target version:
-
[ruby-core:84491]

Description

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
and
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 end in my program and move it around until I find the correct place for it (with something like binary search). Anything faster would be better.
  • Using -w can produce additional output. Trying this out today, I got a message for a missing end keyword, but not for a superfluous end keyword. (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

[different program]

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.

Also available in: Atom PDF