Commit | Line | Data |
---|---|---|
6be07c52 TTN |
1 | ;;; srfi-10.scm --- Hash-Comma Reader Extension |
2 | ||
1b09b607 | 3 | ;; Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc. |
6be07c52 | 4 | ;; |
73be1d9e MV |
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 2.1 of the License, or (at your option) any later version. | |
9 | ;; | |
10 | ;; This library is distributed in the hope that it will be useful, | |
6be07c52 TTN |
11 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
73be1d9e MV |
13 | ;; Lesser General Public License for more details. |
14 | ;; | |
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 | |
92205699 | 17 | ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
e1633bf3 MG |
18 | |
19 | ;;; Commentary: | |
20 | ||
6be07c52 TTN |
21 | ;; This module implements the syntax extension #,(), also called |
22 | ;; hash-comma, which is defined in SRFI-10. | |
23 | ;; | |
24 | ;; The support for SRFI-10 consists of the procedure | |
25 | ;; `define-reader-ctor' for defining new reader constructors and the | |
26 | ;; read syntax form | |
27 | ;; | |
28 | ;; #,(<ctor> <datum> ...) | |
29 | ;; | |
30 | ;; where <ctor> must be a symbol for which a read constructor was | |
31 | ;; defined previously. | |
32 | ;; | |
33 | ;; Example: | |
34 | ;; | |
35 | ;; (define-reader-ctor 'file open-input-file) | |
36 | ;; (define f '#,(file "/etc/passwd")) | |
37 | ;; (read-line f) | |
38 | ;; => | |
39 | ;; "root:x:0:0:root:/root:/bin/bash" | |
40 | ;; | |
41 | ;; Please note the quote before the #,(file ...) expression. This is | |
42 | ;; necessary because ports are not self-evaluating in Guile. | |
43 | ;; | |
44 | ;; This module is fully documented in the Guile Reference Manual. | |
e1633bf3 MG |
45 | |
46 | ;;; Code: | |
47 | ||
48 | (define-module (srfi srfi-10) | |
1a179b03 MD |
49 | :use-module (ice-9 rdelim) |
50 | :export (define-reader-ctor)) | |
e1633bf3 | 51 | |
1b2f40b9 MG |
52 | (cond-expand-provide (current-module) '(srfi-10)) |
53 | ||
e1633bf3 MG |
54 | ;; This hash table stores the association between comma-hash tags and |
55 | ;; the corresponding constructor procedures. | |
56 | ;; | |
57 | (define reader-ctors (make-hash-table 31)) | |
58 | ||
59 | ;; This procedure installs the procedure @var{proc} as the constructor | |
60 | ;; for the comma-hash tag @var{symbol}. | |
61 | ;; | |
62 | (define (define-reader-ctor symbol proc) | |
63 | (hashq-set! reader-ctors symbol proc) | |
64 | (if #f #f)) ; Return unspecified value. | |
65 | ||
66 | ;; Retrieve the constructor procedure for the tag @var{symbol} or | |
67 | ;; throw an error if no such tag is defined. | |
68 | ;; | |
69 | (define (lookup symbol) | |
70 | (let ((p (hashq-ref reader-ctors symbol #f))) | |
71 | (if (procedure? p) | |
72 | p | |
73 | (error "unknown hash-comma tag " symbol)))) | |
74 | ||
75 | ;; This is the actual reader extension. | |
76 | ;; | |
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)))) | |
82 | res)) | |
83 | (error "syntax error in hash-comma expression")))) | |
84 | ||
85 | ;; Install the hash extension. | |
86 | ;; | |
87 | (read-hash-extend #\, hash-comma) | |
6be07c52 TTN |
88 | |
89 | ;;; srfi-10.scm ends here |