forked from skydark/nstools
-
Notifications
You must be signed in to change notification settings - Fork 77
/
nstemplate.py
executable file
·237 lines (193 loc) · 7.27 KB
/
nstemplate.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Usage: nstemplate.py [options] input_file output_file
Options:
-h --help show this help
-b --base BASE_NUM set base number [default: 1000]
-e --encoding ENCODING set input file encoding [default: auto]
"""
# <TODO>(DROPPED!)
# 1. macro
# 2. import with param
# 3. kr_reader_macro
# 4. (NS) kr_reader
# 5. (NS) function rec call
# 6. (NS) setwindow
# 7. (NS) reflect lsp/csp/...
# 8. (NS) mathlib
# 10. (NS) OO
# 13. (NS) text_utils(to_wide, int_to_chinese)
# 14. (NS) queue
# 15. (NS) button interface
# 16. (NS) name/face
import os
import sys
import re
from codecs import open
import chardet
from docopt import docopt
from utils import Struct
DEFAULT_ENCODING = 'utf-8'
py = sys.version_info
py3k = py >= (3, 0, 0)
if py3k:
to_unicode = lambda s, encoding=DEFAULT_ENCODING:\
s.decode(encoding) if isinstance(s, bytes) else str(s)
isstr = lambda s: isinstance(s, str)
encode = lambda s, encoding: bytes(s, encoding)
else:
to_unicode = lambda s, encoding=DEFAULT_ENCODING:\
s if isinstance(s, unicode) else s.decode(encoding)
isstr = lambda s: isinstance(s, basestring)
encode = lambda s, encoding: s.encode(encoding)
def debug(s):
print('DEBUG: ' + str(s))
class NSTParser(object):
_re_exps = {
'comment': r'^(\s*)@##(.*)$',
'raw': r'^(\s*)@#(|[^#].*)$',
'var': r'(%|\$)(\d+)',
'format': r'@\{([^}]+)\}',
'def': r'^(\s*)(@def)(\s+)(\w+)(\s*)(.*)',
'python': r'^(\s*)@python.*$',
'in_python': r'^(\s*)@(.*)$',
'import': r'^\s*@import\s+(?P<filename>[^\s]+)' + \
r'(\s+as\s+(?P<name>[^\s]+))?\s*$',
}
_resubs = {
'raw': r'\1\2',
'in_python': r'\2',
'def_make': r'\tdefsub \4',
}
def __init__(self, **options):
self.locals = {
'__base': 1000,
'__max_var': 0,
'__encoding': 'auto',
'__': lambda s: self.outlines.append(
to_unicode(s.format(**self.locals))),
'__s': lambda s: map(str.strip, s.split(',')),
}
if '__' in options: # can not bind '__': `self`
del options['__']
self.locals.update(options)
self.lines = []
self.outlines = []
self.defsubs = []
self.python_lines = []
for name, re_exp in self._re_exps.items():
setattr(self, '_re_' + name, re.compile(re_exp))
self.state = 'default'
self._parse = dict((state, getattr(self, '_parse_' + state))
for state in ('python', 'default'))
def get_name_by_filename(self, filename):
name = os.path.splitext(os.path.basename(filename))[0]
return name
def read_lines(self, filename, encoding=None):
if encoding is None or encoding == 'auto':
source = open(filename, 'rb').read()
encoding = chardet.detect(source)['encoding']
debug('Guess encoding for %s: %s' % (filename, encoding))
source = open(filename, 'rb', encoding=encoding).read()
lines = source.splitlines()
return lines
def _pre_parse(self, line, lno):
def rebase(m):
groups = m.groups()
var = int(groups[-1])
self.locals['__max_var'] = max(var, self.locals['__max_var'])
return ''.join(groups[:-1]) + str(self.locals['__base'] + var)
if self._re_comment.match(line):
return None
if self._re_raw.match(line):
self.outlines.append(self._re_raw.sub(self._resubs['raw'], line))
return None
line = '"'.join(self._re_var.sub(rebase, part) if i % 2 == 0 else part
for i, part in enumerate(line.split('"')))
return line
def _parse_default(self, line, lno):
if self._re_python.match(line):
self.state = 'python'
return
format = lambda m: str(eval(m.groups()[0], self.locals))
line = '"'.join(self._re_format.sub(format, part)
if i % 2 == 0 else part
for i, part in enumerate(line.split('"')))
if self._re_def.match(line):
def re_def_sub(m):
groups = m.groups()
p = groups[0] + '\tget_param ' + groups[5] if groups[5] else ''
return '{0}*{3}\r\n{p}'.format(*groups, p=p)
re_def = self._re_def
self.defsubs.append(re_def.sub(self._resubs['def_make'], line))
line = re_def.sub(re_def_sub, line)
self.outlines.append(line)
return
m = self._re_import.match(line)
if m:
d = m.groupdict()
filename = d['filename']
name = d['name'] or self.get_name_by_filename(filename)
importer = self.__class__(**self.locals)
lines = importer.read_lines(filename)
importer.parse(name, lines)
self.outlines.extend(importer.outlines)
if name:
self.locals[name] = Struct(**importer.locals)
return
self.outlines.append(line)
def _parse_python(self, line, lno):
if self._re_in_python.match(line):
line = self._re_in_python.sub(self._resubs['in_python'], line)
self.python_lines.append(line)
else:
exec('\n'.join(self.python_lines), self.locals)
self.python_lines = []
self.state = 'default'
return True
def parse(self, name, lines=None):
if lines is None:
lines = name
name = self.get_name_by_filename(name)
if isstr(lines):
lines = self.read_lines(lines, encoding=self.locals['__encoding'])
self.locals['__current_name'] = to_unicode(name)
self.lines = lines
# debug((name, self.locals['__encoding']))
for lno, line in enumerate(lines):
try:
line = self._pre_parse(line, lno)
if line is not None:
while self._parse[self.state](line, lno):
pass
except:
print('Error when parsing %s at line %s: %s'\
% (name, lno + 1, line))
raise
self.make_info_lines()
self.locals['__current_name'] = ''
def make_info_lines(self):
outlines = self.outlines
outlines.append('\r\n*__nst_defsub_%s' % self.locals['__current_name'])
outlines.extend(self.defsubs)
outlines.append('\treturn\r\n')
outlines.append('mov %%__nst_base, %s\r\n\r\n'
% (self.locals['__base'] + self.locals['__max_var'] + 1))
def write(self, output):
if isstr(output):
output = open(output, 'wb', encoding='GBK')
output.write('\r\n'.join(self.outlines))
def help():
exit(__doc__.strip())
def main(args):
options, arguments = docopt(__doc__)
if not(str(options.base).isdigit() and len(arguments) == 2):
help()
encoding = options.encoding
src_name = arguments[0]
dst_name = arguments[1]
parser = NSTParser(__encoding=encoding, __base=int(options.base))
parser.parse(src_name)
parser.write(dst_name)
if __name__ == '__main__':
main(sys.argv)