verilog_generate_label.rb

http://bit.ly/AdODvP のバグ修正版

http://bit.ly/AdODvP のバグ修正版

#encoding : sjis
#Verilog HDLgenerate for
require 'fileutils'
f = File.open(ARGV[0],"r")
FileUtils.cp(f,File.new(ARGV[0]+".bak","w")) #
s = f.read
s2 = s.dup
#
s.gsub!(/\/\*.*?\*\//m){|match|
" "*match.size
}
s.gsub!(%r{//.*}){|match|
" "*match.size
}
label_idx = Array.new #
i = 0
endgen_idx = 0
idx = 0
while (idx = s.index(/(\s|\n|\t)generate(\s|\n|\t)/,endgen_idx)) #serch generate
endgen_idx = s.index(/(\s|\n|\t)endgenerate(\s|\n|\t)/,idx) #serch endgenerate
while (idx = s.index(/(\s|\n|\t)for(\s|\n|\t)/,idx)) #serch for
if (idx>endgen_idx)
break
end
idx = s.index(/(\s|\n|\t)begin(\s|\n|\t)/,idx+4)+6 #serch begin
#
j = idx+1
while s[j] =~ /(\s|\n|\t)/
j += 1
end
if (s[j] != ':')
label_idx[i] = idx
i+=1
end
end
end
print "Result >> Added "+i.to_s+" labels\n"
#
(label_idx.size - 1).downto(0){|i|
s2.insert(label_idx[i]," : _label_"+i.to_s)
}
File.open(ARGV[0],"w").print(s2)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX