From bafc88f1f1cebc53c82a8c8d45d3d6783cc4ae94 Mon Sep 17 00:00:00 2001 From: aamine Date: Thu, 22 Sep 2005 22:08:47 +0000 Subject: * test/ripper/depend: use --output option instead of redirect; nmake does not remove a target when the target file is created by redirect. [ruby-dev:26466] * test/ripper/tools/preproc.rb: new option --output. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9280 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/ripper/tools/preproc.rb | 84 ++++++++++++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 24 deletions(-) (limited to 'ext/ripper/tools') diff --git a/ext/ripper/tools/preproc.rb b/ext/ripper/tools/preproc.rb index a2dba36e02..4b26c5bcb0 100755 --- a/ext/ripper/tools/preproc.rb +++ b/ext/ripper/tools/preproc.rb @@ -1,56 +1,92 @@ +# $Id$ + +require 'stringio' +require 'optparse' + def main - prelude - grammar - usercode + output = nil + parser = OptionParser.new + parser.banner = "Usage: #{File.basename($0)} [--output=PATH] " + parser.on('--output=PATH', 'An output file.') {|path| + output = path + } + parser.on('--help', 'Prints this message and quit.') { + puts parser.help + exit 0 + } + begin + parser.parse! + rescue OptionParser::ParseError => err + $stderr.puts err.message + $stderr.puts parser.help + exit 1 + end + unless ARGV.size == 1 + $stderr.puts "wrong number of arguments (#{ARGV.size} for 1)" + exit 1 + end + out = StringIO.new + File.open(ARGV[0]) {|f| + prelude f, out + grammar f, out + usercode f, out + } + if output + File.open(output, 'w') {|f| + f.write out.string + } + else + print out.string + end end -def prelude - while line = ARGF.gets +def prelude(f, out) + while line = f.gets case line when %r - puts '/*' + out.puts '/*' when %r - puts '*/' + out.puts '*/' when %r<%\*/> - puts + out.puts when /\A%%/ - puts '%%' + out.puts '%%' return when /\A%token/ - puts line.sub(/<\w+>/, '') + out.puts line.sub(/<\w+>/, '') when /\A%type/ - puts line.sub(/<\w+>/, '') + out.puts line.sub(/<\w+>/, '') else - print line + out.print line end end end -def grammar - while line = ARGF.gets +def grammar(f, out) + while line = f.gets case line when %r - puts '#if 0' + out.puts '#if 0' when %r - puts '/*' + out.puts '/*' when %r - puts '*/' + out.puts '*/' when %r - puts '#endif' + out.puts '#endif' when %r<%\*/> - puts + out.puts when /\A%%/ - puts '%%' + out.puts '%%' return else - print line + out.print line end end end -def usercode - while line = ARGF.gets - print line +def usercode(f, out) + while line = f.gets + out.print line end end -- cgit v1.2.3