-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinliner.py
More file actions
232 lines (197 loc) · 7.75 KB
/
inliner.py
File metadata and controls
232 lines (197 loc) · 7.75 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
import os
import dataclasses
import argparse
import tables
import random
import re
HERE = os.path.dirname(os.path.realpath(__file__))
assert os.path.exists(HERE) and os.path.isdir(HERE)
@dataclasses.dataclass
class Config:
use_regions: bool = False
sort: bool = False
@staticmethod
def default():
return Config()
class Inliner:
cur: str
out: list[str]
path: set[str]
done: set[str]
depth: int
config: Config
def __init__(self, config: Config = Config.default()) -> None:
self.cur = os.path.abspath(__file__)
self.out = []
self.path = set()
self.path.add(os.path.join(HERE, 'include'))
self.done = set()
self.depth = 0
self.config = config
def add(self, line: str) -> None:
self.out.append(' ' * self.depth + line)
def lines(self, lines: list[str]) -> None:
depth = 0
for line in lines:
strip = line.strip()
skip = False
if strip.startswith('#'):
directive = strip.removeprefix('#').strip()
if directive.startswith('if') or directive.startswith('ifdef') or directive.startswith('ifndef'):
depth += 1
if directive.startswith('endif'):
depth -= 1
if directive.startswith('pragma'):
pragma = directive.removeprefix('pragma').strip()
if pragma.startswith('ebrew'):
import compile
cmd = pragma.removeprefix('ebrew').strip()
if cmd.startswith('inc'):
file = cmd.removeprefix('inc').strip()
with open(os.path.join(os.path.dirname(self.cur), file[1:-1])) as f:
code = f.read()
self.lines(compile.compile('inc', code).split('\n'))
else:
raise NotImplementedError(line)
skip = True
if directive.startswith('include'):
if depth == 0:
file = directive.removeprefix('include').strip()
if file.startswith('"') and file.endswith('"'):
self.file(os.path.join(os.path.dirname(self.cur), file[1:-1]))
skip = True
if file.startswith('<') and file.endswith('>'):
for dir in self.path:
assert os.path.exists(dir) and os.path.isdir(dir)
test = os.path.join(dir, file[1:-1])
if os.path.exists(test) and os.path.isfile(test):
self.file(test)
skip = True
break
if skip:
continue
if strip != '':
self.add(line)
def file(self, file: str) -> None:
if file in self.done:
return
self.done.add(file)
if not os.path.exists(file):
self.add(f'#include "{file}"')
return
last_dir = self.cur
last_depth = self.depth
try:
if self.config.use_regions:
self.add(f'#pragma region "{file}"')
self.depth += 1
self.cur = file
with open(file, 'r') as f:
lines = f.read().split('\n')
self.lines(lines)
finally:
self.cur = last_dir
if self.config.use_regions:
self.depth = last_depth
self.add(f'#pragma endregion "{file}"')
def output(self, shuffle: bool = True, rename: int = 0) -> str:
ret = []
defines = []
def end():
nonlocal defines
if shuffle:
while len(defines) != 0:
index = random.randrange(0, len(defines))
ret.append(defines.pop(index))
else:
ret.extend(defines)
defines = []
text = '\n'.join(self.out)
text = re.sub(r'\\\n', ' ', text)
text = re.sub(r'//.*', '', text)
text = re.sub(r'\n\n+', '\n', text)
if rename == 2:
map = {
'define': 'define',
}
def get_name(name: str) -> str:
if name not in map:
if name.startswith('M_'):
return 'M_' + get_name(name.removeprefix('M_'))
map[name] = f'v{len(map)+1}'
return map[name]
def rename2(m: re.Match) -> str:
return get_name(m.group(0))
text = re.sub(r'\b(?:M_|let_|eb_|ex_)\w+\b', rename2, text)
if rename == 1:
CAT_PATTERNS = [re.compile(i) for i in [
r'(\b\w+\b(?: *## *\b\w+\b)+)',
r'CAT(?: *DEL *\( *\))* *\(([^)]*)\)'
]]
CAT_NAMES = set([
'CAT',
'token_rec_2x',
])
alternation = []
for line in text.split('\n'):
if match := re.match(r'#define (\w+)\(([^)]*)\)(.*)$', line):
name = match.group(1).strip()
if name in CAT_NAMES:
continue
params = [i.strip() for i in match.group(2).split(',')]
line = match.group(3).strip()
for cat_pattern in CAT_PATTERNS:
for match in cat_pattern.finditer(line):
regex = r'\b'
for part in re.finditer(r'\b\w+\b', match.group(1)):
word = part.group(0)
if word in params:
regex += r'.*'
else:
regex += re.escape(word)
regex += r'\b'
if regex == r'\b.*.*\b':
print(line)
alternation.append(regex)
elif match := re.match(r'\b(\w+)\b *##', line):
print(match.group(2))
print(*(i[2:-2] for i in set(sorted(alternation))))
regex = re.compile(r'^' + r'|'.join(alternation) + r'$')
map = {
'define': 'define',
'__VA_ARGS__': '__VA_ARGS__',
}
for name in set(re.findall(r'\b[a-zA-Z][_a-zA-Z0-9]*\b', text)):
if name not in map:
if regex.match(name):
map[name] = name
else:
print(name)
map[name] = f'd{len(map)+1000}'
text = re.sub(r'\b[a-zA-Z][_a-zA-Z0-9]*\b', lambda m: map[m.group(0)], text)
for line in text.split('\n'):
line = line.strip()
if line.startswith('#define'):
defines.append(line)
else:
end()
ret.append(line)
end()
return '\n'.join(ret)
def main():
try:
tables.main()
parser = argparse.ArgumentParser()
parser.add_argument("input", type = argparse.FileType("r"), nargs = '+')
parser.add_argument("-o", "--output", type=argparse.FileType("w"), required=True)
args = parser.parse_args()
inliner = Inliner()
for input in args.input:
inliner.file(input.name)
input.close()
args.output.write(inliner.output())
args.output.close()
finally:
tables.clean()
if __name__ == "__main__":
main()