~ chicken-core (master) /read-syntax.scm


  1;;;; read-syntax.scm - CHICKEN reader extensions
  2;
  3; Copyright (c) 2008-2022, The CHICKEN Team
  4; Copyright (c) 2000-2007, Felix L. Winkelmann
  5; All rights reserved.
  6;
  7; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
  8; conditions are met:
  9;
 10;   Redistributions of source code must retain the above copyright notice, this list of conditions and the following
 11;     disclaimer.
 12;   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 13;     disclaimer in the documentation and/or other materials provided with the distribution.
 14;   Neither the name of the author nor the names of its contributors may be used to endorse or promote
 15;     products derived from this software without specific prior written permission.
 16;
 17; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 18; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 19; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 20; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 21; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 22; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 23; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 24; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 25; POSSIBILITY OF SUCH DAMAGE.
 26
 27(declare
 28  (unit read-syntax)
 29  (uses internal)
 30  (disable-interrupts))
 31
 32(module chicken.read-syntax
 33  (copy-read-table current-read-table define-reader-ctor set-read-syntax!
 34   set-sharp-read-syntax! set-parameterized-read-syntax!)
 35
 36(import scheme chicken.base chicken.internal chicken.platform)
 37
 38(include "common-declarations.scm")
 39
 40(define (set-read-mark! sym proc)
 41  (let ((a (assq sym ##sys#read-marks)))
 42    (if a
 43	(##sys#setslot a 1 proc)
 44	(set! ##sys#read-marks (cons (cons sym proc) ##sys#read-marks)))))
 45
 46(define current-read-table ##sys#current-read-table)
 47
 48(define ((syntax-setter loc slot wrap) chr proc)
 49  (if (symbol? chr)
 50      (set-read-mark! chr proc)
 51      (let ((crt (current-read-table)))
 52	(##sys#check-char chr loc)
 53	(let ((i (char->integer chr))
 54              (a (assq chr (##sys#slot crt slot))))
 55	  (when proc 
 56            (##sys#check-closure proc loc)
 57            (set! proc (wrap proc)))
 58          (if a 
 59              (##sys#setslot a 1 proc)
 60              (##sys#setslot crt slot 
 61                (cons (cons chr proc) (##sys#slot crt slot))))))))
 62
 63(define set-read-syntax!
 64  (syntax-setter
 65   'set-read-syntax! 1
 66   (lambda (proc)
 67     (lambda (_ port)
 68       (##sys#read-char-0 port)
 69       (proc port)))))
 70
 71(define set-sharp-read-syntax!
 72  (syntax-setter
 73   'set-sharp-read-syntax! 2
 74   (lambda (proc)
 75     (lambda (_ port)
 76       (##sys#read-char-0 port)
 77       (proc port)))))
 78
 79(define set-parameterized-read-syntax!
 80  (syntax-setter
 81   'set-parameterized-read-syntax! 3
 82   (lambda (proc)
 83     (lambda (_ port num)
 84       (##sys#read-char-0 port)
 85       (proc port num)))))
 86
 87;;; Read-table operations:
 88
 89(define (copy-read-table rt)
 90  (define (copy lst)
 91    (map (lambda (a) (cons (car a) (cdr a))) lst))
 92  (##sys#check-structure rt 'read-table 'copy-read-table)
 93  (##sys#make-structure
 94   'read-table
 95   (copy (##sys#slot rt 1))
 96   (copy (##sys#slot rt 2))
 97   (copy (##sys#slot rt 3))))
 98
 99
100;;; SRFI-10:
101
102(define sharp-comma-reader-ctors (make-vector 301 '()))
103
104(define (define-reader-ctor spec proc)
105  (##sys#check-symbol spec 'define-reader-ctor)
106  (hash-table-set! sharp-comma-reader-ctors spec proc))
107
108(set! ##sys#user-read-hook
109  (let ((old ##sys#user-read-hook)
110	(read-char read-char)
111	(read read))
112    (lambda (char port)
113      (cond ((char=? char #\,)
114	     (read-char port)
115	     (let* ((exp (read port))
116		    (err (lambda () (##sys#read-error port "invalid sharp-comma external form" exp))))
117	       (if (or (null? exp) (not (list? exp)))
118		   (err)
119		   (let ([spec (##sys#slot exp 0)])
120		     (if (not (symbol? spec))
121			 (err)
122			 (let ((ctor (hash-table-ref sharp-comma-reader-ctors spec)))
123			   (if ctor
124			       (apply ctor (##sys#slot exp 1))
125			       (##sys#read-error port "undefined sharp-comma constructor" spec))))))))
126	    (else (old char port))))))
127
128(register-feature! 'srfi-10))
Trap