diff --git a/lib/regexp_parser/parser.rb b/lib/regexp_parser/parser.rb index 9176a78..aa14a99 100644 --- a/lib/regexp_parser/parser.rb +++ b/lib/regexp_parser/parser.rb @@ -55,6 +55,10 @@ def parse(input, syntax = "ruby/#{RUBY_VERSION}", options: nil, &block) :captured_group_counts def extract_options(input, options) + if options && !input.is_a?(String) + raise ArgumentError, 'options cannot be supplied unless parsing a String' + end + options = input.options if input.is_a?(::Regexp) return {} unless options diff --git a/lib/regexp_parser/scanner/scanner.rl b/lib/regexp_parser/scanner/scanner.rl index 607047c..aef4856 100644 --- a/lib/regexp_parser/scanner/scanner.rl +++ b/lib/regexp_parser/scanner/scanner.rl @@ -813,6 +813,10 @@ class Regexp::Scanner :group_depth, :set_depth, :conditional_stack def free_spacing?(input_object, options) + if options && !input_object.is_a?(String) + raise ArgumentError, 'options cannot be supplied unless scanning a String' + end + options = input_object.options if input_object.is_a?(::Regexp) return false unless options diff --git a/spec/parser/options_spec.rb b/spec/parser/options_spec.rb index 3c2c2cc..fb9e669 100644 --- a/spec/parser/options_spec.rb +++ b/spec/parser/options_spec.rb @@ -1,10 +1,11 @@ require 'spec_helper' RSpec.describe('passing options to parse') do - it 'ignores options if parsing from a Regexp' do - root = RP.parse(/a+/ix, options: ::Regexp::MULTILINE) - - expect(root.options).to eq(i: true, x: true) + it 'raises if if parsing from a Regexp and options are passed' do + expect { RP.parse(/a+/, options: ::Regexp::EXTENDED) }.to raise_error( + ArgumentError, + 'options cannot be supplied unless parsing a String' + ) end it 'sets options if parsing from a String' do diff --git a/spec/scanner/options_spec.rb b/spec/scanner/options_spec.rb index 9b4737a..d7fd98d 100644 --- a/spec/scanner/options_spec.rb +++ b/spec/scanner/options_spec.rb @@ -5,18 +5,14 @@ def expect_type_tokens(tokens, type_tokens) expect(tokens.map { |type, token, *| [type, token] }).to eq(type_tokens) end - it 'ignores options if parsing from a Regexp' do - expect_type_tokens( - RS.scan(/a+#c/im, options: ::Regexp::EXTENDED), - [ - %i[literal literal], - %i[quantifier one_or_more], - %i[literal literal] - ] + it 'raises if if scanning from a Regexp and options are passed' do + expect { RS.scan(/a+/, options: ::Regexp::EXTENDED) }.to raise_error( + ArgumentError, + 'options cannot be supplied unless scanning a String' ) end - it 'sets free_spacing based on options if parsing from a String' do + it 'sets free_spacing based on options if scanning from a String' do expect_type_tokens( RS.scan('a+#c', options: ::Regexp::MULTILINE | ::Regexp::EXTENDED), [ @@ -27,7 +23,7 @@ def expect_type_tokens(tokens, type_tokens) ) end - it 'does not set free_spacing if parsing from a String and passing no options' do + it 'does not set free_spacing if scanning from a String and passing no options' do expect_type_tokens( RS.scan('a+#c'), [