Class: RegularExpression::AST::CharacterClass

Inherits:
Object
  • Object
show all
Defined in:
lib/regular_expression/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ CharacterClass

Returns a new instance of CharacterClass.



149
150
151
# File 'lib/regular_expression/ast.rb', line 149

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

“w” | “W” | “d” | “D”



147
148
149
# File 'lib/regular_expression/ast.rb', line 147

def value
  @value
end

Instance Method Details

#to_dot(parent) ⇒ Object



153
154
155
# File 'lib/regular_expression/ast.rb', line 153

def to_dot(parent)
  parent.add_node(object_id, label: value, shape: "box")
end

#to_nfa(start, finish) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/regular_expression/ast.rb', line 157

def to_nfa(start, finish)
  case value
  when "\\w"
    start.add_transition(NFA::Transition::Range.new(finish, "a", "z"))
    start.add_transition(NFA::Transition::Range.new(finish, "A", "Z"))
    start.add_transition(NFA::Transition::Range.new(finish, "0", "9"))
    start.add_transition(NFA::Transition::Value.new(finish, "_"))
  when "\\W"
    start.add_transition(NFA::Transition::Invert.new(finish, [*("a".."z"), *("A".."Z"), *("0".."9"), "_"]))
  when "\\d"
    start.add_transition(NFA::Transition::Range.new(finish, "0", "9"))
  when "\\D"
    start.add_transition(NFA::Transition::Range.new(finish, "0", "9", invert: true))
  else
    raise
  end
end