Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: Parser/asdl_c.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Objects/unicodeobject.c ('k') | Parser/Python.asdl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 """Generate C code from an ASDL description.""" 2 """Generate C code from an ASDL description."""
3 3
4 # TO DO 4 # TO DO
5 # handle fields that have a type but no name 5 # handle fields that have a type but no name
6 6
7 import os, sys 7 import os, sys
8 import subprocess 8 import subprocess
9 9
10 import asdl 10 import asdl
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 { 788 {
789 if (obj == Py_None) 789 if (obj == Py_None)
790 obj = NULL; 790 obj = NULL;
791 if (obj) 791 if (obj)
792 PyArena_AddPyObject(arena, obj); 792 PyArena_AddPyObject(arena, obj);
793 Py_XINCREF(obj); 793 Py_XINCREF(obj);
794 *out = obj; 794 *out = obj;
795 return 0; 795 return 0;
796 } 796 }
797 797
798 static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena) 798 #define obj2ast_identifier obj2ast_object
799 { 799 #define obj2ast_string obj2ast_object
800 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
801 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
802 return 1;
803 }
804 return obj2ast_object(obj, out, arena);
805 }
806
807 static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
808 {
809 if (!PyUnicode_CheckExact(obj)) {
810 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
811 return 1;
812 }
813 return obj2ast_object(obj, out, arena);
814 }
815 800
816 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena) 801 static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
817 { 802 {
818 int i; 803 int i;
819 if (!PyLong_Check(obj)) { 804 if (!PyLong_Check(obj)) {
820 PyObject *s = PyObject_Repr(obj); 805 PyObject *s = PyObject_Repr(obj);
821 if (s == NULL) return 1; 806 if (s == NULL) return 1;
822 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s", 807 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
823 PyBytes_AS_STRING(s)); 808 PyBytes_AS_STRING(s));
824 Py_DECREF(s); 809 Py_DECREF(s);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 self.emit("PyMODINIT_FUNC", 0) 892 self.emit("PyMODINIT_FUNC", 0)
908 self.emit("PyInit__ast(void)", 0) 893 self.emit("PyInit__ast(void)", 0)
909 self.emit("{", 0) 894 self.emit("{", 0)
910 self.emit("PyObject *m, *d;", 1) 895 self.emit("PyObject *m, *d;", 1)
911 self.emit("if (!init_types()) return NULL;", 1) 896 self.emit("if (!init_types()) return NULL;", 1)
912 self.emit('m = PyModule_Create(&_astmodule);', 1) 897 self.emit('m = PyModule_Create(&_astmodule);', 1)
913 self.emit("if (!m) return NULL;", 1) 898 self.emit("if (!m) return NULL;", 1)
914 self.emit("d = PyModule_GetDict(m);", 1) 899 self.emit("d = PyModule_GetDict(m);", 1)
915 self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;', 1) 900 self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;', 1)
916 self.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST ) < 0)', 1) 901 self.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST ) < 0)', 1)
902 self.emit("return NULL;", 2)
903 # Value of version: "$Revision$"
904 self.emit('if (PyModule_AddStringConstant(m, "__version__", "%s") < 0)'
905 % (mod.version,), 1)
917 self.emit("return NULL;", 2) 906 self.emit("return NULL;", 2)
918 for dfn in mod.dfns: 907 for dfn in mod.dfns:
919 self.visit(dfn) 908 self.visit(dfn)
920 self.emit("return m;", 1) 909 self.emit("return m;", 1)
921 self.emit("}", 0) 910 self.emit("}", 0)
922 911
923 def visitProduct(self, prod, name): 912 def visitProduct(self, prod, name):
924 self.addObj(name) 913 self.addObj(name)
925 914
926 def visitSum(self, sum, name): 915 def visitSum(self, sum, name):
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
1127 class ChainOfVisitors: 1116 class ChainOfVisitors:
1128 def __init__(self, *visitors): 1117 def __init__(self, *visitors):
1129 self.visitors = visitors 1118 self.visitors = visitors
1130 1119
1131 def visit(self, object): 1120 def visit(self, object):
1132 for v in self.visitors: 1121 for v in self.visitors:
1133 v.visit(object) 1122 v.visit(object)
1134 v.emit("", 0) 1123 v.emit("", 0)
1135 1124
1136 common_msg = "/* File automatically generated by %s. */\n\n" 1125 common_msg = "/* File automatically generated by %s. */\n\n"
1126
1127 c_file_msg = """
1128 /*
1129 __version__ %s.
1130
1131 This module must be committed separately after each AST grammar change;
1132 The __version__ number is set to the revision number of the commit
1133 containing the grammar change.
1134 */
1135
1136 """
1137
1138
1139 def get_file_revision(f):
1140 """Fish out the last change to a file in hg."""
1141 args = ["hg", "log", "--template", "{node|short}", "--limit", "1", f]
1142 p = subprocess.Popen(args, stdout=subprocess.PIPE)
1143 out = p.communicate()[0]
1144 if p.returncode:
1145 print >> sys.stderr, "error return code from hg"
1146 sys.exit(1)
1147 return out
1148
1137 1149
1138 def main(srcfile): 1150 def main(srcfile):
1139 argv0 = sys.argv[0] 1151 argv0 = sys.argv[0]
1140 components = argv0.split(os.sep) 1152 components = argv0.split(os.sep)
1141 argv0 = os.sep.join(components[-2:]) 1153 argv0 = os.sep.join(components[-2:])
1142 auto_gen_msg = common_msg % argv0 1154 auto_gen_msg = common_msg % argv0
1143 mod = asdl.parse(srcfile) 1155 mod = asdl.parse(srcfile)
1144 if not asdl.check(mod): 1156 if not asdl.check(mod):
1145 sys.exit(1) 1157 sys.exit(1)
1158 mod.version = get_file_revision(srcfile)
1146 if INC_DIR: 1159 if INC_DIR:
1147 p = "%s/%s-ast.h" % (INC_DIR, mod.name) 1160 p = "%s/%s-ast.h" % (INC_DIR, mod.name)
1148 f = open(p, "w") 1161 f = open(p, "w")
1149 f.write(auto_gen_msg) 1162 f.write(auto_gen_msg)
1150 f.write('#include "asdl.h"\n\n') 1163 f.write('#include "asdl.h"\n\n')
1151 c = ChainOfVisitors(TypeDefVisitor(f), 1164 c = ChainOfVisitors(TypeDefVisitor(f),
1152 StructVisitor(f), 1165 StructVisitor(f),
1153 PrototypeVisitor(f), 1166 PrototypeVisitor(f),
1154 ) 1167 )
1155 c.visit(mod) 1168 c.visit(mod)
1156 f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") 1169 f.write("PyObject* PyAST_mod2obj(mod_ty t);\n")
1157 f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\ n") 1170 f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\ n")
1158 f.write("int PyAST_Check(PyObject* obj);\n") 1171 f.write("int PyAST_Check(PyObject* obj);\n")
1159 f.close() 1172 f.close()
1160 1173
1161 if SRC_DIR: 1174 if SRC_DIR:
1162 p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") 1175 p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c")
1163 f = open(p, "w") 1176 f = open(p, "w")
1164 f.write(auto_gen_msg) 1177 f.write(auto_gen_msg)
1178 f.write(c_file_msg % (mod.version,))
1165 f.write('#include "Python.h"\n') 1179 f.write('#include "Python.h"\n')
1166 f.write('#include "%s-ast.h"\n' % mod.name) 1180 f.write('#include "%s-ast.h"\n' % mod.name)
1167 f.write('\n') 1181 f.write('\n')
1168 f.write("static PyTypeObject AST_type;\n") 1182 f.write("static PyTypeObject AST_type;\n")
1169 v = ChainOfVisitors( 1183 v = ChainOfVisitors(
1170 PyTypesDeclareVisitor(f), 1184 PyTypesDeclareVisitor(f),
1171 PyTypesVisitor(f), 1185 PyTypesVisitor(f),
1172 Obj2ModPrototypeVisitor(f), 1186 Obj2ModPrototypeVisitor(f),
1173 FunctionVisitor(f), 1187 FunctionVisitor(f),
1174 ObjVisitor(f), 1188 ObjVisitor(f),
(...skipping 16 matching lines...) Expand all
1191 sys.exit(1) 1205 sys.exit(1)
1192 for o, v in opts: 1206 for o, v in opts:
1193 if o == '-h': 1207 if o == '-h':
1194 INC_DIR = v 1208 INC_DIR = v
1195 if o == '-c': 1209 if o == '-c':
1196 SRC_DIR = v 1210 SRC_DIR = v
1197 if len(args) != 1: 1211 if len(args) != 1:
1198 sys.stdout.write("Must specify single input file\n") 1212 sys.stdout.write("Must specify single input file\n")
1199 sys.exit(1) 1213 sys.exit(1)
1200 main(args[0]) 1214 main(args[0])
OLDNEW
« no previous file with comments | « Objects/unicodeobject.c ('k') | Parser/Python.asdl » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+