class Regexp::Expression::Subexpression
Attributes
Public Class Methods
Regexp::Expression::Base::new
# File lib/regexp_parser/expression/subexpression.rb, line 8 def initialize(token, options = {}) super self.expressions = [] end
Public Instance Methods
# File lib/regexp_parser/expression/subexpression.rb, line 20 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
# File lib/regexp_parser/expression/subexpression.rb, line 37 def dig(*indices) exp = self indices.each { |idx| exp = exp.nil? || exp.terminal? ? nil : exp[idx] } exp end
Iterates over the expressions of this expression as an array, passing the expression and its index within its parent to the given block.
# File lib/regexp_parser/expression/methods/traverse.rb, line 39 def each_expression(include_self = false, &block) return enum_for(__method__, include_self) unless block_given? traverse(include_self) do |event, exp, index| yield(exp, index) unless event == :exit end end
Returns a new array with the results of calling the given block once for every expression. If a block is not given, returns an array with each expression and its level index as an array.
# File lib/regexp_parser/expression/methods/traverse.rb, line 50 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
Override base method to clone the expressions as well.
Regexp::Expression::Base#initialize_clone
# File lib/regexp_parser/expression/subexpression.rb, line 15 def initialize_clone(orig) self.expressions = orig.expressions.map(&:clone) super end
# File lib/regexp_parser/expression/methods/match_length.rb, line 114 def inner_match_length dummy = Regexp::Expression::Root.build dummy.expressions = expressions.map(&:clone) dummy.quantifier = quantifier && quantifier.clone dummy.match_length end
# File lib/regexp_parser/expression/methods/match_length.rb, line 107 def match_length MatchLength.new(self, base_min: map { |exp| exp.match_length.min }.inject(0, :+), base_max: map { |exp| exp.match_length.max }.inject(0, :+), reify: ->{ map { |exp| exp.match_length.to_re }.join }) end
# File lib/regexp_parser/expression/methods/strfregexp.rb, line 102 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
# File lib/regexp_parser/expression/subexpression.rb, line 43 def te ts + to_s.length end
# File lib/regexp_parser/expression/subexpression.rb, line 52 def to_h attributes.merge({ text: to_s(:base), expressions: expressions.map(&:to_h) }) end
# File lib/regexp_parser/expression/subexpression.rb, line 47 def to_s(format = :full) # Note: the format does not get passed down to subexpressions. "#{expressions.join}#{quantifier_affix(format)}" end
Traverses the subexpression (depth-first, pre-order) and calls the given block for each expression with three arguments; the traversal event, the expression, and the index of the expression within its parent.
The event argument is passed as follows:
-
For subexpressions, :enter upon entering the subexpression, and :exit upon exiting it.
-
For terminal expressions, :visit is called once.
Returns self.
# File lib/regexp_parser/expression/methods/traverse.rb, line 16 def traverse(include_self = false, &block) return enum_for(__method__, include_self) 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