Class: Regex::PolyadicExpression

Inherits:
CompoundExpression show all
Defined in:
lib/regex/polyadic_expression.rb

Overview

Abstract class. An element that is part of a regular expression & that has its own child sub-expressions.

Instance Attribute Summary collapse

Attributes inherited from Expression

#begin_anchor, #end_anchor

Instance Method Summary collapse

Methods inherited from CompoundExpression

#atomic?

Methods inherited from Expression

#atomic?, #cardinality, #options, #to_str

Constructor Details

#initialize(theChildren) ⇒ PolyadicExpression

Constructor.



13
14
15
16
# File 'lib/regex/polyadic_expression.rb', line 13

def initialize(theChildren)
  super()
  @children = theChildren
end

Instance Attribute Details

#childrenObject (readonly)

The aggregation of child elements



10
11
12
# File 'lib/regex/polyadic_expression.rb', line 10

def children
  @children
end

Instance Method Details

#<<(aChild) ⇒ Object

Append the given child to the list of children. TODO: assess whether to defer to a subclass NAryExpression



20
21
22
23
24
# File 'lib/regex/polyadic_expression.rb', line 20

def <<(aChild)
  @children << aChild

  return self
end

#df_visitorObject

Build a depth-first in-order children visitor. The visitor is implemented as an Enumerator.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/regex/polyadic_expression.rb', line 28

def df_visitor()
  root = children # The visit will start from the children of this object

  visitor = Enumerator.new do |result| # result is a Yielder
    # Initialization part: will run once
    visit_stack = [root] # The LIFO queue of nodes to visit

    begin # Traversal part (as a loop)
      top = visit_stack.pop
      if top.kind_of?(Array)
        next if top.empty?
        currChild = top.pop
        visit_stack.push top
      else
        currChild = top
      end

      result << currChild # Return the visited child

      unless currChild.atomic?
        # in-order traversal implies LIFO queue
        children_to_enqueue = currChild.children.reverse
        visit_stack.push(children_to_enqueue)
      end
    end until visit_stack.empty?
  end
  
  return visitor
end