-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodegrep
More file actions
executable file
·308 lines (279 loc) · 8.76 KB
/
codegrep
File metadata and controls
executable file
·308 lines (279 loc) · 8.76 KB
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
#!/usr/bin/env lua
local arg = {...}
local options = {
word = false,
insensitive = true,
comments = false,
strings = false,
keys = false,
recursive = true,
}
local set_options = {
["-w"] = "word",
["-i"] = "insensitive",
["-c"] = "comments",
["-s"] = "strings",
["-k"] = "keys",
["-r"] = "recursive",
}
repeat
local set = false
for i, a in ipairs(arg) do
if a == "--help" then
options.help = true
set = false
break
end
if a:sub(1,1) == "-" and #a > 2 then
table.remove(arg, i)
for i = 2, #a do
table.insert(arg, "-"..a:sub(i,i))
end
set = true
break
end
if a == "--" then
table.remove(arg, i)
set = false
break
end
local is_set = set_options[a]
if is_set then
table.remove(arg, i)
options[is_set] = true
set = true
break
end
local is_unset = set_options[a:lower()]
if is_unset then
table.remove(arg, i)
options[is_unset] = false
set = true
break
end
end
until not set
if options.help or #arg == 0 then
print([[
codegrep - a grep-like tool tailored for code
codegrep is like grep, but it is able to skip comments and strings in the code.
Usage:
codegrep [-wWiIcCsS] <pattern> [files...]
Pattern is a Lua pattern, which is similar to regex but not the same,
except that '-' is taken literally.
Source files supported are: *.c *.h *.lua *.java *.grace *.js *.html
If files are not given, it recursively searches for source files in the
current directory (-r is accepted for compatibility but ignored).
Options:
-w -W Enable or disable whole-word search (default is off)
-i -I Enable or disable case-insensitive search (default is on)
-c -C Enable or disable search in comments (default is off: skips comments)
-s -S Enable or disable search in strings (default is off: skips strings)
-k -K Enable or disable search in key-like strings, that is, strings
containing only the search term (default is off: skips those strings)
Example:
codegrep -wc read
Looks for the word read in all source files of the tree, including comments.
]])
os.exit(0)
end
local pattern = arg[1]
table.remove(arg, 1)
pattern = pattern:gsub("%-", "%%-")
if options.insensitive then
pattern = pattern:lower()
end
if #arg == 0 then
for name in io.popen("find . -type f -and '(' -name '*.[ch]' -or -name '*.lua' -or -name '*.cpp' -or -name '*.hh' -or -name '*.cc' -or -name '*.java' -or -name '*.grace' -or -name '*.grc' -or -name '*.js' -or -name '*.html' -or -name '*.htm' ')'"):lines() do
table.insert(arg, name)
end
end
local function single_line_cut(line, marker, except)
local sc = line:find(marker, 1, true)
if sc then
if except then
if line:sub(sc, sc+#except-1) == except then
return line
end
end
line = line:sub(1, sc - 1)
end
return line
end
local function check_key(s)
if options.insensitive then
s = s:lower()
end
return s:match("^"..pattern.."$")
end
local function multi_line_cut(line, opener, closer)
local comment = false
local cc = 1
while true do
local mc = line:find(opener, cc, true)
if mc then
cc = line:find(closer, mc+#opener, true)
if cc then
if ((not options.keys) or not check_key(line:sub(mc+#opener, cc-1))) then
line = line:sub(1, mc - 1)..(" "):rep(cc + #closer - mc)..line:sub(cc + #closer, -1)
end
cc = cc + #closer
else
line = line:sub(1, mc - 1)
comment = true
break
end
else
break
end
end
return line, comment
end
local function in_multi_line(line, closer)
local comment = true
local cc = line:find(closer, 1, true)
if cc then
line = (" "):rep(cc + 1)..line:sub(cc + #closer, -1)
comment = false
else
line = ""
end
return line, comment
end
local function single_and_multi_cuts(line, comment, mopen, mclose, sopen, sexcept)
if comment then
line, comment = in_multi_line(line, mclose)
end
if not comment then
line = single_line_cut(line, sopen, sexcept)
line, comment = multi_line_cut(line, mopen, mclose)
end
if comment then
line = single_line_cut(line, sopen, sexcept)
end
return line, comment
end
local found_any = false
local styles = {
c = "c",
cpp = "c",
cc = "c",
hh = "c",
h = "c",
java = "c",
js = "c",
grace = "grace",
grc = "grace",
lua = "lua",
html = "html",
htm = "html",
}
for _, name in ipairs(arg) do
local file = io.open(name)
if file then
local ext = name:match("%.([^%.]*)$")
local style = styles[ext]
local comment = false
local multistring = false
local n = 1
local htmlmode = "html"
for line in file:lines() do
local orig = line
if not options.comments then
if style == "c" then
line, comment = single_and_multi_cuts(line, comment, "/*", "*/", "//")
elseif style == "grace" then
if not comment then
line = single_line_cut(line, "//")
end
elseif style == "lua" then
line, comment = single_and_multi_cuts(line, comment, "--[[", "]]", "--", "--[[")
elseif style == "html" then
if htmlmode == "html" then
if not comment then
line, comment = multi_line_cut(line, "<!--", "-->")
else
line, comment = in_multi_line(line, "-->")
end
if not comment then
local in_js
line, in_js = multi_line_cut(line, "<script", "</script>")
if in_js then
htmlmode = "js"
else
local in_css
line, in_css = multi_line_cut(line, "<style", "</style>")
if in_css then
htmlmode = "css"
end
end
end
elseif htmlmode == "js" then
line, comment = single_and_multi_cuts(line, comment, "/*", "*/", "//")
local in_js
_, in_js = in_multi_line(line, "</script>")
if not in_js then
htmlmode = "html"
end
elseif htmlmode == "css" then
line, comment = single_line_cut(line, "//")
local in_css
_, in_css = in_multi_line(line, "</style>")
if not in_css then
htmlmode = "html"
end
end
end
end
if not options.strings then
if style == "c" then
line = multi_line_cut(line, '"', '"')
line = multi_line_cut(line, "'", "'")
elseif style == "grace" then
line = multi_line_cut(line, '"', '"')
elseif style == "lua" then
if multistring then
line, multistring = in_multi_line(line, ']]')
end
if not multistring then
line = multi_line_cut(line, '"', '"')
line, multistring = multi_line_cut(line, '[[', ']]')
end
elseif style == "html" then
if htmlmode == "js" then
line = multi_line_cut(line, '"', '"')
line = multi_line_cut(line, "'", "'")
end
end
end
if options.insensitive then
line = line:lower()
end
local show = false
local starts, stops = {}, {}
for start, _, stop in line:gmatch("()("..pattern..")()") do
if (not options.word) or ((not line:sub(start-1,start-1):match("[%w_]")) and (not line:sub(stop,stop):match("[%w_]"))) then
table.insert(starts, start)
table.insert(stops, stop)
show = true
end
end
if show then
local out = { "\27[1;35m", name, " \27[1;32m", tostring(n), ":\27[0m" }
local at = 1
for i=1, #starts do
table.insert(out, orig:sub(at, starts[i]-1))
table.insert(out, "\27[1;31m")
table.insert(out, orig:sub(starts[i], stops[i]-1))
table.insert(out, "\27[0m")
at = stops[i]
end
table.insert(out, orig:sub(at, -1))
print(table.concat(out))
found_any = true
end
n = n + 1
end
end
end
os.exit(found_any and 0 or 1)