#!/usr/bin/env ruby # encoding: utf-8 RUBY_VERSION =~ /\A(1\.9|2\.)/ or raise "#{$0} needs ruby 1.9. See README." class STDRedRun def print_usage_and_exit(msg=nil,code=2) msg += "\n\n" if msg msg ||= "" STDERR.print <(s){s.gsub(/[^ \n]/,' ')}; #code = code.gsub(/\n=begin.*?\n=end[^\n]*/um) {|m|whiteout.(m)} code = code.gsub(/\n=kwid.*?\n=cut[^\n]*/um) {|m|whiteout.(m)} code = code.gsub(/\n=pod.*?\n=cut[^\n]*/um) {|m|whiteout.(m)} code = code.gsub(/\n=head1.*?\n=cut[^\n]*/um) {|m|whiteout.(m)} kibitz = false pn = Perl.new(code,at) tree=nil STDERR.print "parsing...\n" if kibitz begin $env_vars.scope_enter(:unitstopper) $env_vars[:unitstopper] = "_EOS" $env_vars.scope_enter(:stop) $env_vars[:stop] = "if you see this dont stop" tree = pn.send(start.to_sym) rescue RuntimeError => e STDERR.print error_message, e.message bt = e.backtrace bt = bt.slice(0,10) if quiet s = " "+bt.join("\n ")+"\n" if m = e.backtrace[0].match('^(.*?)STD_red') s = s.gsub(m[1],'') end STDERR.print s exit(1) rescue Interrupt exit(1) end if not tree STDERR.print "Parse failed.\n" exit(1) end if not format STDERR.print "describing...\n" if kibitz print tree.match_describe elsif format == 'p5a' print out(tree.to_dump0) elsif format == 'yaml' require "yaml" tree.prepare_for_yaml_dump if tree STDERR.print "yaml dumping...\n" if kibitz yml = YAML::dump(tree)+"\n" STDERR.print "yaml gsub'ing...\n" if kibitz p5yml = yml p5yml.gsub!(/^(\s*):/,'\1') p5yml.gsub!(/^(\s*rule: ):/,'\1') p5yml.gsub!(/ !ruby\/object:Match/,' !!perl/hash:Match') STDERR.print "done.\n" if kibitz print out(p5yml) else STDERR.print "Unknown format: #{format}\n" print_usage_and_exit end end def cached_output_for(code,format) cachedir = ENV['STD_RED_CACHEDIR'] return nil if not cachedir or not format input = code+format require 'digest/md5' std_file = File.dirname($0)+"/std.rb" std_code = File.open(std_file,"r"){|f|f.read} std_sig = Digest::MD5.hexdigest(std_code) input_sig = Digest::MD5.hexdigest(input) @cache_file = cachedir+'/parse_'+std_sig.slice(0,8)+'_'+input_sig.slice(0,32) if File.exists? @cache_file File.open(@cache_file,"r:utf-8"){|f|f.read} else nil end end def out(output) output = output.force_encoding('utf-8') File.open(@cache_file,"w"){|f|f.print output} if @cache_file output end end STDRedRun.new.main