-
Notifications
You must be signed in to change notification settings - Fork 0
/
header-crawl.dylan
140 lines (122 loc) · 4.48 KB
/
header-crawl.dylan
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
module: header-crawl
define method read-file (filename :: <string>) => (lines :: <llist>)
let result = make(<llist>);
if (~file-exists?(filename))
format-out("File \"%s\" doesn't exist.\n", filename);
else
let properties = file-properties(filename);
if (~element(properties, #"readable?"))
format-out("File \"%s\" is not readable.\n", filename);
else
let file-stream = make(<file-stream>, locator: filename);
block (done)
while (#t)
let (line, eol) = read-line(file-stream,
on-end-of-stream: "");
add!(result, line);
if (~eol) done(); end;
end;
cleanup
file-stream.close;
end;
result // return stretchy vector of lines
end;
end;
result
end method;
define method matches?(s :: <string>, r :: <regex>) => (match? :: <boolean>)
let match :: false-or(<regex-match>) = regex-search(r, s);
if (match) #t else #f; end;
end method;
define method extract(s :: <string>, r :: <regex>) => (got :: <string>)
let match :: false-or(<regex-match>) = regex-search(r, s);
if (match)
let result = match-group(match, 1);
if (result) result else "" end;
else
""
end;
end method;
define constant $comment-regex :: <regex> = compile-regex("^\\s*//.*$");
define method remove-boring-lines! (lines :: <llist>)
let i = lines.head-iterator;
while (i.valid?)
let j = i;
i := i.next;
if (j.data == "" | matches?(j.data, $comment-regex))
j.erase;
end;
end;
end method;
define constant $ifndef-regex :: <regex> = compile-regex("^#ifndef (\\S*).*$");
define constant $define-regex :: <regex> = compile-regex("^#define (\\S*).*$");
define constant $endif-regex :: <regex> = compile-regex("^#endif.*$");
define method remove-include-guard! (lines :: <llist>)
let i = lines.head-iterator;
let j = lines.tail-iterator;
if (i.valid? & next(i).valid? & j.valid?)
let name1 :: <string> = extract(i.data, $ifndef-regex);
let name2 :: <string> = extract(next(i).data, $define-regex);
let endif :: <boolean> = matches?(j.data, $endif-regex);
if (name1 ~= "" & string-equal?(name1, name2) & endif)
next(i).erase;
i.erase;
j.erase;
end;
end;
end;
define constant $strip-file-regex :: <regex> = compile-regex("^(.*/).*$");
define constant $strip-up-regex :: <regex> = compile-regex("^\\.\\./(.*)$");
define constant $strip-dir-regex :: <regex> = compile-regex("^(.*/).*/$");
define method join-paths(path :: <string>, rel :: <string>) => (out :: <string>)
let dir = extract(path, $strip-file-regex);
while (extract(rel, $strip-up-regex) ~= "")
rel := extract(rel, $strip-up-regex);
dir := extract(dir, $strip-dir-regex);
end;
concatenate(dir, rel)
end method;
define constant $include-regex :: <regex> = compile-regex(
"^#include \"([a-zA-Z0-9._\\- \\\\/]*)\".*$");
define method crawl (file :: <string>, visited :: <string-table>)
=> (source :: <llist>)
if (element(visited, file, default: #f) == #f)
element-setter(#t, visited, file);
format-out("Entering: '%s'\n", file);
force-out();
let lines = read-file(file);
remove-boring-lines!(lines);
remove-include-guard!(lines);
let i = lines.head-iterator;
while (i.valid?)
let included-file = extract(i.data, $include-regex);
if (included-file ~= "")
let included-path = join-paths(file, included-file);
let included-lines = crawl(included-path, visited);
insert-before(i, included-lines);
let to-erase = i;
i := i.next;
erase(to-erase);
else
i := i.next;
end;
end;
lines
else
make(<llist>)
end;
end;
define method main (args :: <vector>)
if (args.size == 0)
format-out("Welcome to header-crawl!\n");
format-out("Please specify a filename to crawl.\n");
exit-application(1);
end;
let filename :: <string> = element(args, 0);
let lines = crawl(filename, make(<string-table>));
for (line in lines)
format-out("%s\n", line);
end;
format-out("Got %d lines.\n", lines.size);
end method;
main(application-arguments());