Project

General

Profile

Actions

Misc #19912

open

[Small potential for slight improvement in regards to trailing / that were omitted in a regex]

Added by rubyFeedback (robert heiler) about 1 year ago. Updated about 1 year ago.

Status:
Open
Assignee:
-
[ruby-core:114955]

Description

Not sure if I should file this under bug or not; it is probably not a bug per se
and just the current behaviour. So I filed it under Misc here.

So let me copy/paste the code next:

alias e puts


i = 'abc'

  case i
  when /help
    e 'No help options are currently documented.'
  when /-?-?rotate=(\d+)$/i
      pp $1.to_s
      exit
  end

end

I isolated this from a larger .rb file. I was adding support for
--rotate= for a class tasked with commandline stuff in regards to
.pdf files, e. g. rotating wrong .pdf files.

When I run this .rb file, that is the one I showed above, I get
the following error, as-is (the extra "end" is kind of deliberate,
I had a much larger case/when structure before).

foo.rb: --> foo.rb
Unmatched `end', missing keyword (`do', `def`, `if`, etc.) ?
>  4  i = 'abc'
>  6    case i
>  7    when /help
>  8      e 'No help options are currently documented.'
>  9    when /-?-?rotate=(\d+)$/i
> 12    end
> 14  end
foo.rb:9: syntax error, unexpected backslash (SyntaxError)
  when /-?-?rotate=(\d+)$/i
                ^

Because there are at the least two problems in this .rb file,
it appears to be that the ruby parser focuses on the last
error found. To those who can not instantly find the error,
I simply forgot a trailing / in the case/when menu, at
"when /help". That actually was the real bug too. But I first
looked down, where ruby alerted me that there is an issue
with "when /-?-?rotate=(\d+)$/i". The real problem was the
missing "/".

Is there potential to improve handling of such small mistakes,
specifically someone forgetting a trailing / in a case/when
menu? I assume ruby has to decide what is going on, but usually
when I use a case/when menu, I have multiple such entries, so
perhaps this gives the ruby parser additional information it
could use, such as: "look at the immediate surrounding and
then determine whether this may be a missing /".

I don't know how difficult it is to add that, and if it is too
much work then I think the time may be spent elsewhere. But on
the other hand, if the ruby parser could detect such small
mistakes more easily (a forgotten /), this would be rather nice
to have.

This may also have to do with how ruby reports errors; I guess
the part "Unmatched end', missing keyword (do', def, if,
etc.) ?" is ruby's attempt to give useful information to the
user, and in many cases this should work. But in case of simple
errors such as the above it may not work, and secondary errors
may disguise or confuse the user momentarily. I usually look at
the last part of any long trace shown by the ruby parser to me,
so that is why I sometimes miss prior errors.

Updated by rubyFeedback (robert heiler) about 1 year ago

Forgot to clarify what I meant with:

I assume ruby has to decide what is going on, but usually
when I use a case/when menu

Here I meant that the ruby parser may have to identify how
/ are used, e. g. for divisions. In the context above, though,
ruby perhaps knows that a / was used before at the least on
that line, so ruby probably "knows" that we are in a case/when
menu (which are absolutely awesome in general; I notice this
every time I have to use python's if/elif instead).

Updated by nobu (Nobuyoshi Nakada) about 1 year ago

Note that complex regexps use often multiple lines, with x option.
So, it might be a possible idea to warn if the x option is not specified for a multi-line regexp.

Updated by nobu (Nobuyoshi Nakada) about 1 year ago

I've forgot about inline option and comment, so it was complicated than I thought first.
https://github.com/nobu/ruby/tree/warn-multiline-regexp

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0