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

547 lines
No EOL
11 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>read-delimited-list | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_read-delimited-list.html"
><LINK REL="next" HREF="f_read-from-string.html" TYPE="text/html" TITLE="read-from-string"
><LINK REL="prev" HREF="f_read.html" TYPE="text/html" TITLE="read, read-preserving-whitespace"
><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-delimited-list.html"
>read-delimited-list</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_read.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-from-string.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-delimited-list"
></SPAN
><B
>read-delimited-list</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-delimited-list</B
> <SPAN CLASS="cmssi"
>char</SPAN
> <SPAN CLASS="cmtt"
>&amp;optional</SPAN
> <SPAN CLASS="cmssi"
>input-stream</SPAN
> <SPAN CLASS="cmssi"
>recursive-p</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>list</SPAN
></P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>char</VAR
> &#8212; a <A HREF="26_1_Glossary.html#character"
><EM CLASS="term"
>character</EM
></A
>. </P
><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
>. The default is <A HREF="26_1_Glossary.html#standard_input"
><EM CLASS="term"
>standard input</EM
></A
>. </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"
>list</VAR
> &#8212; a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> of the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>objects</EM
></A
> read.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_read-delimited-list.html" CLASS="funref"
><B
>read-delimited-list</B
></A
> reads <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>objects</EM
></A
> from <VAR CLASS="param"
>input-stream</VAR
> until the next character after an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>&#8217;s representation (ignoring <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
> characters and comments) is <VAR CLASS="param"
>char</VAR
>. </P
><P CLASS="j"
><A HREF="f_read-delimited-list.html" CLASS="funref"
><B
>read-delimited-list</B
></A
> looks ahead at each step for the next non-<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
> and peeks at it as if with <A HREF="f_peek-char.html" CLASS="funref"
><B
>peek-char</B
></A
>. If it is <VAR CLASS="param"
>char</VAR
>, then the <A HREF="26_1_Glossary.html#character"
><EM CLASS="term"
>character</EM
></A
> is consumed and the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
> of <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>objects</EM
></A
> is returned. If it is a <A HREF="26_1_Glossary.html#constituent"
><EM CLASS="term"
>constituent</EM
></A
> or <A HREF="26_1_Glossary.html#escape"
><EM CLASS="term"
>escape</EM
></A
> <A HREF="26_1_Glossary.html#character"
><EM CLASS="term"
>character</EM
></A
>, then <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> is used to read an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>, which is added to the end of the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>. If it is a <A HREF="26_1_Glossary.html#macro_character"
><EM CLASS="term"
>macro character</EM
></A
>, its <A HREF="26_1_Glossary.html#reader_macro_function"
><EM CLASS="term"
>reader macro function</EM
></A
> is called; if the function returns a <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>value</EM
></A
>, that <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>value</EM
></A
> is added to the <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>. The peek-ahead process is then repeated. </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
>, this call is expected to be embedded in a higher-level call to <A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
> or a similar function. </P
><P CLASS="j"
>It is an error to reach end-of-file during the operation of <A HREF="f_read-delimited-list.html" CLASS="funref"
><B
>read-delimited-list</B
></A
>. </P
><P CLASS="j"
>The consequences are undefined if <VAR CLASS="param"
>char</VAR
> has a <A HREF="26_1_Glossary.html#syntax_type"
><EM CLASS="term"
>syntax type</EM
></A
> of <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
> in the <A HREF="26_1_Glossary.html#current_readtable"
><EM CLASS="term"
>current readtable</EM
></A
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (read-delimited-list #\<SPAN CLASS="cmr"
>]</SPAN
>) 1 2 3 4 5 6 <SPAN CLASS="cmr"
>]</SPAN
>
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 2 3 4 5 6)</PRE
><P CLASS="j"
>Suppose you wanted <CODE CLASS="f"
>#{<I CLASS="i"
><I
>a</I
></I
> <I CLASS="i"
><I
>b</I
></I
> <I CLASS="i"
><I
>c</I
></I
> <I
>.</I
>&#8201;<I
>.</I
>&#8201;<I
>.</I
> <I CLASS="i"
><I
>z</I
></I
>}</CODE
> to read as a list of all pairs of the elements <I CLASS="i"
><I
>a</I
></I
>, <I CLASS="i"
><I
>b</I
></I
>, <I CLASS="i"
><I
>c</I
></I
>, <I
>.</I
>&#8201;<I
>.</I
>&#8201;<I
>.</I
>, <I CLASS="i"
><I
>z</I
></I
>, for example.</P
><PRE CLASS="screen"
>#{p q z a} reads as ((p q) (p z) (p a) (q z) (q a) (z a))</PRE
><P CLASS="j"
>This can be done by specifying a macro-character definition for <CODE CLASS="f"
>#{</CODE
> that does two things: reads in all the items up to the <CODE CLASS="f"
>}</CODE
>, and constructs the pairs. <A HREF="f_read-delimited-list.html" CLASS="funref"
><B
>read-delimited-list</B
></A
> performs the first task.</P
><PRE CLASS="screen"
>(defun |#{-reader| (stream char arg)
(declare (ignore char arg))
(mapcon #'(lambda (x)
(mapcar #'(lambda (y) (list (car x) y)) (cdr x)))
(read-delimited-list #\} stream t))) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> |#{-reader|
(set-dispatch-macro-character #\# #\{ #'|#{-reader|) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> T
(set-macro-character #\} (get-macro-character #\) <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>))</PRE
><P CLASS="j"
>Note that <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
> is supplied for the <VAR CLASS="param"
>recursive-p</VAR
> argument. </P
><P CLASS="j"
>It is necessary here to give a definition to the character <CODE CLASS="f"
>}</CODE
> as well to prevent it from being a constituent. If the line</P
><PRE CLASS="screen"
>(set-macro-character #\} (get-macro-character #\) <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>))</PRE
><P CLASS="j"
>shown above were not included, then the <CODE CLASS="f"
>}</CODE
> in</P
><PRE CLASS="screen"
>#{ p q z a}</PRE
><P CLASS="j"
>would be considered a constituent character, part of the symbol named <CODE CLASS="f"
>a}</CODE
>. This could be corrected by putting a space before the <CODE CLASS="f"
>}</CODE
>, but it is better to call <A HREF="f_set-macro-character.html" CLASS="funref"
><B
>set-macro-character</B
></A
>. </P
><P CLASS="j"
>Giving <CODE CLASS="f"
>}</CODE
> the same definition as the standard definition of the character <CODE CLASS="f"
>)</CODE
> has the twin benefit of making it terminate tokens for use with <A HREF="f_read-delimited-list.html" CLASS="funref"
><B
>read-delimited-list</B
></A
> and also making it invalid for use in any other context. Attempting to read a stray <CODE CLASS="f"
>}</CODE
> will signal an error.</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_readtable.html" CLASS="varref"
><B
>*readtable*</B
></A
>, <A HREF="v_terminal-io.html" CLASS="varref"
><B
>*terminal-io*</B
></A
>.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_read.html" CLASS="funref"
><B
>read</B
></A
>, <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
>.</P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
><A HREF="f_read-delimited-list.html" CLASS="funref"
><B
>read-delimited-list</B
></A
> is intended for use in implementing <A HREF="26_1_Glossary.html#reader_macro"
><EM CLASS="term"
>reader macros</EM
></A
>. Usually it is desirable for <VAR CLASS="param"
>char</VAR
> to be a <A HREF="26_1_Glossary.html#terminating"
><EM CLASS="term"
>terminating</EM
></A
> <A HREF="26_1_Glossary.html#macro_character"
><EM CLASS="term"
>macro character</EM
></A
> so that it can be used to delimit tokens; however, <A HREF="f_read-delimited-list.html" CLASS="funref"
><B
>read-delimited-list</B
></A
> makes no attempt to alter the syntax specified for <VAR CLASS="param"
>char</VAR
> by the current readtable. The caller must make any necessary changes to the readtable syntax explicitly.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_read.html" CLASS="prev"
>&#8592;</A
><A HREF="f_read-from-string.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
>