Class: Regex::PolyadicExpression
- Inherits:
-
CompoundExpression
- Object
- Expression
- CompoundExpression
- Regex::PolyadicExpression
- 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
Instance Attribute Summary collapse
-
#children ⇒ Array<Regex::Expression>
readonly
The aggregation of child elements.
Attributes inherited from Expression
Instance Method Summary collapse
-
#<<(aChild) ⇒ Object
Append the given child to the list of children.
-
#df_visitor ⇒ Object
Build a depth-first in-order children visitor.
-
#done! ⇒ Object
Notification that the parse tree construction is complete.
-
#initialize(theChildren) ⇒ PolyadicExpression
constructor
Constructor.
-
#lazy! ⇒ Object
Apply the 'lazy' option to the child elements.
Methods inherited from CompoundExpression
Methods inherited from Expression
Constructor Details
#initialize(theChildren) ⇒ PolyadicExpression
Constructor.
14 15 16 17 |
# File 'lib/regex/polyadic_expression.rb', line 14 def initialize(theChildren) super() @children = theChildren end |
Instance Attribute Details
#children ⇒ Array<Regex::Expression> (readonly)
Returns 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 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_visitor ⇒ Object
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 |