diff options
author | Boutade <thegoofist@protonmail.com> | 2020-04-22 21:34:50 -0500 |
---|---|---|
committer | Boutade <thegoofist@protonmail.com> | 2020-04-22 21:34:50 -0500 |
commit | ec36347f8f91d365d3563513e1731ee368ca34fb (patch) | |
tree | 5858e00ff6cf12e7bea1c4f108102b187806de40 | |
parent | db9bab71b4660062c601ab527b1dfdbcc884cc21 (diff) |
<<sep-by* and <<filter
-rw-r--r-- | README.org | 2 | ||||
-rw-r--r-- | package.lisp | 2 | ||||
-rw-r--r-- | parzival.lisp | 13 |
3 files changed, 16 insertions, 1 deletions
@@ -8,7 +8,7 @@ In =parzival=, a *parser is a function* that accepts a *stream* and returns three values: - 1. A *parse result* (or =nil=), which is any value. + 1. A *parse result*, which is any value. 2. A *success indicator*, which is =t= or =nil=. 3. The possibly modified *stream* that was initially passed in. diff --git a/package.lisp b/package.lisp index dc26c34..e940a4a 100644 --- a/package.lisp +++ b/package.lisp @@ -16,6 +16,7 @@ #:<<until #:<<ignore-until #:<<or + #:<<filter #:<<~ #:<<? #:<<any-char @@ -68,6 +69,7 @@ #:<<min-times #:<<max-times #:<<sep-by + #:<<sep-by* #:<<brackets #:<<char-brackets #:<<string diff --git a/parzival.lisp b/parzival.lisp index 4c47c32..7eaa611 100644 --- a/parzival.lisp +++ b/parzival.lisp @@ -152,6 +152,14 @@ in then. If the parse fails the combinator else is run instead." (<<and <item< #'rec)))))) #'rec)) +(defun <<filter (parser pred) + "Condition a successful parse on a predicate" + (<<bind parser + (lambda (parsed) + (if (funcall pred parsed) + (<<result parsed) + <fail<)))) + (defun <<or (&rest parsers) "Tries each parser one after the other, rewinding the input stream after each failure, and resulting in the first successful parse." @@ -425,6 +433,10 @@ the character C." (<<cons value-parser (<<* (<<and separator-parser value-parser)))) +(defun <<sep-by* (value-parser separator-parser) + "Just like <<SEP-BY, but matches an empty sequence" + (<<? (<<sep-by value-parser separator-parser))) + (defun <<brackets (left center right) (<<and left (<<bind center (lambda (bracketed-value) @@ -525,3 +537,4 @@ the character C." (defun returning (x) "Returns a lambda that returns x no matter what it gets as an argument" (lambda (ignore) x)) + |