Class: Kanocc::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/kanocc/scanner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(init = {}) ⇒ Scanner

Returns a new instance of Scanner.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kanocc/scanner.rb', line 26

def initialize(init = {})
  @logger = init[:logger] 
  unless @logger
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::WARN
  end
  @ws_regs = [/\s/]
  @terminals = []
  @string_patterns = {}
  @input = ""
  @stringScanner = StringScanner.new(@input)
  @current_match = nil
end

Instance Attribute Details

#current_matchObject

Returns the value of attribute current_match.



24
25
26
# File 'lib/kanocc/scanner.rb', line 24

def current_match
  @current_match
end

#inputObject

Returns the value of attribute input.



24
25
26
# File 'lib/kanocc/scanner.rb', line 24

def input
  @input
end

#loggerObject

Returns the value of attribute logger.



24
25
26
# File 'lib/kanocc/scanner.rb', line 24

def logger
  @logger
end

Instance Method Details

#next_match!Object



74
75
76
77
# File 'lib/kanocc/scanner.rb', line 74

def next_match!
  do_match!
  return @current_match
end

#set_recognized(*recognizables) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kanocc/scanner.rb', line 47

def set_recognized(*recognizables)
  @recognizables = []
  @literals = []
  @tokens = []
  @string_patterns = {}
  recognizables.each do |recognizable|
  unless (recognizable.class == Class and recognizable.ancestors.include?(Token)) or
    recognizable.is_a?(String)
      raise "set_recognized must be given a list of Tokens classes" +
     "and or strings, got #{recognizable.inspect}"
  end
  @recognizables << recognizable
  if recognizable.is_a? String
    @string_patterns[recognizable] = Regexp.new(Regexp.escape(recognizable))
    @literals << recognizable
  else
    @tokens << recognizable
  end
  end
end

#set_whitespace(*ws_regs) ⇒ Object



40
41
42
43
44
45
# File 'lib/kanocc/scanner.rb', line 40

def set_whitespace(*ws_regs)
  raise "set_whitespace must be given a list of Regexp's" \
  if ws_regs.find {|ws_reg| not ws_reg.is_a?(RegExp)}

  @ws_regs = ws_regs
end