Skip to content

Commit

Permalink
Restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
nja committed Feb 14, 2011
1 parent 78d0a66 commit 13728e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
18 changes: 11 additions & 7 deletions decode.lisp
Expand Up @@ -49,23 +49,29 @@ is UTF-8."))

(defmethod decode ((stream flexi-stream) &key &allow-other-keys)
(let ((c (code-char (peek-byte stream))))
(ccase c
(case c
(#\i (decode-integer stream))
((#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)
(decode-string stream))
(decode-string stream))
(#\l (decode-list stream))
(#\d (decode-dictionary stream)))))
(#\d (decode-dictionary stream))
(t (error 'invalid-value-type :octet c)))))

(define-condition unexpected-octet (error)
((expected-octet :initarg :expected-octet :reader expected-octet)
(actual-octet :initarg :actual-octet :reader actual-octet)))

(define-condition invalid-value-type (error)
((octet :initarg :octet :reader octet)))

(defun must-read-char (stream char)
(restart-case
(let ((byte (read-byte stream)))
(if (eql byte (char-code char))
t
(error "Expected 0x~x got 0x~x" (char-code char) byte)))
(error 'unexpected-octet
:expected-octet (char-code char)
:actual-octet byte)))
(continue () t)))

(defun char-integer-p (char)
Expand Down Expand Up @@ -114,9 +120,7 @@ is UTF-8."))
(return octets))
(retry-string (new-external-format)
:report "Set external format and continue decoding from the start of the string"
:interactive (lambda ()
(format t "Enter a flexi-stream external format: ")
(multiple-value-list (eval (read))))
:interactive read-external-format
(setf external-format new-external-format))))))

(defun decode-list (stream)
Expand Down
6 changes: 5 additions & 1 deletion dictionary.lisp
Expand Up @@ -36,7 +36,11 @@
(setf (gethash key dictionary) value)
(restart-case (error 'nonstring-dictionary-key :key key)
(skip-key ())
(use-value (key) :report "Specify string to use as key"
(use-value (key)
:report "Specify string to use as key"
:interactive (lambda ()
(format t "Enter a key string: ")
(list (read)))
(add-key-value key value))))))
(if (consp (car list)) ; alist
(dolist (cons list dictionary)
Expand Down

0 comments on commit 13728e1

Please sign in to comment.