Class: Regexp::Syntax::Base

Inherits:
Object
  • Object
show all
Includes:
Token
Defined in:
lib/regexp_parser/syntax/base.rb

Overview

A lookup map of supported types and tokens in a given syntax

Direct Known Subclasses

Any, V1_8_6

Constant Summary

Constants included from Token

Token::All, Token::Map, Token::Types

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

TODO: drop this backwards compatibility code in v3.0.0, do `private :new`



99
100
101
102
# File 'lib/regexp_parser/syntax/base.rb', line 99

def initialize
  warn 'Using instances of Regexp::Parser::Syntax is deprecated ' \
       "and will no longer be supported in v3.0.0."
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/regexp_parser/syntax/base.rb', line 104

def method_missing(name, *args)
  if self.class.respond_to?(name)
    warn 'Using instances of Regexp::Parser::Syntax is deprecated ' \
         "and will no longer be supported in v3.0.0. Please call "\
         "methods on the class directly, e.g.: #{self.class}.#{name}"
    self.class.send(name, *args)
  else
    super
  end
end

Class Attribute Details

.featuresObject

Returns the value of attribute features.



13
14
15
# File 'lib/regexp_parser/syntax/base.rb', line 13

def features
  @features
end

Class Method Details

.added_featuresObject



46
47
48
# File 'lib/regexp_parser/syntax/base.rb', line 46

def added_features
  @added_features ||= {}
end

.excludes(type, tokens) ⇒ Object



26
27
28
29
# File 'lib/regexp_parser/syntax/base.rb', line 26

def excludes(type, tokens)
  tokens.each { |tok| features[type].delete(tok) }
  removed_features[type] = tokens
end

.implementations(type) ⇒ Object



36
37
38
# File 'lib/regexp_parser/syntax/base.rb', line 36

def implementations(type)
  features[type] || []
end

.implements(type, tokens) ⇒ Object



21
22
23
24
# File 'lib/regexp_parser/syntax/base.rb', line 21

def implements(type, tokens)
  (features[type] ||= []).concat(tokens)
  added_features[type] = tokens
end

.implements!(type, token) ⇒ Object Also known as: check!



40
41
42
43
# File 'lib/regexp_parser/syntax/base.rb', line 40

def implements!(type, token)
  raise NotImplementedError.new(self, type, token) unless
    implements?(type, token)
end

.implements?(type, token) ⇒ Boolean Also known as: check?

Returns:

  • (Boolean)


31
32
33
# File 'lib/regexp_parser/syntax/base.rb', line 31

def implements?(type, token)
  implementations(type).include?(token)
end

.inherited(subclass) ⇒ Object

automatically inherit features through the syntax class hierarchy



16
17
18
19
# File 'lib/regexp_parser/syntax/base.rb', line 16

def inherited(subclass)
  super
  subclass.features = features.to_h.map { |k, v| [k, v.dup] }.to_h
end

.normalize(type, token) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/regexp_parser/syntax/base.rb', line 54

def normalize(type, token)
  case type
  when :group
    normalize_group(type, token)
  when :backref
    normalize_backref(type, token)
  else
    [type, token]
  end
end

.normalize_backref(type, token) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/regexp_parser/syntax/base.rb', line 74

def normalize_backref(type, token)
  case token
  when :name_ref_ab, :name_ref_sq
    %i[backref name_ref]
  when :name_call_ab, :name_call_sq
    %i[backref name_call]
  when :name_recursion_ref_ab, :name_recursion_ref_sq
    %i[backref name_recursion_ref]
  when :number_ref_ab, :number_ref_sq
    %i[backref number_ref]
  when :number_call_ab, :number_call_sq
    %i[backref number_call]
  when :number_rel_ref_ab, :number_rel_ref_sq
    %i[backref number_rel_ref]
  when :number_rel_call_ab, :number_rel_call_sq
    %i[backref number_rel_call]
  when :number_recursion_ref_ab, :number_recursion_ref_sq
    %i[backref number_recursion_ref]
  else
    [type, token]
  end
end

.normalize_group(type, token) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/regexp_parser/syntax/base.rb', line 65

def normalize_group(type, token)
  case token
  when :named_ab, :named_sq
    %i[group named]
  else
    [type, token]
  end
end

.removed_featuresObject



50
51
52
# File 'lib/regexp_parser/syntax/base.rb', line 50

def removed_features
  @removed_features ||= {}
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/regexp_parser/syntax/base.rb', line 115

def respond_to_missing?(name, include_private = false)
  self.class.respond_to?(name) || super
end