diff options
author | Boutade <thegoofist@protonmail.com> | 2019-05-03 13:50:35 -0500 |
---|---|---|
committer | Boutade <thegoofist@protonmail.com> | 2019-05-03 13:50:35 -0500 |
commit | d6b9ef40b4e98752d7969c421d04a0cb2618ff4d (patch) | |
tree | 6213184cf7c7a694ccf72a1d1c3f91ce4170451c /parzival.lisp | |
parent | 640290a3c5a08e89b8deccaff43b862bd2076982 (diff) |
Altered tests.lisp to use character input streams
Diffstat (limited to 'parzival.lisp')
-rw-r--r-- | parzival.lisp | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/parzival.lisp b/parzival.lisp index 8981bbc..4691a07 100644 --- a/parzival.lisp +++ b/parzival.lisp @@ -30,6 +30,7 @@ ;;; The PARSE function. Runs parsers. +;; TODO refactor parse to detect how best to create a replay stream, automatically (defun parse (stream parser &optional string-as-stream-p) "Parse STREAM with PARSER. If STRING-AS-STREAM-P then STREAM can be a string." (if string-as-stream-p @@ -111,12 +112,14 @@ in then. If the parse fails the combinator else is run instead." (lambda (stream) (let ((chkpt (checkpoint stream))) (<<if (result parser1 stream) - (<<result result) + (progn + (free-checkpoint stream chkpt) + (<<result result)) (progn (rewind-to stream chkpt) parser2))))) - +;; I thin i see... checkpoints to the same point are being removed when they should't be (defun <<or (parser1 parser2 &rest parsers) "Tries each parser one after the other, rewinding the input stream after each failure, and resulting in the first successful parse." @@ -326,18 +329,24 @@ the character C." (defun <<* (parser) "Runs the parser PARSER zero or more times, resulting in of list of parsed values." - (lambda (stream) - (let ((result nil) - (parser (<<~ parser))) - (labels ((rec (stream) - (multiple-value-bind - (res ok? stream2) (funcall parser stream) - (if ok? - (progn - (push res result) - (rec stream2)) - (values (nreverse result) t stream2))))) - (rec stream))))) + (<<bind (<<? parser) + (lambda (result) + (if (null result) (<<result result) + (<<map-cons result (<<* parser)))))) + + + ;; (lambda (stream) + ;; (let ((result nil) + ;; (parser (<<~ parser))) + ;; (labels ((rec (stream) + ;; (multiple-value-bind + ;; (res ok? stream2) (funcall parser stream) + ;; (if ok? + ;; (progn + ;; (push res result) + ;; (rec stream2)) + ;; (values (nreverse result) t stream2))))) + ;; (rec stream))))) (defun <<+ (parser) @@ -388,7 +397,6 @@ the character C." (<<brackets (<<char left-char) center (<<char right-char))) - ;;; VALUE PARSERS. The following section contains utilities for parsing common ;;; values like strings or numbers. |