changeset 35:6e7d8f8dfba2 draft

json_test: Fix imports and list parsing
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 22 May 2019 22:00:48 +0200
parents 02cf2ee329a3
children 859c9eaa90c2
files pcombinators/json_test.py
diffstat 1 files changed, 12 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/pcombinators/json_test.py	Wed May 22 01:08:08 2019 +0200
+++ b/pcombinators/json_test.py	Wed May 22 22:00:48 2019 +0200
@@ -6,8 +6,8 @@
 @author: lbo
 """
 
-from combinators import *
-from primitives import *
+from pcombinators.combinators import *
+from pcombinators.primitives import *
 
 JString = Last(Skip(String('"')) + NoneInSet('"') + Skip(String('"')))
 
@@ -35,6 +35,14 @@
 
 # LISTS
 
+def concat_elems_elem(l):
+    if len(l) == 1:
+        return l
+    elif len(l) == 2 and type(l[0]) is list:
+        l[0].append(l[1])
+        return l[0]
+    assert False, "Unexpected list format: {}".format(l)
+
 # An entry is any value.
 entry = Value()
 # A mid entry is a value followed by a comma. Last ensures that the result
@@ -44,9 +52,8 @@
 # closing ]. The list is wrapped in a list to prevent merging in other parsers.
 # Flatten() takes care that the list from Repeat() and the single entry are made
 # into one list.
-List = Flatten(Skip(String('[')) +
-        Repeat(midentry, -1) +
-        entry +
+List = Last(Skip(String('[')) +
+        ((Repeat(midentry, -1) + entry) >> concat_elems_elem) +
         Skip(String(']')))
 
 # DICTS