Class: RegularExpression::AST::CharacterGroup

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, invert: false) ⇒ CharacterGroup

Returns a new instance of CharacterGroup.



121
122
123
124
# File 'lib/regular_expression/ast.rb', line 121

def initialize(items, invert: false)
  @items = items
  @invert = invert
end

Instance Attribute Details

#invertObject (readonly)

bool



119
120
121
# File 'lib/regular_expression/ast.rb', line 119

def invert
  @invert
end

#itemsObject (readonly)

Array[CharacterRange | Character]



118
119
120
# File 'lib/regular_expression/ast.rb', line 118

def items
  @items
end

Instance Method Details

#to_dot(parent) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/regular_expression/ast.rb', line 126

def to_dot(parent)
  label = "CharacterGroup"
  label = "#{label} (invert)" if invert

  node = parent.add_node(object_id, label: label)
  items.each { |item| item.to_dot(node) }
end

#to_nfa(start, finish) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/regular_expression/ast.rb', line 134

def to_nfa(start, finish)
  if invert
    transition = NFA::Transition::Invert.new(finish, items.flat_map(&:to_nfa_values).sort)
    start.add_transition(transition)
  else
    items.each do |item|
      item.to_nfa(start, finish)
    end
  end
end