changeset 9:3de557dc12a0 draft

Implement arith_test Parens parser more succinctly
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 19 May 2019 17:17:25 +0200
parents 07dfebce36c5
children d29f53479f83
files arith_test.py
diffstat 1 files changed, 6 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/arith_test.py	Sun May 19 17:17:10 2019 +0200
+++ b/arith_test.py	Sun May 19 17:17:25 2019 +0200
@@ -8,35 +8,16 @@
 
 from combinators import *
 
-class Parens(Parser):
-
-    def parse(self, st):
-        initial = st.index()
-
-        p1, st = Operator('(').parse(st)
-        print('pr', p1, st)
-        if p1 is None:
-            st.reset(initial)
-            return None, st
 
-        term, st = Term().parse(st)
-        print('pr', term, st)
-        if term is None:
-            st.reset(initial)
-            return None, st
+def Parens():
+    return (Operator('(') + Term() + Operator(')')) >> (lambda l: l[1])
 
-        p2, st = Operator(')').parse(st)
-        print('pr', p2, st)
-        if p2 is None:
-            print('No closing paren!')
-            st.reset(initial)
-            return None, st
-
-        return term, st
+def Variable():
+    return Last(Whitespace() + Regex('\w+'))
 
 def Atom():
-    """An atom is a variable or a float."""
-    return (Float() | Parens() | Regex('\w+'))
+    """An atom is a variable or a float or a parentheses term."""
+    return (Variable() | Parens() | Float())
 
 def Operator(set):
     """An operator or parenthesis."""