Class: RubyLexer::CharHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rubylexer/charhandler.rb

Overview


Constant Summary collapse

CHARSETSPECIALS =
CharSet[?[ ,?] ,?\\ ,?-]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, default, hash) ⇒ CharHandler

Returns a new instance of CharHandler.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubylexer/charhandler.rb', line 30

def initialize(receiver,default,hash) 
  @default=default 
  @receiver=receiver
  if ?A.is_a? String #ruby >= 1.9
    @table={}
  else
    @table=Array.new(0)
  end
  @matcher='^[^'

  hash.each_pair {|pattern,action|
    case pattern
    when Range
      pattern.each { |c|
        self[c]=action
      }
    when String
      CharHandler.each_char(pattern) {|b| self[b]=action }
    when Fixnum
      self[pattern]=action
    else
      raise "invalid pattern class #{pattern.class}: #{pattern}"
    end
  }

  @matcher += ']$'
  @matcher=Regexp.new(@matcher,0,'n')

  freeze
end

Class Method Details

.each_char(str, &block) ⇒ Object



63
64
65
# File 'lib/rubylexer/charhandler.rb', line 63

def self.each_char(str,&block)
  str.each_char(&block)
end

Instance Method Details

#[]=(b, action) ⇒ Object




73
74
75
76
77
78
79
80
81
# File 'lib/rubylexer/charhandler.rb', line 73

def []=(b,action)  #for use in initialize only
  assert b >= ?\x00
  assert b <= ?\x7F
  assert !frozen?

  @table[b]=action
  @matcher << ?\\ if CHARSETSPECIALS===b
  @matcher << b
end

#go(b, *args) ⇒ Object




85
86
87
# File 'lib/rubylexer/charhandler.rb', line 85

def go(b,*args)
  @receiver.send((@table[b] or @default), b.chr, *args)
end