changeset 33:26f8bc28ebfa draft

Extend README examples.
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 22 May 2019 01:00:50 +0200
parents 233370104f9d
children 02cf2ee329a3
files README.md
diffstat 1 files changed, 15 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/README.md	Wed May 22 00:57:28 2019 +0200
+++ b/README.md	Wed May 22 01:00:50 2019 +0200
@@ -3,7 +3,11 @@
 Working on parser combinators for Python, in an understandable manner. I've
 always been fascinated by them, so I wanted to try if I can implement them :-)
 
-For example:
+There are examples in the form of
+* a JSON parser in `pcombinators/json_test.py` and
+* a parser for arithmetic expressions in `pcombinators/arith_test.py`.
+
+More simple examples:
 
 ```python
 
@@ -15,14 +19,21 @@
 p.parse(st)
 # >> (['Hello', ',', 'World', '!'], ParseState(Hello, World!<>))<Paste>
 
-(Float() + String(" ") + NonEmptyString()).parse(ParseState('1.22 abc'))
-# >> ([1.22, ' ', 'abc'], ParseState(1.22 abc<>))
+# '+' constructs AtomicSequence() parsers, which only succeed if every parser succeeds in order
+# (OptimisticSequence() just goes as far as it can). Sequence parsers result in a list of the
+# individual parsers' results.
+#
+# Skip(p) makes the result of a parser disappear; useful when you need to consume input but not use
+# it after. Without Skip, an empty string would appear in the result list.
+(Float() + Skip(String(" ")) + NonEmptyString()).parse(ParseState('1.22 abc'))
+# >> ([1.22, 'abc'], ParseState(1.22 abc<>))
 
 def upper(s):
     return s.upper()
 
 # You can transform parser results with the >> (right shift) operator, and
-# repeat parsers with the * (multiplication) operator.
+# repeat parsers with the * (multiplication) operator. Note that Repeat() and StrictRepeat() offer
+# finer control over the behavior.
 
 # Parse two non-whitespace strings, converting them to uppercase, and a float,
 # multiplying the latter by 2.