class ReVIEW::Repository

Public Class Methods

new(param) click to toggle source
# File lib/review/preprocessor.rb, line 368
def initialize(param)
  @repository = {}
  @config = param
end

Public Instance Methods

fetch(file, type, name) click to toggle source
# File lib/review/preprocessor.rb, line 381
def fetch(file, type, name)
  table = file_descripter(file)[type] or return nil
  table[name]
end
fetch_file(file) click to toggle source
# File lib/review/preprocessor.rb, line 373
def fetch_file(file)
  file_descripter(file)['file']
end
fetch_range(file, name) click to toggle source
# File lib/review/preprocessor.rb, line 377
def fetch_range(file, name)
  fetch(file, 'range', name)
end

Private Instance Methods

_parse_file(f) click to toggle source
# File lib/review/preprocessor.rb, line 412
def _parse_file(f)
  whole = []
  repo = {'file' => whole}
  curr = {'WHOLE' => whole}
  lineno = 1
  yacchack = false # remove ';'-only lines.
  opened = [['(not opened)', '(not opened)']] * 3

  f.each do |line|
    case line
    when /(?:\A\#@|\#@@)([a-z]+)_(begin|end)\((.*)\)/
      type = check_type($1)
      direction = $2
      spec = check_spec($3)
      case direction
      when 'begin'
        key = "#{type}/#{spec}"
        error "begin x2: #{key}" if curr[key]
        (repo[type] ||= {})[spec] = curr[key] = []
      when 'end'
        curr.delete("#{type}/#{spec}") or
                error "end before begin: #{type}/#{spec}"
      else
        raise 'must not happen'
      end

    when %r<(?:\A\#@|\#@@)([a-z]+)/(\w+)\{>
      type = check_type($1)
      spec = check_spec($2)
      key = "#{type}/#{spec}"
      error "begin x2: #{key}" if curr[key]
      (repo[type] ||= {})[spec] = curr[key] = []
      opened.push [type, spec]

    when %r<(?:\A\#@|\#@@)([a-z]+)/(\w+)\}>
      type = check_type($1)
      spec = check_spec($2)
      curr.delete("#{type}/#{spec}") or
          error "end before begin: #{type}/#{spec}"
      opened.delete "#{type}/#{spec}"

    when %r<(?:\A\#@|\#@@)\}>
      type, spec = opened.last
      curr.delete("#{type}/#{spec}") or
          error "closed before open: #{type}/#{spec}"
      opened.pop

    when /(?:\A\#@|\#@@)yacchack/
      yacchack = true

    when /\A\#@-/ # does not increment line number.
      line = canonical($')
      curr.each_value do |list|
        list.push Line.new(nil, line)
      end

    else
      next if yacchack and line.strip == ';'
      line = canonical(line)
      curr.each_value do |list|
        list.push Line.new(lineno, line)
      end
      lineno += 1
    end
  end
  if curr.size > 1
    curr.delete 'WHOLE'
    curr.each do |range, lines|
      $stderr.puts "#{filename()}: unclosed range: #{range} (begin @#{lines.first.number})"
    end
    raise ApplicationError, "ERROR"
  end

  repo
end
canonical(line) click to toggle source
# File lib/review/preprocessor.rb, line 488
def canonical(line)
  tabwidth = 8
  if @config['tabwidth']
    tabwidth = @config['tabwidth']
  end
  if tabwidth > 0
    detab(line, tabwidth).rstrip + "\n"
  else
    line
  end
end
check_spec(spec) click to toggle source
# File lib/review/preprocessor.rb, line 507
def check_spec(spec)
  unless /\A\w+\z/ =~ spec
    error "wrong spec: #{spec.inspect}"
  end
  spec
end
check_type(type) click to toggle source
# File lib/review/preprocessor.rb, line 500
def check_type(type)
  unless Preprocessor::TYPES.index(type)
    error "wrong type: #{type.inspect}"
  end
  type
end
file_descripter(fname) click to toggle source
# File lib/review/preprocessor.rb, line 388
def file_descripter(fname)
  return @repository[fname] if @repository[fname]

  @repository[fname] = git?(fname) ? parse_git_blob(fname) : parse_file(fname)
end
git?(fname) click to toggle source
# File lib/review/preprocessor.rb, line 394
def git?(fname)
  fname.start_with?('git|')
end
parse_file(fname) click to toggle source
# File lib/review/preprocessor.rb, line 405
def parse_file(fname)
  File.open(fname, 'r:BOM|utf-8') {|f|
    init_ErrorUtils f
    return _parse_file(f)
  }
end
parse_git_blob(g_obj) click to toggle source
# File lib/review/preprocessor.rb, line 398
def parse_git_blob(g_obj)
  IO.popen('git show ' + g_obj.sub(/\Agit\|/, ''), 'r') do |f|
    init_ErrorUtils f
    return _parse_file(f)
  end
end