Class: Regexp::Expression::CharacterSet

Inherits:
Base
  • Object
show all
Defined in:
lib/regexp_parser/expression/classes/set.rb

Direct Known Subclasses

CharacterSubSet

Instance Attribute Summary collapse

Attributes inherited from Base

#conditional_level, #level, #options, #quantifier, #set_level, #text, #token, #ts, #type

Instance Method Summary collapse

Methods inherited from Base

#ascii_classes?, #case_insensitive?, #coded_offset, #default_classes?, #free_spacing?, #full_length, #greedy?, #is?, #match, #matches?, #multiline?, #offset, #one_of?, #possessive?, #quantified?, #quantify, #quantity, #reluctant?, #starts_at, #strfregexp, #terminal?, #to_h, #to_re, #type?, #unicode_classes?

Constructor Details

#initialize(token) ⇒ CharacterSet

Returns a new instance of CharacterSet.



6
7
8
9
10
11
# File 'lib/regexp_parser/expression/classes/set.rb', line 6

def initialize(token)
  @members  = []
  @negative = false
  @closed   = false
  super
end

Instance Attribute Details

#membersObject

Returns the value of attribute members.



4
5
6
# File 'lib/regexp_parser/expression/classes/set.rb', line 4

def members
  @members
end

Instance Method Details

#<<(member) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/regexp_parser/expression/classes/set.rb', line 20

def <<(member)
  if @members.last.is_a?(CharacterSubSet) and not @members.last.closed?
    @members.last << member
  else
    @members << member
  end
end

#cloneObject

Override base method to clone set members as well.



14
15
16
17
18
# File 'lib/regexp_parser/expression/classes/set.rb', line 14

def clone
  copy = super
  copy.members = @members.map {|m| m.clone }
  copy
end

#closeObject



63
64
65
66
67
68
69
# File 'lib/regexp_parser/expression/classes/set.rb', line 63

def close
  if @members.last.is_a?(CharacterSubSet) and not @members.last.closed?
    @members.last.close
  else
    @closed = true
  end
end

#closed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/regexp_parser/expression/classes/set.rb', line 71

def closed?
  @closed
end

#each(&block) ⇒ Object



38
39
40
# File 'lib/regexp_parser/expression/classes/set.rb', line 38

def each(&block)
  @members.each {|m| yield m}
end

#each_with_index(&block) ⇒ Object



42
43
44
# File 'lib/regexp_parser/expression/classes/set.rb', line 42

def each_with_index(&block)
  @members.each_with_index {|m, i| yield m, i}
end

#expand_members(use_properties = false) ⇒ Object

Returns an array of the members with any shorthand members like d and W expanded to either traditional form or unicode properties.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/regexp_parser/expression/classes/set.rb', line 77

def expand_members(use_properties = false)
  @members.map do |member|
    case member
    when "\\d"
      use_properties ? '\p{Digit}'  : '0-9'
    when "\\D"
      use_properties ? '\P{Digit}'  : '^0-9'
    when "\\w"
      use_properties ? '\p{Word}'   : 'A-Za-z0-9_'
    when "\\W"
      use_properties ? '\P{Word}'   : '^A-Za-z0-9_'
    when "\\s"
      use_properties ? '\p{Space}'  : ' \t\f\v\n\r'
    when "\\S"
      use_properties ? '\P{Space}'  : '^ \t\f\v\n\r'
    when "\\h"
      use_properties ? '\p{Xdigit}' : '0-9A-Fa-f'
    when "\\H"
      use_properties ? '\P{Xdigit}' : '^0-9A-Fa-f'
    else
      member
    end
  end
end

#include?(member, directly = false) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/regexp_parser/expression/classes/set.rb', line 28

def include?(member, directly = false)
  @members.each do |m|
    if m.is_a?(CharacterSubSet) and not directly
      return true if m.include?(member)
    else
      return true if member == m.to_s
    end
  end; false
end

#lengthObject



46
47
48
# File 'lib/regexp_parser/expression/classes/set.rb', line 46

def length
  @members.length
end

#negateObject



50
51
52
53
54
55
56
# File 'lib/regexp_parser/expression/classes/set.rb', line 50

def negate
  if @members.last.is_a?(CharacterSubSet)
    @members.last.negate
  else
    @negative = true
  end
end

#negative?Boolean Also known as: negated?

Returns:

  • (Boolean)


58
59
60
# File 'lib/regexp_parser/expression/classes/set.rb', line 58

def negative?
  @negative
end

#to_s(format = :full) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/regexp_parser/expression/classes/set.rb', line 102

def to_s(format = :full)
  s = ''

  s << @text.dup
  s << '^' if negative?
  s << @members.join
  s << ']'

  unless format == :base
    s << @quantifier.to_s if quantified?
  end

  s
end