changeset 10:d29f53479f83 draft

Improve exceptions and float parsing
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 19 May 2019 18:03:46 +0200
parents 3de557dc12a0
children b89582ed94be
files combinators.py
diffstat 1 files changed, 9 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/combinators.py	Sun May 19 17:17:25 2019 +0200
+++ b/combinators.py	Sun May 19 18:03:46 2019 +0200
@@ -122,7 +122,11 @@
         if r is None:
             st.reset(initial)
             return None, st
-        return self._transform(r), st2
+        try:
+            return self._transform(r), st2
+        except Exception as e:
+            st.reset(initial)
+            raise Exception('{} (at {} (col {}))'.format(e, st, st.index()))
 
 class _Sequence(Parser):
     _parsers = []
@@ -325,7 +329,10 @@
 
 def Float():
     """Return a parser that parses floats and results in floats. Result is float."""
-    return Last(Whitespace() + (CharSet('0123456789.') >> float))
+    number = OptimisticSequence(
+            Repeat(OneOf('-'), 1) + CharSet('0123456789'),
+            Repeat(OneOf('.'), 1) + CharSet('0123456789'))
+    return (Skip(Whitespace()) + number) >> (lambda l: float(''.join(l)))
 
 def NonEmptyString():
     """Return a parser that parses a string until the first whitespace, skipping whitespace before. Result is string."""