Support for multiple module definitions in the same file.
authorKim Grasman <kim.grasman@gmail.com>
Wed, 1 Jan 2014 16:25:20 +0000 (17:25 +0100)
committerKim Grasman <kim.grasman@gmail.com>
Wed, 1 Jan 2014 17:14:16 +0000 (18:14 +0100)
Based on original work by James Ward.

asn1ate/parser.py
testdata/empty_file.asn [new file with mode: 0644]
testdata/multi_module.asn [new file with mode: 0644]

index 83d4a77..f784a9f 100644 (file)
@@ -31,13 +31,13 @@ from pyparsing import Keyword, Literal, Word, OneOrMore, ZeroOrMore, Combine, Re
 __all__ = ['parse_asn1', 'AnnotatedToken']
 
 
-def parse_asn1(asn1_payload):
-    """ Parse a string containing an ASN.1 module definition
-    and return a syntax tree in the form of a list of
+def parse_asn1(asn1_definition):
+    """ Parse a string containing one or more ASN.1 module definitions.
+    Returns a list of module syntax trees represented as nested lists of
     AnnotatedToken objects.
     """
     grammar = _build_asn1_grammar()
-    parse_result = grammar.parseString(asn1_payload)
+    parse_result = grammar.parseString(asn1_definition)
     parse_tree = parse_result.asList()
     return parse_tree
 
@@ -350,7 +350,8 @@ def _build_asn1_grammar():
     exports.setParseAction(annotate('Exports'))
     assignment_list.setParseAction(annotate('AssignmentList'))
 
-    return module_definition
+    start = ZeroOrMore(module_definition)
+    return start
 
 
 def Unique(token):
diff --git a/testdata/empty_file.asn b/testdata/empty_file.asn
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/testdata/multi_module.asn b/testdata/multi_module.asn
new file mode 100644 (file)
index 0000000..82024a2
--- /dev/null
@@ -0,0 +1,20 @@
+Module1 DEFINITIONS ::=
+BEGIN
+
+OneSequence ::= SEQUENCE {
+    first INTEGER,
+    second BOOLEAN
+}
+
+END
+
+
+Module2 DEFINITIONS ::=
+BEGIN
+
+AnotherSequence ::= SEQUENCE {
+    first INTEGER,
+    second BOOLEAN
+}
+
+END