lib/regexp-examples/parser.rb in regexp-examples-0.5.0 vs lib/regexp-examples/parser.rb in regexp-examples-0.5.1
- old
+ new
@@ -1,14 +1,16 @@
module RegexpExamples
class Parser
attr_reader :regexp_string
def initialize(regexp_string, regexp_options, config_options={})
@regexp_string = regexp_string
- @ignorecase = ( regexp_options & Regexp::IGNORECASE == 1 )
+ @ignorecase = !(regexp_options & Regexp::IGNORECASE).zero?
+ @multiline = !(regexp_options & Regexp::MULTILINE).zero?
+ @extended = !(regexp_options & Regexp::EXTENDED).zero?
@num_groups = 0
@current_position = 0
- RegexpExamples::ResultCountLimiters.configure!(
+ ResultCountLimiters.configure!(
config_options[:max_repeater_variance],
config_options[:max_group_results]
)
end
@@ -26,14 +28,10 @@
repeaters
end
private
- def regexp_options
- {ignorecase: @ignorecase}
- end
-
def parse_group(repeaters)
case next_char
when '('
group = parse_multi_group
when ')'
@@ -56,16 +54,28 @@
if @current_position == (regexp_string.length - 1)
group = parse_single_char_group('') # Ignore the "illegal" character
else
raise IllegalSyntaxError, "Anchors cannot be supported, as they are not regular"
end
+ when /[#\s]/
+ if @extended
+ parse_extended_whitespace
+ group = parse_single_char_group('') # Ignore the whitespace/comment
+ else
+ group = parse_single_char_group(next_char)
+ end
else
group = parse_single_char_group(next_char)
end
group
end
+ def parse_extended_whitespace
+ whitespace_chars = rest_of_string.match(/#.*|\s+/)[0]
+ @current_position += whitespace_chars.length - 1
+ end
+
def parse_after_backslash_group
@current_position += 1
case
when rest_of_string =~ /\A(\d+)/
@current_position += ($1.length - 1) # In case of 10+ backrefs!
@@ -76,11 +86,11 @@
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,
- regexp_options
+ @ignorecase
)
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
@@ -151,11 +161,11 @@
@current_position += (match[3].length + 3)
group_id = match[3]
end
end
groups = parse
- MultiGroup.new(groups, group_id, regexp_options)
+ MultiGroup.new(groups, group_id, @ignorecase)
end
def parse_multi_end_group
MultiGroupEnd.new
end
@@ -179,25 +189,25 @@
# /[\\]/ (match "\")
# /[\\\]]/ (match "\" or "]")
chars << next_char
@current_position += 1
end
- CharGroup.new(chars, regexp_options)
+ CharGroup.new(chars, @ignorecase)
end
def parse_dot_group
- DotGroup.new(regexp_options)
+ DotGroup.new(@multiline)
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, regexp_options)
+ SingleCharGroup.new(char, @ignorecase)
end
def parse_backreference_group(match)
BackReferenceGroup.new(match)
end