Class: RegexpExamples::ChargroupParser

Inherits:
Object
  • Object
show all
Defined in:
lib/regexp-examples/chargroup_parser.rb

Overview

A “sub-parser”, for char groups in a regular expression Some examples of what this class needs to parse:

abc
  • plain characters

a-z
  • ranges

nbd
  • escaped characters (which may represent character sets)

^abc
  • negated group

[a]
  • sub-groups (should match “a”, “b” or “c”)

[:lower:]
  • POSIX group

[a-f]&&
  • set intersection (should match “d”, “f” or “f”)

[^:alpha:]&&a-c
  • all of the above!!!! (should match “n”)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp_string, is_sub_group: false) ⇒ ChargroupParser

Returns a new instance of ChargroupParser.



14
15
16
17
18
19
# File 'lib/regexp-examples/chargroup_parser.rb', line 14

def initialize(regexp_string, is_sub_group: false)
  @regexp_string = regexp_string
  @is_sub_group = is_sub_group
  @current_position = 0
  parse
end

Instance Attribute Details

#regexp_stringObject (readonly)

Returns the value of attribute regexp_string.



13
14
15
# File 'lib/regexp-examples/chargroup_parser.rb', line 13

def regexp_string
  @regexp_string
end

Instance Method Details

#lengthObject



61
62
63
# File 'lib/regexp-examples/chargroup_parser.rb', line 61

def length
  @current_position
end

#parseObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/regexp-examples/chargroup_parser.rb', line 21

def parse
  @charset = []
  @negative = false
  parse_first_chars
  until next_char == "]" do
    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
    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
    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
    else
      @charset.concat parse_checking_backlash
      @current_position += 1
    end
  end

  @charset.uniq!
  @current_position += 1 # To account for final "]"
end

#resultObject



65
66
67
# File 'lib/regexp-examples/chargroup_parser.rb', line 65

def result
  @negative ? (CharSets::Any - @charset) : @charset
end