Partially move generation of the serialize/deserialize routines into TypeXXX
authorNick Downing <nick@ndcode.org>
Sun, 27 Jan 2019 08:52:39 +0000 (19:52 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 27 Jan 2019 08:52:39 +0000 (19:52 +1100)
ast.py

diff --git a/ast.py b/ast.py
index 6ae9370..839db34 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -130,168 +130,45 @@ class AST(element.Element):
     # 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}))
+      context.fout.write(
+        '''{0:s}  self.set('{1:s}', element.serialize_{2:s}(self.{3:s}))
 '''.format(
-            context.indent,
-            context.field_name,
-            type,
-            context.field_name,
-            ', ref_list' if type == 'ref' else ''
-          )
+          context.indent,
+          context.field_name,
+          type,
+          context.field_name
         )
+      )
     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}  ]
+      context.fout.write(
+        '''{0:s}  self.{1:s} = element.deserialize_{2:s}(self.get('{3:s}', '{4: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 ''
-          )
+          context.indent,
+          context.field_name,
+          type,
+          context.field_name,
+          default_value_str[type]
         )
+      )
     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}:
+      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
-          )
+          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):
@@ -791,6 +668,63 @@ class AST(element.Element):
       self.repr_serialize(params)
       return 'ast.AST.TypeList({0:s})'.format(', '.join(params))
     # GENERATE END
+    def generate_serialize(self, context):
+      subtype = element.to_text(self[0])
+      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
+        )
+      )
+    def generate_deserialize(self, context): 
+      subtype = element.to_text(self[0])
+      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
+        )
+      )
+    def generate_repr_serialize(self, context):
+      subtype = element.to_text(self[0])
+      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
+        )
+      )
   class TypeRef(Type):
     # GENERATE ELEMENT() BEGIN
     def __init__(
@@ -818,6 +752,24 @@ class AST(element.Element):
       self.repr_serialize(params)
       return 'ast.AST.TypeRef({0:s})'.format(', '.join(params))
     # GENERATE END
+    def generate_serialize(self, context):
+      context.fout.write(
+        '''{0:s}  self.set('{1:s}', element.serialize_ref(self.{2:s}, ref_list))
+'''.format(
+          context.indent,
+          context.field_name,
+          context.field_name
+        )
+      )
+    def generate_deserialize(self, context): 
+      context.fout.write(
+        '''{0:s}  self.{1:s} = element.deserialize_ref(self.get('{2:s}', '-1'), ref_list)
+'''.format(
+          context.indent,
+          context.field_name,
+          context.field_name
+        )
+      )
   class TypeSet(Type):
     # GENERATE ELEMENT() BEGIN
     def __init__(
@@ -845,6 +797,67 @@ class AST(element.Element):
       self.repr_serialize(params)
       return 'ast.AST.TypeSet({0:s})'.format(', '.join(params))
     # GENERATE END
+    def generate_serialize(self, context):
+      subtype = element.to_text(self[0])
+      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
+        )
+      )
+    def generate_deserialize(self, context): 
+      subtype = element.to_text(self[0])
+      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
+        )
+      )
+    def generate_repr_serialize(self, context):
+      subtype = element.to_text(self[0])
+      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
+        )
+      )
   class TypeStr(Type):
     # GENERATE ELEMENT() BEGIN
     def __init__(