Sha256: cfab016a9e28d2c741352e1628bb22115839e492f0911a379ee66a1d4fc02ecd

Contents?: true

Size: 1.58 KB

Versions: 111

Compression:

Stored size: 1.58 KB

Contents

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

    attr_accessor :expressions

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

    # Override base method to clone the expressions as well.
    def initialize_copy(orig)
      self.expressions = orig.expressions.map do |exp|
        exp.clone.tap { |copy| copy.parent = self }
      end
      super
    end

    def <<(exp)
      exp.parent = self
      expressions << exp
    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 + base_length
    end

    def to_h
      attributes.merge(
        text:        to_s(:base),
        expressions: expressions.map(&:to_h)
      )
    end

    def extract_quantifier_target(quantifier_description)
      pre_quantifier_decorations = []
      target = expressions.reverse.find do |exp|
        if exp.decorative?
          exp.custom_to_s_handling = true
          pre_quantifier_decorations << exp.text
          next
        end
        exp
      end
      target or raise Regexp::Parser::ParserError,
        "No valid target found for '#{quantifier_description}' quantifier"

      target.pre_quantifier_decorations = pre_quantifier_decorations
      target
    end
  end
end

Version data entries

111 entries across 111 versions & 15 rubygems

Version Path
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.65 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.64 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.63 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.62 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.61 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.60 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.59 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.58 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.57 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.56 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.55 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.54 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.53 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.52 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.50 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.49 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.48 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.47 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb
harbr-0.1.46 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/expression/subexpression.rb