read-write.texi 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. @node Reader & writer
  2. @section Reader & writer
  3. Scheme48 has simple S-expression reader & writer libraries, with some
  4. facilities beyond R5RS's @code{read} & @code{write} procedures.
  5. @menu
  6. * Reader::
  7. * Writer::
  8. @end menu
  9. @node Reader
  10. @subsection Reader
  11. @stindex reading
  12. Scheme48's reader facility is exported by the @code{reading}
  13. structure. The @code{read} binding thereby exported is identical to
  14. that of the @code{scheme} structure, which is the binding that R5RS
  15. specifies under the name @code{read}.
  16. @deffn procedure read [port] @returns{} readable-value
  17. Reads a single S-expression from @var{port}, whose default value is the
  18. current input port. If the end of the stream is encountered before the
  19. beginning of an S-expression, @code{read} will return an EOF object.
  20. It will signal a read error if text read from @var{port} does not
  21. constitute a complete, well-formed S-expression.
  22. @end deffn
  23. @deffn procedure define-sharp-macro char proc @returns{} unspecified
  24. Defines a sharp/pound/hash/octothorpe (@code{#}) reader macro. The
  25. next time the reader is invoked, if it encounters an octothorpe/sharp
  26. followed by @var{char}, it applies @var{proc} to @var{char} and the
  27. input port being read from. @var{Char} is @emph{not} consumed in the
  28. input port. If @var{char} is alphabetic, it should be lowercase;
  29. otherwise the reader will not recognize it, since the reader converts
  30. the character following octothorpes to lowercase.
  31. @end deffn
  32. @deffn procedure reading-error port message irritant @dots{} @returns{} unspecified
  33. Signals an error while reading, for custom sharp macros. It is not
  34. likely that calls to @code{reading-error} will return.
  35. @end deffn
  36. @deffn procedure gobble-line port @returns{} unspecified
  37. Reads until a newline from @var{port}. The newline character sequence
  38. is consumed.
  39. @end deffn
  40. @node Writer
  41. @subsection Writer
  42. @stindex writing
  43. Scheme48's @code{writing} structure exports its writer facility. The
  44. @code{write} and @code{display} bindings from it are identical to those
  45. from the @code{scheme} structure, which are the same bindings that R5RS
  46. specifies.
  47. @deffn procedure write object [port] @returns{} unspecified
  48. Writes @var{object} to @var{port}, which defaults to the current output
  49. port, in a machine-readable manner. Strings are written with
  50. double- quotes; characters are prefixed by @code{#\}. Any object that
  51. is unreadable --- anything that does not have a written representation
  52. as an S-expression --- is written based on its @dfn{disclosed}
  53. representation. Such unreadable objects are converted to a disclosed
  54. representation by the @code{disclose} generic procedure (see below).
  55. @end deffn
  56. @deffn procedure display object [port] @returns{} unspecified
  57. Displays @var{object} to @var{port}, which defaults to the value of the
  58. current output port, in a more human-readable manner. Strings are
  59. written without surrounding double-quotes; characters are written as
  60. themselves with no prefix.
  61. @end deffn
  62. @cindex customized writer
  63. @cindex writer, customized
  64. @deffn procedure recurring-write object port recur @returns{} unspecified
  65. Writes @var{object} to @var{port}. Every time this recurs upon a new
  66. object, rather than calling itself or its own looping procedure, it
  67. calls @var{recur}. This allows customized printing routines that still
  68. take advantage of the existence of Scheme48's writer. For example,
  69. @code{display} simply calls @code{recurring-write} with a recurring
  70. procedure that prints strings and characters specially and lets
  71. @code{recurring-write} handle everything else.
  72. @end deffn
  73. @deffn procedure display-type-name name port @returns{} unspecified
  74. If @var{name} is a symbol with an alphabetic initial character, this
  75. writes @var{name} to @var{port} with the first character uppercased and
  76. the remaining character lowercased; otherwise, @code{display-type-name}
  77. simply writes @var{name} to @var{port} with @code{display}.
  78. @lisp
  79. (display-type-name 'foo)
  80. @print{} Foo
  81. (display-type-name (string->symbol "42foo"))
  82. @print{} 42foo
  83. (display-type-name (cons "foo" "bar"))
  84. @print{} (foo . bar)
  85. (display-type-name (string->symbol "fOo-BaR"))
  86. @print{} Foo-bar@end lisp
  87. @noindent
  88. This is used when printing disclosed representations (see below).
  89. @end deffn
  90. @subsubsection Object disclosure
  91. @cindex customized writer
  92. @cindex writer, customized
  93. @stindex methods
  94. The @embedref{Generic dispatch system, @code{methods} structure}
  95. exports the generic procedure @code{disclose} and its method table
  96. @code{&disclose}. When @code{recurring-write} encounters an object it
  97. is unable to write in a rereadable manner, it applies @code{disclose}
  98. to the unreadable object to acquire a @dfn{disclosed representation.}
  99. (If @code{disclose} returns @code{#f}, @ie{} the object has no
  100. disclosed representation, the writer will write @code{#@{Random
  101. object@}}.) After converting a value to its disclosed representation,
  102. @eg{} a list consisting of the symbol @code{foo}, the symbol
  103. @code{bar}, a byte vector, and a pair @code{(1 . 2)}, the writer will
  104. write @code{#@{Foo #@{Byte-vector@} bar (1 . 2)@}}. That is: contents
  105. of the list are surrounded by @code{#@{} and @code{@}}, the first
  106. element of the list (the `type name') is written with
  107. @code{display-type-name}, and then the remaining elements of the list
  108. are recursively printed out with the @var{recur} argument.
  109. Typically, when a programmer creates an abstract data type by using
  110. Scheme48's record facility, he will not add methods to @code{&disclose}
  111. but instead define the record type's discloser with the
  112. @code{define-record-discloser} procedure; @pxref{Records}.
  113. Example:
  114. @lisp
  115. (define-record-type pare rtd/pare
  116. (kons a d)
  117. pare?
  118. (a kar set-kar!)
  119. (d kdr set-kdr!))
  120. (define-record-discloser rtd/pare
  121. (lambda (pare)
  122. `(pare ,(kar pare) *dot* ,(kdr pare))))
  123. (write (kons (kons 5 3) (kons 'a 'b)))
  124. @print{} #@{Pare #@{Pare 5 *dot* 3@} *dot* #@{Pare a *dot* b@}@}@end lisp