1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/ripper/tools/preproc.rb
mame a64054cd34 parse.y: Remove unneeded type decls for Ripper
I think that they are not used.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61964 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-19 15:43:42 +00:00

100 lines
2.1 KiB
Ruby
Executable file

# frozen_string_literal: true
# $Id$
require 'optparse'
def main
output = nil
parser = OptionParser.new
parser.banner = "Usage: #{File.basename($0)} [--output=PATH] <parse.y>"
parser.on('--output=PATH', 'An output file.') {|path|
output = path
}
parser.on('--help', 'Prints this message and quit.') {
puts parser.help
exit true
}
begin
parser.parse!
rescue OptionParser::ParseError => err
$stderr.puts err.message
$stderr.puts parser.help
exit false
end
unless ARGV.size == 1
abort "wrong number of arguments (#{ARGV.size} for 1)"
end
out = "".dup
File.open(ARGV[0]) {|f|
prelude f, out
grammar f, out
usercode f, out
}
if output
File.open(output, 'w') {|f|
f.write out
}
else
print out
end
end
def prelude(f, out)
@exprs = {}
while line = f.gets
case line
when /\A%%/
out << '%%' << $/
return
when /\A%token/
out << line.sub(/<\w+>/, '<val>')
when /\A%type/
out << line.sub(/<\w+>/, '<val>')
else
if (/^enum lex_state_(?:bits|e) \{/ =~ line)..(/^\}/ =~ line)
case line
when /^\s*(EXPR_\w+),\s+\/\*(.+)\*\//
@exprs[$1.chomp("_bit")] = $2.strip
when /^\s*(EXPR_\w+)\s+=\s+(.+)$/
name = $1
val = $2.chomp(",")
@exprs[name] = "equals to " + (val.start_with?("(") ? "<tt>#{val}</tt>" : "+#{val}+")
end
end
out << line
end
end
end
require_relative "dsl"
def grammar(f, out)
while line = f.gets
case line
when %r</\*% *ripper(?:\[(.*?)\])?: *(.*?) *%\*/>
out << DSL.new($2, ($1 || "").split(",")).generate << $/
when %r</\*%%%\*/>
out << '#if 0' << $/
when %r</\*%>
out << '#endif' << $/
when %r<%\*/>
out << $/
when /\A%%/
out << '%%' << $/
return
else
out << line
end
end
end
def usercode(f, out)
require 'erb'
compiler = ERB::Compiler.new('%-')
compiler.put_cmd = compiler.insert_cmd = "out.<<"
lineno = f.lineno
src, = compiler.compile(f.read)
eval(src, binding, f.path, lineno)
end
main