Move generation of the serialize/deserialize routines into the Type object
authorNick Downing <nick@ndcode.org>
Sun, 27 Jan 2019 00:22:20 +0000 (11:22 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 27 Jan 2019 00:23:49 +0000 (11:23 +1100)
ast.py

diff --git a/ast.py b/ast.py
index 417aafb..6ae9370 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -31,7 +31,8 @@ class Context:
     stack = [],
     classes = [],
     base_classes = None,
-    fields = []
+    fields = [],
+    field_name = ''
   ):
     self.fout = fout
     self.package_name = package_name
@@ -40,6 +41,7 @@ class Context:
     self.classes = classes
     self.base_classes = [{'element.Element': []}] if base_classes is None else base_classes # params
     self.fields = fields
+    self.field_name = field_name
 
 class AST(element.Element):
   # internal classes:
@@ -126,6 +128,170 @@ class AST(element.Element):
       self.repr_serialize(params)
       return 'ast.AST.Type({0:s})'.format(', '.join(params))
     # GENERATE END
+    def generate_serialize(self, context):
+      type = element.to_text(self)
+      if type[:5] == 'list(' and type[-1:] == ')':
+        subtype = type[5:-1]
+        context.fout.write(
+          '''{0:s}  self.set(
+{1:s}    '{2:s}',
+{3:s}    ' '.join([element.serialize_{4:s}(i{5:s}) for i in self.{6:s}])
+{7:s}  )
+'''.format(
+            context.indent,
+            context.indent,
+            context.field_name,
+            context.indent,
+            subtype,
+            ', ref_list' if subtype == 'ref' else '',
+            context.field_name,
+            context.indent
+          )
+        )
+      elif type[:4] == 'set(' and type[-1:] == ')':
+        subtype = type[4:-1]
+        context.fout.write(
+          '''{0:s}  self.set(
+{1:s}    '{2:s}',
+{3:s}    ' '.join([element.serialize_{4:s}(i{5:s}) for i in sorted(self.{6:s})])
+{7:s}  )
+'''.format(
+            context.indent,
+            context.indent,
+            context.field_name,
+            context.indent,
+            subtype,
+            ', ref_list' if subtype == 'ref' else '',
+            context.field_name,
+            context.indent
+          )
+        )
+      else:
+        context.fout.write(
+          '''{0:s}  self.set('{1:s}', element.serialize_{2:s}(self.{3:s}{4:s}))
+'''.format(
+            context.indent,
+            context.field_name,
+            type,
+            context.field_name,
+            ', ref_list' if type == 'ref' else ''
+          )
+        )
+    def generate_deserialize(self, context): 
+      type = element.to_text(self)
+      if type[:5] == 'list(' and type[-1:] == ')':
+        subtype = type[5:-1]
+        context.fout.write(
+          '''{0:s}  self.{1:s} = [
+{2:s}    element.deserialize_{3:s}(i{4:s})
+{5:s}    for i in self.get('{6:s}', '').split()
+{7:s}  ]
+'''.format(
+            context.indent,
+            context.field_name,
+            context.indent,
+            subtype,
+            ', ref_list' if subtype == 'ref' else '',
+            context.indent,
+            context.field_name,
+            context.indent
+          )
+        )
+      elif type[:4] == 'set(' and type[-1:] == ')':
+        subtype = type[4:-1]
+        context.fout.write(
+          '''{0:s}  self.{1:s} = set(
+{2:s}    [
+{3:s}      element.deserialize_{4:s}(i{5:s})
+{6:s}      for i in self.get('{7:s}', '').split()
+{8:s}    ]
+{9:s}  )
+'''.format(
+            context.indent,
+            context.field_name,
+            context.indent,
+            context.indent,
+            subtype,
+            ', ref_list' if subtype == 'ref' else '',
+            context.indent,
+            context.field_name,
+            context.indent,
+            context.indent
+          )
+        )
+      else: 
+        context.fout.write(
+          '''{0:s}  self.{1:s} = element.deserialize_{2:s}(self.get('{3:s}', '{4:s}'){5:s})
+'''.format(
+            context.indent,
+            context.field_name,
+            type,
+            context.field_name,
+            default_value_str[type],
+            ', ref_list' if type == 'ref' else ''
+          )
+        )
+    def generate_repr_serialize(self, context):
+      type = element.to_text(self)
+      if type[:5] == 'list(' and type[-1:] == ')':
+        subtype = type[5:-1]
+        context.fout.write(
+          '''{0:s}  if len(self.{1:s}):
+{2:s}    params.append(
+{3:s}      '{4:s} = [{{0:s}}]'.format(
+{5:s}        ', '.join([repr(i) for i in self.{6:s}])
+{7:s}      )
+{8:s}    )
+'''.format(
+            context.indent,
+            context.field_name,
+            context.indent,
+            context.indent,
+            context.field_name,
+            context.indent,
+            context.field_name,
+            context.indent,
+            context.indent
+          )
+        )
+      elif type[:4] == 'set(' and type[-1:] == ')':
+        subtype = type[4:-1]
+        context.fout.write(
+          '''{0:s}  if len(self.{1:s}):
+{2:s}    params.append(
+{3:s}      '{4:s} = set([{{0:s}}])'.format(
+{5:s}        ', '.join([repr(i) for i in sorted(self.{6:s})])
+{7:s}      )
+{8:s}    )
+'''.format(
+            context.indent,
+            context.field_name,
+            context.indent,
+            context.indent,
+            context.field_name,
+            context.indent,
+            context.field_name,
+            context.indent,
+            context.indent
+          )
+        )
+      else:
+        context.fout.write(
+          '''{0:s}  if self.{1:s} != {2:s}:
+{3:s}    params.append(
+{4:s}      '{5:s} = {{0:s}}'.format(repr(self.{6:s}))
+{7:s}    )
+'''.format(
+            context.indent,
+            context.field_name,
+            default_value[type],
+            context.indent,
+            context.indent,
+            context.field_name,
+            context.field_name,
+            context.indent
+          )
+        )
 
   # syntax classes:
   class BaseClass(element.Element):
@@ -890,115 +1056,16 @@ class AST(element.Element):
 '''.format(context.indent, context.indent, full_base_class)
           )
           for i in context.fields[n_base_fields:]:
-            type = element.to_text(i[0])
-            name = i[1].get_text()
-            if type[:5] == 'list(' and type[-1:] == ')':
-              subtype = type[5:-1]
-              context.fout.write(
-                '''{0:s}  self.set(
-{1:s}    '{2:s}',
-{3:s}    ' '.join([element.serialize_{4:s}(i{5:s}) for i in self.{6:s}])
-{7:s}  )
-'''.format(
-                  context.indent,
-                  context.indent,
-                  name,
-                  context.indent,
-                  subtype,
-                  ', ref_list' if subtype == 'ref' else '',
-                  name,
-                  context.indent
-                )
-              )
-            elif type[:4] == 'set(' and type[-1:] == ')':
-              subtype = type[4:-1]
-              context.fout.write(
-                '''{0:s}  self.set(
-{1:s}    '{2:s}',
-{3:s}    ' '.join([element.serialize_{4:s}(i{5:s}) for i in sorted(self.{6:s})])
-{7:s}  )
-'''.format(
-                  context.indent,
-                  context.indent,
-                  name,
-                  context.indent,
-                  subtype,
-                  ', ref_list' if subtype == 'ref' else '',
-                  name,
-                  context.indent
-                )
-              )
-            else:
-              context.fout.write(
-                '''{0:s}  self.set('{1:s}', element.serialize_{2:s}(self.{3:s}{4:s}))
-'''.format(
-                  context.indent,
-                  name,
-                  type,
-                  name,
-                  ', ref_list' if type == 'ref' else ''
-                )
-              )
+            context.field_name = i[1].get_text()
+            i[0].generate_serialize(context)
           context.fout.write(
             '''{0:s}def deserialize(self, ref_list):
 {1:s}  {2:s}.deserialize(self, ref_list)
 '''.format(context.indent, context.indent, full_base_class)
           )
           for i in context.fields[n_base_fields:]:
-            type = element.to_text(i[0])
-            name = i[1].get_text()
-            if type[:5] == 'list(' and type[-1:] == ')':
-              subtype = type[5:-1]
-              context.fout.write(
-                '''{0:s}  self.{1:s} = [
-{2:s}    element.deserialize_{3:s}(i{4:s})
-{5:s}    for i in self.get('{6:s}', '').split()
-{7:s}  ]
-'''.format(
-                  context.indent,
-                  name,
-                  context.indent,
-                  subtype,
-                  ', ref_list' if subtype == 'ref' else '',
-                  context.indent,
-                  name,
-                  context.indent
-                )
-              )
-            elif type[:4] == 'set(' and type[-1:] == ')':
-              subtype = type[4:-1]
-              context.fout.write(
-                '''{0:s}  self.{1:s} = set(
-{2:s}    [
-{3:s}      element.deserialize_{4:s}(i{5:s})
-{6:s}      for i in self.get('{7:s}', '').split()
-{8:s}    ]
-{9:s}  )
-'''.format(
-                  context.indent,
-                  name,
-                  context.indent,
-                  context.indent,
-                  subtype,
-                  ', ref_list' if subtype == 'ref' else '',
-                  context.indent,
-                  name,
-                  context.indent,
-                  context.indent
-                )
-              )
-            else: 
-              context.fout.write(
-                '''{0:s}  self.{1:s} = element.deserialize_{2:s}(self.get('{3:s}', '{4:s}'){5:s})
-'''.format(
-                  context.indent,
-                  name,
-                  type,
-                  name,
-                  default_value_str[type],
-                  ', ref_list' if type == 'ref' else ''
-                )
-              )
+            context.field_name = i[1].get_text()
+            i[0].generate_deserialize(context)
         context.fout.write(
           '''{0:s}def copy(self, factory = None):
 {1:s}  result = {2:s}.copy(
@@ -1038,67 +1105,8 @@ class AST(element.Element):
             )
           )
           for i in context.fields[n_base_fields:]:
-            type = element.to_text(i[0])
-            name = i[1].get_text()
-            if type[:5] == 'list(' and type[-1:] == ')':
-              subtype = type[5:-1]
-              context.fout.write(
-                '''{0:s}  if len(self.{1:s}):
-{2:s}    params.append(
-{3:s}      '{4:s} = [{{0:s}}]'.format(
-{5:s}        ', '.join([repr(i) for i in self.{6:s}])
-{7:s}      )
-{8:s}    )
-'''.format(
-                  context.indent,
-                  name,
-                  context.indent,
-                  context.indent,
-                  name,
-                  context.indent,
-                  name,
-                  context.indent,
-                  context.indent
-                )
-              )
-            elif type[:4] == 'set(' and type[-1:] == ')':
-              subtype = type[4:-1]
-              context.fout.write(
-                '''{0:s}  if len(self.{1:s}):
-{2:s}    params.append(
-{3:s}      '{4:s} = set([{{0:s}}])'.format(
-{5:s}        ', '.join([repr(i) for i in sorted(self.{6:s})])
-{7:s}      )
-{8:s}    )
-'''.format(
-                  context.indent,
-                  name,
-                  context.indent,
-                  context.indent,
-                  name,
-                  context.indent,
-                  name,
-                  context.indent,
-                  context.indent
-                )
-              )
-            else:
-              context.fout.write(
-                '''{0:s}  if self.{1:s} != {2:s}:
-{3:s}    params.append(
-{4:s}      '{5:s} = {{0:s}}'.format(repr(self.{6:s}))
-{7:s}    )
-'''.format(
-                  context.indent,
-                  name,
-                  default_value[type],
-                  context.indent,
-                  context.indent,
-                  name,
-                  name,
-                  context.indent
-                )
-              )
+            context.field_name = i[1].get_text()
+            i[0].generate_repr_serialize(context)
         context.fout.write(
           '''{0:s}def __repr__(self):
 {1:s}  params = []