Sha256: 990b91fbc636370a8f5ee9e284e695cb17c0f653528838d3f4a3aa192971f385

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

require File.expand_path('../syntax/tokens', __FILE__)
require File.expand_path('../syntax/base', __FILE__)
require File.expand_path('../syntax/any', __FILE__)
require File.expand_path('../syntax/versions', __FILE__)

module Regexp::Syntax

  VERSION_FORMAT = '\Aruby/\d+\.\d+(\.\d+)?\z'
  VERSION_REGEXP = /#{VERSION_FORMAT}/

  class SyntaxError < StandardError
    def initialize(what)
      super what
    end
  end

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

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

  # Loads and instantiates an instance of the syntax specification class for
  # the given syntax version name. The special names 'any' and '*' return an
  # instance of Syntax::Any.
  def self.new(name)
    return Regexp::Syntax::Any.new if
      ['*', 'any'].include?( name.to_s )

    raise UnknownSyntaxNameError.new(name) unless supported?(name)

    version_class(name).new
  end

  def self.supported?(name)
    VERSIONS.include?(name)
  end

  def self.version_class(version)
    raise InvalidVersionNameError.new(version) unless
      version =~ VERSION_REGEXP

    version_const_name = version.scan(/\d+/).join

    const_name = "Regexp::Syntax::Ruby::V#{version_const_name}"

    if RUBY_VERSION >= '2.0.0'
      Kernel.const_get(const_name)
    else
      Object.module_eval(const_name, __FILE__, __LINE__)
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
regexp_parser-0.4.13 lib/regexp_parser/syntax.rb
regexp_parser-0.4.12 lib/regexp_parser/syntax.rb
regexp_parser-0.4.11 lib/regexp_parser/syntax.rb
regexp_parser-0.4.10 lib/regexp_parser/syntax.rb