lib/regexp-examples/parser.rb in regexp-examples-0.2.2 vs lib/regexp-examples/parser.rb in regexp-examples-0.2.3
- old
+ new
@@ -52,10 +52,13 @@
@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]])
+ when rest_of_string =~ /\Ac(.)/ # Control character
+ @current_position += 1
+ group = parse_single_char_group( parse_control_character($1) )
# TODO: There are also a bunch of multi-char matches to watch out for:
# http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals
else
group = parse_single_char_group( regexp_string[@current_position] )
# TODO: What about cases like \A, \z, \Z ?
@@ -146,10 +149,15 @@
def parse_backreference_group(match)
BackReferenceGroup.new(match)
end
+ def parse_control_character(char)
+ # TODO: Is there a better way of doing this? I.e. not just brute-force
+ (0..255).detect { |x| x.chr =~ Regexp.new("\\c#{char}") }.chr
+ end
+
def parse_star_repeater(group)
@current_position += 1
StarRepeater.new(group)
end
@@ -162,10 +170,10 @@
@current_position += 1
QuestionMarkRepeater.new(group)
end
def parse_range_repeater(group)
- match = rest_of_string.match(/\A\{(\d+)(,)?(\d+)?\}/)
+ match = rest_of_string.match(/\A\{(\d+)?(,)?(\d+)?\}/)
@current_position += match[0].size
min = match[1].to_i if match[1]
has_comma = !match[2].nil?
max = match[3].to_i if match[3]
RangeRepeater.new(group, min, has_comma, max)