def main(argv): #print lex_mode_e #print dir(lex_mode_e) with open('osh/osh.asdl') as f: asdl_module, _ = ast.LoadSchema(f) # TODO: Generate C files for lex_mode_e, id_e, etc. # It already works for lex_mode. Except it's C++. Maybe write one that's C? # # C doesn't have namespaces! # lex_mode_e__NONE? # lex_mode_e__OUTER? # The re2c code will have a switch statement for that? # # Or maybe just lex_mode__NONE. # # And then it will output id__Lit_Chars ? # This is all generated so we can change it at any time. # Eventually you may also generate code to map Id -> Kind? But right now you # just need Id for the lexer! #print asdl_module #print gen_cpp v = gen_cpp.CEnumVisitor(sys.stdout) v.VisitModule(asdl_module)
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') # To avoid circular dependencies, don't load Id for types.asdl. if os.path.basename(schema_path) == 'types.asdl': app_types = {} else: from core.meta import Id app_types = {'id': runtime.UserType(Id)} 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 == 'py': # Generate Python code so we don't depend on ASDL schemas pickle_out_path = argv[3] with open(schema_path) as f: schema_ast, type_lookup = front_end.LoadSchema(f, app_types) f = sys.stdout f.write("""\ from asdl import const # For const.NO_INTEGER from asdl import runtime from pylib import unpickle from core import util f = util.GetResourceLoader().open('%s') TYPE_LOOKUP = unpickle.load_v2_subset(f) f.close() """ % pickle_out_path) v = gen_python.GenClassesVisitor(f) v.VisitModule(schema_ast) if pickle_out_path: # Pickle version 2 is better. (Pickle version 0 uses # s.decode('string-escape')! ) # In version 2, now I have 16 opcodes + STOP. with open(pickle_out_path, 'w') as f: pickle.dump(type_lookup, f, protocol=2) from core.util import log log('Wrote %s', pickle_out_path) else: raise RuntimeError('Invalid action %r' % action)
def main(argv): #print lex_mode_e #print dir(lex_mode_e) app_types = {'id': asdl.UserType(Id)} with open('osh/types.asdl') as f: asdl_module, _ = asdl.LoadSchema(f, app_types) v = gen_cpp.CEnumVisitor(sys.stdout) v.VisitModule(asdl_module)
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': meta.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')) with open(schema_path) as f: schema_ast, type_lookup = 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("""\ #ifndef %s #define %s """ % (guard, guard)) f.write("""\ #include <cstdint> #include "mylib.h" // for Str, List, etc. """) if pretty_print_methods: f.write(""" #include "hnode_asdl.h" using hnode_asdl::hnode_t; """) f.write("""\ namespace %s { """ % ns) v = gen_cpp.ForwardDeclareVisitor(f) v.VisitModule(schema_ast) v2 = gen_cpp.ClassDefVisitor( f, type_lookup, pretty_print_methods=pretty_print_methods) v2.VisitModule(schema_ast) f.write(""" } // namespace %s #endif // %s """ % (ns, guard)) with open(out_prefix + '.cc', 'w') as f: # HACK until we support 'use' if schema_filename == 'syntax.asdl': f.write('#include "id_kind_asdl.h" // hack\n') f.write('using id_kind_asdl::Id_t; // hack\n') f.write(""" #include <assert.h> #include "hnode_asdl.h" 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; // TODO: Generate this asdl/runtime.h header and include it? namespace runtime { // declare hnode_asdl::hnode__Record* NewRecord(Str* node_type); hnode_asdl::hnode__Leaf* NewLeaf(Str* s, hnode_asdl::color_t e_color); extern Str* TRUE_STR; extern Str* FALSE_STR; } // declare namespace runtime """) f.write(""" #include "%s.h" namespace %s { """ % (ns, ns)) v3 = gen_cpp.MethodDefVisitor( f, type_lookup, pretty_print_methods=pretty_print_methods) v3.VisitModule(schema_ast) f.write(""" } // namespace %s """ % ns) elif action == 'mypy': # Generated typed MyPy code with open(schema_path) as f: schema_ast, type_lookup = 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 for typ in app_types.itervalues(): if isinstance(typ, meta.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') # NOTE: Dict, Any are for AssocArray with 'dict' type. f.write("""\ from asdl import pybase from typing import Optional, List, Tuple, Dict, Any, cast """) 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, type_lookup, abbrev_mod_entries, pretty_print_methods=pretty_print_methods, optional_fields=optional_fields) 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)
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')) 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)) f.write("""\ #include <cstdint> #include "mylib.h" // for Str, List, etc. """) if pretty_print_methods: f.write("""\ #include "hnode_asdl.h" using hnode_asdl::hnode_t; """) if app_types: f.write("""\ #include "id_kind_asdl.h" using id_kind_asdl::Id_t; """) 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)
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') if os.path.basename(schema_path) == 'types.asdl': app_types = {} else: app_types = {'id': meta.UserType('id_kind_asdl', 'Id_t')} 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 == 'mypy': # typed mypy with open(schema_path) as f: schema_ast, type_lookup = 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 for typ in app_types.itervalues(): if isinstance(typ, meta.UserType): f.write('from _devbuild.gen.%s import %s\n' % (typ.mod_name, typ.type_name)) f.write('\n') f.write("""\ from asdl import const # For const.NO_INTEGER from asdl import runtime PrettyLeaf = runtime.PrettyLeaf PrettyArray = runtime.PrettyArray PrettyNode = runtime.PrettyNode Color_TypeName = runtime.Color_TypeName Color_StringConst = runtime.Color_StringConst Color_OtherConst = runtime.Color_OtherConst Color_UserType = runtime.Color_UserType from typing import Optional, List, Tuple """) abbrev_mod_entries = dir(abbrev_mod) if abbrev_mod else [] v = gen_python.GenMyPyVisitor(f, type_lookup, abbrev_mod_entries) 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)
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') if os.path.basename(schema_path) in ('syntax.asdl', 'runtime.asdl'): app_types = {'id': meta.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 with open(schema_path) as f: schema_ast, type_lookup = front_end.LoadSchema(f, app_types) # asdl/typed_arith.asdl -> typed_arith_asdl ns = os.path.basename(schema_path).replace('.', '_') f = sys.stdout f.write("""\ #include <cstdint> #include "runtime.h" // for Str, List, etc. namespace %s { """ % ns) v = gen_cpp.ForwardDeclareVisitor(f) v.VisitModule(schema_ast) v2 = gen_cpp.ClassDefVisitor(f, type_lookup) v2.VisitModule(schema_ast) f.write('} // namespace %s\n' % ns) elif action == 'mypy': # Generated typed MyPy code with open(schema_path) as f: schema_ast, type_lookup = 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 for typ in app_types.itervalues(): if isinstance(typ, meta.UserType): f.write('from _devbuild.gen.%s import %s\n' % (typ.mod_name, typ.type_name)) f.write('\n') # NOTE: Dict, Any are for AssocArray with 'dict' type. f.write("""\ from asdl import const # For const.NO_INTEGER from asdl import runtime from asdl.runtime import ( PrettyLeaf, PrettyArray, PrettyNode, Color_TypeName, Color_StringConst, Color_OtherConst, Color_UserType, ) from typing import Optional, List, Tuple, Dict, Any """) abbrev_mod_entries = dir(abbrev_mod) if abbrev_mod else [] v = gen_python.GenMyPyVisitor(f, type_lookup, abbrev_mod_entries) 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)