Class: Windstorm::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/windstorm/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(t) ⇒ Object



7
8
9
10
11
# File 'lib/windstorm/parser.rb', line 7

def create(t)
  ps = self.new
  ps.table = t
  ps
end

Instance Method Details

#build(source) ⇒ Object



59
60
61
# File 'lib/windstorm/parser.rb', line 59

def build(source)
  convert(filter(source))
end

#convert(fs) ⇒ Object



55
56
57
# File 'lib/windstorm/parser.rb', line 55

def convert(fs)
  [fs].flatten.map{|s| dict[s]}.compact
end

#dictObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/windstorm/parser.rb', line 33

def dict
  @dict ||= lambda do
    dic = {}
    table.each do |c, s|
      [s].flatten.each{|t| dic[t] = c}
    end
    dic
  end.call
  @dict
end

#filter(source) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/windstorm/parser.rb', line 44

def filter(source)
  return [] unless source
  reg = /#{dict.keys.map{|k| Regexp.escape(k)}.join('|')}/
  ret = []
  source.lines do |l|
    next if l.start_with?('#') || l.start_with?('//')
    ret << l.scan(reg).flatten
  end
  ret.flatten
end

#tableObject



28
29
30
31
# File 'lib/windstorm/parser.rb', line 28

def table
  raise 'definition not found' if @table.nil? || @table.empty?
  @table
end

#table=(t) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/windstorm/parser.rb', line 15

def table=(t)
  raise 'definition blank' if t.nil? || t.empty?
  @table = {}
  COMMANDS.each do |c|
    li = [t[c]].flatten.compact.uniq
    next if li.empty?
    @table[c] = li
  end
  raise 'definition not found' if @table.empty?
  @dict = nil
  @table
end