1
0
Fork 0
cl-sites/novaspec.org/cl/f_read.html
2025-02-05 18:52:26 +01:00

688 lines
No EOL
15 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>read, read-preserving-whitespace | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_read.html"
><LINK REL="next" HREF="f_read-delimited-list.html" TYPE="text/html" TITLE="read-delimited-list"
><LINK REL="prev" HREF="f_make-dispatch-macro-character.html" TYPE="text/html" TITLE="make-dispatch-macro-character"
><LINK REL="up" HREF="23_2_Reader_Dictionary.html" TYPE="text/html" TITLE="23.2 Reader Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="23_Reader.html"
>23. Reader</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="23_2_Reader_Dictionary.html"
>23.2 Reader Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_read.html"
>read, read-preserving-whitespace</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_make-dispatch-macro-character.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="f_read-delimited-list.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="read, read-preserving-whitespace"
></SPAN
><B
>read, read-preserving-whitespace</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Function</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
><B
>read</B
> <SPAN CLASS="cmtt"
>&amp;optional</SPAN
> <SPAN CLASS="cmssi"
>input-stream</SPAN
> <SPAN CLASS="cmssi"
>eof-error-p</SPAN
> <SPAN CLASS="cmssi"
>eof-value</SPAN
> <SPAN CLASS="cmssi"
>recursive-p</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>object</SPAN
></P
><TABLE CELLPADDING="0" CELLSPACING="0"
><TR
><TD VALIGN="BASELINE" ALIGN="LEFT" NOWRAP="NOWRAP"
><B
>read-preserving-whitespace</B
><SPAN CLASS="cmtt"
>&#160;</SPAN
><SPAN CLASS="cmtt"
>&amp;optional</SPAN
><SPAN CLASS="cmssi"
>&#160;</SPAN
></TD
><TD VALIGN="BASELINE" ALIGN="LEFT"
><SPAN CLASS="cmssi"
>input-stream</SPAN
> <SPAN CLASS="cmssi"
>eof-error-p</SPAN
><BR
><SPAN CLASS="cmssi"
>eof-value</SPAN
> <SPAN CLASS="cmssi"
>recursive-p</SPAN
></TD
></TR
></TABLE
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmssi"
>object</SPAN
></DIV
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>input-stream</VAR
> &#8212; an <A HREF="26_1_Glossary.html#input"
><EM CLASS="term"
>input</EM
></A
> <A HREF="26_1_Glossary.html#stream_designator"
><EM CLASS="term"
>stream designator</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>eof-error-p</VAR
> &#8212; a <A HREF="26_1_Glossary.html#generalized_boolean"
><EM CLASS="term"
>generalized boolean</EM
></A
>. The default is <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>eof-value</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. The default is <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. </P
><P CLASS="j"
><VAR CLASS="param"
>recursive-p</VAR
> &#8212; a <A HREF="26_1_Glossary.html#generalized_boolean"
><EM CLASS="term"
>generalized boolean</EM
></A
>. The default is <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>object</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> (parsed by the <A HREF="26_1_Glossary.html#Lisp_reader"
><EM CLASS="term"
>Lisp reader</EM
></A
>) or the <VAR CLASS="param"
>eof-value</VAR
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> parses the printed representation of an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> from <VAR CLASS="param"
>input-stream</VAR
> and builds such an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. </P
><P CLASS="j"
><A HREF="f_read.html" CLASS="funref"
><B
>read-preserving-whitespace</B
></A
> is like <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> but preserves any <A HREF="26_1_Glossary.html#whitespace"
><EM CLASS="term"
>whitespace</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>2</SPAN
></SUB
></SPAN
> <A HREF="26_1_Glossary.html#character"
><EM CLASS="term"
>character</EM
></A
> that delimits the printed representation of the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. <A HREF="f_read.html" CLASS="funref"
><B
>read-preserving-whitespace</B
></A
> is exactly like <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> when the <VAR CLASS="param"
>recursive-p</VAR
> <A HREF="26_1_Glossary.html#argument"
><EM CLASS="term"
>argument</EM
></A
> to <A HREF="f_read.html" CLASS="funref"
><B
>read-preserving-whitespace</B
></A
> is <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>. </P
><P CLASS="j"
>When <A HREF="v_read-suppress.html" CLASS="varref"
><B
>*read-suppress*</B
></A
> is <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>, <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> throws away the delimiting <A HREF="26_1_Glossary.html#character"
><EM CLASS="term"
>character</EM
></A
> required by certain printed representations if it is a <A HREF="26_1_Glossary.html#whitespace"
><EM CLASS="term"
>whitespace</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>2</SPAN
></SUB
></SPAN
> <A HREF="26_1_Glossary.html#character"
><EM CLASS="term"
>character</EM
></A
>; but <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> preserves the character (using <A HREF="f_unread-char.html" CLASS="funref"
><B
>unread-char</B
></A
>) if it is syntactically meaningful, because it could be the start of the next expression. </P
><P CLASS="j"
>If a file ends in a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> or a <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>number</EM
></A
> immediately followed by an <A HREF="26_1_Glossary.html#end_of_file"
><EM CLASS="term"
>end of file</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
>, <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> reads the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> or <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>number</EM
></A
> successfully; when called again, it sees the <A HREF="26_1_Glossary.html#end_of_file"
><EM CLASS="term"
>end of file</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>1</SPAN
></SUB
></SPAN
> and only then acts according to <VAR CLASS="param"
>eof-error-p</VAR
>. If a file contains ignorable text at the end, such as blank lines and comments, <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> does not consider it to end in the middle of an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. </P
><P CLASS="j"
>If <VAR CLASS="param"
>recursive-p</VAR
> is <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>, the call to <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> is expected to be made from within some function that itself has been called from <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> or from a similar input function, rather than from the top level. </P
><P CLASS="j"
>Both functions return the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> read from <VAR CLASS="param"
>input-stream</VAR
>. <VAR CLASS="param"
>Eof-value</VAR
> is returned if <VAR CLASS="param"
>eof-error-p</VAR
> is <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
> and end of file is reached before the beginning of an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (read)
<SPAN CLASS="cmr"
>&#8883;</SPAN
> <U CLASS="underline"
><SPAN CLASS="cmtt"
>'a</SPAN
></U
>
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (QUOTE A)
(with-input-from-string (is " ") (read is nil 'the-end)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 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)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 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)))) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #\x, #\Space, NIL</PRE
><P CLASS="j"
>As an example, consider this <A HREF="26_1_Glossary.html#reader_macro"
><EM CLASS="term"
>reader macro</EM
></A
> definition:</P
><PRE CLASS="screen"
>(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
><P CLASS="j"
>Consider now calling <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> on this expression:</P
><PRE CLASS="screen"
>(zyedh /usr/games/zork /usr/games/boggle)</PRE
><P CLASS="j"
>The <CODE CLASS="f"
>/</CODE
> macro reads objects separated by more <CODE CLASS="f"
>/</CODE
> characters; thus <CODE CLASS="f"
>/usr/games/zork</CODE
> is intended to read as <CODE CLASS="f"
>(path usr games zork)</CODE
>. The entire example expression should therefore be read as</P
><PRE CLASS="screen"
>(zyedh (path usr games zork) (path usr games boggle))</PRE
><P CLASS="j"
>However, if <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> had been used instead of <A HREF="f_read.html" CLASS="funref"
><B
>read-preserving-whitespace</B
></A
>, then after the reading of the symbol <CODE CLASS="f"
>zork</CODE
>, the following space would be discarded; the next call to <A HREF="f_peek-char.html" CLASS="funref"
><B
>peek-char</B
></A
> would see the following <CODE CLASS="f"
>/</CODE
>, and the loop would continue, producing this interpretation:</P
><PRE CLASS="screen"
>(zyedh (path usr games zork usr games boggle))</PRE
><P CLASS="j"
>There are times when <A HREF="26_1_Glossary.html#whitespace"
><EM CLASS="term"
>whitespace</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>2</SPAN
></SUB
></SPAN
> should be discarded. If a command interpreter takes single-character commands, but occasionally reads an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> then if the <A HREF="26_1_Glossary.html#whitespace"
><EM CLASS="term"
>whitespace</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>2</SPAN
></SUB
></SPAN
> after a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> is not discarded it might be interpreted as a command some time later after the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> had been read.</P
></DD
><DT
><B
>Affected By</B
></DT
><DD
><P CLASS="j"
><A HREF="v_debug-io.html" CLASS="varref"
><B
>*standard-input*</B
></A
>, <A HREF="v_terminal-io.html" CLASS="varref"
><B
>*terminal-io*</B
></A
>, <A HREF="v_readtable.html" CLASS="varref"
><B
>*readtable*</B
></A
>, <A HREF="v_read-default-float-format.html" CLASS="varref"
><B
>*read-default-float-format*</B
></A
>, <A HREF="v_read-base.html" CLASS="varref"
><B
>*read-base*</B
></A
>, <A HREF="v_read-suppress.html" CLASS="varref"
><B
>*read-suppress*</B
></A
>, <A HREF="v_package.html" CLASS="varref"
><B
>*package*</B
></A
>, <A HREF="v_read-eval.html" CLASS="varref"
><B
>*read-eval*</B
></A
>.</P
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
><A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> signals an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_end-of-file.html" CLASS="typeref"
><B
>end-of-file</B
></A
>, regardless of <VAR CLASS="param"
>eof-error-p</VAR
>, if the file ends in the middle of an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> representation. For example, if a file does not contain enough right parentheses to balance the left parentheses in it, <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> signals an error. This is detected when <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> or <A HREF="f_read.html" CLASS="funref"
><B
>read-preserving-whitespace</B
></A
> is called with <VAR CLASS="param"
>recursive-p</VAR
> and <VAR CLASS="param"
>eof-error-p</VAR
> <A HREF="26_1_Glossary.html#non-nil"
><EM CLASS="term"
>non-nil</EM
></A
>, and end-of-file is reached before the beginning of an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. </P
><P CLASS="j"
>If <VAR CLASS="param"
>eof-error-p</VAR
> is <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>, an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_end-of-file.html" CLASS="typeref"
><B
>end-of-file</B
></A
> is signaled at the end of file.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_peek-char.html" CLASS="funref"
><B
>peek-char</B
></A
>, <A HREF="f_read-char.html" CLASS="funref"
><B
>read-char</B
></A
>, <A HREF="f_unread-char.html" CLASS="funref"
><B
>unread-char</B
></A
>, <A HREF="f_read-from-string.html" CLASS="funref"
><B
>read-from-string</B
></A
>, <A HREF="f_read-delimited-list.html" CLASS="funref"
><B
>read-delimited-list</B
></A
>, <A HREF="f_parse-integer.html" CLASS="funref"
><B
>parse-integer</B
></A
>, Chapter 2 (Syntax), Section 23.1 (Reader Concepts)</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_make-dispatch-macro-character.html" CLASS="prev"
>&#8592;</A
><A HREF="f_read-delimited-list.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>