class Core::Parse::Parser

Public Class Methods

new(file, parsables, klass, stfu=false) click to toggle source
# 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

Public Instance Methods

parse() click to toggle source
# 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

Private Instance Methods

cast_type(str, sym) click to toggle source
# File lib/parse.rb, line 91
def cast_type(str, sym)
  case sym
  when :string
    return str
  when :symbol
    return str.to_sym
  when :int
    return str.to_i
  when :float
    return str.to_f
  when :array
    s = str.gsub!(/\s|\[|\]/, '')
    ary = s.split(',')
    ary.each { |el|
      if el.include?('*')
        a = el.split('*')
        ary.delete(el)
        a[0].to_i.times {
          ary.push(a[1])
        }
      end
    }
    return ary
  when :symarray
    s = str.gsub!(/\s|\[|\]/, '')
    ary = s.split(',')
    ary.each { |el|
      if el.include?('*')
        a = el.split('*')
        ary.delete(el)
        a[0].to_i.times {
          ary.push(a[1])
        }
      end
    }
    return ary.map(&:to_sym)
  when :intarray
    s = str.gsub!(/\s|\[|\]/, '')
    ary = s.split(',')
    ary.each { |el|
      if el.include?('*')
        a = el.split('*')
        ary.delete(el)
        a[0].to_i.times {
          ary.push(a[1])
        }
      end
    }
    return ary.map(&:to_i)
  when :color
    s = str.gsub!(/\s|\[|\]/, '')
    ary = s.split(',')
    ary.each { |el|
      if el.include?('*')
        a = el.split('*')
        ary.delete(el)
        a[0].to_i.times {
          ary.push(a[1])
        }
      end
    }
    ary = ary.map(&:to_i)
    color = Gosu::Color.new(ary[3], ary[0], ary[1], ary[2])
    return color
  when :image
    return Core.sprite(str)
  end
end
eval_type(sym) click to toggle source
# File lib/parse.rb, line 74
def eval_type(sym)
  case sym
  when :string
    return ""
  when :symbol
    return :nil
  when :int
    return 0
  when :float
    return 0.0
  when :array, :symarray, :intarray
    return []
  when :color
    return Gosu::Color::WHITE
  end
end