Sha256: 5b9905d325d84d01fe7468fcb37fd0e1ff2ff0802602c845d4a59322e5c79fb1

Contents?: true

Size: 1.93 KB

Versions: 114

Compression:

Stored size: 1.93 KB

Contents

module Regexp::Syntax
  VERSION_FORMAT = '\Aruby/\d+\.\d+(\.\d+)?\z'
  VERSION_REGEXP = /#{VERSION_FORMAT}/
  VERSION_CONST_REGEXP = /\AV\d+_\d+(?:_\d+)?\z/

  class InvalidVersionNameError < Regexp::Syntax::SyntaxError
    def initialize(name)
      super "Invalid version name '#{name}'. Expected format is '#{VERSION_FORMAT}'"
    end
  end

  class UnknownSyntaxNameError < Regexp::Syntax::SyntaxError
    def initialize(name)
      super "Unknown syntax name '#{name}'."
    end
  end

  module_function

  # Returns the syntax specification class for the given syntax
  # version name. The special names 'any' and '*' return Syntax::Any.
  def for(name)
    (@alias_map ||= {})[name] ||= version_class(name)
  end

  def new(name)
    warn 'Regexp::Syntax.new is deprecated in favor of Regexp::Syntax.for. '\
         'It does not return distinct instances and will be removed in v3.0.0.'
    self.for(name)
  end

  def supported?(name)
    name =~ VERSION_REGEXP && comparable(name) >= comparable('1.8.6')
  end

  def version_class(version)
    return Regexp::Syntax::Any if ['*', 'any'].include?(version.to_s)

    version =~ VERSION_REGEXP || raise(InvalidVersionNameError, version)
    version_const_name = "V#{version.to_s.scan(/\d+/).join('_')}"
    const_get(version_const_name) || raise(UnknownSyntaxNameError, version)
  end

  def const_missing(const_name)
    if const_name =~ VERSION_CONST_REGEXP
      return fallback_version_class(const_name)
    end
    super
  end

  def fallback_version_class(version)
    sorted = (specified_versions + [version]).sort_by { |ver| comparable(ver) }
    index = sorted.index(version)
    index > 0 && const_get(sorted[index - 1])
  end

  def specified_versions
    constants.select { |const_name| const_name =~ VERSION_CONST_REGEXP }
  end

  def comparable(name)
    # add .99 to treat versions without a patch value as latest patch version
    Gem::Version.new((name.to_s.scan(/\d+/) << 99).join('.'))
  end
end

Version data entries

114 entries across 114 versions & 17 rubygems

Version Path
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.70 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/regexp_parser-2.8.2/lib/regexp_parser/syntax/version_lookup.rb