1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# 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 %r</\*%%%\*/>
out << '/*' << $/
when %r</\*%>
out << '*/' << $/
when %r<%\*/>
out << $/
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
def grammar(f, out)
while line = f.gets
case line
when %r</\*%%%\*/>
out << '#if 0' << $/
when %r</\*%c%\*/>
out << '/*' << $/
when %r</\*%c>
out << '*/' << $/
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
|