class Regex::PolyadicExpression

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

Attributes

children[R]

@return [Array<Regex::Expression>] The aggregation of child elements

Public Class Methods

new(theChildren) click to toggle source

Constructor. @param theChildren [Array<Regex::Expression>]

Calls superclass method
# File lib/regex/polyadic_expression.rb, line 16
def initialize(theChildren)
  super()
  @children = theChildren
end

Public Instance Methods

<<(aChild) click to toggle source

Append the given child to the list of children. TODO: assess whether to defer to a subclass NAryExpression param aChild [Regex::Expression]

# File lib/regex/polyadic_expression.rb, line 24
def <<(aChild)
  @children << aChild

  return self
end
df_visitor() click to toggle source

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

# File lib/regex/polyadic_expression.rb, line 52
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

    loop do # 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

      break if visit_stack.empty?
    end
  end

  return visitor
end
done!() click to toggle source

Notification that the parse tree construction is complete.

# File lib/regex/polyadic_expression.rb, line 31
def done!
  children.each(&:done!)
  children.each_with_index do |child, index|
    break if index == children.size - 1

    next_child = children[index + 1]
    if next_child.kind_of?(Lookaround) && next_child.dir == :behind
      # Swap children: lookbehind regex must precede pattern
      @children[index + 1] = child
      @children[index] = next_child
    end
  end
end
lazy!() click to toggle source

Apply the 'lazy' option to the child elements

# File lib/regex/polyadic_expression.rb, line 46
def lazy!
  children.each(&:lazy!)
end