1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
# -*- ruby -*-
require "hoe"
Hoe.plugin :seattlerb
Hoe.plugin :racc
Hoe.plugin :isolate
Hoe.plugin :rdoc
Hoe.add_include_dirs "lib"
Hoe.add_include_dirs "../../sexp_processor/dev/lib"
Hoe.add_include_dirs "../../minitest/dev/lib"
Hoe.add_include_dirs "../../oedipus_lex/dev/lib"
Hoe.add_include_dirs "../../ruby2ruby/dev/lib"
V2 = %w[20 21 22 23 24 25 26 27]
V3 = %w[30 31 32 33]
VERS = V2 + V3
ENV["FAST"] = VERS.last if ENV["FAST"] && !VERS.include?(ENV["FAST"])
VERS.replace [ENV["FAST"]] if ENV["FAST"]
racc_flags = nil
Hoe.spec "ruby_parser" do
developer "Ryan Davis", "ryand-ruby@zenspider.com"
license "MIT"
dependency "sexp_processor", "~> 4.16"
dependency "racc", "~> 1.5"
dependency "rake", [">= 10", "< 15"], :developer
dependency "oedipus_lex", "~> 2.6", :developer
require_ruby_version [">= 2.6", "< 4"]
if plugin? :perforce then # generated files
VERS.each do |n|
self.perforce_ignore << "lib/ruby_parser#{n}.rb"
end
VERS.each do |n|
self.perforce_ignore << "lib/ruby_parser#{n}.y"
end
self.perforce_ignore << "lib/ruby_lexer.rex.rb"
end
if plugin?(:racc)
self.racc_flags << " -t" if ENV["DEBUG"]
self.racc_flags << " --superclass RubyParser::Parser"
racc_flags = self.racc_flags
end
end
def maybe_add_to_top path, string
file = File.read path
return if file.start_with? string
warn "Altering top of #{path}"
tmp_path = "#{path}.tmp"
File.open(tmp_path, "w") do |f|
f.puts string
f.puts
f.write file
# TODO: make this deal with encoding comments properly?
end
File.rename tmp_path, path
end
def unifdef?
@unifdef ||= system("which unifdef") or abort <<~EOM
unifdef not found!
Please install 'unifdef' package on your system or `rake generate` on a mac.
EOM
end
def racc?
@racc ||= system("which racc") or abort <<~EOM
racc not found! `gem install racc`
EOM
end
generate_parser = proc do |t|
unifdef?
racc?
n = t.name[/\d+/]
sh "unifdef -tk -DV=%s %s | racc %s /dev/stdin -o %s" % [n, t.source, racc_flags, t.name]
maybe_add_to_top t.name, "# frozen_string_literal: true"
end
V2.each do |n|
file "lib/ruby_parser#{n}.rb" => "lib/ruby_parser2.yy", &generate_parser
end
V3.each do |n|
file "lib/ruby_parser#{n}.rb" => "lib/ruby_parser3.yy", &generate_parser
end
file "lib/ruby_lexer.rex.rb" => "lib/ruby_lexer.rex"
task :generate => [:lexer, :parser]
task :clean do
rm_rf(Dir["**/*~"] +
Dir["diff.diff"] + # not all diffs. bit me too many times
Dir["coverage.info"] +
Dir["coverage"] +
Dir["lib/ruby_parser2*.y"] +
Dir["lib/ruby_parser3*.y"] +
Dir["lib/*.output"])
end
task :sort do
sh "grepsort '^ +def' lib/ruby_lexer.rb"
sh "grepsort '^ +def (test|util)' test/test_ruby_lexer.rb"
end
desc "what was that command again?"
task :huh? do
puts "ruby #{Hoe::RUBY_FLAGS} bin/ruby_parse -q -g ..."
end
def (task(:phony)).timestamp
Time.at 0
end
task :isolate => :phony
def dl v, f
dir = v[/^\d+\.\d+/]
url = "https://cache.ruby-lang.org/pub/ruby/#{dir}/ruby-#{v}.tar.xz"
warn "Downloading ruby #{v}"
system "curl -s -o #{f} #{url}"
end
task :parser => :isolate
multitask :compare_build
task :compare_build => :generate
task :compare => :compare_build
def ruby_parse version
v = version[/^\d+\.\d+/].delete "."
diff = "compare/diff#{v}.diff"
rp_txt = "compare/rp#{v}.txt"
mri_txt = "compare/mri#{v}.txt"
parse_y = "compare/parse#{v}.y"
tarball = "compare/ruby-#{version}.tar.xz"
ruby_dir = "compare/ruby-#{version}"
rp_out = "lib/ruby_parser#{v}.output"
rp_y_rb = "lib/ruby_parser#{v}.rb"
normalize = "compare/normalize.rb"
file tarball do
dl version, tarball
end
desc "fetch all tarballs"
task :fetch => tarball
file parse_y => tarball do
extract_glob = case
when version > "3.3" then
"{id.h,parse.y,tool/{id2token.rb,lrama},defs/id.def}"
when version > "3.2" then
"{id.h,parse.y,tool/id2token.rb,defs/id.def}"
when version > "2.7" then
"{id.h,parse.y,tool/{id2token.rb,lib/vpath.rb}}"
else
"{id.h,parse.y,tool/{id2token.rb,vpath.rb}}"
end
system "tar xf #{tarball} -C compare #{File.basename ruby_dir}/#{extract_glob}"
# Debugging a new parse build system:
#
# Unpack the ruby tarball in question, configure, and run the following:
#
# % touch parse.y; make -n parse.c
# ...
# echo generating parse.c
# /Users/ryan/.rubies.current/bin/ruby --disable=gems ./tool/id2token.rb parse.y | \
# ruby ./tool/lrama/exe/lrama -oparse.c -Hparse.h - parse.y
#
# Then integrate these commands into the mess below:
d = ruby_dir
cmd = if version > "3.2" then
"ruby #{d}/tool/id2token.rb #{d}/parse.y | expand > #{parse_y}"
else
"ruby #{d}/tool/id2token.rb --path-separator=.:./ #{d}/id.h #{d}/parse.y | expand | ruby -pe 'gsub(/^%pure-parser/, \"%define api.pure\")' > #{parse_y}"
end
sh cmd
if File.exist? "#{d}/tool/lrama" then # UGH: this is dumb
rm_rf "compare/lrama"
sh "mv #{d}/tool/lrama compare"
end
sh "rm -rf #{d}"
end
bison = Dir["/opt/homebrew/opt/bison/bin/bison",
"/usr/local/opt/bison/bin/bison",
`which bison`.chomp,
].first
file mri_txt => [parse_y, normalize] do
if version > "3.3" then
sh "./compare/lrama/exe/lrama -r all -ocompare/parse#{v}.tab.c #{parse_y}"
else
sh "#{bison} -r all #{parse_y}"
mv Dir["parse#{v}.*"], "compare"
end
sh "#{normalize} compare/parse#{v}.output > #{mri_txt}"
rm ["compare/parse#{v}.output", "compare/parse#{v}.tab.c"]
end
file rp_out => rp_y_rb
file rp_txt => [rp_out, normalize] do
sh "#{normalize} #{rp_out} > #{rp_txt}"
end
compare = "compare#{v}"
compare_build = "compare_build#{v}"
desc "Compare all grammars to MRI"
task :compare => compare
task :compare_build => compare_build
task compare_build => diff
file diff => [mri_txt, rp_txt] do
sh "diff -du #{mri_txt} #{rp_txt} > #{diff}; true"
end
desc "Compare #{v} grammar to MRI #{version}"
task compare => diff do
system "wc -l #{diff}"
end
task :clean do
rm_f Dir[mri_txt, rp_txt]
end
task :realclean do
rm_f Dir[parse_y, tarball]
end
end
task :versions do
require "open-uri"
require "net/http" # avoid require issues in threads
require "net/https"
versions = VERS.map { |s| s.split(//).join "." }
base_url = "https://cache.ruby-lang.org/pub/ruby"
class Array
def human_sort
sort_by { |item| item.to_s.split(/(\d+)/).map { |e| [e.to_i, e] } }
end
end
versions = versions.map { |ver|
Thread.new {
URI
.parse("#{base_url}/#{ver}/")
.read
.scan(/ruby-\d+\.\d+\.\d+[-\w.]*?.tar.gz/)
.reject { |s| s =~ /-(?:rc|preview)\d/ }
.human_sort
.last
.delete_prefix("ruby-")
.delete_suffix ".tar.gz"
}
}.map(&:value).sort
puts versions.map { |v| "ruby_parse %p" % [v] }
end
ruby_parse "2.0.0-p648"
ruby_parse "2.1.10"
ruby_parse "2.2.10"
ruby_parse "2.3.8"
ruby_parse "2.4.10"
ruby_parse "2.5.9"
ruby_parse "2.6.10"
ruby_parse "2.7.8"
ruby_parse "3.0.6"
ruby_parse "3.1.4"
ruby_parse "3.2.2"
ruby_parse "3.3.0"
task :debug => :isolate do
ENV["V"] ||= VERS.last
Rake.application[:parser].invoke # this way we can have DEBUG set
Rake.application[:lexer].invoke # this way we can have DEBUG set
$:.unshift "lib"
require "ruby_parser"
require "pp"
klass = Object.const_get("Ruby#{ENV["V"]}Parser") rescue nil
raise "Unsupported version #{ENV["V"]}" unless klass
parser = klass.new
time = (ENV["RP_TIMEOUT"] || 10).to_i
n = ENV["BUG"]
file = (n && "bug#{n}.rb") || ENV["F"] || ENV["FILE"] || "debug.rb"
ruby = ENV["R"] || ENV["RUBY"]
if ruby then
file = "env"
else
ruby = File.read file
end
begin
pp parser.process(ruby, file, time)
rescue ArgumentError, Racc::ParseError => e
p e
puts e.backtrace.join "\n "
ss = parser.lexer.ss
src = ss.string
lines = src[0..ss.pos].split(/\n/)
abort "on #{file}:#{lines.size}"
end
end
task :debug3 do
file = ENV["F"] || "debug.rb"
version = ENV["V"] || ""
verbose = ENV["VERBOSE"] ? "-v" : ""
munge = "./tools/munge.rb #{verbose}"
abort "Need a file to parse, via: F=path.rb" unless file
ENV.delete "V"
ruby = "ruby#{version}"
sh "#{ruby} -v"
sh "#{ruby} -y #{file} 2>&1 | #{munge} > tmp/ruby"
sh "#{ruby} ./tools/ripper.rb -d #{file} | #{munge} > tmp/rip"
sh "rake debug F=#{file} DEBUG=1 2>&1 | #{munge} > tmp/rp"
sh "diff -U 999 -d tmp/{ruby,rp}"
end
task :cmp do
sh %(emacsclient --eval '(ediff-files "tmp/ruby" "tmp/rp")')
end
task :cmp3 do
sh %(emacsclient --eval '(ediff-files3 "tmp/ruby" "tmp/rip" "tmp/rp")')
end
task :extract => :isolate do
ENV["V"] ||= VERS.last
Rake.application[:parser].invoke # this way we can have DEBUG set
file = ENV["F"] || ENV["FILE"] || abort("Need to provide F=<path>")
ruby "-Ilib", "bin/ruby_parse_extract_error", file
end
task :parse => :isolate do
ENV["V"] ||= VERS.last
Rake.application[:parser].invoke # this way we can have DEBUG set
file = ENV["F"] || ENV["FILE"] || abort("Need to provide F=<path>")
ruby "-Ilib", "bin/ruby_parse", file
end
task :bugs do
sh "for f in bug*.rb bad*.rb ; do #{Gem.ruby} -S rake debug F=$f && rm $f ; done"
end
# vim: syntax=Ruby
|