lib/regexp-examples/parser.rb in regexp-examples-0.4.2 vs lib/regexp-examples/parser.rb in regexp-examples-0.5.0
- old
+ new
@@ -1,15 +1,16 @@
module RegexpExamples
class Parser
attr_reader :regexp_string
- def initialize(regexp_string, options={})
+ def initialize(regexp_string, regexp_options, config_options={})
@regexp_string = regexp_string
+ @ignorecase = ( regexp_options & Regexp::IGNORECASE == 1 )
@num_groups = 0
@current_position = 0
RegexpExamples::ResultCountLimiters.configure!(
- options[:max_repeater_variance],
- options[:max_group_results]
+ config_options[:max_repeater_variance],
+ config_options[:max_group_results]
)
end
def parse
repeaters = []
@@ -25,10 +26,14 @@
repeaters
end
private
+ def regexp_options
+ {ignorecase: @ignorecase}
+ end
+
def parse_group(repeaters)
case next_char
when '('
group = parse_multi_group
when ')'
@@ -70,11 +75,12 @@
group = parse_backreference_group($1)
when BackslashCharMap.keys.include?(next_char)
group = CharGroup.new(
# Note: The `.dup` is important, as it prevents modifying the constant, in
# CharGroup#init_ranges (where the '-' is moved to the front)
- BackslashCharMap[next_char].dup
+ BackslashCharMap[next_char].dup,
+ regexp_options
)
when rest_of_string =~ /\A(c|C-)(.)/ # Control character
@current_position += $1.length
group = parse_single_char_group( parse_control_character($2) )
when rest_of_string =~ /\Ax(\h{1,2})/ # Escape sequence
@@ -145,11 +151,11 @@
@current_position += (match[3].length + 3)
group_id = match[3]
end
end
groups = parse
- MultiGroup.new(groups, group_id)
+ MultiGroup.new(groups, group_id, regexp_options)
end
def parse_multi_end_group
MultiGroupEnd.new
end
@@ -173,25 +179,25 @@
# /[\\]/ (match "\")
# /[\\\]]/ (match "\" or "]")
chars << next_char
@current_position += 1
end
- CharGroup.new(chars)
+ CharGroup.new(chars, regexp_options)
end
def parse_dot_group
- DotGroup.new
+ DotGroup.new(regexp_options)
end
def parse_or_group(left_repeaters)
@current_position += 1
right_repeaters = parse
OrGroup.new(left_repeaters, right_repeaters)
end
def parse_single_char_group(char)
- SingleCharGroup.new(char)
+ SingleCharGroup.new(char, regexp_options)
end
def parse_backreference_group(match)
BackReferenceGroup.new(match)
end