Class: CodeParser

Inherits:
Object
  • Object
show all
Includes:
Digest
Defined in:
lib/codegraph.rb

Constant Summary collapse

@@ctagsOpts =
'--c-kinds=f --fortran-kinds=fsip --php-kinds=f --perl-kinds=f'
@@workDir =
ENV['HOME'] + '/.codegraph'
@@filesDBfile =
@@workDir+'/filesDB.json'
@@filesDB =
File.exist?(@@filesDBfile) ? JSON.parse(File.open(@@filesDBfile).read) : Hash.new
@@filesCk =
@@filesDB.hash
@@lock =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = false, dir = "#{ENV['HOME']}/.codegraph") ⇒ CodeParser



35
36
37
38
39
40
41
# File 'lib/codegraph.rb', line 35

def initialize(debug=false,dir="#{ENV['HOME']}/.codegraph")
  @dir     = dir
  @debug   = debug
  @funx    = {}
  @files   = {}
  @exclude = []
end

Instance Attribute Details

#excludeObject

Returns the value of attribute exclude.



17
18
19
# File 'lib/codegraph.rb', line 17

def exclude
  @exclude
end

#filesObject (readonly)

Returns the value of attribute files.



16
17
18
# File 'lib/codegraph.rb', line 16

def files
  @files
end

#funxObject (readonly)

Returns the value of attribute funx.



16
17
18
# File 'lib/codegraph.rb', line 16

def funx
  @funx
end

Class Method Details

.filesCkObject



30
31
32
# File 'lib/codegraph.rb', line 30

def CodeParser.filesCk
  @@filesCk
end

.filesDBObject

come class methods



27
28
29
# File 'lib/codegraph.rb', line 27

def CodeParser.filesDB
  @@filesDB
end

Instance Method Details

#read(*filelist) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/codegraph.rb', line 43

def read(*filelist)
  jobqueue = JobQueue.new

  filelist.each {|file|
    jobqueue.push {
      unless File.exist?(file)
        warn "Cannot find file #{file}"
        return
      else
        puts "Processing #{file} ..." if @debug
      end

      checksum = hexencode(file)
      if @@filesDB.has_key?(checksum) and @@filesDB.has_key?(file)
        code,funxLocations = @@filesDB[checksum]
        @files[file]       = @@filesDB[file]
      else
        basefile = File.basename(file)
        tempfile = "_" + basefile
        case File.extname(file)
        when '.c','.h'
          cppCommand = "cpp -w -fpreprocessed -E  -fdirectives-only #{file}  -o #{@dir}/#{tempfile} 2>/dev/null"
          cppCommand = "cpp -fpreprocessed #{file}  -o #{@dir}/#{tempfile} 2>/dev/null"
          grep       = "grep -v -e '^$' #{@dir}/#{tempfile} | grep -v -e '^#' | perl -pi -e " +'\'s/".[^"]*"//g\'' +" > #{@dir}/#{basefile}"
        when '.f','.f77','.f90','.f95'
          cppCommand = "cp #{file} #{@dir}/#{tempfile}"
          grep       = "grep -v -e '^$' #{@dir}/#{tempfile} | grep -v -e '^ *!' > #{@dir}/#{basefile}"
        else
          cppCommand = "cp #{file} #{@dir}/#{tempfile}"
          grep       = "grep -v -e '^$' #{@dir}/#{tempfile} | grep -v -e '^#' > #{@dir}/#{basefile}"
        end
        gen4ctags  = "ctags -x #{@@ctagsOpts}  #{@dir}/#{basefile} | sort -n -k 3"
        command    = [cppCommand,grep].join(";")

        puts gen4ctags if @debug
        puts command if @debug
        system(command)

        code          = open(@dir+'/'+ File.basename(file)).readlines
        funxLocations = IO.popen(gen4ctags).readlines.map {|l| l.split[0,4]}
        @@lock.synchronize { 
          @@filesDB[checksum] = [code,funxLocations]
          @@filesDB[file]     = funxLocations.map {|v| v[0]}
          @files[file]        = @@filesDB[file]
        }

        # cleanup
        FileUtils.rm("#{@dir}/#{tempfile}") unless @debug
        FileUtils.rm("#{@dir}/#{basefile}") unless @debug
      end
      funxLocations.each_with_index {|ary,i|
        name, kind, line, file = ary
        next if @exclude.include?(name)
        puts name if @debug
        line           = line.to_i
        startLineIndex = line - 1
        endLineIndex   = (i+1 < funxLocations.size) ? funxLocations[i+1][2].to_i - 2 : -1
        body = code[startLineIndex..endLineIndex].join
        @@lock.synchronize {
          @funx.store(name,body)
        }
      }
    }
  }
  jobqueue.run

  updateFilesDB
end