Processed comments of asn1ate's author kimgr on coding style
authorRick van Rein <rick@openfortress.nl>
Fri, 8 Jul 2016 11:26:52 +0000 (12:26 +0100)
committerRick van Rein <rick@openfortress.nl>
Fri, 8 Jul 2016 11:26:52 +0000 (12:26 +0100)
README.txt
asn1ate/asn2quickder.py
asn1ate/sema.py

index 7d2f087..5438034 100644 (file)
@@ -5,9 +5,8 @@ Introduction
 ------------
 
 ``asn1ate`` is a Python library for translating ASN.1 into other forms.
-It is intended for code generation from formal ASN.1 definitions, and a
-code generator for ``pyasn1`` is included.  Additional code for the
-``Quick DER`` format was added later.
+It is intended for code generation from formal ASN.1 definitions, and
+code generators for ``pyasn1`` as well as ``Quick DER`` are included.
 
 ``asn1ate`` is released under a 3-clause BSD license. For details, see
 LICENSE.txt.
@@ -53,12 +52,17 @@ and overlay structures for this format.  The command to do this is::
 It will store an include file suitable for use with ``Quick DER`` in ``source.h``,
 which is the source file name with its extension changed to ``.h``.
 
-This tool can be used for your private ASN.1 projects, but one purpose of
-Quick DER is also to have a developer's toolkit with precompiled header
-files that can simply be included as
+This tool can be used for your personal ASN.1 projects, but one purpose of
+Quick DER is to supply developers with a toolkit that installs header files
+for commonly used DER formats, that can be simply included as
 
   #include <quick-der/rfc5280.h>
 
+When building Quick DER, such header files are constructed for the ASN.1
+files included in the Quick DER package, and they are installed alongside
+the rest of Quick DER.  That use of the `asn2quickder` backend could be
+considered a "build-time" dependency of Quick DER.
+
 
 Dependencies
 ------------
index 9596791..c4ceb92 100755 (executable)
 # a very big thank you to Schneider Electric Buildings AB for helping to
 # make this program possible!
 #
-# Copyright 2016 InternetWide.org and the ARPA2.net project.
+# Copyright (c) 2016 OpenFortress B.V. and InternetWide.org
+# All rights reserved.
+# 
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#     * Redistributions of source code must retain the above copyright
+#       notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above copyright
+#       notice, this list of conditions and the following disclaimer in the
+#       documentation and/or other materials provided with the distribution.
+#     * Neither the name of Schneider Electric Buildings AB nor the
+#       names of contributors may be used to endorse or promote products
+#       derived from this software without specific prior written permission.
+#
 
 
 import sys
@@ -20,7 +33,7 @@ from asn1ate import parser
 from asn1ate.sema import * 
 
 
-def toCsym (name):
+def tosym(name):
     """Replace unsupported characters in ASN.1 symbol names"""
     return str(name).replace(' ', '').replace('-', '_')
 
@@ -40,10 +53,10 @@ class QuickDERgen():
        this is a walking path for the der_pack() and der_unpack() instructions.
        In addition, there will be a struct for each of the symbols:
 
-       struct unit_SyntaxDeclSym_ovly {
+       struct unit_SyntaxDeclSym_overlay {
            dercursor field1;
            dercursor field2;
-           struct unit_EmbeddedSym_ovly field3;
+           struct unit_EmbeddedSym_overlay field3;
            dercursor field4;
        };
 
@@ -57,24 +70,23 @@ class QuickDERgen():
         self.refmods = refmods
         if outfn [-2:] == '.h':
             raise Exception('File cannot overwrite itself -- use another extension than .h for input files')
-        self.unit = toCsym(outfn.rsplit('.', 1) [0])
+        self.unit = tosym(outfn.rsplit('.', 1) [0])
         self.outfile = open(self.unit + '.h', 'w')
-        self.wout = self.outfile.write
         # Setup function maps
-        self.ovly_funmap = {
-            DefinedType: self.ovlyDefinedType,
+        self.overlay_funmap = {
+            DefinedType: self.overlayDefinedType,
             ValueAssignment: self.ignore_node,
-            TypeAssignment: self.ovlyTypeAssignment,
-            TaggedType: self.ovlyTaggedType,
-            SimpleType: self.ovlySimpleType,
-            BitStringType: self.ovlySimpleType,
-            ValueListType: self.ovlySimpleType,
-            SequenceType: self.ovlyConstructedType,
-            SetType: self.ovlyConstructedType,
-            ChoiceType: self.ovlyConstructedType,
-            SequenceOfType: self.ovlySimpleType,    # var sized
-            SetOfType: self.ovlySimpleType,         # var sized
-            ComponentType: self.ovlySimpleType,  #TODO#
+            TypeAssignment: self.overlayTypeAssignment,
+            TaggedType: self.overlayTaggedType,
+            SimpleType: self.overlaySimpleType,
+            BitStringType: self.overlaySimpleType,
+            ValueListType: self.overlaySimpleType,
+            SequenceType: self.overlayConstructedType,
+            SetType: self.overlayConstructedType,
+            ChoiceType: self.overlayConstructedType,
+            SequenceOfType: self.overlaySimpleType,    # var sized
+            SetOfType: self.overlaySimpleType,         # var sized
+            ComponentType: self.overlaySimpleType,  #TODO#
         }
         self.pack_funmap = {
             DefinedType: self.packDefinedType,
@@ -83,7 +95,7 @@ class QuickDERgen():
             TaggedType: self.packTaggedType,
             SimpleType: self.packSimpleType,
             BitStringType: self.packSimpleType,
-            ValueListType: self.ovlySimpleType,
+            ValueListType: self.overlaySimpleType,
             SequenceType: self.packSequenceType,
             SetType: self.packSetType,
             ChoiceType: self.packChoiceType,
@@ -92,193 +104,220 @@ class QuickDERgen():
             ComponentType: self.packSimpleType,  #TODO#
         }
 
+    def write(self, txt):
+        self.outfile.write(txt)
+
+    def writeln(self, txt=''):
+        self.outfile.write(txt + '\n')
+
     def newcomma(self, comma, firstcomma=''):
         self.comma0 = firstcomma
         self.comma1 = comma
 
     def comma(self):
-        self.wout(self.comma0)
+        self.write(self.comma0)
         self.comma0 = self.comma1
 
     def close(self):
         self.outfile.close()
 
     def generate_head(self):
-        self.wout('/*\n * asn2quickder output for ' + self.semamod.name + ' -- automatically generated\n *\n * For information on Quick `n\' Easy DER, see https://github.com/vanrein/quick-der\n *\n * For information on the code generator, see https://github.com/vanrein/asn2quickder\n *\n */\n\n\n#include <quick-der/api.h>\n\n\n')
+        self.writeln('/*')
+        self.writeln(' * asn2quickder output for ' + self.semamod.name + ' -- automatically generated')
+        self.writeln(' *')
+        self.writeln(' * For information on Quick `n\' Easy DER, see https://github.com/vanrein/quick-der')
+        self.writeln(' *')
+        self.writeln(' * For information on the code generator, see https://github.com/vanrein/asn2quickder')
+        self.writeln(' *')
+        self.writeln(' */')
+        self.writeln('')
+        self.writeln('')
+        self.writeln('#include <quick-der/api.h>')
+        self.writeln('')
+        self.writeln('')
         closer = ''
         for rm in self.semamod.imports.module2symbols.keys():
-            rmfn = toCsym(rm.rsplit('.', 1) [0]).lower()
-            self.wout('#include <quick-der/' + rmfn + '.h>\n')
+            rmfn = tosym(rm.rsplit('.', 1) [0]).lower()
+            self.writeln('#include <quick-der/' + rmfn + '.h>')
             closer = '\n\n'
-        self.wout(closer)
+        self.write(closer)
         closer = ''
         for rm in self.semamod.imports.module2symbols.keys():
-            rmfn = toCsym(rm.rsplit('.', 1) [0]).lower()
+            rmfn = tosym(rm.rsplit('.', 1) [0]).lower()
             for sym in self.semamod.imports.module2symbols [rm]:
-                self.wout('typedef DER_OVLY_' + toCsym(rmfn) + '_' + toCsym(sym) + ' DER_OVLY_' + toCsym(self.unit) + '_' + toCsym(sym) + ';\n')
+                self.writeln('typedef DER_OVLY_' + tosym(rmfn) + '_' + tosym(sym) + ' DER_OVLY_' + tosym(self.unit) + '_' + tosym(sym) + ';')
                 closer = '\n\n'
-        self.wout(closer)
+        self.write(closer)
         closer = ''
         for rm in self.semamod.imports.module2symbols.keys():
-            rmfn = toCsym(rm.rsplit('.', 1) [0]).lower()
+            rmfn = tosym(rm.rsplit('.', 1) [0]).lower()
             for sym in self.semamod.imports.module2symbols [rm]:
-                self.wout('#define DER_PACK_' + toCsym(self.unit) + '_' + toCsym(sym) + ' DER_PACK_' + toCsym(rmfn) + '_' + toCsym(sym) + '\n')
+                self.writeln('#define DER_PACK_' + tosym(self.unit) + '_' + tosym(sym) + ' DER_PACK_' + tosym(rmfn) + '_' + tosym(sym) + '')
                 closer = '\n\n'
-        self.wout(closer)
+        self.write(closer)
 
     def generate_tail(self):
-        self.wout('\n\n/* asn2quickder output for ' + self.semamod.name + ' ends here */\n')
-
-    def generate_ovly(self):
-        self.wout('\n\n/* Overlay structures with ASN.1 derived nesting and labelling */\n\n')
+        self.writeln()
+        self.writeln()
+        self.writeln('/* asn2quickder output for ' + self.semamod.name + ' ends here */')
+
+    def generate_overlay(self):
+        self.writeln()
+        self.writeln()
+        self.writeln('/* Overlay structures with ASN.1 derived nesting and labelling */')
+        self.writeln()
         for assigncompos in dependency_sort(self.semamod.assignments):
             for assign in assigncompos:
-                self.generate_ovly_node(assign)
+                self.generate_overlay_node(assign)
 
     def generate_pack(self):
-        self.wout('\n\n/* Parser definitions in terms of ASN.1 derived bytecode instructions */\n\n')
+        self.writeln()
+        self.writeln()
+        self.writeln('/* Parser definitions in terms of ASN.1 derived bytecode instructions */')
+        self.writeln()
         for assigncompos in dependency_sort(self.semamod.assignments):
             for assign in assigncompos:
                 tnm = type(assign)
                 if tnm in self.pack_funmap:
                     self.pack_funmap [tnm](assign)
                 else:
-                    print 'No pack generator for ' + str(tnm)
+                    raise Exception('No pack generator for ' + str(tnm))
 
-    def generate_ovly_node(self, node):
+    def generate_overlay_node(self, node):
         tnm = type(node)
-        if tnm in self.ovly_funmap:
-            self.ovly_funmap [tnm](node)
+        if tnm in self.overlay_funmap:
+            self.overlay_funmap [tnm](node)
         else:
-            print('No overlay generator for ' + str(tnm))
-            raise Exception('RAISED WHERE?')
+            raise Exception('No overlay generator for ' + str(tnm))
 
     def generate_pack_node(self, node):
         tnm = type(node)
         if tnm in self.pack_funmap:
             self.pack_funmap [tnm](node)
         else:
-            print('No pack generator for ' + str(tnm))
+            raise Exception('No pack generator for ' + str(tnm))
 
     def ignore_node(self, node):
         pass
 
-    def ovlyTypeAssignment(self, node):
-        self.wout('typedef ')
-        self.generate_ovly_node(node.type_decl)
-        self.wout(' DER_OVLY_' + self.unit + '_' + toCsym(node.type_name) + ';\n\n')
+    def overlayTypeAssignment(self, node):
+        self.write('typedef ')
+        self.generate_overlay_node(node.type_decl)
+        self.writeln(' DER_OVLY_' + self.unit + '_' + tosym(node.type_name) + ';')
+        self.writeln()
 
     def packTypeAssignment(self, node):
-        self.wout('#define DER_PACK_' + self.unit + '_' + toCsym(node.type_name))
+        self.write('#define DER_PACK_' + self.unit + '_' + tosym(node.type_name))
         self.newcomma(', \\\n\t', ' \\\n\t')
         self.generate_pack_node(node.type_decl)
-        self.wout('\n\n')
+        self.writeln()
+        self.writeln()
 
-    def ovlyDefinedType(self, node):
+    def overlayDefinedType(self, node):
         mod = node.module_name or self.unit
-        self.wout('DER_OVLY_' + toCsym(mod) + '_' + toCsym(node.type_name))
+        self.write('DER_OVLY_' + tosym(mod) + '_' + tosym(node.type_name))
 
     def packDefinedType(self, node):
         mod = node.module_name or self.unit
         self.comma()
-        self.wout('DER_PACK_' + toCsym(mod) + '_' + toCsym(node.type_name))
+        self.write('DER_PACK_' + tosym(mod) + '_' + tosym(node.type_name))
 
-    def ovlySimpleType(self, node):
-        self.wout('dercursor')
+    def overlaySimpleType(self, node):
+        self.write('dercursor')
 
     def packSimpleType(self, node):
         self.comma()
-        self.wout('DER_PACK_STORE | DER_TAG_' + node.type_name.replace(' ', '').upper())
+        self.write('DER_PACK_STORE | DER_TAG_' + node.type_name.replace(' ', '').upper())
 
-    def ovlyTaggedType(self, node):
+    def overlayTaggedType(self, node):
         # tag = str(node) 
         # tag = tag [:tag.find(']')] + ']'
-        # self.wout('/* ' + tag + ' */ ')
+        # self.write('/* ' + tag + ' */ ')
         # if node.implicity == TagImplicity.IMPLICIT:
         #     tag = tag + ' IMPLICIT'
         # elif node.implicity == TagImplicity.IMPLICIT:
         #     tag = tag + ' EXPLICIT'
-        self.generate_ovly_node(node.type_decl)
+        self.generate_overlay_node(node.type_decl)
 
     def packTaggedType(self, node):
         #TODO# Need to push down node.implicity == TagImplicity.IMPLICIT
         #TODO# Need to process tag class
         self.comma()
-        self.wout('DER_PACK_ENTER | DER_TAG_' +(node.class_name or 'CONTEXT') + '(' + node.class_number + ')')
+        self.write('DER_PACK_ENTER | DER_TAG_' +(node.class_name or 'CONTEXT') + '(' + node.class_number + ')')
         self.generate_pack_node(node.type_decl)
         self.comma()
-        self.wout('DER_PACK_LEAVE')
+        self.write('DER_PACK_LEAVE')
 
     # Sequence, Set, Choice
-    def ovlyConstructedType(self, node):
-        self.wout('struct {\n');
+    def overlayConstructedType(self, node):
+        self.writeln('struct {');
         for comp in node.components:
             if isinstance(comp, ExtensionMarker):
-                self.wout('\t/* ...extensions... */\n')
+                self.writeln('\t/* ...extensions... */')
                 continue
             if isinstance(comp, ComponentType) and comp.components_of_type is not None:
-                self.wout('\t/* TODO: COMPONENTS OF TYPE ' + str(comp.components_of_type) + ' */\n')
+                self.writeln('\t/* TODO: COMPONENTS OF TYPE ' + str(comp.components_of_type) + ' */')
                 continue
-            self.wout('\t')
-            self.generate_ovly_node(comp.type_decl)
-            self.wout(' ' + toCsym(comp.identifier) + '; // ' + str(comp.type_decl) + '\n')
-        self.wout('}')
+            self.write('\t')
+            self.generate_overlay_node(comp.type_decl)
+            self.writeln(' ' + tosym(comp.identifier) + '; // ' + str(comp.type_decl))
+        self.write('}')
 
     def packSequenceType(self, node):
         self.comma()
-        self.wout('DER_PACK_ENTER | DER_TAG_SEQUENCE')
+        self.write('DER_PACK_ENTER | DER_TAG_SEQUENCE')
         for comp in node.components:
             if isinstance(comp, ExtensionMarker):
                 self.comma()
-                self.wout('/* ...ASN.1 extensions... */')
+                self.write('/* ...ASN.1 extensions... */')
                 continue
             if comp.optional:
                 self.comma()
-                self.wout('DER_PACK_OPTIONAL')
+                self.write('DER_PACK_OPTIONAL')
             if comp.type_decl is not None:
                 # TODO: None would be due to components_of_type
                 self.generate_pack_node(comp.type_decl)
         self.comma()
-        self.wout('DER_PACK_LEAVE')
+        self.write('DER_PACK_LEAVE')
 
     def packSetType(self, node):
         self.comma()
-        self.wout('DER_PACK_ENTER | DER_TAG_SET')
+        self.write('DER_PACK_ENTER | DER_TAG_SET')
         for comp in node.components:
             if isinstance(comp, ExtensionMarker):
                 self.comma()
-                self.wout('/* ...extensions... */')
+                self.write('/* ...extensions... */')
                 continue
             if comp.optional:
                 self.comma()
-                self.wout('DER_PACK_OPTIONAL')
+                self.write('DER_PACK_OPTIONAL')
             if comp.type_decl is not None:
                 # TODO: None would be due to components_of_type
                 self.generate_pack_node(comp.type_decl)
         self.comma()
-        self.wout('DER_PACK_LEAVE')
+        self.write('DER_PACK_LEAVE')
 
     def packChoiceType(self, node):
         self.comma()
-        self.wout('DER_PACK_CHOICE_BEGIN')
+        self.write('DER_PACK_CHOICE_BEGIN')
         for comp in node.components:
             if isinstance(comp, ExtensionMarker):
                 self.comma()
-                self.wout('/* ...extensions... */')
+                self.write('/* ...extensions... */')
                 continue
             if comp.type_decl is not None:
                 # TODO: None would be due to components_of_type
                 self.generate_pack_node(comp.type_decl)
         self.comma()
-        self.wout('DER_PACK_CHOICE_END')
+        self.write('DER_PACK_CHOICE_END')
 
     def packSequenceOfType(self, node):
         self.comma()
-        self.wout('DER_PACK_STORE | DER_TAG_SEQUENCE')
+        self.write('DER_PACK_STORE | DER_TAG_SEQUENCE')
 
     def packSetOfType(self, node):
         self.comma()
-        self.wout('DER_PACK_STORE | DER_TAG_SEQUENCE')
+        self.write('DER_PACK_STORE | DER_TAG_SEQUENCE')
 
 
 """The main program asn2quickder is called with one or more .asn1 files,
@@ -295,7 +334,7 @@ mods = []
 for file in sys.argv [1:]:
     print('Parsing', file)
     with open(file, 'r') as asn1fh:
-       asn1txt  = asn1fh.read ()
+        asn1txt  = asn1fh.read()
         asn1tree = parser.parse_asn1(asn1txt)
     print('Building semantic model for', file)
     asn1sem = build_semantic_model(asn1tree)
@@ -305,7 +344,7 @@ for file in sys.argv [1:]:
 cogen = QuickDERgen(mods [-1], os.path.basename(sys.argv [1]), mods [1:])
 
 cogen.generate_head()
-cogen.generate_ovly()
+cogen.generate_overlay()
 cogen.generate_pack()
 cogen.generate_tail()
 
index 846b907..969b4d4 100644 (file)
@@ -722,15 +722,16 @@ class ValueListType(SemaNode):
 
 
 class Imports(SemaNode):
-    def __init__(self,elements):
+    def __init__(self, elements):
        self.module2symbols = { }
-       for i in range (0,len(elements),2):
+       for i in range (0, len(elements), 2):
                self.module2symbols [elements [i+1].elements[0]] = set (elements [i])
-       print 'Imported from', self.module2symbols.keys ()
 
-class Exports(Imports):
-    def __init__(self,elements):
-       Imports.__init__ (self, elements)
+class Exports(SemaNode):
+    def __init__(self, elements):
+       self.module2symbols = { }
+       for i in range (0, len(elements), 2):
+               self.module2symbols [elements [i+1].elements[0]] = set (elements [i])
 
 
 class BitStringType(SemaNode):