srfi-10.scm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ;;; srfi-10.scm --- Hash-Comma Reader Extension
  2. ;; Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library 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. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;; Commentary:
  18. ;; This module implements the syntax extension #,(), also called
  19. ;; hash-comma, which is defined in SRFI-10.
  20. ;;
  21. ;; The support for SRFI-10 consists of the procedure
  22. ;; `define-reader-ctor' for defining new reader constructors and the
  23. ;; read syntax form
  24. ;;
  25. ;; #,(<ctor> <datum> ...)
  26. ;;
  27. ;; where <ctor> must be a symbol for which a read constructor was
  28. ;; defined previously.
  29. ;;
  30. ;; Example:
  31. ;;
  32. ;; (define-reader-ctor 'file open-input-file)
  33. ;; (define f '#,(file "/etc/passwd"))
  34. ;; (read-line f)
  35. ;; =>
  36. ;; "root:x:0:0:root:/root:/bin/bash"
  37. ;;
  38. ;; Please note the quote before the #,(file ...) expression. This is
  39. ;; necessary because ports are not self-evaluating in Guile.
  40. ;;
  41. ;; This module is fully documented in the Guile Reference Manual.
  42. ;;; Code:
  43. (define-module (srfi srfi-10)
  44. :use-module (ice-9 rdelim)
  45. :export (define-reader-ctor))
  46. (cond-expand-provide (current-module) '(srfi-10))
  47. ;; This hash table stores the association between comma-hash tags and
  48. ;; the corresponding constructor procedures.
  49. ;;
  50. (define reader-ctors (make-hash-table 31))
  51. ;; This procedure installs the procedure @var{proc} as the constructor
  52. ;; for the comma-hash tag @var{symbol}.
  53. ;;
  54. (define (define-reader-ctor symbol proc)
  55. (hashq-set! reader-ctors symbol proc)
  56. (if #f #f)) ; Return unspecified value.
  57. ;; Retrieve the constructor procedure for the tag @var{symbol} or
  58. ;; throw an error if no such tag is defined.
  59. ;;
  60. (define (lookup symbol)
  61. (let ((p (hashq-ref reader-ctors symbol #f)))
  62. (if (procedure? p)
  63. p
  64. (error "unknown hash-comma tag " symbol))))
  65. ;; This is the actual reader extension.
  66. ;;
  67. (define (hash-comma char port)
  68. (let* ((obj (read port)))
  69. (if (and (list? obj) (positive? (length obj)) (symbol? (car obj)))
  70. (let ((p (lookup (car obj))))
  71. (let ((res (apply p (cdr obj))))
  72. res))
  73. (error "syntax error in hash-comma expression"))))
  74. ;; Install the hash extension.
  75. ;;
  76. (read-hash-extend #\, hash-comma)
  77. ;;; srfi-10.scm ends here