Is there something similar in Lisp?
#|
multi line comments don't work with
e.g. strange |# strings.
|#
No, but you can implement it yourself:
(defun end-reader (stream char)
(declare (ignore char))
(flet ((rd () (read-char stream nil stream t)))
(assert (eql (rd) #\E))
(assert (eql (rd) #\N))
(assert (eql (rd) #\D))
(loop until (eq (rd) stream))
(values)))
(set-macro-character #\@ #'end-reader)
@END
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
The views expressed are my own, and not those of my employer.
> On 09/04/2011 10:20, hans wrote:
>> In Perl you can put
>> __END__
>> somewhere in the file,
>> making Perl ignore all staff after that.
>>
>> Is there something similar in Lisp?
>> #|
>> multi line comments don't work with
>> e.g. strange |# strings.
>> |#
>
> No, but you can implement it yourself:
>
> (defun end-reader (stream char)
> (declare (ignore char))
> (flet ((rd () (read-char stream nil stream t)))
> (assert (eql (rd) #\E))
> (assert (eql (rd) #\N))
> (assert (eql (rd) #\D))
> (loop until (eq (rd) stream))
> (values)))
> (set-macro-character #\@ #'end-reader)
>
> @END
No good. What happens if you write: (length '(@WELCOME @AT @RHEA)) ; !
(defvar *old-readtable* (copy-readtable))
(defun end-reader (stream char)
(let ((buffer (make-array 8 :element-type 'character
:fill-pointer 0
:adjustable t)))
(vector-push-extend char buffer)
(flet ((rd () (let ((char (read-char stream nil stream t)))
(when (characterp char) (vector-push-extend char buffer))
char)))
(if (and (eql (rd) #\E)
(eql (rd) #\N)
(eql (rd) #\D)
(let ((last-char (rd)))
(or (find last-char #(#\space #\newline #\return #\linefeed #\tab #\page))
(eql stream last-char))))
(loop :until (eql stream (read-line stream nil stream t)))
(let ((*readtable* *old-readtable*))
(with-input-from-string (buf buffer)
(read (make-concatenated-stream buf stream) t nil t)))))))
(set-macro-character #\@ 'end-reader t)
'(@begin @welcome @at @rhea @end)
--> (|@BEGIN| |@WELCOME| |@AT| |@RHEA| |@END|)
@END
And this is it.
Nothing else is passed to the lisp reader.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
Sure, everything can be improved in Lisp. ;)
Now we need an escape if somebody actually wants something like this:
'(@no @end @in @sight)
Ah, you already did this.
However, the OP asked for a marker that allows for arbitrary text to
come after it...
Some time ago I accidentally dicovered that CMUCL has a way
o do this, at least for LOADed files [but it doesn't work with
COMPILE-FILE]. CMUCL wraps a CONTINUE restart around LOADs, so
you can return from a LOAD in the middle of the file by calling
the function CONTINUE, e.g.:
$ cat test_load_continue.lisp
(format t "Before the CONTINUE...~%")
(continue)
(format t "...and after the CONTINUE.~%")
Random
junk
here...
$ cmucl -noinit -load test_load_continue.lisp -eval '(quit)'
; Loading #P"/u/rpw3/test_load_continue.lisp".
Before the CONTINUE...
$
It's certainly non-portable, but the OP might look for something similar
in whatever implementation he's using. [Or do his own LOAD-wrapping...]
-Rob
-----
Rob Warnock <rp...@rpw3.org>
627 26th Avenue <http://rpw3.org/>
San Mateo, CA 94403