Mercurial > lbo > hg > pcombinators
changeset 14:3a28ea3325e4 draft
Fix integer parsing.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 19 May 2019 20:30:45 +0200 |
parents | 484382f18838 |
children | f405236a2f6c |
files | combinators.py |
diffstat | 1 files changed, 7 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/combinators.py Sun May 19 19:20:14 2019 +0200 +++ b/combinators.py Sun May 19 20:30:45 2019 +0200 @@ -64,10 +64,6 @@ return '' return self._input[self._index:] - def sub(self, start, length): - assert self._index+start < self._index+start+length <= len(self._input) - return ParseState(self._input[self._index+start:self._index+start+length]) - class Parser: """Super class for all parsers. Implements operator overloading for easier chaining of parsers.""" @@ -191,7 +187,8 @@ _atomic = True class OptimisticSequence(_Sequence): - """Execute a series of parsers after each other, as far as possible. Result is list of results of the parsers.""" + """Execute a series of parsers after each other, as far as possible + (until the first parser fails). Result is list of results of the parsers.""" _atomic = False class _Alternative(Parser): @@ -323,6 +320,9 @@ # Small specific parsers. +def Nothing(): + return String('') + def CharSet(s): """Matches arbitrarily many characters from the set s (which can be a string). Result is string.""" @@ -335,6 +335,7 @@ def Float(): """Return a parser that parses floats and results in floats. Result is float.""" def c(l): + """Convert parts of a number into a float.""" if l and len(l) > 0: return float(''.join(l)) return None @@ -349,4 +350,4 @@ def Whitespace(): """Parse whitespace (space, newline, tab). Result is string.""" - return CharSet(' \n\r\t') \ No newline at end of file + return CharSet(' \n\r\t') | Nothing() \ No newline at end of file