Class: Regexp::Syntax::Base

Inherits:
Object
  • Object
show all
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, Ruby::V186

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



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

def initialize
  @implements = {}

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

Instance Method Details

#excludes(type, tokens) ⇒ Object

removes



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/regexp_parser/syntax/base.rb', line 31

def excludes(type, tokens)
  if tokens
    tokens = [tokens] unless tokens.is_a?(Array)
  end

  if @implements[type]
    if tokens
      @implements[type] = @implements[type] - tokens
      @implements[type] = nil if @implements[type].empty?
    else
      @implements[type] = nil
    end
  end
end

#implementationObject



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

def implementation
  @implements
end

#implements(type, tokens) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/regexp_parser/syntax/base.rb', line 22

def implements(type, tokens)
  if @implements[type]
    @implements[type] = (@implements[type] + tokens).uniq
  else
    @implements[type] = tokens
  end
end

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



52
53
54
55
# File 'lib/regexp_parser/syntax/base.rb', line 52

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)


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

def implements?(type, token)
  return true if @implements[type] and @implements[type].include?(token)
  false
end

#normalize(type, token) ⇒ Object



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

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/regexp_parser/syntax/base.rb', line 78

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_nest_ref_ab, :name_nest_ref_sq
    [:backref, :name_nest_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_nest_ref_ab, :number_nest_ref_sq
    [:backref, :number_nest_ref]
  else
    [type, token]
  end
end

#normalize_group(type, token) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/regexp_parser/syntax/base.rb', line 69

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