-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.rb
186 lines (166 loc) · 4.92 KB
/
parse.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
load 'macro.rb'
load 'cp.rb'
#load 'preprocessor.rb'
def parse_block(s, method="FunctionBody")
p "parse using #{method}, #{s}"
scanner = Scanner.new(s)
error = MyError.new("whaterver", scanner)
parser = Parser.new(scanner, error)
parser.Get
ret = parser.send(method)
error.PrintListing
return ret
end
# parse string
# preprocess - true: do preprocess first, false: no do preprocess, just parse
def parse(s, preprocess = true, to_ruby=true)
#p "parse #{s}"
t_start = Time.now.to_f
if preprocess
p "Preprocessing..."
s = preprocess(s)
end
p ("preprocess line #{s.count("\n")}")
scanner = Scanner.new(s)
error = MyError.new("whaterver", scanner)
parser = Parser.new(scanner, error, $g_classdefs)
=begin
# parser.Get
if preprocess
content = parser.Preprocess
begin
aFile = File.new("pre.#{Time.now.to_i}", "w+")
aFile.puts content
aFile.close
rescue Exception=>e
p e
end
scanner.Reset
# parser.Get
end
=end
p "===== start parsing ====="
parser.Get
begin
ret = parser.C
rescue Exception=>e
parser.dump_pos
s = ""
parser.parse_stack.cur[:stack].each{|e|
s += SYMS[e[:sym]]+" "
}
p "Parsing #{s}, current sym #{SYMS[parser.sym]}(#{parser.curString()})"
ar = []
node = parser.parse_stack.cur
while (node)
ar.insert(0, node)
node = node[:parent]
end
p "---------- parsing stack --------"
for i in 0..ar.size-1
s = " "*2*i
ar[i][:stack].each{|e|
s += SYMS[e[:sym]]+" "
}
p s
end
p "---------- parsing stack end --------"
throw e
end
p ret
error.PrintListing
p "===== end of parsing ====="
parser.dump_classes_as_ruby if to_ruby
# $classdefs = parser.classdefs
# $classdefs.each{|k,v|
# p "class #{k}:"
# p " class name: #{v.class_name}"
# p " parent: #{v.parent}"
# p " modules: #{v.modules}"
# p " methods:"
# v.methods{|k,v|
# p " methods signature:#{k}"
# p " methods name:#{v[:name]}"
# p " src:#{v[:src]}"
# }
# }
p "Took #{Time.now.to_f - t_start} seconds"
return ret
end
def preprocess(s)
# p "content to prepro:#{s}"
scanner = Scanner.new(s)
error = MyError.new("whaterver", scanner)
parser = Preprocessor.new(scanner, error)
_t = Time.now.to_i
begin
content = parser.Preprocess
rescue Exception=>e
p "Preprocessing cost #{Time.now.to_i - _t} second"
p "Preprocessor current line #{scanner.currLine}/#{scanner.nextSym.line}"
parser.show_macros
parser.dump_macros_to_file("allmacros")
parser.dump_pos(nil, 10)
p "****** #ifstack ******"
for i in 0..parser.ifstack.size
p parser.ifstack[i]
end
p "****** end ******"
dump_file = "dump#{Time.now.to_i}"
parser.dump_buffer_to_file(dump_file)
p "buffer dumped to file '#{dump_file}'"
throw e
end
parser.show_macros
parser.dump_macros_to_file("allmacros")
parser.dump_classes_to_file("allclasses.#{Time.now.to_i}")
p "Preprocessor current line #{scanner.currLine}/#{scanner.nextSym.line}"
begin
fname = "pre.#{$g_cur_parse_file.split("/").last}.#{_t}"
aFile = File.new(fname, "w+")
aFile.puts content
aFile.close
p "Write preprocess result to file #{fname}"
rescue Exception=>e
p e
end
p "after preprocess:#{content}"
p "===== Preprocess end with #{error.error_list.size} errors"
return content
end
def preprocess_file(fname)
s = read_file(fname)
preprocess(s)
end
# parse file
# preprocess - true: do preprocess first, false: no do preprocess, just parse
def parse_file(fname, preprocess = "my", to_ruby=true)
content = read_file(fname)
if preprocess == "gcc"
fs = "pre.#{$g_cur_parse_file.split("/").last}.#{Time.now.to_f}"
p "process using gcc -E"
p `gcc -E #{fname} > #{fs} `
content = read_file(fs)
p "content = #{content}"
content.gsub!(/^#.*?$\n/, '')
p "content1 = #{content}"
begin
aFile = File.new(fs+".2", "w+")
aFile.puts content
aFile.close
p "Write preprocess result to file #{fname}"
rescue Exception=>e
p e
end
parse(content, false, to_ruby)
elsif preprocess == "no"
parse(content, false, to_ruby)
else
parse(content, true, to_ruby)
end
end
def test
# p parse_file("pre.1424096273", false)
p parse_file("pre.1431165136", false)
end
# test