Class: LLIP::RegexpAbstractScanner

Inherits:
AbstractScanner
  • Object
show all
Defined in:
lib/llip/regexp_abstract_scanner.rb

Overview

The RegexpAbstractScanner is the main abstract scanner of LLIP. To have a real scanner, just subclass it and add some regular expressions.

See ClassMethods to know how.

Direct Known Subclasses

RegexpScanner

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RegexpAbstractScanner

Returns a new instance of RegexpAbstractScanner.



17
18
19
20
# File 'lib/llip/regexp_abstract_scanner.rb', line 17

def initialize(*args)
  super
  self.class.build unless self.class.built?
end

Class Method Details

.inherited(other) ⇒ Object



13
14
15
# File 'lib/llip/regexp_abstract_scanner.rb', line 13

def self.inherited(other)
  other.extend(ClassMethods)
end

Instance Method Details

#nextObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/llip/regexp_abstract_scanner.rb', line 22

def next
  return @current = Token.new(:nil,nil,@current_line,@current_char) unless @next_char
  
  line = @current_line
  char = @current_char
  
  regexp = self.class.scanning_table[@next_char]
  unless regexp
    token = Token.new(:nil,@next_char,line,char)
    raise LLIPError.new(token,"there isn't a regular expression which starts with #{@next_char}")
  end
  
  state = regexp.init
  string = ""
  while state[@next_char] != :error and @next_char
    state = state[@next_char]
    string << @next_char
    read_next
  end
  
  token = Token.new(state.regexp.name,string,line,char)
  if state.final?
    @current = token
  else
    raise UnvalidTokenError.new(token)
  end
end