Class: RegexpExamples::ChargroupParser

Inherits:
Object
  • Object
show all
Includes:
CharsetNegationHelper, ParseAfterBackslashGroupHelper, ParseGroupHelper
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”, “e” or “f”)

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CharsetNegationHelper

#negate_if

Constructor Details

#initialize(regexp_string, is_sub_group: false) ⇒ ChargroupParser

Returns a new instance of ChargroupParser.



24
25
26
27
28
29
30
# File 'lib/regexp-examples/chargroup_parser.rb', line 24

def initialize(regexp_string, is_sub_group: false)
  @regexp_string = regexp_string
  @is_sub_group = is_sub_group
  @current_position = 0
  @charset = []
  @negative = false
end

Instance Attribute Details

#current_positionObject (readonly) Also known as: length

Returns the value of attribute current_position.



21
22
23
# File 'lib/regexp-examples/chargroup_parser.rb', line 21

def current_position
  @current_position
end

#regexp_stringObject (readonly)

Returns the value of attribute regexp_string.



21
22
23
# File 'lib/regexp-examples/chargroup_parser.rb', line 21

def regexp_string
  @regexp_string
end

Instance Method Details

#parseObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/regexp-examples/chargroup_parser.rb', line 32

def parse
  parse_first_chars
  until next_char == ']'
    case next_char
    when '['
      parse_sub_group_concat
    when '-'
      parse_after_hyphen
    when '&'
      parse_after_ampersand
    else
      @charset.concat parse_checking_backlash
    end
  end

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

#resultObject



51
52
53
# File 'lib/regexp-examples/chargroup_parser.rb', line 51

def result
  negate_if(@charset, @negative)
end