lib/regexp-examples/parser.rb in regexp-examples-0.5.1 vs lib/regexp-examples/parser.rb in regexp-examples-0.5.2

- old
+ new

@@ -144,27 +144,70 @@ def parse_multi_group @current_position += 1 @num_groups += 1 group_id = nil # init - rest_of_string.match(/\A(\?)?(:|!|=|<(!|=|[^!=][^>]*))?/) do |match| + previous_ignorecase = @ignorecase + previous_multiline = @multiline + previous_extended = @extended + rest_of_string.match( + / + \A + (\?)? # Is it a "special" group, i.e. starts with a "?"? + ( + : # Non capture group + |! # Neglookahead + |= # Lookahead + |\# # Comment group + |< # Lookbehind or named capture + ( + ! # Neglookbehind + |= # Lookbehind + |[^>]+ # Named capture + ) + |[mix]*-?[mix]* # Option toggle + )? + /x + ) do |match| case when match[1].nil? # e.g. /(normal)/ group_id = @num_groups.to_s when match[2] == ':' # e.g. /(?:nocapture)/ @current_position += 2 - group_id = nil + when match[2] == '#' # e.g. /(?#comment)/ + comment_group = rest_of_string.match(/.*?[^\\](?:\\{2})*\)/)[0] + @current_position += comment_group.length + when match[2] =~ /\A(?=[mix-]+)([mix]*)-?([mix]*)/ # e.g. /(?i-mx)/ + regexp_options_toggle($1, $2) + @current_position += $&.length + 1 + if next_char == ':' # e.g. /(?i:subexpr)/ + @current_position += 1 + else + return parse_single_char_group('') + end when %w(! =).include?(match[2]) # e.g. /(?=lookahead)/, /(?!neglookahead)/ raise IllegalSyntaxError, "Lookaheads are not regular; cannot generate examples" when %w(! =).include?(match[3]) # e.g. /(?<=lookbehind)/, /(?<!neglookbehind)/ raise IllegalSyntaxError, "Lookbehinds are not regular; cannot generate examples" else # e.g. /(?<name>namedgroup)/ @current_position += (match[3].length + 3) group_id = match[3] end end groups = parse - MultiGroup.new(groups, group_id, @ignorecase) + @ignorecase = previous_ignorecase + @multiline = previous_multiline + @extended = previous_extended + MultiGroup.new(groups, group_id) + end + + def regexp_options_toggle(on, off) + @ignorecase = true if (on.include? "i") + @ignorecase = false if (off.include? "i") + @multiline = true if (on.include? "m") + @multiline = false if (off.include? "m") + @extended = true if (on.include? "x") + @extended = false if (off.include? "x") end def parse_multi_end_group MultiGroupEnd.new end