guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Guile 2.0.9, reader: Cannot 'read' an '*unspecified*' value


From: Artyom Poptsov
Subject: Guile 2.0.9, reader: Cannot 'read' an '*unspecified*' value
Date: Tue, 03 Nov 2015 16:56:32 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux)

Hello Guilers,

it seems that currently there's no way to 'read' back an '*unspecified*'
value, but in some cases such a feature might be handy.  Here's the
description of the problem; a patch is attached as well.

To be more specific, this expression fails in GNU Guile 2.0.9 and
2.1.0.455-73f61-dirty (which I compiled from the master branch):

--8<---------------cut here---------------start------------->8---
(read (open-input-string (object->string *unspecified*)))
-| ERROR: In procedure read:
-| ERROR: In procedure scm_lreadr: #<unknown port>:1:3: Unknown # object: #\<
-|
-| Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
--8<---------------cut here---------------end--------------->8---

I faced with this problem when tried to 'read' back a vector returned by
'make-vector' with the default filling (that are '*unspecified*'
values).

Looking through the Guile sources I found that one could fix a problem
with reading of a (custom) object using 'read-hash-extend' procedure.
But alas -- the problem, again, that I cannot return an unspecified
value from my Guile reader callback because 'scm_read_sharp' from
'libguile/read.c' considers such a value as an indication that the
procedure is unable to read an object.

With that said, I think the fix could be pretty simple -- we could
return a multiple values object from the Guile reader callback in the
case when we need to 'read' an unspecified value where the 2nd value
could indicate whether the returned unspecified value is *the* value or
an indication that we could not read the value.  As far as I understand,
this solution is backward compatible so current callbacks will work as
usual.

As an example:

--8<---------------cut here---------------start------------->8---
(read-hash-extend #\<
                  (lambda (c port)
                    (let ((str     "")
                          (pending 0)
                          (c       (read-char port)))
                      (while (not (and (char=? c #\>) (= pending 0)))
                        (and (char=? c #\<)
                             (set! pending (1+ pending)))
                        (and (char=? c #\>)
                             (set! pending (1- pending)))
                        (set! str (string-append str (string c)))
                        (set! c (read-char port)))
                      (if (string=? str "unspecified")
                          (values *unspecified* #t)
                          *unspecified*))))

(define (read-string str)
  (read (open-input-string str)))

(write (read-string (object->string (make-vector 2))))
;; => #(#<unspecified> #<unspecified>)

(write (read-string (object->string *unspecified*)))
;; => #<unspecified>
--8<---------------cut here---------------end--------------->8---

As I said, the patch is attached.  I'd love to hear any comments on the
patch, especially given that this patch is my first attempt to make a
contribution to GNU Guile.

Thanks,

- Artyom

-- 
Artyom V. Poptsov <address@hidden>;  GPG Key: 0898A02F
Home page: http://poptsov-artyom.narod.ru/

Attachment: 0001-Provide-means-to-read-an-unspecified-object.patch
Description: [PATCH] Provide means to 'read' an unspecified object

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]