From: Alberto Lavelli
Subject: read-from-string
Date: 
Message-ID: <4j8vs6$7ui@wonder.itc.it>
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?

thanks

	alberto

From: William Paul Vrotney
Subject: Re: read-from-string
Date: 
Message-ID: <vrotneyDowKGn.329@netcom.com>
In article <··········@wonder.itc.it> ·······@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))


-- 

William P. Vrotney - ·······@netcom.com
From: Marco Antoniotti
Subject: Re: read-from-string
Date: 
Message-ID: <s08u3za1nxh.fsf@lox.ICSI.Berkeley.EDU>
In article <·················@netcom.com> ·······@netcom.com (William Paul Vrotney) writes:

   From: ·······@netcom.com (William Paul Vrotney)
   Newsgroups: comp.lang.lisp
   Date: Wed, 27 Mar 1996 01:36:22 GMT
   Organization: NETCOM On-line Communication Services (408 261-4700 guest)
   References: <··········@wonder.itc.it>
   Lines: 33
   Sender: ·······@netcom.netcom.com

   In article <··········@wonder.itc.it> ·······@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))

Apart from this,  I noticed the same behavior in CMUCL 17f and ECL
0.22.  However, it would seem that such behavior is inappropriate.

The call

	(read-from-string "(A S" nil 'incomplete-sexpr))

should, at least as I understand it, return 'incomplete-sexpr because
of the eof-error-p setting to nil.

Any idea?

Marco
-- 
Marco Antoniotti - Resistente Umano
===============================================================================
International Computer Science Institute	| ·······@icsi.berkeley.edu
1947 Center STR, Suite 600			| tel. +1 (510) 643 9153
Berkeley, CA, 94704-1198, USA			|      +1 (510) 642 4274 x149
===============================================================================
	...it is simplicity that is difficult to make.
	...e` la semplicita` che e` difficile a farsi.
				Bertholdt Brecht
From: William Paul Vrotney
Subject: Re: read-from-string
Date: 
Message-ID: <vrotneyDp0G5r.G8u@netcom.com>
In article <···············@lox.ICSI.Berkeley.EDU>
·······@lox.icsi.berkeley.edu (Marco Antoniotti) writes:

> 
> In article <·················@netcom.com> ·······@netcom.com (William Paul Vrotney) writes:
> 
> 
>    In article <··········@wonder.itc.it> ·······@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 - ·······@netcom.com
From: Marco Antoniotti
Subject: Re: read-from-string
Date: 
Message-ID: <s08lokjn3jc.fsf@lox.ICSI.Berkeley.EDU>
In article <·················@netcom.com> ·······@netcom.com (William Paul Vrotney) writes:

   From: ·······@netcom.com (William Paul Vrotney)
   Newsgroups: comp.lang.lisp
   Date: Fri, 29 Mar 1996 03:53:50 GMT
   Organization: NETCOM On-line Communication Services (408 261-4700 guest)
   Path: agate!howland.reston.ans.net!ix.netcom.com!netcom.com!vrotney
   Lines: 66
   Sender: ·······@netcom.netcom.com

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

	...

   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.

Oh come on!  I love quibbling over something fun for once, instead of
the usual "C++ good, Lisp no good" stuff (or viceversa). :) :)

I think this is a case where the error semantics should be specified a
little better.  I interpreted the request as one for an essentially
boolean function returing a yes or no.

A better definition would be

(defun sexpr-in-string-p (s)
  (handler-case
     (progn
       (read-from-string s)
       (values t nil))
    (end-of-file () (values nil 'eof-in-string))
    (reader-error () (values nil 'unreadable-object))
    (error (cnd) nil (values nil cnd))
    ))

An so on...

Or more simply

(defun sexpr-in-string-p (s)
  (handler-case
     (progn
       (read-from-string s)
       (values t nil))
   (error (cnd) (values nil cnd))))

You can change it as you like it. :)

Cheers

-- 
Marco Antoniotti - Resistente Umano
===============================================================================
International Computer Science Institute	| ·······@icsi.berkeley.edu
1947 Center STR, Suite 600			| tel. +1 (510) 643 9153
Berkeley, CA, 94704-1198, USA			|      +1 (510) 642 4274 x149
===============================================================================
	...it is simplicity that is difficult to make.
	...e` la semplicita` che e` difficile a farsi.
				Bertholdt Brecht
From: Barry Margolin
Subject: Re: read-from-string
Date: 
Message-ID: <4jei39$44a@tools.bbnplanet.com>
In article <··········@wonder.itc.it>, Alberto Lavelli <·······@irst.it> wrote:
>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?

eof-error-p controls what happens if EOF is encountered *before* any
objects are read.

As it says at the top of p. 568 of CLtL2:

	Functions such as READ that read the representation of an object
	rather than a single character will always signal an error,
	regardless of <eof-error-p>, if the file ends in the middle of an
	object representation. ...  Thus an <eof-error-p> argument controls
	what happens when the file ends *between* objects.

The purpose of eof-error-p is to allow you to tell when you've read all the
objects in a file (or other stream).  You can't simply check whether you're
at EOF before doing the read, because there could be trailing whitespace
between the last object and EOF.
-- 
Barry Margolin
BBN PlaNET Corporation, Cambridge, MA
······@bbnplanet.com
Phone (617) 873-3126 - Fax (617) 873-6351