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.

Direct Known Subclasses

Alternation, CharClass, CharRange, Concatenation

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?, #options, #to_str

Constructor Details

#initialize(theChildren) ⇒ PolyadicExpression

Constructor.

Parameters:



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

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

Instance Attribute Details

#childrenArray<Regex::Expression> (readonly)

Returns The aggregation of child elements.

Returns:



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 param aChild [Regex::Expression]



22
23
24
25
26
# File 'lib/regex/polyadic_expression.rb', line 22

def <<(aChild)
  @children << aChild

  return self
end

#df_visitorObject

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/regex/polyadic_expression.rb', line 49

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!Object

Notification that the parse tree construction is complete.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/regex/polyadic_expression.rb', line 29

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!Object

Apply the 'lazy' option to the child elements



43
44
45
# File 'lib/regex/polyadic_expression.rb', line 43

def lazy!
  children.each(&:lazy!)
end