Class: Core::Parse::Parser

Inherits:
Object show all
Defined in:
lib/parse.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, parsables, klass, stfu = false) ⇒ Parser

Returns a new instance of Parser.



19
20
21
22
23
24
25
26
27
28
# File 'lib/parse.rb', line 19

def initialize(file, parsables, klass, stfu=false)
  begin
    @file = File.open("#{Core::LIBRARY_PATH}/#{file}")
  rescue
    warn("ERROR: Failed to open file #{file}")
  end
  @parsables = parsables
  @klass = klass
  @stfu = stfu
end

Instance Method Details

#parseObject



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
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/parse.rb', line 30

def parse
  parsed = []
  vars = []
  block = false
  
  @parsables.each_key { |str|
    vars.push("@#{str}_parsed".to_sym)
  }
    
  @file.each_line { |line|
    line.sub!("\n", "")
    if line[0] == "#" or line.length == 0
      break if line.index("#EOF")
      next
    end
    if line[0] == "{"
      block = true
      next
    end
    if line[0] == "}"
      block = false
      parsed.push(@klass.send(:new))
      vars.each { |var|
        parsed.last.ivs(var.to_s[0...-7].to_sym, ivg(var))
      }
      next
    end
    if block
      line.gsub!("\"", "")
      line =~ /(.+)\s\=\s(.+)/
      key = $1
      val = cast_type($2, @parsables[key])
      ivs("@#{key}_parsed".to_sym, val)
    end
  }
  if !@stfu
    puts("INFO: Parsed #{parsed.length} #{File.basename(@file).sub('.def', '')}")
  end
  @file.close
  return parsed
end