lib/regexp-examples/parser.rb in regexp-examples-0.2.3 vs lib/regexp-examples/parser.rb in regexp-examples-0.2.4

- old
+ new

@@ -52,15 +52,20 @@ @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: + when rest_of_string =~ /\A(c|C-)(.)/ # Control character # http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals + @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 + group = parse_single_char_group( parse_escape_sequence($1) ) + when rest_of_string =~ /\Au(\h{4})/ # Unicode sequence + @current_position += 4 + group = parse_single_char_group( parse_unicode_sequence($1) ) else group = parse_single_char_group( regexp_string[@current_position] ) # TODO: What about cases like \A, \z, \Z ? end group @@ -150,11 +155,19 @@ 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 + (char.ord % 32).chr # Black magic! + # eval "?\\C-#{char.chr}" # Doesn't work for e.g. char = "?" + end + + def parse_escape_sequence(match) + eval "?\\x#{match}" + end + + def parse_unicode_sequence(match) + eval "?\\u#{match}" end def parse_star_repeater(group) @current_position += 1 StarRepeater.new(group)