lib/regexp-examples/parser.rb in regexp-examples-0.3.1 vs lib/regexp-examples/parser.rb in regexp-examples-0.3.2
- old
+ new
@@ -10,11 +10,13 @@
def parse
repeaters = []
while @current_position < regexp_string.length
group = parse_group(repeaters)
break if group.is_a? MultiGroupEnd
- repeaters = [] if group.is_a? OrGroup
+ if group.is_a? OrGroup
+ return [OneTimeRepeater.new(group)]
+ end
@current_position += 1
repeaters << parse_repeater(group)
end
repeaters
end
@@ -63,11 +65,14 @@
when rest_of_string =~ /\Ak<([^>]+)>/ # Named capture group
@current_position += ($1.length + 2)
group = parse_backreference_group($1)
when BackslashCharMap.keys.include?(regexp_string[@current_position])
group = CharGroup.new(
- BackslashCharMap[regexp_string[@current_position]])
+ # Note: The `.dup` is important, as it prevents modifying the constant, in
+ # CharGroup#init_ranges (where the '-' is moved to the front)
+ BackslashCharMap[regexp_string[@current_position]].dup
+ )
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
@current_position += $1.length
@@ -96,10 +101,9 @@
else
raise IllegalSyntaxError, "Anchors cannot be supported, as they are not regular"
end
else
group = parse_single_char_group( regexp_string[@current_position] )
- # TODO: What about cases like \A, \z, \Z ?
end
group
end
def parse_repeater(group)