~ chicken-core (master) /read-syntax.scm
Trap1;;;; read-syntax.scm - CHICKEN reader extensions2;3; Copyright (c) 2008-2022, The CHICKEN Team4; Copyright (c) 2000-2007, Felix L. Winkelmann5; All rights reserved.6;7; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following8; conditions are met:9;10; Redistributions of source code must retain the above copyright notice, this list of conditions and the following11; disclaimer.12; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following13; 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 promote15; 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 EXPRESS18; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY19; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR20; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR21; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR22; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR24; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE25; POSSIBILITY OF SUCH DAMAGE.2627(declare28 (unit read-syntax)29 (uses internal)30 (disable-interrupts))3132(module chicken.read-syntax33 (copy-read-table current-read-table define-reader-ctor set-read-syntax!34 set-sharp-read-syntax! set-parameterized-read-syntax!)3536(import scheme chicken.base chicken.internal chicken.platform)3738(include "common-declarations.scm")3940(define (set-read-mark! sym proc)41 (let ((a (assq sym ##sys#read-marks)))42 (if a43 (##sys#setslot a 1 proc)44 (set! ##sys#read-marks (cons (cons sym proc) ##sys#read-marks)))))4546(define current-read-table ##sys#current-read-table)4748(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 (unless (##sys#slot crt slot)53 (##sys#setslot crt slot (##sys#make-vector 256 #f)))54 (##sys#check-char chr loc)55 (let ((i (char->integer chr)))56 (##sys#check-range i 0 256 loc)57 (cond (proc58 (##sys#check-closure proc loc)59 (##sys#setslot (##sys#slot crt slot) i (wrap proc)))60 (else61 (##sys#setslot (##sys#slot crt slot) i #f)))))))6263(define set-read-syntax!64 (syntax-setter65 'set-read-syntax! 166 (lambda (proc)67 (lambda (_ port)68 (##sys#read-char-0 port)69 (proc port)))))7071(define set-sharp-read-syntax!72 (syntax-setter73 'set-sharp-read-syntax! 274 (lambda (proc)75 (lambda (_ port)76 (##sys#read-char-0 port)77 (proc port)))))7879(define set-parameterized-read-syntax!80 (syntax-setter81 'set-parameterized-read-syntax! 382 (lambda (proc)83 (lambda (_ port num)84 (##sys#read-char-0 port)85 (proc port num)))))8687;;; Read-table operations:8889(define (copy-read-table rt)90 (##sys#check-structure rt 'read-table 'copy-read-table)91 (##sys#make-structure92 'read-table93 (let ((t1 (##sys#slot rt 1)))94 (and t1 (##sys#vector-resize t1 (##sys#size t1) #f)))95 (let ((t2 (##sys#slot rt 2)))96 (and t2 (##sys#vector-resize t2 (##sys#size t2) #f)))97 (let ((t3 (##sys#slot rt 3)))98 (and t3 (##sys#vector-resize t3 (##sys#size t3) #f)))))99100;;; SRFI-10:101102(define sharp-comma-reader-ctors (make-vector 301 '()))103104(define (define-reader-ctor spec proc)105 (##sys#check-symbol spec 'define-reader-ctor)106 (hash-table-set! sharp-comma-reader-ctors spec proc))107108(set! ##sys#user-read-hook109 (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 ctor124 (apply ctor (##sys#slot exp 1))125 (##sys#read-error port "undefined sharp-comma constructor" spec))))))))126 (else (old char port))))))127128(register-feature! 'srfi-10))