Project

General

Profile

Actions

Bug #13371

closed

Syntax Error with regex when parens are omitted

Added by ndn (Nikola Nenkov) over 7 years ago. Updated over 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:80377]

Description

When you skip the parens of method invocations and pass a regex, you can get a SyntaxError if the regex starts with a space.

''.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:

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)

Actions #1

Updated by ndn (Nikola Nenkov) over 7 years ago

  • Description updated (diff)

Updated by shyouhei (Shyouhei Urabe) over 7 years ago

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?

Updated by ndn (Nikola Nenkov) over 7 years ago

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.

Updated by shyouhei (Shyouhei Urabe) over 7 years ago

ndn (Nikola Nenkov) wrote:

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.

I can't say for sure from memory but this language ambiguity between regular expressions versus binary division is very hard to reroute, if not impossible. So even if this is a bug, yes I recommend you to use %r. I think it wouldn't be fixed anytime soon.

Updated by duerst (Martin Dürst) over 7 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

I don't have any first-hand knowledge, but my guess is that a / followed by a space is always interpreted as a division operator, because that's the most likely result that can be obtained without looking ahead more than one character. I have assigned the issue to Matz because he will know for sure, but Nobu or others might also know.

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

  • Status changed from Open to Rejected
  • Assignee deleted (matz (Yukihiro Matsumoto))

It's unable to tell that slash is a part of a regexp.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0