Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!swrinde!howland.reston.ans.net!ix.netcom.com!netcom.com!vrotney
From: vrotney@netcom.com (William Paul Vrotney)
Subject: Re: read-from-string
In-Reply-To: marcoxa@lox.icsi.berkeley.edu's message of 27 Mar 1996 08:49:29 -0800
Message-ID: <vrotneyDp0G5r.G8u@netcom.com>
Organization: NETCOM On-line Communication Services (408 261-4700 guest)
References: <4j8vs6$7ui@wonder.itc.it> <vrotneyDowKGn.329@netcom.com> <s08u3za1nxh.fsf@lox.ICSI.Berkeley.EDU>
Date: Fri, 29 Mar 1996 03:53:50 GMT
Lines: 66
Sender: vrotney@netcom.netcom.com

In article <s08u3za1nxh.fsf@lox.ICSI.Berkeley.EDU>
marcoxa@lox.icsi.berkeley.edu (Marco Antoniotti) writes:

> 
> In article <vrotneyDowKGn.329@netcom.com> vrotney@netcom.com (William Paul Vrotney) writes:
> 
> 
>    In article <4j8vs6$7ui@wonder.itc.it> lavelli@irst.it (Alberto Lavelli) writes:
> 
>    >    I'd need a function that tells me if a string contains a s-expr or not.  
>    >    My first thought was that of using 'read-from-string' with the eof-error-p
>    >    parameter set to nil; i.e.
>    > 
>    > 
>    >    (read-from-string "(A S" nil 'error)
>    > 
>    > 
>    >    But Allegro complains and I get an error saying that the reader has
>    >    encountered eof on the stream. I read the manual and my understanding is
>    >    this should not be the correct behaviour. Am I missing something 
>    >    fundamental?
> 
>    I noticed that in the latest Allegro CL man page for READ-FROM-STRING it
>    says
> 
>        The function read-from-string always signals an error
>        if the end of string is reached when a COMMON LISP object  is  partially
>        but not completely read.
> 
>    I know it's yuckie but you can probably solve your problem with
> 
>        (multiple-value-bind (value condition)
> 	(ignore-errors
> 	 (read-from-string "(A S"))
> 	(if (typep condition 'condition) 'error value))
> 
> 
> The best way to do this sort of things is not to 'ignore-errors' but
> to set up an appropriate handler for the end of file error generated.
> 
> 	(handler-case
> 	    (read-from-string "(A S")
> 	  (end-of-file () 'incomplete-sexpr))
> 


Not to quibble with Marco Antoniotti here but Alberto Lavelli said "I'd need
a function that tells me if a string contains a s-expr or not".  So for
example

    (multiple-value-bind (value condition)
        (ignore-errors
         (read-from-string "#<yuck>"))
      (if (typep condition 'condition) 'error value))
 
has the desired behavior, whereas

    (handler-case
     (read-from-string "#<yuck>")
     (end-of-file () 'incomplete-sexpr))

breaks.

-- 

William P. Vrotney - vrotney@netcom.com
