Module: LLIP::RegexpAbstractScanner::ClassMethods

Defined in:
lib/llip/regexp_abstract_scanner.rb

Instance Method Summary collapse

Instance Method Details

#add_regexp(regexp) ⇒ Object

It allows to add a RegularExpression to the scanner and it makes sure that all the specified tokens don’t collide.

If a RegexpSpecification has starting_chars == :everything, it’s set to the default value of the scanning_table.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/llip/regexp_abstract_scanner.rb', line 61

def add_regexp(regexp)
  starting_chars = regexp.starting_chars
  if starting_chars.kind_of? Symbol 
    scanning_table.default = regexp
  else
    common_chars = starting_chars.select { |c| scanning_table.has_key? c } 
    starting_chars = starting_chars - common_chars
    starting_chars.each { |c| scanning_table[c] = regexp }
    colliding_states = common_chars.map { |c| scanning_table[c] }
    colliding_states.uniq!
    colliding_states.zip(common_chars).each { |r,c| scanning_table[c] = RegexpSpecification.mix(regexp,r) }
  end 
  
  if @built
    build
  end
  
  self
end

#buildObject

It fix a problem with all the regexp that ends with “.*” or “.+”. If such a regexp is given without calling this method, all the successive chars are going to be included by that regexp. This method add :error in the last state of that regexp for all starting chars in the scanner.

This method is automatically called when a new scanner is istantiated.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/llip/regexp_abstract_scanner.rb', line 88

def build
  regexps = scanning_table.values.uniq
  regexps << scanning_table.default if scanning_table.default
  
  fixable = []
  regexps.each do |regexp|
    regexp.last.each do |state|
      fixable << state if state.error == state
    end
  end
  
  starting_chars = scanning_table.keys
  fixable.each do |state|
    starting_chars.each do |char|
      state[char] = :error
    end
  end
  @built = true
  self
end

#built?Boolean

It returns true if the build method has been called.

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/llip/regexp_abstract_scanner.rb', line 110

def built?
  @built = false if @built.nil?
  @built
end

#scanning_tableObject

Its where all the regular expressions are stored. The keys are the starting_chars of the RegexpSpecification. While the table can be modified directly, it’s reccomanded to use the add_regexp method.



54
55
56
# File 'lib/llip/regexp_abstract_scanner.rb', line 54

def scanning_table
  @scanning_table ||= Hash.new 
end