srfi-10.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;;; srfi-10.scm --- Hash-Comma Reader Extension
  2. ;; Copyright (C) 2001, 2002 Free Software Foundation, Inc.
  3. ;;
  4. ;; This program is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU General Public License as
  6. ;; published by the Free Software Foundation; either version 2, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this software; see the file COPYING. If not, write to
  16. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. ;; Boston, MA 02110-1301 USA
  18. ;;
  19. ;; As a special exception, the Free Software Foundation gives permission
  20. ;; for additional uses of the text contained in its release of GUILE.
  21. ;;
  22. ;; The exception is that, if you link the GUILE library with other files
  23. ;; to produce an executable, this does not by itself cause the
  24. ;; resulting executable to be covered by the GNU General Public License.
  25. ;; Your use of that executable is in no way restricted on account of
  26. ;; linking the GUILE library code into it.
  27. ;;
  28. ;; This exception does not however invalidate any other reasons why
  29. ;; the executable file might be covered by the GNU General Public License.
  30. ;;
  31. ;; This exception applies only to the code released by the
  32. ;; Free Software Foundation under the name GUILE. If you copy
  33. ;; code from other Free Software Foundation releases into a copy of
  34. ;; GUILE, as the General Public License permits, the exception does
  35. ;; not apply to the code that you add in this way. To avoid misleading
  36. ;; anyone as to the status of such modified files, you must delete
  37. ;; this exception notice from them.
  38. ;;
  39. ;; If you write modifications of your own for GUILE, it is your choice
  40. ;; whether to permit this exception to apply to your modifications.
  41. ;; If you do not wish that, delete this exception notice.
  42. ;;; Commentary:
  43. ;; This module implements the syntax extension #,(), also called
  44. ;; hash-comma, which is defined in SRFI-10.
  45. ;;
  46. ;; The support for SRFI-10 consists of the procedure
  47. ;; `define-reader-ctor' for defining new reader constructors and the
  48. ;; read syntax form
  49. ;;
  50. ;; #,(<ctor> <datum> ...)
  51. ;;
  52. ;; where <ctor> must be a symbol for which a read constructor was
  53. ;; defined previously.
  54. ;;
  55. ;; Example:
  56. ;;
  57. ;; (define-reader-ctor 'file open-input-file)
  58. ;; (define f '#,(file "/etc/passwd"))
  59. ;; (read-line f)
  60. ;; =>
  61. ;; "root:x:0:0:root:/root:/bin/bash"
  62. ;;
  63. ;; Please note the quote before the #,(file ...) expression. This is
  64. ;; necessary because ports are not self-evaluating in Guile.
  65. ;;
  66. ;; This module is fully documented in the Guile Reference Manual.
  67. ;;; Code:
  68. (define-module (srfi srfi-10)
  69. :use-module (ice-9 rdelim)
  70. :export (define-reader-ctor))
  71. (cond-expand-provide (current-module) '(srfi-10))
  72. ;; This hash table stores the association between comma-hash tags and
  73. ;; the corresponding constructor procedures.
  74. ;;
  75. (define reader-ctors (make-hash-table 31))
  76. ;; This procedure installs the procedure @var{proc} as the constructor
  77. ;; for the comma-hash tag @var{symbol}.
  78. ;;
  79. (define (define-reader-ctor symbol proc)
  80. (hashq-set! reader-ctors symbol proc)
  81. (if #f #f)) ; Return unspecified value.
  82. ;; Retrieve the constructor procedure for the tag @var{symbol} or
  83. ;; throw an error if no such tag is defined.
  84. ;;
  85. (define (lookup symbol)
  86. (let ((p (hashq-ref reader-ctors symbol #f)))
  87. (if (procedure? p)
  88. p
  89. (error "unknown hash-comma tag " symbol))))
  90. ;; This is the actual reader extension.
  91. ;;
  92. (define (hash-comma char port)
  93. (let* ((obj (read port)))
  94. (if (and (list? obj) (positive? (length obj)) (symbol? (car obj)))
  95. (let ((p (lookup (car obj))))
  96. (let ((res (apply p (cdr obj))))
  97. res))
  98. (error "syntax error in hash-comma expression"))))
  99. ;; Install the hash extension.
  100. ;;
  101. (read-hash-extend #\, hash-comma)
  102. ;;; srfi-10.scm ends here