2015-12-16 00:31:54 -05:00
|
|
|
# frozen_string_literal: false
|
2005-09-22 18:08:47 -04:00
|
|
|
# $Id$
|
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
|
2004-09-12 13:34:30 -04:00
|
|
|
def main
|
2005-09-22 18:08:47 -04:00
|
|
|
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
|
2005-09-23 07:10:59 -04:00
|
|
|
exit true
|
2005-09-22 18:08:47 -04:00
|
|
|
}
|
|
|
|
begin
|
|
|
|
parser.parse!
|
|
|
|
rescue OptionParser::ParseError => err
|
|
|
|
$stderr.puts err.message
|
|
|
|
$stderr.puts parser.help
|
2005-09-23 07:10:59 -04:00
|
|
|
exit false
|
2005-09-22 18:08:47 -04:00
|
|
|
end
|
|
|
|
unless ARGV.size == 1
|
2005-09-23 07:10:59 -04:00
|
|
|
abort "wrong number of arguments (#{ARGV.size} for 1)"
|
2005-09-22 18:08:47 -04:00
|
|
|
end
|
2005-09-23 07:10:59 -04:00
|
|
|
out = ""
|
2005-09-22 18:08:47 -04:00
|
|
|
File.open(ARGV[0]) {|f|
|
|
|
|
prelude f, out
|
|
|
|
grammar f, out
|
|
|
|
usercode f, out
|
|
|
|
}
|
|
|
|
if output
|
|
|
|
File.open(output, 'w') {|f|
|
2005-09-23 07:10:59 -04:00
|
|
|
f.write out
|
2005-09-22 18:08:47 -04:00
|
|
|
}
|
|
|
|
else
|
2005-09-23 07:10:59 -04:00
|
|
|
print out
|
2005-09-22 18:08:47 -04:00
|
|
|
end
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
|
2005-09-22 18:08:47 -04:00
|
|
|
def prelude(f, out)
|
|
|
|
while line = f.gets
|
2004-09-12 13:34:30 -04:00
|
|
|
case line
|
|
|
|
when %r</\*%%%\*/>
|
2005-09-23 07:10:59 -04:00
|
|
|
out << '/*' << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
when %r</\*%>
|
2005-09-23 07:10:59 -04:00
|
|
|
out << '*/' << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
when %r<%\*/>
|
2005-09-23 07:10:59 -04:00
|
|
|
out << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
when /\A%%/
|
2005-09-23 07:10:59 -04:00
|
|
|
out << '%%' << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
return
|
|
|
|
when /\A%token/
|
2005-10-20 09:23:26 -04:00
|
|
|
out << line.sub(/<\w+>/, '<val>')
|
2004-09-12 13:34:30 -04:00
|
|
|
when /\A%type/
|
2005-10-20 09:23:26 -04:00
|
|
|
out << line.sub(/<\w+>/, '<val>')
|
2004-09-12 13:34:30 -04:00
|
|
|
else
|
2005-09-23 07:10:59 -04:00
|
|
|
out << line
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-09-22 18:08:47 -04:00
|
|
|
def grammar(f, out)
|
|
|
|
while line = f.gets
|
2004-09-12 13:34:30 -04:00
|
|
|
case line
|
|
|
|
when %r</\*%%%\*/>
|
2005-09-23 07:10:59 -04:00
|
|
|
out << '#if 0' << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
when %r</\*%c%\*/>
|
2005-09-23 07:10:59 -04:00
|
|
|
out << '/*' << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
when %r</\*%c>
|
2005-09-23 07:10:59 -04:00
|
|
|
out << '*/' << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
when %r</\*%>
|
2005-09-23 07:10:59 -04:00
|
|
|
out << '#endif' << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
when %r<%\*/>
|
2005-09-23 07:10:59 -04:00
|
|
|
out << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
when /\A%%/
|
2005-09-23 07:10:59 -04:00
|
|
|
out << '%%' << $/
|
2004-09-12 13:34:30 -04:00
|
|
|
return
|
|
|
|
else
|
2005-09-23 07:10:59 -04:00
|
|
|
out << line
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-09-22 18:08:47 -04:00
|
|
|
def usercode(f, out)
|
|
|
|
while line = f.gets
|
2005-09-23 07:10:59 -04:00
|
|
|
out << line
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
main
|