Sha256: 0497249d1f3fc2d60e47175a2d75c4f8a41b17450b4db19a6cfb019791c39e75

Contents?: true

Size: 1.55 KB

Versions: 13

Compression:

Stored size: 1.55 KB

Contents

module RegexpExamples
  # A collection of related helper methods, utilised by the `Parser` class
  module ParseRepeaterHelper
    protected

    def parse_star_repeater(group)
      @current_position += 1
      parse_reluctant_or_possessive_repeater
      StarRepeater.new(group)
    end

    def parse_plus_repeater(group)
      @current_position += 1
      parse_reluctant_or_possessive_repeater
      PlusRepeater.new(group)
    end

    def parse_reluctant_or_possessive_repeater
      if next_char =~ /[?+]/
        # Don't treat these repeaters any differently when generating examples
        @current_position += 1
      end
    end

    def parse_question_mark_repeater(group)
      @current_position += 1
      parse_reluctant_or_possessive_repeater
      QuestionMarkRepeater.new(group)
    end

    def parse_range_repeater(group)
      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]
      repeater = RangeRepeater.new(group, min, has_comma, max)
      parse_reluctant_or_possessive_range_repeater(repeater, min, has_comma, max)
    end

    def parse_reluctant_or_possessive_range_repeater(repeater, min, has_comma, max)
      # .{1}? should be equivalent to (?:.{1})?, i.e. NOT a "non-greedy quantifier"
      if min && !has_comma && !max && next_char == '?'
        repeater = parse_question_mark_repeater(repeater)
      else
        parse_reluctant_or_possessive_repeater
      end
      repeater
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
regexp-examples-1.5.1 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.5.0 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.4.4 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.4.3 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.4.2 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.4.1 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.4.0 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.3.2 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.3.1 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.3.0 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.2.1 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.2.0 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb
regexp-examples-1.1.4 lib/regexp-examples/parser_helpers/parse_repeater_helper.rb