1 ;;; srfi-10.scm --- Hash-Comma Reader Extension
3 ;; Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 ;; This module implements the syntax extension #,(), also called
22 ;; hash-comma, which is defined in SRFI-10.
24 ;; The support for SRFI-10 consists of the procedure
25 ;; `define-reader-ctor' for defining new reader constructors and the
28 ;; #,(<ctor> <datum> ...)
30 ;; where <ctor> must be a symbol for which a read constructor was
31 ;; defined previously.
35 ;; (define-reader-ctor 'file open-input-file)
36 ;; (define f '#,(file "/etc/passwd"))
39 ;; "root:x:0:0:root:/root:/bin/bash"
41 ;; Please note the quote before the #,(file ...) expression. This is
42 ;; necessary because ports are not self-evaluating in Guile.
44 ;; This module is fully documented in the Guile Reference Manual.
48 (define-module (srfi srfi-10)
49 :use-module (ice-9 rdelim)
50 :export (define-reader-ctor))
52 (cond-expand-provide (current-module) '(srfi-10))
54 ;; This hash table stores the association between comma-hash tags and
55 ;; the corresponding constructor procedures.
57 (define reader-ctors (make-hash-table 31))
59 ;; This procedure installs the procedure @var{proc} as the constructor
60 ;; for the comma-hash tag @var{symbol}.
62 (define (define-reader-ctor symbol proc)
63 (hashq-set! reader-ctors symbol proc)
64 (if #f #f)) ; Return unspecified value.
66 ;; Retrieve the constructor procedure for the tag @var{symbol} or
67 ;; throw an error if no such tag is defined.
69 (define (lookup symbol)
70 (let ((p (hashq-ref reader-ctors symbol #f)))
73 (error "unknown hash-comma tag " symbol))))
75 ;; This is the actual reader extension.
77 (define (hash-comma char port)
78 (let* ((obj (read port)))
79 (if (and (list? obj) (positive? (length obj)) (symbol? (car obj)))
80 (let ((p (lookup (car obj))))
81 (let ((res (apply p (cdr obj))))
83 (error "syntax error in hash-comma expression"))))
85 ;; Install the hash extension.
87 (read-hash-extend #\, hash-comma)
89 ;;; srfi-10.scm ends here