Sha256: 9457225b792d7a294ec1efcf885a987b6a7c6b834bfda15b7d9a58e790633748

Contents?: true

Size: 1.3 KB

Versions: 36

Compression:

Stored size: 1.3 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(orig)
      self.expressions = orig.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.
      "#{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

36 entries across 29 versions & 6 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/subexpression.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/subexpression.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/subexpression.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/subexpression.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/subexpression.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/regexp_parser-1.8.0/lib/regexp_parser/expression/subexpression.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.0.3/lib/regexp_parser/expression/subexpression.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.8.2/lib/regexp_parser/expression/subexpression.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.2/lib/regexp_parser/expression/subexpression.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/regexp_parser-1.8.2/lib/regexp_parser/expression/subexpression.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-5.1.4/vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.8.2/lib/regexp_parser/expression/subexpression.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/regexp_parser-2.0.3/lib/regexp_parser/expression/subexpression.rb
regexp_parser-2.0.3 lib/regexp_parser/expression/subexpression.rb
regexp_parser-2.0.2 lib/regexp_parser/expression/subexpression.rb
regexp_parser-2.0.1 lib/regexp_parser/expression/subexpression.rb
tdiary-5.1.4 vendor/bundle/ruby/2.7.0/gems/regexp_parser-1.8.2/lib/regexp_parser/expression/subexpression.rb
regexp_parser-2.0.0 lib/regexp_parser/expression/subexpression.rb
regexp_parser-1.8.2 lib/regexp_parser/expression/subexpression.rb
regexp_parser-1.8.1 lib/regexp_parser/expression/subexpression.rb
regexp_parser-1.8.0 lib/regexp_parser/expression/subexpression.rb