class RegexpExamples::Parser

A Regexp parser, used to build a structured collection of objects that represents the regular expression. This object can then be used to generate strings that match the regular expression.

Attributes

regexp_string[R]

Public Class Methods

new(regexp_string, regexp_options) click to toggle source
# File lib/regexp-examples/parser.rb, line 22
def initialize(regexp_string, regexp_options)
  @regexp_string = regexp_string
  @ignorecase = !(regexp_options & Regexp::IGNORECASE).zero?
  @multiline = !(regexp_options & Regexp::MULTILINE).zero?
  @extended = !(regexp_options & Regexp::EXTENDED).zero?
  @num_groups = 0
  @current_position = 0
end

Public Instance Methods

parse() click to toggle source
# File lib/regexp-examples/parser.rb, line 31
def parse
  repeaters = [PlaceHolderGroup.new]
  until end_of_regexp
    group = parse_group(repeaters)
    return [group] if group.is_a? OrGroup
    @current_position += 1
    repeaters << parse_repeater(group)
  end
  repeaters
end

Private Instance Methods

end_of_regexp() click to toggle source
# File lib/regexp-examples/parser.rb, line 94
def end_of_regexp
  next_char == ')' || @current_position >= regexp_string.length
end
next_char() click to toggle source
# File lib/regexp-examples/parser.rb, line 90
def next_char
  regexp_string[@current_position]
end
parse_group(repeaters) click to toggle source
# File lib/regexp-examples/parser.rb, line 44
def parse_group(repeaters)
  case next_char
  when '('
    parse_multi_group
  when '['
    parse_char_group
  when '.'
    parse_dot_group
  when '|'
    parse_or_group(repeaters)
  when '\\'
    parse_after_backslash_group
  when '^'
    parse_caret
  when '$'
    parse_dollar
  when /[#\s]/
    parse_extended_whitespace
  else
    parse_single_char_group(next_char)
  end
end
parse_one_time_repeater(group) click to toggle source
# File lib/regexp-examples/parser.rb, line 82
def parse_one_time_repeater(group)
  OneTimeRepeater.new(group)
end
parse_repeater(group) click to toggle source
# File lib/regexp-examples/parser.rb, line 67
def parse_repeater(group)
  case next_char
  when '*'
    parse_star_repeater(group)
  when '+'
    parse_plus_repeater(group)
  when '?'
    parse_question_mark_repeater(group)
  when '{'
    parse_range_repeater(group)
  else
    parse_one_time_repeater(group)
  end
end
rest_of_string() click to toggle source
# File lib/regexp-examples/parser.rb, line 86
def rest_of_string
  regexp_string[@current_position..-1]
end