Revert 6 commits 4e05c5b20c-c25aac2b27
authorKim Grasman <kim.grasman@gmail.com>
Wed, 28 Aug 2013 12:18:42 +0000 (14:18 +0200)
committerKim Grasman <kim.grasman@gmail.com>
Wed, 28 Aug 2013 12:18:42 +0000 (14:18 +0200)
Some of them were breaking changes, and I want to do those
in a branch instead.

asn1ate/parser.py
asn1ate/pyasn1gen.py
asn1ate/sema.py
testdata/builtin_types.asn
testdata/constraints.asn [deleted file]
testdata/time_types.asn [deleted file]

index c4adf16..8bbb7ae 100644 (file)
@@ -149,10 +149,6 @@ def _build_asn1_grammar():
     VideotexString = Keyword('VideotexString')
     VisibleString = Keyword('VisibleString')
 
-    # Useful types
-    GeneralizedTime = Keyword('GeneralizedTime')
-    UTCTime = Keyword('UTCTime')
-
     # Literals
     number = Word(nums)
     signed_number = Combine(Optional('-') + number)  # todo: consider defined values from 18.1
@@ -213,6 +209,10 @@ def _build_asn1_grammar():
     # extensions
     extension_default = EXTENSIBILITY_IMPLIED | empty
 
+    # types
+    defined_type = Unique(typereference)  # todo: consider other defined types from 13.1
+    referenced_type = Unique(defined_type)  # todo: consider other ref:d types from 16.3
+
     # Forward-declare these, they can only be fully defined once
     # we have all types defined. There are some circular dependencies.
     named_type = Forward()
@@ -261,27 +261,21 @@ def _build_asn1_grammar():
                                       T61String | UniversalString | \
                                       UTF8String | VideotexString | VisibleString
     characterstring_type = restricted_characterstring_type | unrestricted_characterstring_type
-    useful_type = GeneralizedTime | UTCTime  # TODO: ObjectDescriptor
 
     # todo: consider other builtins from 16.2
-    defined_type = Unique(typereference)  # todo: consider other defined types from 13.1
-
-    # these productions are used for custom parse actions,
-    # because they typically generate similar code.
-    simple_type = (boolean_type | null_type | octetstring_type | characterstring_type | real_type | plain_integer_type | object_identifier_type)
+    simple_type = (boolean_type | null_type | octetstring_type | characterstring_type | real_type | plain_integer_type | object_identifier_type) + Optional(constraint)
+    constructed_type = choice_type | sequence_type | set_type
     value_list_type = restricted_integer_type | enumerated_type
+    builtin_type = tagged_type | simple_type | constructed_type | sequenceof_type | setof_type | value_list_type | bitstring_type
 
-    builtin_type = value_list_type | tagged_type | simple_type | choice_type | sequence_type | set_type | sequenceof_type | setof_type | bitstring_type
-    referenced_type = defined_type | useful_type  # todo: consider other ref:d types from 16.3
-
-    type_ << ((builtin_type | referenced_type) + Optional(constraint))
+    type_ << (builtin_type | referenced_type)
 
-    # EXT: identifier should not be Optional here,
+    # BUG: identifier should not be Optional here,
     # but our ASN.1 interpreter supports unnamed members,
     # and we use them.
     named_type << (Optional(identifier) + type_)
 
-    # EXT: Trailing semi-colon is not allowed by standard grammar, but our ASN.1 interpreter accepts it
+    # BUG: Trailing semi-colon is not allowed by standard grammar, but our ASN.1 interpreter accepts it
     # and we happen to use it.
     type_assignment = typereference + '::=' + type_ + Suppress(Optional(';'))
     value_assignment = valuereference + type_ + '::=' + value
index e700b11..897609e 100644 (file)
@@ -71,8 +71,7 @@ class Pyasn1Backend(object):
             SequenceOfType: self.decl_sequenceof_type,
             SetOfType: self.decl_setof_type,
             TypeAssignment: self.decl_type_assignment,
-            ValueAssignment: self.decl_value_assignment,
-            Type: self.decl_type,
+            ValueAssignment: self.decl_value_assignment
         }
 
         self.expr_generators = {
@@ -87,11 +86,10 @@ class Pyasn1Backend(object):
             ChoiceType: self.expr_constructed_type,
             SequenceType: self.expr_constructed_type,
             SetType: self.expr_constructed_type,
-            Type: self.expr_type,
         }
 
     def generate_code(self):
-        self.writer.write_line('from pyasn1.type import univ, char, namedtype, namedval, tag, constraint, useful')
+        self.writer.write_line('from pyasn1.type import univ, char, namedtype, namedval, tag, constraint')
         self.writer.write_blanks(2)
 
         # TODO: Only generate _OID if sema_module
@@ -121,36 +119,29 @@ class Pyasn1Backend(object):
         fragment.write_line('class %s(%s):' % (assigned_type, base_type))
 
         fragment.push_indent()
-        fragment.write_block(self.generate_decl(type_decl) or 'pass')
+        fragment.write_block(self.generate_decl(type_decl))
         fragment.pop_indent()
 
         return str(fragment)
 
-    def expr_type(self, t):
-        type_expr = self.generate_expr(t.type_decl)
+    def expr_simple_type(self, t):
+        type_expr = _translate_type(t.type_name) + '()'
         if t.constraint:
             type_expr += '.subtype(subtypeSpec=constraint.ValueRangeConstraint(%s, %s))' % (t.constraint.min_value, t.constraint.max_value)
 
         return type_expr
 
-    def decl_type(self, t):
-        type_decl = self.generate_decl(t.type_decl)
-        if t.constraint:
-            type_decl += 'subtypeSpec = constraint.ValueRangeConstraint(%s, %s)' % (t.constraint.min_value, t.constraint.max_value)
-
-        return type_decl
-
-    def expr_simple_type(self, t):
-        return _translate_type(t.type_name) + '()'
-
     def decl_simple_type(self, t):
-        return ''
+        if t.constraint:
+            return 'subtypeSpec = constraint.ValueRangeConstraint(%s, %s)' % (t.constraint.min_value, t.constraint.max_value)
+        else:
+            return 'pass'
 
     def expr_userdefined_type(self, t):
         return t.type_name + '()'
 
     def decl_userdefined_type(self, t):
-        return ''
+        return 'pass'
 
     def decl_constructed_type(self, t):
         fragment = self.writer.get_fragment()
@@ -205,7 +196,7 @@ class Pyasn1Backend(object):
         tag_type = 'tagImplicitly' if t.implicit else 'tagExplicitly'
         base_type = _translate_type(t.type_decl.type_name)
         fragment.write_line('tagSet = %s.tagSet.%s(%s)' % (base_type, tag_type, self.build_tag_expr(t)))
-        fragment.write_line(self.generate_decl(t.type_decl))
+        fragment.write_line(self.generate_decl(t.type_decl))  # possibly 'pass'. but that's OK in a decl
 
         return str(fragment)
 
@@ -256,6 +247,8 @@ class Pyasn1Backend(object):
 
             fragment.pop_indent()
             fragment.write_line(')')
+        else:
+            fragment.write_line('pass')
 
         return str(fragment)
 
@@ -279,6 +272,8 @@ class Pyasn1Backend(object):
 
             fragment.pop_indent()
             fragment.write_line(')')
+        else:
+            fragment.write_line('pass')
 
         return str(fragment)
 
@@ -378,9 +373,7 @@ _ASN1_BUILTIN_TYPES = {
     'SET OF': 'univ.SetOf',
     'UTF8String': 'char.UTF8String',
     'GeneralString': 'char.GeneralString',
-    'OBJECT IDENTIFIER': 'univ.ObjectIdentifier',
-    'GeneralizedTime': 'useful.GeneralizedTime',
-    'UTCTime': 'useful.UTCTime',
+    'OBJECT IDENTIFIER': 'univ.ObjectIdentifier'
 }
 
 
@@ -418,7 +411,7 @@ def main(args):
         print('WARNING: More than one module generated to the same stream.', file=sys.stderr)
 
     for module in modules:
-        print(pygen.auto_generated_header(args[0]))
+        print(pygen.auto_generated_header())
         generate_pyasn1(module, sys.stdout)
 
     return 0
index e2f00d3..766a0e0 100644 (file)
@@ -153,11 +153,6 @@ class Module(object):
         """
         user_types = self.user_types()
 
-        # Drill through Type, it acts as a container
-        # for type + metadata like constraints.
-        if isinstance(type_decl, Type):
-            type_decl = type_decl.type_decl
-
         if isinstance(type_decl, UserDefinedType):
             return self.resolve_type_decl(user_types[type_decl.type_name])
         else:
@@ -248,40 +243,6 @@ class ValueReference(object):
     __repr__ = __str__
 
 
-class Type(object):
-    """ A container for all types, contains
-    additional metadata such as constraints.
-    """
-    def __init__(self, elements):
-        self.type_decl = _create_sema_node(elements[0])
-        if len(elements) == 2:
-            assert elements[1].ty == 'Constraint'
-            self.constraint = Constraint(elements[1].elements)
-        else:
-            self.constraint = None
-
-    @property
-    def type_name(self):
-        return self.type_decl.type_name
-
-    def reference_name(self):
-        return self.type_decl.type_name
-
-    def references(self):
-        if self.constraint:
-            return self.constraint.references()
-
-        return []
-
-    def __str__(self):
-        if self.constraint:
-            return '%s %s' % (self.type_decl, self.constraint)
-        else:
-            return str(self.type_decl)
-
-    __repr__ = __str__
-
-
 class ConstructedType(object):
     """ Base type for SEQUENCE, SET and CHOICE. """
     def __init__(self, elements):
@@ -716,7 +677,9 @@ def _create_sema_node(token):
     elif token.ty == 'NamedValue':
         return NamedValue(token.elements)
     elif token.ty == 'Type':
-        return Type(token.elements)
+        # Type tokens have a more specific type category
+        # embedded as their first element
+        return _create_sema_node(token.elements[0])
     elif token.ty == 'SimpleType':
         return SimpleType(token.elements)
     elif token.ty == 'ReferencedType':
index b5b5a51..305cb3b 100644 (file)
@@ -6,11 +6,6 @@ BEGIN
   Int ::= INTEGER
   Real ::= REAL
 
-  RestrictedInteger ::= INTEGER {
-    v1(1),
-    v2(2)
-  }
-
   -- Enumerated types
   BitString ::= BIT STRING {
     a(1),
diff --git a/testdata/constraints.asn b/testdata/constraints.asn
deleted file mode 100644 (file)
index 6e96f06..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-TEST DEFINITIONS ::=
-BEGIN
-  UnsignedInt ::= INTEGER (0..4294967295)
-
-  intMax INTEGER ::= 4294967295
-  UnsignedIntAgain ::= INTEGER (0..intMax)
-
---  PkcsTricky ::= INTEGER {v3(3)}(v3,...)
-
-  Seq ::= SEQUENCE {
-    valueConstrained INTEGER (0..4294967295),
-    taggedValueConstrained [1] INTEGER (0..4294967295),
-    definedValueConstrained INTEGER (0..intMax)
---    inlineBracedListAndConstraint INTEGER {v3(3)}(v3,...)
-  }
-
-  ConstrainedList ::= SEQUENCE OF INTEGER (1..64)
-
-  Bob ::= UnsignedInt (10..20)
-
-END
diff --git a/testdata/time_types.asn b/testdata/time_types.asn
deleted file mode 100644 (file)
index 52a85cb..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-TEST DEFINITIONS ::=
-BEGIN
-  GTime ::= GeneralizedTime
-  UTime ::= UTCTime
-END