@@ -60,6 +60,9 @@ def reflow_lines(s, depth):
6060 lines .append (padding + cur )
6161 return lines
6262
63+ def reflow_c_string (s , depth ):
64+ return '"%s"' % s .replace ('\n ' , '\\ n"\n %s"' % (' ' * depth * TABSIZE ))
65+
6366def is_simple (sum ):
6467 """Return True if a sum is a simple.
6568
@@ -71,6 +74,21 @@ def is_simple(sum):
7174 return False
7275 return True
7376
77+ def asdl_of (name , obj ):
78+ if isinstance (obj , asdl .Product ) or isinstance (obj , asdl .Constructor ):
79+ fields = ", " .join (map (str , obj .fields ))
80+ if fields :
81+ fields = "({})" .format (fields )
82+ return "{}{}" .format (name , fields )
83+ else :
84+ if is_simple (obj ):
85+ types = " | " .join (type .name for type in obj .types )
86+ else :
87+ sep = "\n {}| " .format (" " * (len (name ) + 1 ))
88+ types = sep .join (
89+ asdl_of (type .name , type ) for type in obj .types
90+ )
91+ return "{} = {}" .format (name , types )
7492
7593class EmitVisitor (asdl .VisitorBase ):
7694 """Visit that emits lines"""
@@ -764,7 +782,7 @@ def visitModule(self, mod):
764782};
765783
766784static PyObject *
767- make_type(const char *type, PyObject* base, const char* const* fields, int num_fields)
785+ make_type(const char *type, PyObject* base, const char* const* fields, int num_fields, const char *doc )
768786{
769787 PyObject *fnames, *result;
770788 int i;
@@ -778,11 +796,12 @@ def visitModule(self, mod):
778796 }
779797 PyTuple_SET_ITEM(fnames, i, field);
780798 }
781- result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO }",
799+ result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOOOs }",
782800 type, base,
783801 astmodulestate_global->_fields, fnames,
784802 astmodulestate_global->__module__,
785- astmodulestate_global->_ast);
803+ astmodulestate_global->_ast,
804+ astmodulestate_global->__doc__, doc);
786805 Py_DECREF(fnames);
787806 return result;
788807}
@@ -947,8 +966,9 @@ def visitProduct(self, prod, name):
947966 fields = name + "_fields"
948967 else :
949968 fields = "NULL"
950- self .emit ('state->%s_type = make_type("%s", state->AST_type, %s, %d); ' %
969+ self .emit ('state->%s_type = make_type("%s", state->AST_type, %s, %d, ' %
951970 (name , name , fields , len (prod .fields )), 1 )
971+ self .emit ('%s);' % reflow_c_string (asdl_of (name , prod ), 2 ), 2 , reflow = False )
952972 self .emit ("if (!state->%s_type) return 0;" % name , 1 )
953973 self .emit_type ("AST_type" )
954974 self .emit_type ("%s_type" % name )
@@ -961,8 +981,9 @@ def visitProduct(self, prod, name):
961981 self .emit_defaults (name , prod .attributes , 1 )
962982
963983 def visitSum (self , sum , name ):
964- self .emit ('state->%s_type = make_type("%s", state->AST_type, NULL, 0); ' %
984+ self .emit ('state->%s_type = make_type("%s", state->AST_type, NULL, 0, ' %
965985 (name , name ), 1 )
986+ self .emit ('%s);' % reflow_c_string (asdl_of (name , sum ), 2 ), 2 , reflow = False )
966987 self .emit_type ("%s_type" % name )
967988 self .emit ("if (!state->%s_type) return 0;" % name , 1 )
968989 if sum .attributes :
@@ -980,8 +1001,9 @@ def visitConstructor(self, cons, name, simple):
9801001 fields = cons .name + "_fields"
9811002 else :
9821003 fields = "NULL"
983- self .emit ('state->%s_type = make_type("%s", state->%s_type, %s, %d); ' %
1004+ self .emit ('state->%s_type = make_type("%s", state->%s_type, %s, %d, ' %
9841005 (cons .name , cons .name , name , fields , len (cons .fields )), 1 )
1006+ self .emit ('%s);' % reflow_c_string (asdl_of (cons .name , cons ), 2 ), 2 , reflow = False )
9851007 self .emit ("if (!state->%s_type) return 0;" % cons .name , 1 )
9861008 self .emit_type ("%s_type" % cons .name )
9871009 self .emit_defaults (cons .name , cons .fields , 1 )
@@ -1279,8 +1301,15 @@ def generate_module_def(f, mod):
12791301 visitor .visit (mod )
12801302 visitor_list .add (visitor )
12811303
1282- state_strings = set (["__dict__" , "_attributes" , "_fields" , "__module__" , "_ast" ])
1283- module_state = set (["__dict__" , "_attributes" , "_fields" , "__module__" , "_ast" ])
1304+ state_strings = {
1305+ "_ast" ,
1306+ "_fields" ,
1307+ "__doc__" ,
1308+ "__dict__" ,
1309+ "__module__" ,
1310+ "_attributes" ,
1311+ }
1312+ module_state = state_strings .copy ()
12841313 for visitor in visitor_list :
12851314 for identifier in visitor .identifiers :
12861315 module_state .add (identifier )
0 commit comments