forked from msgpack-rpc/msgpack-rpc
/
command.rb
107 lines (81 loc) · 1.85 KB
/
command.rb
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
#!/usr/bin/env ruby
require 'optparse'
conf = {
:outdir => nil,
:lang => nil,
:verbose => false,
:devel => false,
}
op = OptionParser.new
op.on('-o', '--output DIR') {|s| conf[:outdir] = s }
op.on('-g', '--gen LANG') {|s| conf[:lang] = s }
op.on('-v', '--verbose') { conf[:verbose] = true }
op.on('', '--devel') { conf[:devel] = true }
op.banner += " <input>"
(class<<self;self;end).module_eval {
define_method(:usage) {|msg|
puts op.to_s
puts msg if msg
exit 1
}
}
op.parse!(ARGV)
begin
op.parse!(ARGV)
usage(nil) if ARGV.length != 1
input = ARGV.shift
lang = conf[:lang]
usage("-g option is required") unless lang
outdir = conf[:outdir]
outdir ||= "gen-#{lang}"
verbose = conf[:verbose]
rescue
usage($!.to_s)
end
require 'treetop/runtime'
require 'parser'
require 'ast'
require 'mplex'
LANGDIR = File.expand_path File.join(File.dirname(__FILE__), "lang")
begin
require "#{LANGDIR}/#{lang}"
rescue LoadError
usage("'#{lang}' is not supported.")
end
if input == "-"
in_fname = "(stdin)"
in_body = STDIN.read
else
in_fname = input
in_body = File.read(in_fname)
end
def expand_include(src, fname, body="")
s = src.split(/(?:(?![a-zA-Z0-9_]).|\A)include[ \t\r\n]*[\'\"]([^\'\"]*)[\'\"]/)
s.each_with_index do |m,i|
if i % 2 == 1
path = File.expand_path(m, File.dirname(fname))
expand_include(File.read(path), path, body)
else
body << m
end
end
body
end
body = expand_include(in_body, in_fname)
parser = MessagePackIDLParser.new
parser.consume_all_input = true
sn = parser.parse(body)
if sn.nil?
puts "Parse error at #{in_fname}:#{parser.failure_line}:#{parser.failure_column}:"
puts parser.failure_reason
exit 1
end
doc = sn.ast
doc.normalize!(conf)
if verbose
require 'pp'
pp doc
end
Dir.mkdir(outdir) unless File.directory?(outdir)
outdir = File.expand_path(outdir)
generate(doc, outdir, LANGDIR)