2017-02-05 02:54:32 -05:00
|
|
|
# frozen_string_literal: true
|
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
|
2017-02-05 02:54:32 -05:00
|
|
|
out = "".dup
|
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)
|
2017-09-14 06:53:47 -04:00
|
|
|
@exprs = {}
|
2018-06-15 04:53:17 -04:00
|
|
|
lex_state_def = false
|
2005-09-22 18:08:47 -04:00
|
|
|
while line = f.gets
|
2004-09-12 13:34:30 -04:00
|
|
|
case line
|
|
|
|
when /\A%%/
|
2020-01-20 02:53:31 -05:00
|
|
|
out << "%%\n"
|
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>')
|
2018-06-15 04:53:17 -04:00
|
|
|
when /^enum lex_state_(?:bits|e) \{/
|
|
|
|
lex_state_def = true
|
|
|
|
out << line
|
|
|
|
when /^\}/
|
|
|
|
lex_state_def = false
|
|
|
|
out << line
|
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
|
2018-06-15 04:53:17 -04:00
|
|
|
if lex_state_def
|
|
|
|
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
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-19 05:48:08 -05:00
|
|
|
require_relative "dsl"
|
|
|
|
|
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
|
2018-01-19 05:48:08 -05:00
|
|
|
when %r</\*% *ripper(?:\[(.*?)\])?: *(.*?) *%\*/>
|
2020-01-20 02:53:31 -05:00
|
|
|
out << DSL.new($2, ($1 || "").split(",")).generate << "\n"
|
2004-09-12 13:34:30 -04:00
|
|
|
when %r</\*%%%\*/>
|
2020-01-20 02:53:31 -05:00
|
|
|
out << "#if 0\n"
|
2004-09-12 13:34:30 -04:00
|
|
|
when %r</\*%>
|
2020-01-20 02:53:31 -05:00
|
|
|
out << "#endif\n"
|
2004-09-12 13:34:30 -04:00
|
|
|
when %r<%\*/>
|
2020-01-20 02:53:31 -05:00
|
|
|
out << "\n"
|
2004-09-12 13:34:30 -04:00
|
|
|
when /\A%%/
|
2020-01-20 02:53:31 -05:00
|
|
|
out << "%%\n"
|
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)
|
2017-09-14 06:53:47 -04:00
|
|
|
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)
|
2004-09-12 13:34:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
main
|