-
Notifications
You must be signed in to change notification settings - Fork 2
/
rubynette.rb
executable file
·362 lines (350 loc) · 9.08 KB
/
rubynette.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env ruby #********************************************************* #
# #
# ::: :::::::: #
# rubynette.rb :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mbacoux <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2013/12/10 15:24:10 by mbacoux #+# #+# #
# Updated: 2014/01/02 22:12:23 by mbacoux ### ########.fr #
# #
# **************************************************************************** #
def main
rubynette = Rubynette.new
rubynette.run
end
class Rubynette
def initialize
@version = "1.1.1 Lemon"
end
def hello
puts "\e[36;1mRubynette\e[37;1m version \e[32;1m" + @version + "\e[0m"
end
def usage
puts "Usage: rubynette [file1] [file2] [file3] ..."
puts "Rubynette can guess your project config if there is a Makefile."
end
def run
hello
parse_args
end
def parse_args
if ARGV.empty?
if File.exist? "Makefile"
do_file "Makefile"
else
puts "No file provided and no Makefile found."
usage
end
end
ARGV.each do |a|
do_file(a)
end
end
def do_file(file)
if !(File.exist? file)
puts "File " + file + " does not exist."
else
Parser.descendants.each do |parser|
if parser.can_handle? file and parser.handle_file_type? file
p = parser.new(self)
p.handle file
return
end
end
puts "No parser found for file '" + file + "'."
end
end
end
class Parser
def initialize(rubynette)
@rubynette = rubynette
end
def self.descendants
ObjectSpace.each_object(Class).select { |klass| klass < self }
end
def self.can_handle? (file)
return false;
end
def self.handle_file_type? (file)
return false
end
def handle(filename)
@filename = filename
@file = File.open filename
sp = " " * 15
puts "\e[37;1m[ \e[35;1m ---------- PARSING FILE #{File.basename(filename.upcase)} #{sp[0, (sp.size - File.basename(filename).size)]} ---------- \e[37;1m]\e[0m"
parse
@file.close
end
def error(str)
puts "[\e[31;1m ERROR \e[0m] In " + @filename + " " + str
end
def warning(str)
puts "[\e[33;1m WARNING \e[0m] In " + @filename + " " + str
end
end
class ParserFile < Parser
def self.handle_file_type? (file)
return File.file? file
end
end
class ParserDir < Parser
def self.can_handle? (file)
return true
end
def self.handle_file_type? (file)
return File.directory? file
end
def handle (file)
Dir.foreach file do |f|
if (f[0] != "."[0])
@rubynette.do_file((file == "/" ? "" : file) + "/" + f)
end
end
end
end
class ParserBin < ParserFile
def self.can_handle? (file)
ext = File.extname(file)
if (ext == ".a" or ext == ".o" or ext == ".dylib" or ext == ".so")
return true
elsif File.executable? file
return true
end
return false
end
def parse
puts "Your binary file '#{File.basename(@filename)}' contains the following calls :"
calls = `nm -gu #{@filename} | sed 's/\$.*//' | sed '/^_/!D' | sed '/^__/D' | cut -c 2- | sed '/^ft_/D' | sort -u`
if calls
calls.each_line do |c|
puts "--> #{c}"
end
end
end
end
class ParserText < ParserFile
def expand_tabs(s)
s.gsub(/([^\t\n]*)\t/) do
$1 + " " * (4 - ($1.size % 4))
end
end
def parse
check_header
@file.each_with_index do |line, n|
check_line(line, n)
end
end
def check_line(line, n)
if expand_tabs(line).size > 81
error_line("Row have more than 80 characters.", n)
end
line.each_byte do |c|
if c > 127
error_line("Non-ASCII character.", n)
end
end
end
def check_header
err = false
lines = @file.readlines.slice(0..10)
if lines.length != 11
err = true
end
lines.each do |s|
if expand_tabs(s).size != 81
err = true
end
end
if err
error(": Missing header.")
else
check_filename(lines[3])
end
@file.rewind
end
def check_filename(line)
name = (line.split)[1]
if name != File.basename(@filename)
warning("Filename does not match header.")
end
end
def error_line(str, line)
error("at line " + (line + 1).to_s + " : " + str)
end
end
class ParserSource < ParserText
def initialize(r)
super
@func_count = 0
@line_count = -1
@space_indent = 0
@banned_keywords = [ "for", "do", "switch", "case", "default", "goto", "lbl" ]
@keywords = [ "unsigned", "signed", "static", "const", "int", "short", "char",
"float", "long", "double", "size_t", "auto", "struct", "typedef",
"return", "break", "continue", "extern", "register", "restrict",
"void", "enum", "union", "volatile", "inline" ]
@binary_ops = [ ">>=", "<<=", ">>", "<<", "&&", "||", "==", "+=", "-=", "*=", "/=", "%=",
">=", "<=", "?", ":", "!=", "&=", "|=", "^=", "^", "|",
"=", "/", ">", "<"]
end
def self.can_handle? (file)
return (File.extname(file) == ".c" or File.extname(file) == ".h")
end
def is_operator?(str, op)
find = nil;
if str["->"]
return false
end
@binary_ops.each do |o|
if str[o]
find = o
break
end
end
return (find == op)
end
def get_def_indent(line)
str = line[/[a-z]/]
if str != nil
return line.index(str) - 1
else
return nil
end
end
def get_def_word(line)
start = get_def_indent(line)
len = line[/(([a-z])+)/]
if start == nil or len == nil
return nil
end
return line[start + 1, len.size]
end
def check_line(line, n)
super
if line[0, 2] == "/*" or line[0, 2] == "**" or line[0, 2] == "*/"
return
end
if line.include? "//"
error_line("C++ style comments are not allowed.", n)
return
end
if line[0] == "}"[0] and !line.include?(";")
@func_count += 1
end
if line[/^([\t ]+)\n$/]
error_line("Empty line with trailing spaces or tabs.", n)
elsif line[/^(.*)([\t ]+)\n$/]
error_line("Trailing whitespace.", n)
end
line.gsub!(%r{"(.*)"}, "\"\"")
line.gsub!(%r{'(.?)'}, "\' \'")
if line.count(";") > 1
error_line("More than one instruction.", n)
end
if (line.include?("if ") or line.include?("while ")) and line[0] != "#"[0]
if !line[/^((\t)+)((else )?)if /] and !line[/^((\t)+)while /]
error_line("Invalid text before control structure.", n)
end
if line.include?(";") or line.include?("{")
error_line("No newline at end of control structure.", n)
end
end
if line[/,(?![\n ])/]
error_line("No space after comma.", n)
end
@banned_keywords.each do |w|
reg = '[\t ]' + w + '[ \t\n(]'
if line[/#{reg}/]
error_line("Banned keyword '#{w}'.", n)
end
end
@keywords.each do |w|
reg = '^(([\t (])*)' + w + '[;(]'
if line[/#{reg}/]
error_line("Keyword '#{w}' not followed by whitespace.", n)
end
end
if !line[/^[\t ]/] and !line.include?("=") and line.count(",") > 3
error_line("Function have too many parameters.", n)
end
if line == "}\n"
if @line_count > 25
error_line("Function have " + @line_count.to_s + " lines.", n);
end
@line_count = -1
end
if @line_count != -1
@line_count += 1
end
if line == "{\n"
@line_count = 0
end
if line[/sizeof(?![(])/]
error_line("Whitespace after sizeof.", n)
end
@binary_ops.each do |o|
if (line[0] == "#"[0])
break
end
reg = '(?![ \n])' + o + '(?![ \n])'
i = line.index(/#{reg}/)
if i and is_operator?(line[i - 1, 4], o)
error_line("Missing spaces around operator '#{o}'.", n);
end
end
if line[0] == "#"[0]
word = get_def_word(line)
if (word == nil)
error_line("Lone sharp.", n)
else
if (word == "endif")
@space_indent -= 1
end
check = @space_indent
if word == "else" or word == "elif"
check -= 1
end
if (get_def_indent(line) != check)
error_line("Bad sharp indent, should be #{check.to_s}.", n)
end
if word == "if" or word == "ifdef" or word == "ifndef"
@space_indent += 1
end
end
end
end
def parse
super
if @func_count > 5
error " : More than 5 functions."
end
end
end
class ParserMakefile < ParserText
def self.can_handle? (file)
return (File.basename(file) == "Makefile")
end
def handle (file)
path = File.dirname(file)
files = `make -Bn -C #{path} | grep -o "[.a-zA-Z0-9_/-]*\\.c"`
files.each_line do |f|
@rubynette.do_file(path + (path == "/" ? "" : "/") + f.gsub(/[\n]/, ''));
end
files = `make -Bn -C #{path} | grep -o "\\-I[ ]*[.a-zA-Z0-9_/-]*" | sed "s/-I//" | tr -d " " | sort -u`
files = files + "."
files.each_line do |f|
dir = Dir.foreach(path + (path == "/" ? "" : "/") + f.gsub(/[\n]/, "")) do |d|
if File.extname(path + (path == "/" ? "" : "/") + f.gsub(/[\n]/, "") + "/" + d.gsub(/[\n]/, "")) == ".h"
@rubynette.do_file(path + (path == "/" ? "" : "/") + f.gsub(/[\n]/, "") + "/" + d.gsub(/[\n]/, ""))
end
end
end
files = `make -Bn -C #{path} | grep -o "make -C [.a-zA-Z0-9_/-]*" | cut -c 8- | tr -d " "`
files.each_line do |f|
@rubynette.do_file(path + (path == "/" ? "" : "/") + f.gsub(/[\n]/, "") + "/" + "Makefile")
end
end
end
main