Project

General

Profile

Actions

Bug #17621

closed

Ripper can call on_parse_error multiple times for syntax error. Is this correct?

Added by mauro_oto (Mauro Otonelli) about 3 years ago. Updated about 3 years ago.

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

Description

Here's an example subclass of Ripper:

class MyRipperSubclass < Ripper
  def on_parse_error(msg)
    @count ||= 0
    @count += 1
    puts [count: @count, msg: msg]
  end
end
source = <<~EOM
  module Hey
    class Foo
      def initialize
        [1,2,3
      end
      def call
      end
    end
  end
EOM
MyRipperSubclass.new(source).parse

The source it's parsing has a syntax error, so I would only expect one parse error. However, when you execute this code, you see two:

MyRipperSubclass.new(source).parse
# => {:count=>1, :msg=>"syntax error, unexpected `end', expecting ']'"}
# => {:count=>2, :msg=>"syntax error, unexpected end-of-input, expecting `end'"}

Is this a bug, or is this expected? If this is expected, can you give me more context? Is there an upper bound to the number of parse errors a document can generate? Is the order guaranteed?

Updated by nobu (Nobuyoshi Nakada) about 3 years ago

mauro_oto (Mauro Otonelli) wrote:

The source it's parsing has a syntax error, so I would only expect one parse error. However, when you execute this code, you see two:

MyRipperSubclass.new(source).parse
# => {:count=>1, :msg=>"syntax error, unexpected `end', expecting ']'"}
# => {:count=>2, :msg=>"syntax error, unexpected end-of-input, expecting `end'"}

As you can see with ruby -c, it is an expected behavior.

$ ruby -c
  module Hey
    class Foo
      def initialize
        [1,2,3
      end
      def call
      end
    end
  end
-:5: syntax error, unexpected `end', expecting ']'
      end
-:9: syntax error, unexpected end-of-input, expecting `end'

It is very usual that one error causes many succeeding errors, not only in Ruby.

If this is expected, can you give me more context?

What do you mean by "more context"?

Is there an upper bound to the number of parse errors a document can generate?

No upper bound currently.

Is the order guaranteed?

The order is the errors are generated.
But it may not be same as the source code.

Updated by schneems (Richard Schneeman) about 3 years ago

What do you mean by "more context"?

The responses from Ilya here make sense https://twitter.com/schneems/status/1359613202460459010. Basically just because a syntax error is reached it doesn't mean that parsing stops. Multiple errors can generate multiple syntax errors.

class MyRipperSubclass < Ripper
  def on_parse_error(msg)
    @count ||= 0
    @count += 1
    puts [count: @count, msg: msg]
  end
end
source = <<~EOM
  module Hey
    class Foo
      def bar
        [1,2,3
      end
      def bar
        [1,2,3
      end
      def bar
        [1,2,3
      end
      def bar
        [1,2,3
      end
      def call
      end
    end
  end


EOM
MyRipperSubclass.new(source).parse
{:count=>1, :msg=>"syntax error, unexpected `end', expecting ']'"}
{:count=>2, :msg=>"syntax error, unexpected `end', expecting ']'"}
{:count=>3, :msg=>"syntax error, unexpected `end', expecting ']'"}
{:count=>4, :msg=>"syntax error, unexpected `end', expecting ']'"}
{:count=>5, :msg=>"syntax error, unexpected end-of-input, expecting `end'"}

Thank you for the response Nobu 🙏

Actions #3

Updated by jeremyevans0 (Jeremy Evans) about 3 years ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0