forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.py
executable file
·280 lines (218 loc) · 6.85 KB
/
tool.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
#!/usr/bin/env python2
"""
asdl_gen.py - Generate Python and C from OSH ASDL schemas.
"""
from __future__ import print_function
import os
import sys
from asdl import asdl_
from asdl import front_end
from asdl import gen_cpp
from asdl import gen_python
#from core.pyerror import log
# Special cases like Id
_SIMPLE = ['state', 'emit', 'char_kind']
class UserType(object):
"""
TODO: Delete this class after we have modules with 'use'?
"""
def __init__(self, mod_name, type_name):
self.mod_name = mod_name
self.type_name = type_name
def __repr__(self):
return '<UserType %s %s>' % (self.mod_name, self.type_name)
def main(argv):
try:
action = argv[1]
except IndexError:
raise RuntimeError('Action required')
try:
schema_path = argv[2]
except IndexError:
raise RuntimeError('Schema path required')
schema_filename = os.path.basename(schema_path)
if schema_filename in ('syntax.asdl', 'runtime.asdl'):
app_types = {'id': UserType('id_kind_asdl', 'Id_t')}
else:
app_types = {}
if action == 'c': # Generate C code for the lexer
with open(schema_path) as f:
schema_ast = front_end.LoadSchema(f, app_types)
v = gen_cpp.CEnumVisitor(sys.stdout)
v.VisitModule(schema_ast)
elif action == 'cpp': # Generate C++ code for ASDL schemas
out_prefix = argv[3]
pretty_print_methods = bool(os.getenv('PRETTY_PRINT_METHODS', 'yes'))
gc = bool(os.getenv('GC', ''))
with open(schema_path) as f:
schema_ast = front_end.LoadSchema(f, app_types)
# asdl/typed_arith.asdl -> typed_arith_asdl
ns = os.path.basename(schema_path).replace('.', '_')
with open(out_prefix + '.h', 'w') as f:
guard = ns.upper()
f.write("""\
// %s.h is generated by asdl/tool.py
#ifndef %s
#define %s
""" % (out_prefix, guard, guard))
#define MYLIB_LEGACY 1 // get rid conflicting types
f.write("""\
#include <cstdint>
""")
if gc:
f.write("""
#include "gc_heap.h" // for new object header
""")
else:
f.write("""
#include "mylib.h" // for old Str, List, etc.
""")
if pretty_print_methods:
f.write("""\
#include "hnode_asdl%s.h"
using hnode_asdl::hnode_t;
""" % ('.gc' if gc else ''))
if app_types:
f.write("""\
#include "id_kind_asdl.h"
using id_kind_asdl::Id_t;
""")
if gc:
f.write("""\
using gc_heap::Obj;
using gc_heap::Dict;
using gc_heap::List;
using gc_heap::NewStr;
using gc_heap::Str;
""")
for use in schema_ast.uses:
# Forward declarations in the header, like
# namespace syntax_asdl { class command_t; }
# must come BEFORE namespace, so it can't be in the visitor.
# assume sum type for now!
cpp_names = [
'class %s;' % asdl_.TypeNameHeuristic(n) for n in use.type_names]
f.write('namespace %s_asdl { %s }\n' % (
use.mod_name, ' '.join(cpp_names)))
f.write('\n')
f.write("""\
namespace %s {
""" % ns)
v = gen_cpp.ForwardDeclareVisitor(f)
v.VisitModule(schema_ast)
debug_info = {}
v2 = gen_cpp.ClassDefVisitor(f, pretty_print_methods=pretty_print_methods,
simple_int_sums=_SIMPLE,
debug_info=debug_info)
v2.VisitModule(schema_ast)
f.write("""
} // namespace %s
#endif // %s
""" % (ns, guard))
try:
debug_info_path = argv[4]
except IndexError:
pass
else:
with open(debug_info_path, 'w') as f:
from pprint import pformat
f.write('''\
cpp_namespace = %r
tags_to_types = \\
%s
''' % (ns, pformat(debug_info)))
with open(out_prefix + '.cc', 'w') as f:
f.write("""\
// %s.cc is generated by asdl/tool.py
#include "%s.h"
#include <assert.h>
#include "asdl/runtime.h" // generated code uses wrappers here
""" % (out_prefix, ns))
# To call pretty-printing methods
for use in schema_ast.uses:
f.write('#include "%s_asdl.h" // ASDL use\n' % use.mod_name)
f.write("""\
// Generated code uses these types
using hnode_asdl::hnode__Record;
using hnode_asdl::hnode__Array;
using hnode_asdl::hnode__External;
using hnode_asdl::hnode__Leaf;
using hnode_asdl::field;
using hnode_asdl::color_e;
""")
if app_types:
f.write('using id_kind_asdl::Id_str;\n')
f.write("""
namespace %s {
""" % ns)
v3 = gen_cpp.MethodDefVisitor(f,
pretty_print_methods=pretty_print_methods,
simple_int_sums=_SIMPLE)
v3.VisitModule(schema_ast)
f.write("""
} // namespace %s
""" % ns)
elif action == 'mypy': # Generated typed MyPy code
with open(schema_path) as f:
schema_ast = front_end.LoadSchema(f, app_types)
try:
abbrev_module_name = argv[3]
except IndexError:
abbrev_mod = None
else:
# Weird Python rule for importing: fromlist needs to be non-empty.
abbrev_mod = __import__(abbrev_module_name, fromlist=['.'])
f = sys.stdout
# TODO: Remove Any once we stop using it
f.write("""\
from asdl import pybase
from typing import Optional, List, Tuple, Dict, Any, cast, TYPE_CHECKING
""")
if schema_ast.uses:
f.write('\n')
f.write('if TYPE_CHECKING:\n')
for use in schema_ast.uses:
py_names = [asdl_.TypeNameHeuristic(n) for n in use.type_names]
# indented
f.write(' from _devbuild.gen.%s_asdl import %s\n' % (
use.mod_name, ', '.join(py_names)))
f.write('\n')
for typ in app_types.itervalues():
if isinstance(typ, UserType):
f.write('from _devbuild.gen.%s import %s\n' % (
typ.mod_name, typ.type_name))
# HACK
f.write('from _devbuild.gen.%s import Id_str\n' % typ.mod_name)
f.write('\n')
pretty_print_methods = bool(os.getenv('PRETTY_PRINT_METHODS', 'yes'))
optional_fields = bool(os.getenv('OPTIONAL_FIELDS', 'yes'))
if pretty_print_methods:
f.write("""
from asdl import runtime # For runtime.NO_SPID
from asdl.runtime import NewRecord, NewLeaf
from _devbuild.gen.hnode_asdl import color_e, hnode, hnode_e, hnode_t, field
""")
abbrev_mod_entries = dir(abbrev_mod) if abbrev_mod else []
v = gen_python.GenMyPyVisitor(f, abbrev_mod_entries,
pretty_print_methods=pretty_print_methods,
optional_fields=optional_fields,
simple_int_sums=_SIMPLE)
v.VisitModule(schema_ast)
if abbrev_mod:
f.write("""\
#
# CONCATENATED FILE
#
""")
package, module = abbrev_module_name.split('.')
path = os.path.join(package, module + '.py')
with open(path) as in_f:
f.write(in_f.read())
else:
raise RuntimeError('Invalid action %r' % action)
if __name__ == '__main__':
try:
main(sys.argv)
except RuntimeError as e:
print('FATAL: %s' % e, file=sys.stderr)
sys.exit(1)