Class: RegularExpression::AST::Quantifier::Range

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lower, upper) ⇒ Range

Returns a new instance of Range.



340
341
342
343
# File 'lib/regular_expression/ast.rb', line 340

def initialize(lower, upper)
  @lower = lower
  @upper = upper
end

Instance Attribute Details

#lowerObject (readonly)

Integer



338
339
340
# File 'lib/regular_expression/ast.rb', line 338

def lower
  @lower
end

#upperObject (readonly)

Integer



338
339
340
# File 'lib/regular_expression/ast.rb', line 338

def upper
  @upper
end

Instance Method Details

#quantify(start, finish) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/regular_expression/ast.rb', line 349

def quantify(start, finish)
  states = [start, *(upper - 1).times.map { NFA::State.new }, finish]

  upper.times do |index|
    yield states[index], states[index + 1]
  end

  (upper - lower).times do |index|
    transition = NFA::Transition::Epsilon.new(states[-1])
    states[lower + index].add_transition(transition)
  end
end

#to_dot(parent) ⇒ Object



345
346
347
# File 'lib/regular_expression/ast.rb', line 345

def to_dot(parent)
  parent.add_node(object_id, label: "{#{lower},#{upper}}", shape: "box")
end