Class: Rux::Lex::CharsetPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/rux/lex/patterns.rb

Direct Known Subclasses

NegatedCharsetPattern

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chars) ⇒ CharsetPattern



26
27
28
# File 'lib/rux/lex/patterns.rb', line 26

def initialize(chars)
  @chars = Set.new(chars)
end

Instance Attribute Details

#charsObject (readonly)

Returns the value of attribute chars.



24
25
26
# File 'lib/rux/lex/patterns.rb', line 24

def chars
  @chars
end

Class Method Details

.parse(str) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rux/lex/patterns.rb', line 10

def self.parse(str)
  pairs = str.scan(/(.)-?(.)?/)

  new(
    pairs.flat_map do |pair|
      if pair[1]
        (pair[0]..pair[1]).to_a
      else
        [pair[0]]
      end
    end
  )
end

Instance Method Details

#matches?(char) ⇒ Boolean



30
31
32
# File 'lib/rux/lex/patterns.rb', line 30

def matches?(char)
  chars.include?(char)
end