104 lines
12 KiB
HTML
104 lines
12 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Function READ, READ-PRESERVING-WHITESPACE</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="f_mk_dis.htm">
|
|
<LINK REL=UP HREF="c_reader.htm">
|
|
<LINK REL=NEXT HREF="f_rd_del.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="f_mk_dis.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_reader.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="f_rd_del.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="read"><A NAME="read-preserving-whitespace"><I>Function</I> <B>READ, READ-PRESERVING-WHITESPACE</B></A></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>read</B> <I><TT>&optional</TT> input-stream eof-error-p eof-value recursive-p</I> => <I>object</I><P>
|
|
<P>
|
|
|
|
<B>read-preserving-whitespace</B> <I><TT>&optional</TT> input-stream eof-error-p eof-value recursive-p</I><P> => <I>object</I><P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>input-stream</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#input"><I>input</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#stream_designator"><I>stream designator</I></A>. <P>
|
|
<I>eof-error-p</I>---a <A REL=DEFINITION HREF="26_glo_g.htm#generalized_boolean"><I>generalized boolean</I></A>. The default is <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A>. <P>
|
|
<I>eof-value</I>---an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. The default is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
|
|
<I>recursive-p</I>---a <A REL=DEFINITION HREF="26_glo_g.htm#generalized_boolean"><I>generalized boolean</I></A>. The default is <A REL=DEFINITION HREF="26_glo_f.htm#false"><I>false</I></A>. <P>
|
|
<I>object</I>---an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> (parsed by the <A REL=DEFINITION HREF="26_glo_l.htm#lisp_reader"><I>Lisp reader</I></A>) or the <I>eof-value</I>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#read"><B>read</B></A> parses the printed representation of an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> from <I>input-stream</I> and builds such an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P>
|
|
<A REL=DEFINITION HREF="#read-preserving-whitespace"><B>read-preserving-whitespace</B></A> is like <A REL=DEFINITION HREF="#read"><B>read</B></A> but preserves any <A REL=DEFINITION HREF="26_glo_w.htm#whitespace"><I>whitespace</I></A>[2] <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> that delimits the printed representation of the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <A REL=DEFINITION HREF="#read-preserving-whitespace"><B>read-preserving-whitespace</B></A> is exactly like <A REL=DEFINITION HREF="#read"><B>read</B></A> when the <I>recursive-p</I> <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>argument</I></A> to <A REL=DEFINITION HREF="#read-preserving-whitespace"><B>read-preserving-whitespace</B></A> is <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A>. <P>
|
|
When <A REL=DEFINITION HREF="v_rd_sup.htm#STread-suppressST"><B>*read-suppress*</B></A> is <A REL=DEFINITION HREF="26_glo_f.htm#false"><I>false</I></A>, <A REL=DEFINITION HREF="#read"><B>read</B></A> throws away the delimiting <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> required by certain printed representations if it is a <A REL=DEFINITION HREF="26_glo_w.htm#whitespace"><I>whitespace</I></A>[2] <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>; but <A REL=DEFINITION HREF="#read"><B>read</B></A> preserves the character (using <A REL=DEFINITION HREF="f_unrd_c.htm#unread-char"><B>unread-char</B></A>) if it is syntactically meaningful, because it could be the start of the next expression. <P>
|
|
If a file ends in a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> or a <A REL=DEFINITION HREF="26_glo_n.htm#number"><I>number</I></A> immediately followed by an <A REL=DEFINITION HREF="26_glo_e.htm#end_of_file"><I>end of file</I></A>[1], <A REL=DEFINITION HREF="#read"><B>read</B></A> reads the <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> or <A REL=DEFINITION HREF="26_glo_n.htm#number"><I>number</I></A> successfully; when called again, it sees the <A REL=DEFINITION HREF="26_glo_e.htm#end_of_file"><I>end of file</I></A>[1] and only then acts according to <I>eof-error-p</I>. If a file contains ignorable text at the end, such as blank lines and comments, <A REL=DEFINITION HREF="#read"><B>read</B></A> does not consider it to end in the middle of an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P>
|
|
If <I>recursive-p</I> is <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A>, the call to <A REL=DEFINITION HREF="#read"><B>read</B></A> is expected to be made from within some function that itself has been called from <A REL=DEFINITION HREF="#read"><B>read</B></A> or from a similar input function, rather than from the top level. <P>
|
|
Both functions return the <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> read from <I>input-stream</I>. <I>Eof-value</I> is returned if <I>eof-error-p</I> is <A REL=DEFINITION HREF="26_glo_f.htm#false"><I>false</I></A> and end of file is reached before the beginning of an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P>
|
|
<P><B>Examples:</B><P>
|
|
<P>
|
|
<PRE>
|
|
(read)
|
|
>> 'a
|
|
=> (QUOTE A)
|
|
(with-input-from-string (is " ") (read is nil 'the-end)) => THE-END
|
|
(defun skip-then-read-char (s c n)
|
|
(if (char= c #\{) (read s t nil t) (read-preserving-whitespace s))
|
|
(read-char-no-hang s)) => SKIP-THEN-READ-CHAR
|
|
(let ((*readtable* (copy-readtable nil)))
|
|
(set-dispatch-macro-character #\# #\{ #'skip-then-read-char)
|
|
(set-dispatch-macro-character #\# #\} #'skip-then-read-char)
|
|
(with-input-from-string (is "#{123 x #}123 y")
|
|
(format t "~S ~S" (read is) (read is)))) => #\x, #\Space, NIL
|
|
</PRE>
|
|
</TT> <P>
|
|
As an example, consider this <A REL=DEFINITION HREF="26_glo_r.htm#reader_macro"><I>reader macro</I></A> definition: <P>
|
|
<PRE>
|
|
(defun slash-reader (stream char)
|
|
(declare (ignore char))
|
|
`(path . ,(loop for dir = (read-preserving-whitespace stream t nil t)
|
|
then (progn (read-char stream t nil t)
|
|
(read-preserving-whitespace stream t nil t))
|
|
collect dir
|
|
while (eql (peek-char nil stream nil nil t) #\/))))
|
|
(set-macro-character #\/ #'slash-reader)
|
|
</PRE>
|
|
</TT> <P>
|
|
Consider now calling <A REL=DEFINITION HREF="#read"><B>read</B></A> on this expression: <P>
|
|
<PRE>
|
|
(zyedh /usr/games/zork /usr/games/boggle)
|
|
</PRE>
|
|
</TT> The <TT>/</TT> macro reads objects separated by more <TT>/</TT> characters; thus <TT>/usr/games/zork</TT> is intended to read as <TT>(path usr games zork)</TT>. The entire example expression should therefore be read as <P>
|
|
<PRE>
|
|
(zyedh (path usr games zork) (path usr games boggle))
|
|
</PRE>
|
|
</TT> However, if <A REL=DEFINITION HREF="#read"><B>read</B></A> had been used instead of <A REL=DEFINITION HREF="#read-preserving-whitespace"><B>read-preserving-whitespace</B></A>, then after the reading of the symbol <TT>zork</TT>, the following space would be discarded; the next call to <A REL=DEFINITION HREF="f_peek_c.htm#peek-char"><B>peek-char</B></A> would see the following <TT>/</TT>, and the loop would continue, producing this interpretation: <P>
|
|
<PRE>
|
|
(zyedh (path usr games zork usr games boggle))
|
|
</PRE>
|
|
</TT> There are times when <A REL=DEFINITION HREF="26_glo_w.htm#whitespace"><I>whitespace</I></A>[2] should be discarded. If a command interpreter takes single-character commands, but occasionally reads an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> then if the <A REL=DEFINITION HREF="26_glo_w.htm#whitespace"><I>whitespace</I></A>[2] after a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> is not discarded it might be interpreted as a command some time later after the <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> had been read. <P>
|
|
<P><B>Affected By:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="v_debug_.htm#STstandard-inputST"><B>*standard-input*</B></A>, <A REL=DEFINITION HREF="v_termin.htm#STterminal-ioST"><B>*terminal-io*</B></A>, <A REL=DEFINITION HREF="v_rdtabl.htm#STreadtableST"><B>*readtable*</B></A>, <A REL=DEFINITION HREF="v_rd_def.htm#STread-default-float-formatST"><B>*read-default-float-format*</B></A>, <A REL=DEFINITION HREF="v_rd_bas.htm#STread-baseST"><B>*read-base*</B></A>, <A REL=DEFINITION HREF="v_rd_sup.htm#STread-suppressST"><B>*read-suppress*</B></A>, <A REL=DEFINITION HREF="v_pkg.htm#STpackageST"><B>*package*</B></A>, <A REL=DEFINITION HREF="v_rd_eva.htm#STread-evalST"><B>*read-eval*</B></A>. <P>
|
|
<P><B>Exceptional Situations:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#read"><B>read</B></A> signals an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_end_of.htm#end-of-file"><B>end-of-file</B></A>, regardless of <I>eof-error-p</I>, if the file ends in the middle of an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> representation. For example, if a file does not contain enough right parentheses to balance the left parentheses in it, <A REL=DEFINITION HREF="#read"><B>read</B></A> signals an error. This is detected when <A REL=DEFINITION HREF="#read"><B>read</B></A> or <A REL=DEFINITION HREF="#read-preserving-whitespace"><B>read-preserving-whitespace</B></A> is called with <I>recursive-p</I> and <I>eof-error-p</I> <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>, and end-of-file is reached before the beginning of an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P>
|
|
If <I>eof-error-p</I> is <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A>, an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_end_of.htm#end-of-file"><B>end-of-file</B></A> is signaled at the end of file. <P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="f_peek_c.htm#peek-char"><B>peek-char</B></A>, <A REL=DEFINITION HREF="f_rd_cha.htm#read-char"><B>read-char</B></A>, <A REL=DEFINITION HREF="f_unrd_c.htm#unread-char"><B>unread-char</B></A>, <A REL=DEFINITION HREF="f_rd_fro.htm#read-from-string"><B>read-from-string</B></A>, <A REL=DEFINITION HREF="f_rd_del.htm#read-delimited-list"><B>read-delimited-list</B></A>, <A REL=DEFINITION HREF="f_parse_.htm#parse-integer"><B>parse-integer</B></A>, <A REL=CHILD HREF="02_.htm">Section 2 (Syntax)</A>, <A REL=CHILD HREF="23_a.htm">Section 23.1 (Reader Concepts)</A> <P>
|
|
<P><B>Notes:</B> None.
|
|
<P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss013.htm">ARGUMENTS-UNDERSPECIFIED:SPECIFY</A><P></UL><HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|