79 lines
3.5 KiB
Python
79 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Mechanical Lua 5.0 (WoW 1.12) -> Lua 5.1 (WoW 70334) rewrite for the ported aux add-on.
|
|
|
|
Token-aware (strings, long strings and comments are never touched):
|
|
* generic for over a bare table `for k, v in t do` -> `for k, v in __it(t) do`
|
|
(__it passes iterator functions through, so pairs/ipairs/gmatch loops are unchanged)
|
|
* vararg functions get Lua 5.0's implicit `arg` table: `function f(a, ...)` -> `... local arg = __va(...);`
|
|
* method `:gfind(` -> `:gmatch(`
|
|
Usage: lua50to51.py FILE... (in place). Idempotent: already-rewritten files are left as they are.
|
|
"""
|
|
import re, sys
|
|
|
|
TOKEN = re.compile(r'''
|
|
(?P<comment>--\[(?P<ceq>=*)\[.*?\](?P=ceq)\]|--[^\n]*)
|
|
|(?P<long>\[(?P<leq>=*)\[.*?\](?P=leq)\])
|
|
|(?P<str>"(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*')
|
|
|(?P<name>[A-Za-z_][A-Za-z0-9_]*)
|
|
|(?P<num>0[xX][0-9a-fA-F]+|\d+\.?\d*(?:[eE][+-]?\d+)?|\.\d+(?:[eE][+-]?\d+)?)
|
|
|(?P<op>\.\.\.|\.\.|==|~=|<=|>=|[-+*/%^#<>=(){}\[\];:,.])
|
|
|(?P<ws>\s+)
|
|
''', re.S | re.X)
|
|
|
|
def lex(src):
|
|
out, at = [], 0
|
|
while at < len(src):
|
|
m = TOKEN.match(src, at)
|
|
if not m: raise SystemExit(f'lex error at {at}: {src[at:at+40]!r}')
|
|
kind = m.lastgroup if m.lastgroup not in ('ceq', 'leq') else None
|
|
for k in ('comment', 'long', 'str', 'name', 'num', 'op', 'ws'):
|
|
if m.group(k) is not None: kind = k; break
|
|
out.append([kind, m.group(0)]); at = m.end()
|
|
return out
|
|
|
|
def code(toks): # indices of significant tokens
|
|
return [i for i, t in enumerate(toks) if t[0] not in ('ws', 'comment')]
|
|
|
|
def rewrite(src):
|
|
if '__it(' in src or '__va(' in src: return src
|
|
toks = lex(src); sig = code(toks); ins = {} # token index -> text inserted after it
|
|
pre = {} # token index -> text inserted before it
|
|
for n, i in enumerate(sig):
|
|
k, v = toks[i]
|
|
if k == 'name' and v == 'for':
|
|
# find 'in' or '=' before 'do'
|
|
j = n + 1
|
|
while j < len(sig) and toks[sig[j]][1] not in ('in', '=', 'do'): j += 1
|
|
if j >= len(sig) or toks[sig[j]][1] != 'in': continue
|
|
depth, e = 0, j + 1
|
|
while e < len(sig):
|
|
t = toks[sig[e]][1]
|
|
if t in '([{': depth += 1
|
|
elif t in ')]}': depth -= 1
|
|
elif t == 'do' and depth == 0 and toks[sig[e]][0] == 'name': break
|
|
e += 1
|
|
if e >= len(sig): raise SystemExit('for without do')
|
|
ins[sig[j]] = ins.get(sig[j], '') + ' __it('
|
|
pre[sig[e]] = ') ' + pre.get(sig[e], '')
|
|
elif k == 'name' and v == 'function':
|
|
j = n + 1
|
|
while j < len(sig) and toks[sig[j]][1] != '(': j += 1
|
|
depth, e = 0, j
|
|
while e < len(sig):
|
|
t = toks[sig[e]][1]
|
|
if t == '(': depth += 1
|
|
elif t == ')':
|
|
depth -= 1
|
|
if depth == 0: break
|
|
e += 1
|
|
if e - 1 > j and toks[sig[e - 1]][1] == '...':
|
|
ins[sig[e]] = ins.get(sig[e], '') + ' local arg = __va(...);'
|
|
elif k == 'op' and v == ':' and n + 1 < len(sig) and toks[sig[n + 1]][1] == 'gfind':
|
|
toks[sig[n + 1]][1] = 'gmatch'
|
|
return ''.join(pre.get(i, '') + t[1] + ins.get(i, '') for i, t in enumerate(toks))
|
|
|
|
if __name__ == '__main__':
|
|
for p in sys.argv[1:]:
|
|
s = open(p, encoding='utf-8').read(); r = rewrite(s)
|
|
if r != s: open(p, 'w', encoding='utf-8').write(r); print('rewrote', p)
|