1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/02_b.htm
2024-04-01 10:24:07 +02:00

54 lines
18 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: Section 2.2</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="02_adga.htm">
<LINK REL=UP HREF="02_.htm">
<LINK REL=NEXT HREF="02_c.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="02_adga.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="02_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="02_c.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
2.2 Reader Algorithm</H2> <P>
This section describes the algorithm used by the <A REL=DEFINITION HREF="26_glo_l.htm#lisp_reader"><I>Lisp reader</I></A> to parse <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>objects</I></A> from an <A REL=DEFINITION HREF="26_glo_i.htm#input"><I>input</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A>, including how the <A REL=DEFINITION HREF="26_glo_l.htm#lisp_reader"><I>Lisp reader</I></A> processes <A REL=DEFINITION HREF="26_glo_m.htm#macro_character"><I>macro characters</I></A>. <P>
When dealing with <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>tokens</I></A>, the reader's basic function is to distinguish representations of <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbols</I></A> from those of <A REL=DEFINITION HREF="26_glo_n.htm#number"><I>numbers</I></A>. When a <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> is accumulated, it is assumed to represent a <A REL=DEFINITION HREF="26_glo_n.htm#number"><I>number</I></A> if it satisfies the syntax for numbers listed in <A REL=DEFINITION HREF="02_ca.htm#syntaxfornumerictokens">Figure 2-9</A>. If it does not represent a <A REL=DEFINITION HREF="26_glo_n.htm#number"><I>number</I></A>, it is then assumed to be a <A REL=DEFINITION HREF="26_glo_p.htm#potential_number"><I>potential number</I></A> if it satisfies the rules governing the syntax for a <A REL=DEFINITION HREF="26_glo_p.htm#potential_number"><I>potential number</I></A>. If a valid <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> is neither a representation of a <A REL=DEFINITION HREF="26_glo_n.htm#number"><I>number</I></A> nor a <A REL=DEFINITION HREF="26_glo_p.htm#potential_number"><I>potential number</I></A>, it represents a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A>. <P>
The algorithm performed by the <A REL=DEFINITION HREF="26_glo_l.htm#lisp_reader"><I>Lisp reader</I></A> is as follows: <P>
<P><DL><DT>1. If at end of file, end-of-file processing is performed as specified in <A REL=DEFINITION HREF="f_rd_rd.htm#read"><B>read</B></A>. Otherwise, one <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, <I>x</I>, is read from the <A REL=DEFINITION HREF="26_glo_i.htm#input"><I>input</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A>, and dispatched according to the <A REL=DEFINITION HREF="26_glo_s.htm#syntax_type"><I>syntax type</I></A> of <I>x</I> to one of steps 2 to 7. <P><DD>
<DT>2. If <I>x</I> is an <A REL=DEFINITION HREF="26_glo_i.htm#invalid"><I>invalid</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_rder_e.htm#reader-error"><B>reader-error</B></A> is signaled. <P><DD>
<DT>3. If <I>x</I> 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>, then it is discarded and step 1 is re-entered. <P><DD>
<DT>4. If <I>x</I> is a <A REL=DEFINITION HREF="26_glo_t.htm#terminating"><I>terminating</I></A> or <A REL=DEFINITION HREF="26_glo_n.htm#non-terminating"><I>non-terminating</I></A> <A REL=DEFINITION HREF="26_glo_m.htm#macro_character"><I>macro character</I></A> then its associated <A REL=DEFINITION HREF="26_glo_r.htm#reader_macro_function"><I>reader macro function</I></A> is called with two <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>arguments</I></A>, the <A REL=DEFINITION HREF="26_glo_i.htm#input"><I>input</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A> and <I>x</I>. <P><DD>
The <A REL=DEFINITION HREF="26_glo_r.htm#reader_macro_function"><I>reader macro function</I></A> may read <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>characters</I></A> from the <A REL=DEFINITION HREF="26_glo_i.htm#input"><I>input</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A>; if it does, it will see those <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>characters</I></A> following the <A REL=DEFINITION HREF="26_glo_m.htm#macro_character"><I>macro character</I></A>. The <A REL=DEFINITION HREF="26_glo_l.htm#lisp_reader"><I>Lisp reader</I></A> may be invoked recursively from the <A REL=DEFINITION HREF="26_glo_r.htm#reader_macro_function"><I>reader macro function</I></A>. <P>
The <A REL=DEFINITION HREF="26_glo_r.htm#reader_macro_function"><I>reader macro function</I></A> must not have any side effects other than on the <A REL=DEFINITION HREF="26_glo_i.htm#input"><I>input</I></A> <A REL=DEFINITION HREF="26_glo_s.htm#stream"><I>stream</I></A>; because of backtracking and restarting of the <A REL=DEFINITION HREF="f_rd_rd.htm#read"><B>read</B></A> operation, front ends to the <A REL=DEFINITION HREF="26_glo_l.htm#lisp_reader"><I>Lisp reader</I></A> (e.g., ``editors'' and ``rubout handlers'') may cause the <A REL=DEFINITION HREF="26_glo_r.htm#reader_macro_function"><I>reader macro function</I></A> to be called repeatedly during the reading of a single <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A> in which <I>x</I> only appears once. <P>
The <A REL=DEFINITION HREF="26_glo_r.htm#reader_macro_function"><I>reader macro function</I></A> may return zero values or one value. If one value is returned, then that value is returned as the result of the read operation; the algorithm is done. If zero values are returned, then step 1 is re-entered. <P>
<DT>5. If <I>x</I> is a <A REL=DEFINITION HREF="26_glo_s.htm#single_escape"><I>single escape</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> then the next <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, <I>y</I>, is read, or 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 if at the end of file. <I>y</I> is treated as if it is a <A REL=DEFINITION HREF="26_glo_c.htm#constituent"><I>constituent</I></A> whose only <A REL=DEFINITION HREF="26_glo_c.htm#constituent_trait"><I>constituent trait</I></A> is <A REL=DEFINITION HREF="26_glo_a.htm#alphabetic"><I>alphabetic</I></A>[2]. <I>y</I> is used to begin a <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A>, and step 8 is entered. <P><DD>
<DT>6. If <I>x</I> is a <A REL=DEFINITION HREF="26_glo_m.htm#multiple_escape"><I>multiple escape</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> then a <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> (initially containing no <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>characters</I></A>) is begun and step 9 is entered. <P><DD>
<DT>7. If <I>x</I> is a <A REL=DEFINITION HREF="26_glo_c.htm#constituent"><I>constituent</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, then it begins a <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A>. After the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> is read in, it will be interpreted either as a Lisp <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> or as being of invalid syntax. If the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> represents an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>, that <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> is returned as the result of the read operation. If the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> is of invalid syntax, an error is signaled. If <I>x</I> is a <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> with <A REL=DEFINITION HREF="26_glo_c.htm#case"><I>case</I></A>, it might be replaced with the corresponding <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> of the opposite <A REL=DEFINITION HREF="26_glo_c.htm#case"><I>case</I></A>, depending on the <A REL=DEFINITION HREF="26_glo_r.htm#readtable_case"><I>readtable case</I></A> of the <A REL=DEFINITION HREF="26_glo_c.htm#current_readtable"><I>current readtable</I></A>, as outlined in <A REL=CHILD HREF="23_ab.htm">Section 23.1.2 (Effect of Readtable Case on the Lisp Reader)</A>. <I>X</I> is used to begin a <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A>, and step 8 is entered. <P><DD>
<DT>8. At this point a <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> is being accumulated, and an even number of <A REL=DEFINITION HREF="26_glo_m.htm#multiple_escape"><I>multiple escape</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>characters</I></A> have been encountered. If at end of file, step 10 is entered. Otherwise, a <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, <I>y</I>, is read, and one of the following actions is performed according to its <A REL=DEFINITION HREF="26_glo_s.htm#syntax_type"><I>syntax type</I></A>: <P><DD>
<P><DL><DT>* If <I>y</I> is a <A REL=DEFINITION HREF="26_glo_c.htm#constituent"><I>constituent</I></A> or <A REL=DEFINITION HREF="26_glo_n.htm#non-terminating"><I>non-terminating</I></A> <A REL=DEFINITION HREF="26_glo_m.htm#macro_character"><I>macro character</I></A>: <P><DD><DL><DT>-- If <I>y</I> is a <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> with <A REL=DEFINITION HREF="26_glo_c.htm#case"><I>case</I></A>, it might be replaced with the corresponding <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> of the opposite <A REL=DEFINITION HREF="26_glo_c.htm#case"><I>case</I></A>, depending on the <A REL=DEFINITION HREF="26_glo_r.htm#readtable_case"><I>readtable case</I></A> of the <A REL=DEFINITION HREF="26_glo_c.htm#current_readtable"><I>current readtable</I></A>, as outlined in <A REL=CHILD HREF="23_ab.htm">Section 23.1.2 (Effect of Readtable Case on the Lisp Reader)</A>. <DD><DT>-- <I>Y</I> is appended to the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> being built. <DD><DT>-- Step 8 is repeated. <P><DD></DL><P>
<DT>* If <I>y</I> is a <A REL=DEFINITION HREF="26_glo_s.htm#single_escape"><I>single escape</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, then the next <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, <I>z</I>, is read, or 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 if at end of file. <I>Z</I> is treated as if it is a <A REL=DEFINITION HREF="26_glo_c.htm#constituent"><I>constituent</I></A> whose only <A REL=DEFINITION HREF="26_glo_c.htm#constituent_trait"><I>constituent trait</I></A> is <A REL=DEFINITION HREF="26_glo_a.htm#alphabetic"><I>alphabetic</I></A>[2]. <I>Z</I> is appended to the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> being built, and step 8 is repeated. <P><DD>
<DT>* If <I>y</I> is a <A REL=DEFINITION HREF="26_glo_m.htm#multiple_escape"><I>multiple escape</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, then step 9 is entered. <P><DD>
<DT>* If <I>y</I> is an <A REL=DEFINITION HREF="26_glo_i.htm#invalid"><I>invalid</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_rder_e.htm#reader-error"><B>reader-error</B></A> is signaled. <P><DD>
<DT>* If <I>y</I> is a <A REL=DEFINITION HREF="26_glo_t.htm#terminating"><I>terminating</I></A> <A REL=DEFINITION HREF="26_glo_m.htm#macro_character"><I>macro character</I></A>, then it terminates the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A>. First the <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> <I>y</I> is unread (see <A REL=DEFINITION HREF="f_unrd_c.htm#unread-char"><B>unread-char</B></A>), and then step 10 is entered. <P><DD>
<DT>* If <I>y</I> 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>, then it terminates the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A>. First the <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A> <I>y</I> is unread if appropriate (see <A REL=DEFINITION HREF="f_rd_rd.htm#read-preserving-whitespace"><B>read-preserving-whitespace</B></A>), and then step 10 is entered. <P><DD></DL><P>
<DT>9. At this point a <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> is being accumulated, and an odd number of <A REL=DEFINITION HREF="26_glo_m.htm#multiple_escape"><I>multiple escape</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>characters</I></A> have been encountered. If at end of file, 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. Otherwise, a <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, <I>y</I>, is read, and one of the following actions is performed according to its <A REL=DEFINITION HREF="26_glo_s.htm#syntax_type"><I>syntax type</I></A>: <P><DD>
<P><DL><DT>* If <I>y</I> is a <A REL=DEFINITION HREF="26_glo_c.htm#constituent"><I>constituent</I></A>, macro, or <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>, <I>y</I> is treated as a <A REL=DEFINITION HREF="26_glo_c.htm#constituent"><I>constituent</I></A> whose only <A REL=DEFINITION HREF="26_glo_c.htm#constituent_trait"><I>constituent trait</I></A> is <A REL=DEFINITION HREF="26_glo_a.htm#alphabetic"><I>alphabetic</I></A>[2]. <I>Y</I> is appended to the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> being built, and step 9 is repeated. <P><DD>
<DT>* If <I>y</I> is a <A REL=DEFINITION HREF="26_glo_s.htm#single_escape"><I>single escape</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, then the next <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, <I>z</I>, is read, or 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 if at end of file. <I>Z</I> is treated as a <A REL=DEFINITION HREF="26_glo_c.htm#constituent"><I>constituent</I></A> whose only <A REL=DEFINITION HREF="26_glo_c.htm#constituent_trait"><I>constituent trait</I></A> is <A REL=DEFINITION HREF="26_glo_a.htm#alphabetic"><I>alphabetic</I></A>[2]. <I>Z</I> is appended to the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> being built, and step 9 is repeated. <P><DD>
<DT>* If <I>y</I> is a <A REL=DEFINITION HREF="26_glo_m.htm#multiple_escape"><I>multiple escape</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, then step 8 is entered. <P><DD>
<DT>* If <I>y</I> is an <A REL=DEFINITION HREF="26_glo_i.htm#invalid"><I>invalid</I></A> <A REL=DEFINITION HREF="26_glo_c.htm#character"><I>character</I></A>, an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_rder_e.htm#reader-error"><B>reader-error</B></A> is signaled. <P><DD></DL><P>
<DT>10. An entire <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> has been accumulated. The <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> represented by the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> is returned as the result of the read operation, or an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_rder_e.htm#reader-error"><B>reader-error</B></A> is signaled if the <A REL=DEFINITION HREF="26_glo_t.htm#token"><I>token</I></A> is not of valid syntax. <P><DD></DL><P>
<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>