examples/general/SRL/lib/regex/polyadic_expression.rb in rley-0.6.00 vs examples/general/SRL/lib/regex/polyadic_expression.rb in rley-0.6.01
- old
+ new
@@ -1,64 +1,60 @@
# File: polyadic_expression.rb
-require_relative "compound_expression" # Access the superclass
+require_relative 'compound_expression' # Access the superclass
module Regex # This module is used as a namespace
+ # Abstract class. An element that is part of a regular expression &
+ # that has its own child sub-expressions.
+ class PolyadicExpression < CompoundExpression
+ # The aggregation of child elements
+ attr_reader(:children)
-# Abstract class. An element that is part of a regular expression &
-# that has its own child sub-expressions.
-class PolyadicExpression < CompoundExpression
- # The aggregation of child elements
- attr_reader(:children)
-
- # Constructor.
- def initialize(theChildren)
- super()
- @children = theChildren
- end
-
-public
- # Append the given child to the list of children.
- # TODO: assess whether to defer to a subclass NAryExpression
- def <<(aChild)
- @children << aChild
-
- return self
- end
-
- # Build a depth-first in-order children visitor.
- # The visitor is implemented as an Enumerator.
- 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)
- if top.empty?
- next
- else
- currChild = top.pop()
- visit_stack.push top
- end
- else
- currChild = top
- end
-
- result << currChild # Return the visited child
-
- unless currChild.atomic?
- children_to_enqueue = currChild.children.reverse() # in-order traversal implies LIFO queue
- visit_stack.push(children_to_enqueue)
- end
- end until visit_stack.empty?
- end
- end
+ # Constructor.
+ def initialize(theChildren)
+ super()
+ @children = theChildren
+ end
-end # class
+ # Append the given child to the list of children.
+ # TODO: assess whether to defer to a subclass NAryExpression
+ def <<(aChild)
+ @children << aChild
+ return self
+ end
+
+ # Build a depth-first in-order children visitor.
+ # The visitor is implemented as an Enumerator.
+ 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
+ end # class
end # module
-# End of file
\ No newline at end of file
+# End of file