changeset 81:40f9e05d8f06 draft

Move whitespace removal function to util module
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 25 May 2019 23:48:28 +0200
parents 901b2bafad36
children 27fb2e50dad2
files pcombinators/tests/json.py pcombinators/util.py
diffstat 2 files changed, 19 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/pcombinators/tests/json.py	Sat May 25 23:48:03 2019 +0200
+++ b/pcombinators/tests/json.py	Sat May 25 23:48:28 2019 +0200
@@ -8,6 +8,8 @@
 
 from pcombinators.combinators import *
 from pcombinators.primitives import *
+import pcombinators.state as st
+import pcombinators.util as ut
 
 JString = Last(Skip(String('"')) + NoneInSet('"') + Skip(String('"')))
 
@@ -66,17 +68,9 @@
 # Convert the list of tuples into a dict.
 Dict = dct >> dict
 
-def remove_unused_whitespace(s):
-    acc = []
-    lvl = 0
-    ws = set(' \n\t\r')
-    for c in s:
-        if c == '"':
-            lvl += 1 if lvl == 0 else -1
-        if lvl == 0 and c in ws:
-            continue
-        acc.append(c)
-    return ''.join(acc)
+def parse_json(json):
+    return Value().parse(st.ParseState(ut.remove_unused_whitespace(json)))
 
-def parse_json(json):
-    return Value().parse(ParseState(remove_unused_whitespace(json)))
\ No newline at end of file
+def json_result(json):
+    r, st = parse_json(json)
+    return r
\ No newline at end of file
--- a/pcombinators/util.py	Sat May 25 23:48:03 2019 +0200
+++ b/pcombinators/util.py	Sat May 25 23:48:28 2019 +0200
@@ -15,3 +15,15 @@
         print(f, time.time()-before)
         return r
     return f_
+
+def remove_unused_whitespace(s):
+    acc = []
+    lvl = 0
+    ws = set(' \n\t\r')
+    for c in s:
+        if c == '"':
+            lvl += 1 if lvl == 0 else -1
+        if lvl == 0 and c in ws:
+            continue
+        acc.append(c)
+    return ''.join(acc)