forked from trigrass2/v_repStubsGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_to_xml.py
71 lines (62 loc) · 2.58 KB
/
lua_to_xml.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
from sys import argv, exit
import re
if len(argv) != 3:
print('usage: {} <input-lua-file> <output-xml-file>'.format(argv[0]))
exit(1)
luafile = argv[1]
outfile = argv[2]
fun = None
args, rets = [], []
with open(outfile, 'w') as fout:
fout.write('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n')
fout.write('<plugin>\n')
def processTableType(t):
if '.' in t:
table, itemtype = t.split('.')
if table == 'table':
return 'table" item-type="%s' % itemtype
return t
def output():
if fun:
f, fdesc = fun
fout.write(' <command name="{}">\n'.format(f))
fout.write(' <description>{}</description>\n'.format(fdesc))
fout.write(' <params>\n')
for (t, n, d) in args:
t = processTableType(t)
fout.write(' <param name="{}" type="{}">\n'.format(n, t))
fout.write(' <description>{}</description>\n'.format(d))
fout.write(' </param>\n')
fout.write(' </params>\n')
fout.write(' <return>\n')
for (t, n, d) in rets:
t = processTableType(t)
fout.write(' <param name="{}" type="{}">'.format(n, t))
fout.write(' <description>{}</description>\n'.format(d))
fout.write(' </param>\n')
fout.write(' </return>\n')
fout.write(' </command>\n')
with open(luafile, 'r') as f:
for line in f:
m = re.match(r'\s*--\s*@([^\s]+)\s+(.*)$', line)
if m:
tag, line = map(lambda s: s.strip(), m.groups())
if tag == 'fun':
m = re.match(r'([^\s]+)\s*(.*)$', line)
if m:
name, description = map(lambda s: s.strip(), m.groups())
fun = (name, description)
elif tag in ('arg', 'ret'):
m = re.match(r'([^\s]+)\s+([^\s]+)\s*(.*)$', line)
if m:
dtype, name, description = map(lambda s: s.strip(), m.groups())
if tag == 'arg':
args.append((dtype, name, description))
elif tag == 'ret':
rets.append((dtype, name, description))
else:
output()
fun = None
args, rets = [], []
output()
fout.write('</plugin>\n')