class Regexp::Expression::Subexpression

def <<(exp)

def <<(exp)
  if exp.is_a?(WhiteSpace) && last && last.is_a?(WhiteSpace)
    last.merge(exp)
  else
    exp.nesting_level = nesting_level + 1
    expressions << exp
  end
end

def each_expression(include_self = false, &block)

the expression and its index within its parent to the given block.
Iterates over the expressions of this expression as an array, passing
def each_expression(include_self = false, &block)
  traverse(include_self) do |event, exp, index|
    yield(exp, index) unless event == :exit
  end
end

def flat_map(include_self = false, &block)

each expression and its level index as an array.
for every expression. If a block is not given, returns an array with
Returns a new array with the results of calling the given block once
def flat_map(include_self = false, &block)
  result = []
  each_expression(include_self) do |exp, index|
    if block_given?
      result << yield(exp, index)
    else
      result << [exp, index]
    end
  end
  result
end

def initialize(token, options = {})

def initialize(token, options = {})
  super
  self.expressions = []
end

def initialize_clone(other)

Override base method to clone the expressions as well.
def initialize_clone(other)
  other.expressions = expressions.map(&:clone)
  super
end

def strfregexp_tree(format = '%a', include_self = true, separator = "\n")

def strfregexp_tree(format = '%a', include_self = true, separator = "\n")
  output = include_self ? [self.strfregexp(format)] : []
  output += flat_map do |exp, index|
    exp.strfregexp(format, (include_self ? 1 : 0), index)
  end
  output.join(separator)
end

def te

def te
  ts + to_s.length
end

def to_h

def to_h
  super.merge({
    text:        to_s(:base),
    expressions: expressions.map(&:to_h)
  })
end

def to_s(format = :full)

def to_s(format = :full)
  # Note: the format does not get passed down to subexpressions.
  # Note: cant use #text accessor, b/c it is overriden as def text; to_s end
  # in Expression::Sequence, causing infinite recursion. Clean-up needed.
  "#{@text}#{expressions.join}#{quantifier_affix(format)}"
end

def traverse(include_self = false, &block)

Returns self.

- For terminal expressions, :visit is called once.

:exit upon exiting it.
- For subexpressions, :enter upon entering the subexpression, and

The event argument is passed as follows:

the expression, and the index of the expression within its parent.
block for each expression with three arguments; the traversal event,
Traverses the subexpression (depth-first, pre-order) and calls the given
def traverse(include_self = false, &block)
  raise 'traverse requires a block' unless block_given?
  block.call(:enter, self, 0) if include_self
  each_with_index do |exp, index|
    if exp.terminal?
      block.call(:visit, exp, index)
    else
      block.call(:enter, exp, index)
      exp.traverse(&block)
      block.call(:exit, exp, index)
    end
  end
  block.call(:exit, self, 0) if include_self
  self
end