Parser support for SIZE constraints on SET OF and SEQUENCE OF.
authorKim Grasman <kim.grasman@gmail.com>
Mon, 29 Jul 2013 08:28:56 +0000 (10:28 +0200)
committerKim Grasman <kim.grasman@gmail.com>
Mon, 29 Jul 2013 08:28:56 +0000 (10:28 +0200)
asn1ate/parser.py
asn1ate/sema.py

index 6df2c3f..4eb3f12 100644 (file)
@@ -115,12 +115,12 @@ def _build_asn1_grammar():
     EXTENSIBILITY_IMPLIED = Keyword('EXTENSIBILITY IMPLIED')
     COMPONENTS_OF = Keyword('COMPONENTS OF')
     ELLIPSIS = Keyword('...')
+    SIZE = Keyword('SIZE')
+    OF = Keyword('OF')
 
     # Built-in types
     SEQUENCE = Keyword('SEQUENCE')
     SET = Keyword('SET')
-    SEQUENCE_OF = Keyword('SEQUENCE OF')
-    SET_OF = Keyword('SET OF')
     CHOICE = Keyword('CHOICE')
     ENUMERATED = Keyword('ENUMERATED')
     BIT_STRING = Keyword('BIT STRING')
@@ -203,7 +203,10 @@ def _build_asn1_grammar():
     value_range_min = (signed_number | valuereference | MIN)
     value_range_max = (signed_number | valuereference | MAX)
     value_range_constraint = value_range_min + Suppress('..') + value_range_max
-    constraint = Suppress('(') + value_range_constraint + Suppress(')')  # todo: consider exception spec from 45.6
+    size_constraint = SIZE + Suppress('(') + value_range_constraint + Suppress(')')
+    constraint = Suppress('(') + value_range_constraint + Suppress(')')
+    optional_paren_l = Optional(Suppress('('))
+    optional_paren_r = Optional(Suppress(')'))
 
     # TODO: consider exception syntax from 24.1
     extension_marker = Unique(ELLIPSIS)
@@ -221,8 +224,8 @@ def _build_asn1_grammar():
 
     set_type = SET + braced_list(component_type | extension_marker)
     sequence_type = SEQUENCE + braced_list(component_type | extension_marker)
-    sequenceof_type = SEQUENCE_OF + (type_ | named_type)
-    setof_type = SET_OF + (type_ | named_type)
+    sequenceof_type = SEQUENCE + Optional(optional_paren_l + size_constraint + optional_paren_r) + OF + (type_ | named_type)
+    setof_type = SET + Optional(optional_paren_l + size_constraint + optional_paren_r) + OF + (type_ | named_type)
     choice_type = CHOICE + braced_list(named_type | extension_marker)
     enumerated_type = ENUMERATED + braced_list(enumeration)
     bitstring_type = BIT_STRING + braced_list(named_number)
index 853f4ee..4f44f4e 100644 (file)
@@ -248,7 +248,8 @@ class SetType(ConstructedType):
 
 class SequenceOfType(object):
     def __init__(self, elements):
-        type_name, type_token = elements
+        # TODO: handle optional size constraint
+        type_name, of_keyword, type_token = elements
         self.type_name = type_name
         self.type_decl = _create_sema_node(type_token)
 
@@ -263,7 +264,8 @@ class SequenceOfType(object):
 
 class SetOfType(object):
     def __init__(self, elements):
-        type_name, type_token = elements
+        # TODO: handle optional size constraint
+        type_name, of_keyword, type_token = elements
         self.type_name = type_name
         self.type_decl = _create_sema_node(type_token)