lib/regexp-examples/chargroup_parser.rb in regexp-examples-1.1.2 vs lib/regexp-examples/chargroup_parser.rb in regexp-examples-1.1.3

- old
+ new

@@ -23,33 +23,15 @@ @negative = false parse_first_chars until next_char == ']' case next_char when '[' - @current_position += 1 - sub_group_parser = self.class.new(rest_of_string, is_sub_group: true) - @charset.concat sub_group_parser.result - @current_position += sub_group_parser.length + parse_sub_group_concat when '-' - if regexp_string[@current_position + 1] == ']' # e.g. /[abc-]/ -- not a range! - @charset << '-' - @current_position += 1 - else - @current_position += 1 - @charset.concat (@charset.last..parse_checking_backlash.first).to_a - @current_position += 1 - end + parse_after_hyphen when '&' - if regexp_string[@current_position + 1] == '&' - @current_position += 2 - sub_group_parser = self.class.new(rest_of_string, is_sub_group: @is_sub_group) - @charset &= sub_group_parser.result - @current_position += (sub_group_parser.length - 1) - else - @charset << '&' - @current_position += 1 - end + parse_after_ampersand else @charset.concat parse_checking_backlash @current_position += 1 end end @@ -77,18 +59,26 @@ case rest_of_string when /\A[-\]]/ # e.g. /[]]/ (match "]") or /[-]/ (match "-") @charset << next_char @current_position += 1 when /\A:(\^?)([^:]+):\]/ # e.g. [[:alpha:]] - POSIX group - if @is_sub_group - chars = Regexp.last_match(1).empty? ? POSIXCharMap[Regexp.last_match(2)] : (CharSets::Any - POSIXCharMap[Regexp.last_match(2)]) - @charset.concat chars - @current_position += (Regexp.last_match(1).length + Regexp.last_match(2).length + 2) - end + parse_posix_group(Regexp.last_match(1), Regexp.last_match(2)) if @is_sub_group end end + def parse_posix_group(negation_flag, name) + chars = if negation_flag.empty? + POSIXCharMap[name] + else + CharSets::Any - POSIXCharMap[name] + end + @charset.concat chars + @current_position += (negation_flag.length + # 0 or 1, if '^' is present + name.length + + 2) # Length of opening and closing colons (always 2) + end + # Always returns an Array, for consistency def parse_checking_backlash if next_char == '\\' @current_position += 1 parse_after_backslash @@ -103,9 +93,43 @@ BackslashCharMap[next_char] when 'b' ["\b"] else [next_char] + end + end + + def parse_sub_group_concat + @current_position += 1 + sub_group_parser = self.class.new(rest_of_string, is_sub_group: true) + @charset.concat sub_group_parser.result + @current_position += sub_group_parser.length + end + + def parse_after_ampersand + if regexp_string[@current_position + 1] == '&' + parse_sub_group_intersect + else + @charset << '&' + @current_position += 1 + end + end + + def parse_sub_group_intersect + @current_position += 2 + sub_group_parser = self.class.new(rest_of_string, is_sub_group: true) + @charset &= sub_group_parser.result + @current_position += (sub_group_parser.length - 1) + end + + def parse_after_hyphen + if regexp_string[@current_position + 1] == ']' # e.g. /[abc-]/ -- not a range! + @charset << '-' + @current_position += 1 + else + @current_position += 1 + @charset.concat (@charset.last..parse_checking_backlash.first).to_a + @current_position += 1 end end def rest_of_string regexp_string[@current_position..-1]