The position in a stream

Discussion of Common Lisp
Post Reply
Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

The position in a stream

Post by Goheeca » Thu Aug 09, 2012 7:42 am

I have a question, whether the position in:

Code: Select all

(set-macro-character #\! #'(lambda (s c) (format t "~s:~a~%" c (file-position s)) (values)))
is determined by the standard during reading from a string:

Code: Select all

(read-from-string "(1 ! 2)")
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: The position in a stream

Post by ramarren » Fri Aug 10, 2012 8:18 am

I have a strong feeling that this is not an use case anyone writing the standard thought of. I would say strictly speaking no, since the only requirement is "as if read had been called on an input stream containing those same characters. ", without requiring that the stream is fresh, although I don't really see how the stream could be reused in practice.

And in any case the file position glossary entry says that for character streams the requirement is only the the position is monotonic, so the exact value is definitely not specified.

pjstirling
Posts: 166
Joined: Sun Nov 28, 2010 4:21 pm

Re: The position in a stream

Post by pjstirling » Fri Aug 10, 2012 10:05 am

Er, what is the problem here?

The obvious implementation of READ-FROM-STRING is in terms of MAKE-STRING-INPUT-STREAM and READ (looking at the sbcl source this is definitely the case here, in the non-compiler-macro case)

FILE-POSITION should probably be named STREAM-POSITION, because it is defined in terms of streams, so it should give you an appropriate value.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: The position in a stream

Post by Goheeca » Sun Aug 12, 2012 5:16 am

Hm
I'm writing an indentation-sensitive reader macro, which works great, but I've invented usage of that in a nested invocation, which is a bit different and it needs to know where is the begin of line where the macro has invoked.
So I need some generalization (more characters) of read/unread/peek in both direction (back and forth) or an elegant way to find out where in the string it leads to invocation of my macro.
// I tried this with SBCL and CLISP both work, but for example a lisp by ufasoft (and they claim it's CL*) in the mobile version it leads to a type-error that on the string-stream can't do that *on the other hand file-position exceptional situations disallow this behavior.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

pjstirling
Posts: 166
Joined: Sun Nov 28, 2010 4:21 pm

Re: The position in a stream

Post by pjstirling » Sun Aug 12, 2012 9:25 am

Filing a bug against the implementation is the appropriate thing to do here, but since their last build was in 2009 that may not get you anywhere.

Code: Select all

(defun stream-position (stream)
  #+ufasoft-lisp
  (if (string-stream-p stream)
      (string-input-stream-index stream)
      ;; else
      (file-position stream))
  #-ufasoft-lisp
  (file-position stream))
might work, I get `make: *** No rule to make target `lispeng/lispmsg.mc', needed by `lispmsg.h'. Stop.` when I try to compile the runtime so I can't check

Post Reply