Sha256: 65c96608e6a2904f806ed98030ff66d58cb1b6fc35239e8462082e914dee39e1

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

module Regexp::Expression

  class Subexpression < Regexp::Expression::Base
    include Enumerable

    attr_accessor :expressions

    def initialize(token, options = {})
      super

      self.expressions = []
    end

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

    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

    %w[[] at each empty? fetch index join last length values_at].each do |method|
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{method}(*args, &block)
          expressions.#{method}(*args, &block)
        end
      RUBY
    end

    def dig(*indices)
      exp = self
      indices.each { |idx| exp = exp.nil? || exp.terminal? ? nil : exp[idx] }
      exp
    end

    def te
      ts + to_s.length
    end

    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 to_h
      attributes.merge({
        text:        to_s(:base),
        expressions: expressions.map(&:to_h)
      })
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
tdiary-5.0.13 vendor/bundle/gems/regexp_parser-1.3.0/lib/regexp_parser/expression/subexpression.rb
regexp_parser-1.4.0 lib/regexp_parser/expression/subexpression.rb
tdiary-5.0.12.1 vendor/bundle/gems/regexp_parser-1.3.0/lib/regexp_parser/expression/subexpression.rb
tdiary-5.0.11 vendor/bundle/gems/regexp_parser-1.3.0/lib/regexp_parser/expression/subexpression.rb
regexp_parser-1.3.0 lib/regexp_parser/expression/subexpression.rb
regexp_parser-1.2.0 lib/regexp_parser/expression/subexpression.rb