Add support for ENUMERATED in identifier-only form
authorGiovanni Bajo <rasky@develer.com>
Tue, 8 Apr 2014 16:38:12 +0000 (18:38 +0200)
committerGiovanni Bajo <rasky@develer.com>
Tue, 8 Apr 2014 16:39:56 +0000 (18:39 +0200)
asn1ate/parser.py
asn1ate/sema.py
testdata/enumeration.asn [new file with mode: 0644]

index c77ef8c..e8ec30f 100644 (file)
@@ -244,7 +244,8 @@ def _build_asn1_grammar():
 
     named_number_value = Suppress('(') + signed_number + Suppress(')')
     named_number = identifier + named_number_value
-    enumeration = named_number | identifier
+    named_nonumber = identifier.copy()
+    enumeration = named_number | named_nonumber
 
     set_type = SET + braced_list(component_type | extension_marker)
     sequence_type = SEQUENCE + braced_list(component_type | extension_marker)
@@ -324,6 +325,7 @@ def _build_asn1_grammar():
     sequenceof_type.setParseAction(annotate('SequenceOfType'))
     setof_type.setParseAction(annotate('SetOfType'))
     named_number.setParseAction(annotate('NamedValue'))
+    named_nonumber.setParseAction(annotate('NamedValue'))
     constraint.setParseAction(annotate('Constraint'))
     size_constraint.setParseAction(annotate('SizeConstraint'))
     component_type.setParseAction(annotate('ComponentType'))
index b5cac07..aaf204e 100644 (file)
@@ -196,7 +196,7 @@ class ValueAssignment(object):
         self.type_decl = _create_sema_node(type_name)
 
         if isinstance(value, parser.AnnotatedToken):
-            self.value = _create_sema_node(value) 
+            self.value = _create_sema_node(value)
         else:
             self.value = value
 
@@ -517,6 +517,12 @@ class ValueListType(object):
         self.type_name = elements[0]
         if len(elements) > 1:
             self.named_values = [_create_sema_node(token) for token in elements[1]]
+            for idx,n in enumerate(self.named_values):
+                if isinstance(n, NamedValue) and n.value is None:
+                    if idx == 0:
+                        n.value = str(0)
+                    else:
+                        n.value = str(int(self.named_values[idx-1].value) + 1)
         else:
             self.named_values = None
 
@@ -558,9 +564,14 @@ class BitStringType(object):
 
 class NamedValue(object):
     def __init__(self, elements):
-        identifier_token, value_token = elements
-        self.identifier = identifier_token.elements[0]
-        self.value = value_token.elements[0]
+        if len(elements) == 1:
+            identifier_token = elements[0]
+            self.identifier = identifier_token
+            self.value = None
+        else:
+            identifier_token, value_token = elements
+            self.identifier = identifier_token.elements[0]
+            self.value = value_token.elements[0]
 
     def references(self):
         # TODO: This appears to never be called. Investigate.
diff --git a/testdata/enumeration.asn b/testdata/enumeration.asn
new file mode 100644 (file)
index 0000000..5e1ca9f
--- /dev/null
@@ -0,0 +1,25 @@
+TEST DEFINITIONS ::=
+BEGIN
+
+EnumType1 ::= ENUMERATED
+{
+  one (1),
+  two (2),
+  three (3)
+}
+
+EnumType2 ::= ENUMERATED
+{
+  one,
+  two,
+  three
+}
+
+EnumType3 ::= ENUMERATED
+{
+  one,
+  two(2),
+  three
+}
+
+END