Module: Regexp::Syntax

Defined in:
lib/regexp_parser/syntax/token.rb,
lib/regexp_parser/syntax.rb,
lib/regexp_parser/syntax/any.rb,
lib/regexp_parser/syntax/base.rb,
lib/regexp_parser/syntax/token.rb,
lib/regexp_parser/syntax/token/keep.rb,
lib/regexp_parser/syntax/token/meta.rb,
lib/regexp_parser/syntax/token/group.rb,
lib/regexp_parser/syntax/token/anchor.rb,
lib/regexp_parser/syntax/token/escape.rb,
lib/regexp_parser/syntax/version_lookup.rb,
lib/regexp_parser/syntax/versions/1.8.6.rb,
lib/regexp_parser/syntax/versions/1.9.1.rb,
lib/regexp_parser/syntax/versions/1.9.3.rb,
lib/regexp_parser/syntax/versions/2.0.0.rb,
lib/regexp_parser/syntax/versions/2.2.0.rb,
lib/regexp_parser/syntax/versions/2.3.0.rb,
lib/regexp_parser/syntax/versions/2.4.0.rb,
lib/regexp_parser/syntax/versions/2.4.1.rb,
lib/regexp_parser/syntax/versions/2.5.0.rb,
lib/regexp_parser/syntax/versions/2.6.0.rb,
lib/regexp_parser/syntax/versions/2.6.2.rb,
lib/regexp_parser/syntax/versions/2.6.3.rb,
lib/regexp_parser/syntax/versions/3.1.0.rb,
lib/regexp_parser/syntax/token/assertion.rb,
lib/regexp_parser/syntax/token/quantifier.rb,
lib/regexp_parser/syntax/token/conditional.rb,
lib/regexp_parser/syntax/token/posix_class.rb,
lib/regexp_parser/syntax/token/backreference.rb,
lib/regexp_parser/syntax/token/character_set.rb,
lib/regexp_parser/syntax/token/character_type.rb,
lib/regexp_parser/syntax/token/unicode_property.rb

Overview

After loading all the tokens the map is full. Extract all tokens and types into the All and Types constants.

Defined Under Namespace

Modules: Token Classes: Any, Base, InvalidVersionNameError, NotImplementedError, SyntaxError, UnknownSyntaxNameError, V1_8_6, V1_9_1, V1_9_3, V2_0_0, V2_2_0, V2_3_0, V2_4_0, V2_4_1, V2_5_0, V2_6_0, V2_6_2, V2_6_3, V3_1_0

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.comparable_version(name) ⇒ Object


71
72
73
74
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 71

def comparable_version(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

.const_missing(const_name) ⇒ Object


43
44
45
46
47
48
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 43

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

.fallback_version_class(version) ⇒ Object


50
51
52
53
54
55
56
57
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 50

def fallback_version_class(version)
  sorted_versions = (specified_versions + [version])
                    .sort_by { |name| comparable_version(name) }
  return if (version_index = sorted_versions.index(version)) < 1

  next_lower_version = sorted_versions[version_index - 1]
  inherit_from_version(next_lower_version, version)
end

.inherit_from_version(parent_version, new_version) ⇒ Object


59
60
61
62
63
64
65
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 59

def inherit_from_version(parent_version, new_version)
  new_const = version_const_name(new_version)
  parent = const_get(version_const_name(parent_version))
  const_defined?(new_const) || const_set(new_const, Class.new(parent))
  warn_if_future_version(new_const)
  const_get(new_const)
end

.new(name) ⇒ Object

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.


23
24
25
26
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 23

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

.specified_versionsObject


67
68
69
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 67

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

.supported?(name) ⇒ Boolean

Returns:

  • (Boolean)

28
29
30
31
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 28

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

.version_class(version) ⇒ Object


33
34
35
36
37
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 33

def version_class(version)
  version =~ VERSION_REGEXP || raise(InvalidVersionNameError, version)
  version_const_name = version_const_name(version)
  const_get(version_const_name) || raise(UnknownSyntaxNameError, version)
end

.version_const_name(version_string) ⇒ Object


39
40
41
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 39

def version_const_name(version_string)
  "V#{version_string.to_s.scan(/\d+/).join('_')}"
end

.warn_if_future_version(const_name) ⇒ Object


76
77
78
79
80
81
# File 'lib/regexp_parser/syntax/version_lookup.rb', line 76

def warn_if_future_version(const_name)
  return if comparable_version(const_name) < comparable_version('4.0.0')

  warn('This library has only been tested up to Ruby 3.x, '\
       "but you are running with #{const_get(const_name).inspect}")
end