forked from thisismypassport/shrinko8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pico_preprocess.py
334 lines (260 loc) · 11.1 KB
/
pico_preprocess.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
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
from utils import *
class CodeMapping(Tuple):
"""Specifies that code starting at index 'idx' maps to the given source starting at index 'src_idx'"""
idx = src_path = src_code = src_idx = src_line = ...
k_long_brackets_re = re.compile(r"\[(=*)\[(.*?)\]\1\]", re.S)
k_wspace = " \t\r\n"
def preprocess_code(path, code, start_line=0, preprocessor=None):
"""preprocess the given pico8 code (e.g. handle #include-s)"""
outparts = []
outmappings = []
i = start_i = out_i = 0
if preprocessor is None:
preprocessor = PicoPreprocessor()
preprocessor.start(path, code, outparts)
def skip_long_brackets():
nonlocal i
m = k_long_brackets_re.match(code, i)
if m:
i = m.end()
return True
def flush_output():
nonlocal start_i, out_i
if i > start_i and preprocessor.active:
outparts.append(code[start_i:i])
outmappings.append(CodeMapping(out_i, path, code, start_i, start_line))
out_i += (i - start_i)
start_i = i
strict = preprocessor.strict
while i < len(code):
ch = code[i]
if ch == '-' and list_get(code, i + 1) == '-' and strict: # comment
i += 2
if not skip_long_brackets():
while list_get(code, i) not in ('\n', None):
i += 1
elif ch == '[' and list_get(code, i + 1) == '[' and strict: # long string
skip_long_brackets()
elif ch in ('"', "'") and strict:
i += 1
while list_get(code, i) not in (ch, None):
i += 2 if code[i] == '\\' else 1
i += 1
elif ch != '#':
i += 1
elif list_get(code, i + 1) == '[' and list_get(code, i + 2) != '[': # #[...] inline directive (not used by pico preprocessor)
flush_output()
i, start_i, out_i = preprocessor.handle_inline(path, code, i, start_i, out_i, outparts, outmappings)
elif list_get(code, i + 1, '') not in k_wspace and list_get(code, i + 1) != '(': # normal directive?
# is it at the start of the line? (potentially with some whitespace before)
hash_i = i
while list_get(code, i - 1, '') in k_wspace:
if i == 0 or code[i - 1] == '\n':
break
i -= 1
else:
i = hash_i + 1
continue
flush_output()
i, start_i, out_i = preprocessor.handle(path, code, hash_i, start_i, out_i, outparts, outmappings)
else:
i += 1
flush_output()
preprocessor.finish(path, code, outparts)
return "".join(outparts), outmappings
class PicoPreprocessor:
"""The standard pico8 preprocessor (supporting #include and nothing else)"""
def __init__(m, strict=True, include_notifier=None):
m.strict = strict
m.include_notifier = include_notifier
m.active = True # always
def start(m, path, code, outparts):
pass
# note: we support recursive includes (unlike pico8) - it's pointless not to
def read_included_cart(m, orig_path, inc_name, out_i, outparts, outmappings):
tab_idx = None
if re.fullmatch(r".*:[0-9a-fA-F]", inc_name):
tab_idx = int(inc_name[-1], 16)
inc_name = inc_name[:-2]
inc_path = path_join(path_dirname(orig_path), inc_name)
if not path_exists(inc_path):
# windows path outside windows, maybe?
inc_path = inc_path.replace("\\", "/")
if not path_exists(inc_path):
throw(f"cannot open included cart at: {inc_path}")
if m.include_notifier:
m.include_notifier(inc_path)
inc_cart = read_cart(inc_path, preprocessor=m)
if e(tab_idx):
trim_cart_to_tab(inc_cart, tab_idx)
if inc_cart.code_map:
for map in inc_cart.code_map:
outmappings.append(CodeMapping(out_i + map.idx, map.src_path, map.src_code, map.src_idx, map.src_line))
else:
outmappings.append(CodeMapping(out_i, inc_path, inc_cart.code, 0, 0))
outparts.append(inc_cart.code)
return out_i + len(inc_cart.code)
def handle(m, path, code, i, start_i, out_i, outparts, outmappings):
end_i = code.find("\n", i)
end_i = end_i if end_i >= 0 else len(code)
args = code[i:end_i].split(maxsplit=1)
if len(args) == 2 and args[0] == "#include":
out_i = m.read_included_cart(path, args[1], out_i, outparts, outmappings)
return end_i, end_i, out_i
else:
return i + 1, start_i, out_i
def handle_inline(m, path, code, i, start_i, out_i, outparts, outmappings):
return i + 1, start_i, out_i
def finish(m, path, code, outparts):
pass
k_tab_break = "\n-->8\n" # yes, pico8 doesn't accept consecutive/initial/final tab breaks
def trim_cart_to_tab(cart, target_tab):
tab = start = 0
limit = len(cart.code)
while start < limit:
end = cart.code.find(k_tab_break, start)
if end < 0:
end = limit
if tab == target_tab:
break
tab += 1
start = end + len(k_tab_break)
else:
throw(f"Couldn't find tab {target_tab} in cart: {cart.path}")
cart.code = cart.code[start:end]
new_code_map = []
for map in cart.code_map:
if map.idx > end:
break
if map.idx <= start:
new_code_map.clear()
new_idx = max(map.idx - start, 0)
new_src_idx = map.src_idx + (start if map.src_path == cart.path else 0)
new_code_map.append(CodeMapping(new_idx, map.src_path, map.src_code, new_src_idx, map.src_line))
cart.code_map = new_code_map
k_custom_pp_inline_delims = k_wspace + "[]"
class CustomPreprocessor(PicoPreprocessor):
"""A custom preprocessor that isn't enabled by default (and is quite quirky & weird)"""
def __init__(m, defines=None, pp_handler=None, **kwargs):
super().__init__(**kwargs)
m.defines = defines.copy() if defines else {}
m.pp_handler = pp_handler
m.ppstack = []
m.recurse = 0
m.active = True
def get_active(m):
return m.ppstack[-1] if m.ppstack else True
def start(m, path, code, outparts):
if m.pp_handler:
m.pp_handler(op=True, args=(), ppline="", active=True, outparts=outparts, negate=False)
m.recurse += 1
def parse_args(m, args, code, i, inline):
while True:
while str_get(code, i, '\0') in k_wspace:
i += 1
if str_get(code, i) == None:
return None
elif str_get(code, i) == ']' and inline:
return i
if str_get(code, i) == '[':
match = k_long_brackets_re.match(code, i)
if not match:
throw("Unterminated custom preprocessor long brackets")
i = match.end()
args.append(match.group(2))
else:
arg_i = i
while str_get(code, i, '') not in (k_custom_pp_inline_delims if inline else k_wspace):
i += 1
args.append(code[arg_i:i])
def handle(m, path, code, i, start_i, out_i, outparts, outmappings):
end_i = code.find("\n", i)
while end_i >= 0 and code[end_i - 1] == '\\':
end_i = code.find("\n", end_i + 1)
end_i = end_i if end_i >= 0 else len(code)
line = code[i:end_i].replace("\\\n", "\n")
args = []
m.parse_args(args, line, 0, inline=False)
op = args[0] if args else ""
if op == "#include" and len(args) == 2:
if m.active:
out_i = m.read_included_cart(path, args[1], out_i, outparts, outmappings)
elif op == "#define" and len(args) >= 2:
if m.active:
value = line.split(maxsplit=2)[2].rstrip() if len(args) > 2 else ""
if "#[" in value:
value, _ = preprocess_code("(define)", value, preprocessor=m)
m.defines[args[1]] = value
elif op == "#undef" and len(args) == 2:
if m.active:
del m.defines[args[1]]
elif op == "#ifdef" and len(args) == 2:
m.active &= args[1] in m.defines
m.ppstack.append(m.active)
elif op == "#ifndef" and len(args) == 2:
m.active &= args[1] not in m.defines
m.ppstack.append(m.active)
elif op == "#else" and len(args) == 1 and m.ppstack:
old_active = m.ppstack.pop()
m.active = m.get_active() and not old_active
m.ppstack.append(m.active)
elif op == "#endif" and len(args) == 1 and m.ppstack:
m.ppstack.pop()
m.active = m.get_active()
else:
negate = False
if op.startswith("#!"):
op = str_replace_at(op, 1, 1, "")
negate = True
value = None
if m.pp_handler:
value = m.pp_handler(op=op, args=args, ppline=line, active=m.active, outparts=outparts, negate=negate)
if value in (None, False):
throw(f"Invalid custom preprocessor line: {line}")
if isinstance(value, str) and value:
outparts.append(value)
out_i += len(value)
return end_i, end_i, out_i
def handle_inline(m, path, code, i, start_i, out_i, outparts, outmappings):
if not m.active:
return i + 1, start_i, out_i
orig_i = i
i += 2
negate = False
if list_get(code, i) == '!':
i += 1
negate = True
args = []
i = m.parse_args(args, code, i, inline=True)
if i is None:
throw("Unterminated custom preprocessor args")
key = args[0] if args[0] else ""
op = f"#[{key}]"
end_i = i + 1
ppline = code[orig_i:end_i]
value = m.pp_handler(op=op, args=args, ppline=ppline, active=True, outparts=outparts, negate=negate) if m.pp_handler else None
if value in (None, False):
if len(args) == 1 and not negate:
if key in m.defines:
value = m.defines[key]
else:
throw(f"Undefined custom preprocessor variable: {key}")
elif 2 <= len(args) <= 3:
if (key in m.defines) ^ negate:
value = list_get(args, 1, "")
else:
value = list_get(args, 2, "")
else:
throw(f"Wrong inline custom preprocessor directive: {ppline}")
if value is True:
value = ""
if value:
outparts.append(value)
return end_i, end_i, out_i + len(value)
def finish(m, path, code, outparts):
m.recurse -= 1
if m.recurse == 0 and m.ppstack:
throw("Unterminated custom preprocessor ifs")
if m.pp_handler:
m.pp_handler(op=False, args=(), ppline="", active=True, outparts=outparts, negate=False)
from pico_cart import read_cart