Skip to content

Commit

Permalink
fixup! Allow options to be passed when processing a String pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
owst committed Sep 13, 2020
1 parent 7c4d268 commit eea67dd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
4 changes: 4 additions & 0 deletions lib/regexp_parser/parser.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/regexp_parser/scanner/scanner.rl
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions 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
Expand Down
16 changes: 6 additions & 10 deletions spec/scanner/options_spec.rb
Expand Up @@ -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),
[
Expand All @@ -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'),
[
Expand Down

0 comments on commit eea67dd

Please sign in to comment.