Allow empty module body. Sema now sees exports, imports and assignments.
authorKim Grasman <kim.grasman@gmail.com>
Sat, 21 Sep 2013 12:07:41 +0000 (14:07 +0200)
committerKim Grasman <kim.grasman@gmail.com>
Sat, 21 Sep 2013 12:07:41 +0000 (14:07 +0200)
asn1ate/parser.py
asn1ate/sema.py
testdata/empty.asn [new file with mode: 0644]

index 814f92e..3960f26 100644 (file)
@@ -25,7 +25,7 @@
 
 import re
 from copy import copy
-from pyparsing import Keyword, Literal, Word, OneOrMore, Combine, Regex, Forward, Optional, Group, Suppress, delimitedList, cStyleComment, nums, alphanums, empty, srange, dblQuotedString
+from pyparsing import Keyword, Literal, Word, OneOrMore, ZeroOrMore, Combine, Regex, Forward, Optional, Group, Suppress, delimitedList, cStyleComment, nums, alphanums, empty, srange, dblQuotedString
 
 
 __all__ = ['parse_asn1', 'AnnotatedToken']
@@ -290,7 +290,7 @@ def _build_asn1_grammar():
     value_assignment = valuereference + type_ + '::=' + value
 
     assignment = type_assignment | value_assignment
-    assignment_list = OneOrMore(assignment)
+    assignment_list = ZeroOrMore(assignment)
 
     assigned_identifier = Optional(object_identifier_value | defined_value)
     global_module_reference = module_reference + assigned_identifier
@@ -303,7 +303,7 @@ def _build_asn1_grammar():
     exports = Optional(Suppress(EXPORTS) + symbol_list + Suppress(';'))
     imports = Optional(Suppress(IMPORTS) + symbols_imported + Suppress(';'))
 
-    module_body = (exports + imports + assignment_list) | empty
+    module_body = (exports + imports + assignment_list)
     module_defaults = Suppress(tag_default + extension_default)  # we don't want these in the AST
     module_identifier = module_reference + definitive_identifier
     module_definition = module_identifier + DEFINITIONS + module_defaults + '::=' + BEGIN + module_body + END
index 736c8cb..6989b2c 100644 (file)
@@ -130,13 +130,10 @@ class Module(object):
         self._user_types = {}
 
         module_reference, _, _, _, _, module_body, _ = elements
-        self.name = module_reference.elements[0]
+        exports, imports, assignments = module_body.elements
 
-        if module_body.elements:
-            _, _, assignments = module_body.elements
-            self.assignments = [_create_sema_node(token) for token in assignments.elements]
-        else:
-            self.assignments = []
+        self.name = module_reference.elements[0]
+        self.assignments = [_create_sema_node(token) for token in assignments.elements]
 
     def user_types(self):
         if not self._user_types:
diff --git a/testdata/empty.asn b/testdata/empty.asn
new file mode 100644 (file)
index 0000000..62a70f6
--- /dev/null
@@ -0,0 +1,4 @@
+TEST DEFINITIONS ::=
+BEGIN
+-- No imports/exports or assignments
+END