[ruby-core:80379] [Ruby trunk Bug#13371] Syntax Error with regex when parens are omitted

From: ndnenkov@...
Date: 2017-03-27 13:38:13 UTC
List: ruby-core #80379
Issue #13371 has been updated by ndn (Nikola Nenkov).


shyouhei (Shyouhei Urabe) wrote:
> ndn (Nikola Nenkov) wrote:
> > As per the syntax highlighting in my text editor and a discussion with another person, it seems that the opening slash is interpreted as division.
> 
> Yes.  Your text editor parses the line correctly.  If you want to put regular expression literals without parens, you can write `''.split %r/ ./` .  Does that work for you?

Yes, but it was more of a question if it's a bug or not. Like should I use `%r//` when omitting parens as a principle. If yes - I would submit a PR in Rubocop and so on.

----------------------------------------
Bug #13371: Syntax Error with regex when parens are omitted
https://bugs.ruby-lang.org/issues/13371#change-63860

* Author: ndn (Nikola Nenkov)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.4.1
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
When you skip the parens of method invocations and pass a regex, you can get a `SyntaxError` if the regex starts with a space.

~~~ ruby
''.split(/ ./) # => []
''.split /./   # => []
''.split / ./  # !> SyntaxError: unexpected '.'
~~~

As per the syntax highlighting in my text editor and a discussion with another person, it seems that the opening slash is interpreted as division:

~~~ ruby
require 'ripper'

Ripper.lex("''.split /./")
  # =>
  # [[[1, 0], :on_tstring_beg, "'"],
  #  [[1, 1], :on_tstring_end, "'"],
  #  [[1, 2], :on_period, "."],
  #  [[1, 3], :on_ident, "split"],
  #  [[1, 8], :on_sp, " "],
  #  [[1, 9], :on_regexp_beg, "/"],
  #  #         ^^^
  #  [[1, 10], :on_tstring_content, "."],
  #  [[1, 11], :on_regexp_end, "/"]]

Ripper.lex("''.split / ./")
  # =>
  # [[[1, 0], :on_tstring_beg, "'"],
  #  [[1, 1], :on_tstring_end, "'"],
  #  [[1, 2], :on_period, "."],
  #  [[1, 3], :on_ident, "split"],
  #  [[1, 8], :on_sp, " "],
  #  [[1, 9], :on_op, "/"],
  #  #         ^^^
  #  [[1, 10], :on_sp, " "],
  #  [[1, 11], :on_period, "."],
  #  [[1, 12], :on_op, "/"]]
~~~


Reproducible with older versions as well (tried it until 2.1.2)



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next