def main(outdir, grammar="Python.asdl"): mod = asdl.parse(grammar) if not asdl.check(mod): sys.exit(1) c = ChainOfVisitors(AnalyzeVisitor(outdir), JavaVisitor(outdir), VisitorVisitor(outdir)) c.visit(mod)
def main(asdlfile, outputfile): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) auto_gen_msg = common_msg % argv0 mod = asdl.parse(asdlfile) if not asdl.check(mod): sys.exit(1) f = open(outputfile, "w") f.write(auto_gen_msg) f.write("/* Object that holds all nodes */\n"); f.write("Sk.astnodes = {};\n\n"); c = ChainOfVisitors(TypeDefVisitor(f), ) c.visit(mod) f.write("\n"*5) f.write("/* ---------------------- */\n") f.write("/* constructors for nodes */\n") f.write("/* ---------------------- */\n") f.write("\n"*5) v = ChainOfVisitors( FunctionVisitor(f), FieldNamesVisitor(f), ) v.visit(mod) f.write('Sk.exportSymbol("Sk.astnodes", Sk.astnodes);\n'); f.close()
def main(srcfile, dump_module=False): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) auto_gen_msg = common_msg % argv0 mod = asdl.parse(srcfile) if dump_module: print('Parsed Module:') print(mod) if not asdl.check(mod): sys.exit(1) if H_FILE: with open(H_FILE, "w") as f: f.write(auto_gen_msg) f.write('#ifndef Py_PYTHON_AST_H\n') f.write('#define Py_PYTHON_AST_H\n') f.write('#ifdef __cplusplus\n') f.write('extern "C" {\n') f.write('#endif\n') f.write('\n') f.write('#include "asdl.h"\n') f.write('\n') f.write('#undef Yield /* undefine macro conflicting with <winbase.h> */\n') f.write('\n') c = ChainOfVisitors(TypeDefVisitor(f), StructVisitor(f)) c.visit(mod) f.write("// Note: these macros affect function definitions, not only call sites.\n") PrototypeVisitor(f).visit(mod) f.write("\n") f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") f.write("int PyAST_Check(PyObject* obj);\n") f.write('\n') f.write('#ifdef __cplusplus\n') f.write('}\n') f.write('#endif\n') f.write('#endif /* !Py_PYTHON_AST_H */\n') if C_FILE: with open(C_FILE, "w") as f: f.write(auto_gen_msg) f.write('#include <stddef.h>\n') f.write('\n') f.write('#include "Python.h"\n') f.write('#include "%s-ast.h"\n' % mod.name) f.write('\n') f.write("static PyTypeObject AST_type;\n") v = ChainOfVisitors( PyTypesDeclareVisitor(f), PyTypesVisitor(f), Obj2ModPrototypeVisitor(f), FunctionVisitor(f), ObjVisitor(f), Obj2ModVisitor(f), ASTModuleVisitor(f), PartingShots(f), ) v.visit(mod)
def main(asdlfile, outputfile): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) auto_gen_msg = common_msg % argv0 mod = asdl.parse(asdlfile) if not asdl.check(mod): sys.exit(1) f = open(outputfile, "wb") f.write(auto_gen_msg) c = ChainOfVisitors(TypeDefVisitor(f), ) c.visit(mod) f.write("\n"*5) f.write("/* ---------------------- */\n") f.write("/* constructors for nodes */\n") f.write("/* ---------------------- */\n") f.write("\n"*5) v = ChainOfVisitors( FunctionVisitor(f), FieldNamesVisitor(f), ) v.visit(mod) f.close()
def main(srcfile): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) auto_gen_msg = common_msg % argv0 mod = asdl.parse(srcfile) if not asdl.check(mod): sys.exit(1) if INC_DIR: p = "%s/%s-ast.h" % (INC_DIR, mod.name) f = open(p, "wb") f.write(auto_gen_msg) f.write('#include "asdl.h"\n\n') f.write('#ifdef __cplusplus\n' 'extern "C" {\n' '#endif\n\n') c = ChainOfVisitors( TypeDefVisitor(f), StructVisitor(f), PrototypeVisitor(f), ) c.visit(mod) f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") f.write( "mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") f.write("int PyAST_Check(PyObject* obj);\n") f.write('\n#ifdef __cplusplus\n' '}\n' # To end the extern "C" '#endif\n') f.close() if SRC_DIR: p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") f = open(p, "wb") f.write(auto_gen_msg) f.write(c_file_msg % parse_version(mod)) f.write('#include "Python.h"\n') f.write('#include "%s-ast.h"\n' % mod.name) f.write('\n') f.write("static PyTypeObject AST_type;\n") v = ChainOfVisitors( PyTypesDeclareVisitor(f), PyTypesVisitor(f), Obj2ModPrototypeVisitor(f), FunctionVisitor(f), ObjVisitor(f), Obj2ModVisitor(f), ASTModuleVisitor(f), PartingShots(f), ) v.visit(mod) f.close()
def main(srcfile): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) auto_gen_msg = common_msg % argv0 mod = asdl.parse(srcfile) if not asdl.check(mod): sys.exit(1) if INC_DIR: p = "%s/%s-ast.h" % (INC_DIR, mod.name) f = open(p, "wb") f.write(auto_gen_msg) f.write('#include "asdl.h"\n\n') f.write('#ifdef __cplusplus\n' 'extern "C" {\n' '#endif\n\n') c = ChainOfVisitors(TypeDefVisitor(f), StructVisitor(f), PrototypeVisitor(f), ) c.visit(mod) f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") f.write("int PyAST_Check(PyObject* obj);\n") f.write('\n#ifdef __cplusplus\n' '}\n' # To end the extern "C" '#endif\n') f.close() if SRC_DIR: p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") f = open(p, "wb") f.write(auto_gen_msg) f.write(c_file_msg % parse_version(mod)) f.write('#include "Python.h"\n') f.write('#include "%s-ast.h"\n' % mod.name) f.write('\n') f.write("static PyTypeObject AST_type;\n") v = ChainOfVisitors( PyTypesDeclareVisitor(f), PyTypesVisitor(f), Obj2ModPrototypeVisitor(f), FunctionVisitor(f), ObjVisitor(f), Obj2ModVisitor(f), ASTModuleVisitor(f), PartingShots(f), ) v.visit(mod) f.close()
def main(input_file, c_file, h_file, dump_module=False): auto_gen_msg = AUTOGEN_MESSAGE.format("/".join(Path(__file__).parts[-2:])) mod = asdl.parse(input_file) if dump_module: print('Parsed Module:') print(mod) if not asdl.check(mod): sys.exit(1) for file, writer in (c_file, write_source), (h_file, write_header): if file is not None: with file.open("w") as f: f.write(auto_gen_msg) writer(f, mod) print(file, "regenerated.")
def main(srcfile, output_header, output_file): # determine "auto gen" message to appear at top of output argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) common_msg = "/* File automatically generated by %s. */\n\n" auto_gen_msg = common_msg % argv0 # parse the ASDL file mod = asdl.parse(srcfile) if not asdl.check(mod): sys.exit(1) # write the header f = open(output_header, "w") f.write(auto_gen_msg) f.write("#pragma once\n\n") f.write('#include <vector>\n') f.write('#include <string>\n') f.write('#include <any>\n') c = ChainOfVisitors( NamespaceVisitor(f), TypeDefVisitor(f), StructVisitor(f), BaseVisitorVistor(f), InheritanceVisitor(f), PrototypeVisitor(f), ToStringPrototypeVisitor(f), ) c.visit(mod) f.write("}\n") f.close() # write the output f = open(output_file, "w") f.write(auto_gen_msg) f.write('#include <set>\n') f.write('#include <sstream>\n\n') f.write('#include "%s"\n\n' % output_header) c = ChainOfVisitors( NamespaceVisitor(f), FunctionVisitor(f), ToStringVisitorVisitor(f), ToStringFunctionVisitor(f), ) c.visit(mod) f.write("}\n") f.close()
def main(srcfile, dump_module=False): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) auto_gen_msg = common_msg % argv0 mod = asdl.parse(srcfile) if dump_module: print('Parsed Module:') print(mod) if not asdl.check(mod): sys.exit(1) if INC_DIR: p = "%s/%s-ast.h" % (INC_DIR, mod.name) f = open(p, "w") f.write(auto_gen_msg) f.write('#include "asdl.h"\n\n') c = ChainOfVisitors(TypeDefVisitor(f), StructVisitor(f), PrototypeVisitor(f), ) c.visit(mod) f.write("PyObject* Ta35AST_mod2obj(mod_ty t);\n") f.write("mod_ty Ta35AST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") f.write("int Ta35AST_Check(PyObject* obj);\n") f.close() if SRC_DIR: p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") f = open(p, "w") f.write(auto_gen_msg) f.write('#include <stddef.h>\n') f.write('\n') f.write('#include "Python.h"\n') f.write('#include "%s-ast.h"\n' % mod.name) f.write('\n') f.write("static PyTypeObject AST_type;\n") v = ChainOfVisitors( PyTypesDeclareVisitor(f), PyTypesVisitor(f), Obj2ModPrototypeVisitor(f), FunctionVisitor(f), ObjVisitor(f), Obj2ModVisitor(f), ASTModuleVisitor(f), PartingShots(f), ) v.visit(mod) f.close()
def main(srcfile): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) auto_gen_msg = '/* File automatically generated by %s */\n' % argv0 mod = asdl.parse(srcfile) if not asdl.check(mod): sys.exit(1) if INC_DIR: p = "%s/%s-ast.h" % (INC_DIR, mod.name) f = open(p, "wb") print >> f, auto_gen_msg print >> f, '#ifndef %s_AST_H' % str(mod.name).upper() print >> f, '#define %s_AST_H\n' % str(mod.name).upper() print >> f, '#include "asdl.h"\n' c = ChainOfVisitors(TypeDefVisitor(f), StructVisitor(f), PrototypePreamble(f), PrototypeVisitor(f), PrototypePostamble(f)) c.visit(mod) print >> f, "PyObject* PyAST_mod2obj(mod_ty t);" print >> f, "\n#endif" f.close() if SRC_DIR: p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") f = open(p, "wb") print >> f, auto_gen_msg print >> f, '#include "Python.h"' print >> f, '#include "%s-ast.h"' % mod.name print >> f print >> f, "static PyTypeObject* AST_type;" v = ChainOfVisitors( PyTypesDeclareVisitor(f), PyTypesVisitor(f), FunctionVisitor(f), ObjVisitor(f), ASTModuleVisitor(f), PartingShots(f), ) v.visit(mod) f.close()
def main(input_filename, ast_mod_filename, ast_def_filename, dump_module=False): auto_gen_msg = AUTOGEN_MESSAGE.format("/".join(Path(__file__).parts[-2:])) mod = asdl.parse(input_filename) if dump_module: print('Parsed Module:') print(mod) if not asdl.check(mod): sys.exit(1) typeinfo = {} FindUserdataTypesVisitor(typeinfo).visit(mod) with ast_def_filename.open("w") as def_file, \ ast_mod_filename.open("w") as mod_file: def_file.write(auto_gen_msg) write_ast_def(mod, typeinfo, def_file) mod_file.write(auto_gen_msg) write_ast_mod(mod, mod_file) print(f"{ast_def_filename}, {ast_mod_filename} regenerated.")
def main(srcfile): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) auto_gen_msg = '/* File automatically generated by %s */\n' % argv0 mod = asdl.parse(srcfile) if not asdl.check(mod): sys.exit(1) if INC_DIR: p = "%s/%s-ast.h" % (INC_DIR, mod.name) f = open(p, "wb") print >> f, auto_gen_msg print >> f, '#include "asdl.h"\n' c = ChainOfVisitors(TypeDefVisitor(f), StructVisitor(f), PrototypeVisitor(f), ) c.visit(mod) print >>f, "PyObject* PyAST_mod2obj(mod_ty t);" f.close() if SRC_DIR: p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") f = open(p, "wb") print >> f, auto_gen_msg print >> f, '#include "Python.h"' print >> f, '#include "%s-ast.h"' % mod.name print >> f print >>f, "static PyTypeObject* AST_type;" v = ChainOfVisitors( PyTypesDeclareVisitor(f), PyTypesVisitor(f), FunctionVisitor(f), ObjVisitor(f), ASTModuleVisitor(f), PartingShots(f), ) v.visit(mod) f.close()
def main(srcfile): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) auto_gen_msg = common_msg % argv0 mod = asdl.parse(srcfile) if not asdl.check(mod): sys.exit(1) p = "%s-ast.go" % mod.name f = open(p, "w") f.write(auto_gen_msg) f.write('package ast\n') f.write('import "github.com/go-python/gpython/py"\n') c = ChainOfVisitors(TypeDefVisitor(f), StructVisitor(f), PrototypeVisitor(f), ) c.visit(mod) #f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") #f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") #f.write("int PyAST_Check(PyObject* obj);\n") #f.write("PyTypeObject AST_type;\n") v = ChainOfVisitors( PyTypesDeclareVisitor(f), PyTypesVisitor(f), Obj2ModPrototypeVisitor(f), FunctionVisitor(f), ### FIXME ObjVisitor(f), ### FIXME Obj2ModVisitor(f), ### FIXME ASTModuleVisitor(f), ### FIXME PartingShots(f), ) v.visit(mod) f.close() subprocess.check_call(["gofmt", "-w", p])
def setUpClass(cls): # Parse Python.asdl into a ast.Module and run the check on it. # There's no need to do this for each test method, hence setUpClass. cls.mod = asdl.parse('./Python.asdl') cls.assertTrue(asdl.check(cls.mod), 'Module validation failed')
def main(srcfile, dump_module=False): argv0 = sys.argv[0] components = argv0.split(os.sep) argv0 = os.sep.join(components[-2:]) mod = asdl.parse(srcfile) if dump_module: print('Parsed Module:') print(mod) if not asdl.check(mod): sys.exit(1) if H_FILE: with open(H_FILE, "w") as f: f.write("\ #ifndef COSMOPOLITAN_THIRD_PARTY_PYTHON_INCLUDE_PYTHON_AST_H_\n\ #define COSMOPOLITAN_THIRD_PARTY_PYTHON_INCLUDE_PYTHON_AST_H_\n\ #include \"third_party/python/Include/asdl.h\"\n\ #if !(__ASSEMBLER__ + __LINKER__ + 0)\n\ COSMOPOLITAN_C_START_\n\ /* clang-format off */\n\ /* File automatically generated by %s. */\n\ \n\ " % argv0) c = ChainOfVisitors( TypeDefVisitor(f), StructVisitor(f), PrototypeVisitor(f), ) c.visit(mod) f.write("\ PyObject* PyAST_mod2obj(mod_ty t);\n\ mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n\ int PyAST_Check(PyObject* obj);\n\ \n\ COSMOPOLITAN_C_END_\n\ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */\n\ #endif /* COSMOPOLITAN_THIRD_PARTY_PYTHON_INCLUDE_PYTHON_AST_H_ */\n") if C_FILE: with open(C_FILE, "w") as f: f.write('\ #include "third_party/python/Include/%s-ast.h"\n\ #include "third_party/python/Include/abstract.h"\n\ #include "third_party/python/Include/boolobject.h"\n\ #include "third_party/python/Include/descrobject.h"\n\ #include "third_party/python/Include/dictobject.h"\n\ #include "third_party/python/Include/listobject.h"\n\ #include "third_party/python/Include/longobject.h"\n\ #include "third_party/python/Include/modsupport.h"\n\ #include "third_party/python/Include/object.h"\n\ #include "third_party/python/Include/objimpl.h"\n\ #include "third_party/python/Include/pyerrors.h"\n\ #include "third_party/python/Include/pythonrun.h"\n\ #include "third_party/python/Include/tupleobject.h"\n\ #include "third_party/python/Include/yoink.h"\n\ /* clang-format off */\n\ \n\ PYTHON_PROVIDE("_ast");\n\ \n\ /* File automatically generated by %s. */\n\ \n\ static PyTypeObject AST_type;\n\ ' % (mod.name, argv0)) v = ChainOfVisitors( PyTypesDeclareVisitor(f), PyTypesVisitor(f), Obj2ModPrototypeVisitor(f), FunctionVisitor(f), ObjVisitor(f), Obj2ModVisitor(f), ASTModuleVisitor(f), PartingShots(f), ) v.visit(mod)
def visitSum(self, sum, name, depth): if not sum.simple: for t in sum.types: self.visit(t, name, depth) def visitProduct(self, product, name, depth): pass def visitConstructor(self, cons, name, depth): self.ctors.append(cons.name) class ChainOfVisitors: def __init__(self, *visitors): self.visitors = visitors def visit(self, object): for v in self.visitors: v.visit(object) if __name__ == "__main__": if len(sys.argv) < 2: sys.argv.append('Python.asdl') mod = asdl.parse(sys.argv[1]) if not asdl.check(mod): sys.exit(1) c = ChainOfVisitors(AnalyzeVisitor(), JavaVisitor(), VisitorVisitor()) c.visit(mod)
def setUp(self): self.mod = asdl.parse(os.path.join(test_dir, 'Python.asdl')) self.assertTrue(asdl.check(self.mod), 'Module validation failed') self.types = self.mod.types
def visitSum(self, sum, name, depth): if not sum.simple: for t in sum.types: self.visit(t, name, depth) def visitProduct(self, product, name, depth): pass def visitConstructor(self, cons, name, depth): self.ctors.append(cons.name) class ChainOfVisitors: def __init__(self, *visitors): self.visitors = visitors def visit(self, object): for v in self.visitors: v.visit(object) if __name__ == "__main__": mod = asdl.parse(sys.argv[1]) if not asdl.check(mod): sys.exit(1) c = ChainOfVisitors(AnalyzeVisitor(), JavaVisitor(), VisitorVisitor()) c.visit(mod)