Skip to content

Commit

Permalink
Merge pull request #8 from fiddlerwoaroof/master
Browse files Browse the repository at this point in the history
Add a restart for better error-recovery
  • Loading branch information
drewc committed Apr 21, 2016
2 parents 25a5fa3 + 311422a commit 3da6123
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion smug.lisp
Expand Up @@ -10,6 +10,7 @@
#:input-empty-p
#:input-first
#:input-rest
#:replace-invalid
#:run
#:parse
#:.plus
Expand Down Expand Up @@ -47,8 +48,27 @@
,@body))))
`(progn ,@body)))

(defun replace-subseq (needle haystack replacement)
(let* ((haystack (copy-seq haystack))
(idx (search needle haystack :test 'equal)))
(when idx
(replace haystack replacement :start1 idx))
haystack))

(defun replace-invalid (old new)
(let ((replace-invalid (find-restart 'replace-invalid)))
(when replace-invalid
(invoke-restart replace-invalid old new))))

(defun run (parser input)
(funcall parser input))
(restart-case
(progn
(funcall parser input))
(replace-invalid (old new)
(let ((next-replace-invalid (find-restart 'replace-invalid)))
(if (and next-replace-invalid (not (search old input)))
(invoke-restart 'replace-invalid old new)
(funcall parser (replace-subseq old (copy-seq input) new)))))))

(defun parse (parser input)
(let ((result (run parser input)))
Expand Down

0 comments on commit 3da6123

Please sign in to comment.