-
Notifications
You must be signed in to change notification settings - Fork 20
/
forwards.py
146 lines (124 loc) · 5.3 KB
/
forwards.py
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
import sys
import re
import os
import codecs;
license = """/*******************************
Copyright (c) 2016-2023 Grégoire Angerand
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**********************************/
"""
def remove_comments(content):
find_comments = re.compile(r"\/\/[^\n\r]+?(?:\*\)|[\n\r])")
find_ml_comments = re.compile(r"/\*.*?\*/", re.MULTILINE | re.DOTALL)
return find_comments.sub("", find_ml_comments.sub("", content))
def find_classes(content):
find_class = re.compile(r"(.*)\s+class ([A-Za-z0-9_]+)\s")
find_struct = re.compile(r"(.*)\s+struct ([A-Za-z0-9_]+)\s")
types = set()
for c in find_class.findall(content):
if "template" in c[0] or "enum" in c[0]:
continue
types.add("class " + c[1])
for s in find_struct.findall(content):
if "template" in s[0]:
continue
types.add("struct " + s[1])
return types;
def merge_dicts(a, b):
for (n, c) in b.items():
if n in a:
a[n] += c
else:
a[n] = c
def find_namespaces(content, current_name):
namespaces = {current_name: "\n"}
namespace_index = 0
while True:
next_namespace_index = content.find("namespace ", namespace_index)
if next_namespace_index >= 0:
namespaces[current_name] += "\n" + content[namespace_index:next_namespace_index].strip()
namespace_index = next_namespace_index
if content[:namespace_index].endswith("using "):
namespace_index += 10
else:
begin = content.find("{", namespace_index) + 1
name = content[namespace_index + 10 : begin - 1].strip()
end = begin
braces = 1
for c in content[begin:]:
if c == '{':
braces += 1
elif c == '}':
braces -= 1
if braces == 0:
break
end += 1
merge_dicts(namespaces, find_namespaces(content[begin:end].strip(), current_name + "::" + name))
namespace_index = end + 1
else:
namespaces[current_name] += "\n" + content[namespace_index:].strip()
break
return namespaces
def find_namespaces_in_folder(folder):
namespaces = {}
for root, dirs, files in os.walk(folder):
for file in files:
if extension(file) not in {"h"}:
continue
fullname = root + "/" + file
with open(fullname, "r") as f:
content = f.read()
content = remove_comments(content)
merge_dicts(namespaces, find_namespaces(content, ""))
return namespaces
def extension(file):
return file.split(".")[-1]
def process_project(projects):
for proj in projects:
forward_file_name = proj + "/utils/forward.h";
os.remove(forward_file_name)
namespaces = find_namespaces_in_folder(proj);
cl_per_ns = {}
for (n, c) in namespaces.items():
if len(n) == 0:
continue
cc = find_classes(c)
if n in cl_per_ns:
cl_per_ns[n] += cc
else:
cl_per_ns[n] = cc
with codecs.open(forward_file_name, "w", "utf-8") as f:
f.write(license)
guard = proj.upper() + "_UTILS_FORWARD_H"
f.write("#ifndef " + guard + "\n")
f.write("#define " + guard + "\n")
f.write("\n// Auto generated: forward definitions for most non template classes")
f.write("\n// TODO: Nested classes will not be declared correctly")
for (namespace, classes) in cl_per_ns.items():
if len(classes) == 0:
continue
if namespace[0:2] == "::":
namespace = namespace[2:]
if namespace == "std" or len(namespace) == 0:
continue
f.write("\n\nnamespace " + namespace + " {\n");
for cl in sorted(classes):
f.write(cl + ";\n")
f.write("}\n")
f.write("\n\n#endif // " + guard)
projects = {"yave", "editor"}
process_project(projects)