@@ -60,6 +60,9 @@ def reflow_lines(s, depth):
60
60
lines .append (padding + cur )
61
61
return lines
62
62
63
+ def reflow_c_string (s , depth ):
64
+ return '"%s"' % s .replace ('\n ' , '\\ n"\n %s"' % (' ' * depth * TABSIZE ))
65
+
63
66
def is_simple (sum ):
64
67
"""Return True if a sum is a simple.
65
68
@@ -71,6 +74,21 @@ def is_simple(sum):
71
74
return False
72
75
return True
73
76
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 )
74
92
75
93
class EmitVisitor (asdl .VisitorBase ):
76
94
"""Visit that emits lines"""
@@ -764,7 +782,7 @@ def visitModule(self, mod):
764
782
};
765
783
766
784
static 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 )
768
786
{
769
787
PyObject *fnames, *result;
770
788
int i;
@@ -778,11 +796,12 @@ def visitModule(self, mod):
778
796
}
779
797
PyTuple_SET_ITEM(fnames, i, field);
780
798
}
781
- result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOO }",
799
+ result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){OOOOOs }",
782
800
type, base,
783
801
astmodulestate_global->_fields, fnames,
784
802
astmodulestate_global->__module__,
785
- astmodulestate_global->_ast);
803
+ astmodulestate_global->_ast,
804
+ astmodulestate_global->__doc__, doc);
786
805
Py_DECREF(fnames);
787
806
return result;
788
807
}
@@ -947,8 +966,9 @@ def visitProduct(self, prod, name):
947
966
fields = name + "_fields"
948
967
else :
949
968
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, ' %
951
970
(name , name , fields , len (prod .fields )), 1 )
971
+ self .emit ('%s);' % reflow_c_string (asdl_of (name , prod ), 2 ), 2 , reflow = False )
952
972
self .emit ("if (!state->%s_type) return 0;" % name , 1 )
953
973
self .emit_type ("AST_type" )
954
974
self .emit_type ("%s_type" % name )
@@ -961,8 +981,9 @@ def visitProduct(self, prod, name):
961
981
self .emit_defaults (name , prod .attributes , 1 )
962
982
963
983
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, ' %
965
985
(name , name ), 1 )
986
+ self .emit ('%s);' % reflow_c_string (asdl_of (name , sum ), 2 ), 2 , reflow = False )
966
987
self .emit_type ("%s_type" % name )
967
988
self .emit ("if (!state->%s_type) return 0;" % name , 1 )
968
989
if sum .attributes :
@@ -980,8 +1001,9 @@ def visitConstructor(self, cons, name, simple):
980
1001
fields = cons .name + "_fields"
981
1002
else :
982
1003
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, ' %
984
1005
(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 )
985
1007
self .emit ("if (!state->%s_type) return 0;" % cons .name , 1 )
986
1008
self .emit_type ("%s_type" % cons .name )
987
1009
self .emit_defaults (cons .name , cons .fields , 1 )
@@ -1279,8 +1301,15 @@ def generate_module_def(f, mod):
1279
1301
visitor .visit (mod )
1280
1302
visitor_list .add (visitor )
1281
1303
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 ()
1284
1313
for visitor in visitor_list :
1285
1314
for identifier in visitor .identifiers :
1286
1315
module_state .add (identifier )
0 commit comments