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 Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



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

def initialize
  @implements = {}

  implements Token::Literal::Type,   Token::Literal::All
  implements Token::FreeSpace::Type, Token::FreeSpace::All
end

Class Method Details

.inspectObject



91
92
93
# File 'lib/regexp_parser/syntax/base.rb', line 91

def self.inspect
  "#{super} (feature set of #{ancestors[1].to_s.split('::').last})"
end

Instance Method Details

#excludes(type, tokens) ⇒ Object



33
34
35
# File 'lib/regexp_parser/syntax/base.rb', line 33

def excludes(type, tokens)
  implementations(type).subtract(Array(tokens))
end

#featuresObject



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

def features
  @implements
end

#implementations(type) ⇒ Object



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

def implementations(type)
  @implements[type] ||= Set.new
end

#implements(type, tokens) ⇒ Object



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

def implements(type, tokens)
  implementations(type).merge(Array(tokens))
end

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



42
43
44
45
# File 'lib/regexp_parser/syntax/base.rb', line 42

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)


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

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

#normalize(type, token) ⇒ Object



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/regexp_parser/syntax/base.rb', line 68

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

#normalize_group(type, token) ⇒ Object



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

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