concept-reader.tex 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. % -*- Mode: TeX -*-
  2. \beginsubsection{Dynamic Control of the Lisp Reader}
  3. Various aspects of the \term{Lisp reader} can be controlled dynamically.
  4. \Seesection\Readtables\ and \secref\ReaderVars.
  5. \endsubsection%{Dynamic Control of the Lisp Reader}
  6. \beginsubsection{Effect of Readtable Case on the Lisp Reader}
  7. \DefineSection{ReadtableCaseReadEffect}
  8. The \term{readtable case} of the \term{current readtable} affects the \term{Lisp reader}
  9. in the following ways:
  10. \beginlist
  11. \itemitem{\kwd{upcase}}
  12. When the \term{readtable case} is \kwd{upcase},
  13. unescaped constituent \term{characters} are converted to \term{uppercase},
  14. as specified in \secref\ReaderAlgorithm.
  15. \itemitem{\kwd{downcase}}
  16. When the \term{readtable case} is \kwd{downcase},
  17. unescaped constituent \term{characters} are converted to \term{lowercase}.
  18. \itemitem{\kwd{preserve}}
  19. When the \term{readtable case} is \kwd{preserve},
  20. the case of all \term{characters} remains unchanged.
  21. \itemitem{\kwd{invert}}
  22. When the \term{readtable case} is \kwd{invert},
  23. then if all of the unescaped letters in the extended token are of the same \term{case},
  24. those (unescaped) letters are converted to the opposite \term{case}.
  25. \endlist
  26. \beginsubsubsection{Examples of Effect of Readtable Case on the Lisp Reader}
  27. \DefineSection{ReadtableCaseReadExamples}
  28. \code
  29. (defun test-readtable-case-reading ()
  30. (let ((*readtable* (copy-readtable nil)))
  31. (format t "READTABLE-CASE Input Symbol-name~
  32. ~%-----------------------------------~
  33. ~%")
  34. (dolist (readtable-case '(:upcase :downcase :preserve :invert))
  35. (setf (readtable-case *readtable*) readtable-case)
  36. (dolist (input '("ZEBRA" "Zebra" "zebra"))
  37. (format t "~&:~A~16T~A~24T~A"
  38. (string-upcase readtable-case)
  39. input
  40. (symbol-name (read-from-string input)))))))
  41. \endcode
  42. The output from \f{(test-readtable-case-reading)} should be as follows:
  43. \code
  44. READTABLE-CASE Input Symbol-name
  45. -------------------------------------
  46. :UPCASE ZEBRA ZEBRA
  47. :UPCASE Zebra ZEBRA
  48. :UPCASE zebra ZEBRA
  49. :DOWNCASE ZEBRA zebra
  50. :DOWNCASE Zebra zebra
  51. :DOWNCASE zebra zebra
  52. :PRESERVE ZEBRA ZEBRA
  53. :PRESERVE Zebra Zebra
  54. :PRESERVE zebra zebra
  55. :INVERT ZEBRA zebra
  56. :INVERT Zebra Zebra
  57. :INVERT zebra ZEBRA
  58. \endcode
  59. \endsubsubsection%{Examples of Effect of Readtable Case on the Lisp Reader}
  60. \endsubsection%{Effect of Readtable Case on the Lisp Reader}
  61. \beginsubsection{Argument Conventions of Some Reader Functions}
  62. \beginsubsubsection{The EOF-ERROR-P argument}
  63. %% 22.2.1 2
  64. \param{Eof-error-p} in input function calls
  65. controls what happens if input is from a file (or any other
  66. input source that has a definite end) and the end of the file is reached.
  67. If \param{eof-error-p} is \term{true} (the default),
  68. an error \oftype{end-of-file} is signaled
  69. at end of file. If it is \term{false}, then no error is signaled, and instead
  70. the function returns \param{eof-value}.
  71. Functions such as \funref{read} that read the representation
  72. of an \term{object} rather than a single
  73. character always signals an error, regardless of \param{eof-error-p}, if
  74. the file ends in the middle of an object representation.
  75. For example, if a file does
  76. not contain enough right parentheses to balance the left parentheses in
  77. it, \funref{read} signals an error. If a file ends in a
  78. \term{symbol} or a \term{number}
  79. immediately followed by end-of-file, \funref{read} reads the
  80. \term{symbol} or
  81. \term{number}
  82. successfully and when called again will
  83. %%Barmar thought this wasn't needed:
  84. % see the end-of-file and
  85. % only then
  86. act according to \param{eof-error-p}.
  87. Similarly, \thefunction{read-line}
  88. successfully reads the last line of a file even if that line
  89. is terminated by end-of-file rather than the newline character.
  90. Ignorable text, such as lines containing only \term{whitespace}\meaning{2} or comments,
  91. are not considered to begin an \term{object};
  92. if \funref{read} begins to read an \term{expression} but sees only such
  93. ignorable text, it does not consider the file to end in the middle of an \term{object}.
  94. Thus an \param{eof-error-p} argument controls what happens
  95. when the file ends between \term{objects}.
  96. \endsubsubsection%{The EOF-ERROR-P argument}
  97. \beginsubsubsection{The RECURSIVE-P argument}
  98. %% 22.2.1 4
  99. If \param{recursive-p} is supplied and not \nil, it specifies that
  100. this function call is not an outermost call to \funref{read} but an
  101. embedded call, typically from a \term{reader macro function}.
  102. It is important to distinguish such recursive calls for three reasons.
  103. %% 22.2.1 5
  104. \beginlist
  105. \itemitem{1.}
  106. An outermost call establishes the context within which the
  107. \f{\#\param{n}=} and \f{\#\param{n}\#} syntax is scoped. Consider, for example,
  108. the expression
  109. \code
  110. (cons '#3=(p q r) '(x y . #3#))
  111. \endcode
  112. If the \term{single-quote} \term{reader macro} were defined in this way:
  113. \code
  114. (set-macro-character #\\' ;incorrect
  115. #'(lambda (stream char)
  116. (declare (ignore char))
  117. (list 'quote (read stream))))
  118. \endcode
  119. % then the expression could not be read properly, because there would be no way
  120. % to know when \funref{read} is called recursively by the first
  121. % occurrence of \f{'} that the label \f{\#3=} would be referred to
  122. % later in the containing expression.
  123. % There would be no way to know because \funref{read}
  124. % could not determine that it was called by a \term{reader macro function}
  125. % rather than from an outermost form.
  126. %% per JonL:
  127. then each call to the \term{single-quote} \term{reader macro function} would establish
  128. independent contexts for the scope of \funref{read} information, including the scope of
  129. identifications between markers like ``\f{\#3=}'' and ``\f{\#3\#}''. However, for
  130. this expression, the scope was clearly intended to be determined by the outer set
  131. of parentheses, so such a definition would be incorrect.
  132. The correct way to define the \term{single-quote}
  133. \term{reader macro} uses \param{recursive-p}:
  134. \code
  135. (set-macro-character #\\' ;correct
  136. #'(lambda (stream char)
  137. (declare (ignore char))
  138. (list 'quote (read stream t nil t))))
  139. \endcode
  140. %% 22.2.1 6
  141. \itemitem{2.}
  142. A recursive call does not alter whether the reading process
  143. is to preserve \term{whitespace}\meaning{2} or not (as determined by whether the
  144. outermost call was to \funref{read} or \funref{read-preserving-whitespace}).
  145. Suppose again that \term{single-quote}
  146. % had the first, incorrect, \term{reader macro}
  147. % definition shown above.
  148. %% per JonL:
  149. were to be defined as shown above in the incorrect definition.
  150. Then a call to \funref{read-preserving-whitespace}
  151. that read the expression \f{'foo\SpaceChar} would fail to preserve the space
  152. character following the symbol \f{foo} because the \term{single-quote}
  153. \term{reader macro function} calls \funref{read},
  154. not \funref{read-preserving-whitespace},
  155. to read the following expression (in this case \f{foo}).
  156. The correct definition, which passes the value \term{true} for \param{recursive-p}
  157. to \funref{read}, allows the outermost call to determine
  158. whether \term{whitespace}\meaning{2} is preserved.
  159. %% 22.2.1 8
  160. \itemitem{3.}
  161. When end-of-file is encountered and the \param{eof-error-p} argument
  162. is not \nil, the kind of error that is signaled may depend on the value
  163. of \param{recursive-p}. If \param{recursive-p}
  164. is \term{true}, then the end-of-file
  165. is deemed to have occurred within the middle of a printed representation;
  166. if \param{recursive-p} is \term{false}, then the end-of-file may be deemed to have
  167. occurred between \term{objects} rather than within the middle of one.
  168. \endlist
  169. \endsubsubsection%{The EOF-ERROR-P argument}
  170. \endsubsection%{Argument Conventions of Some Reader Functions}