raw-string.scm (1434B)
1 (define-module (pstk raw-string) 2 #:use-module (ice-9 rdelim) 3 #:use-module (ice-9 textual-ports)) 4 5 (define (read-expected expected-string port) 6 (with-input-from-port port 7 (lambda () 8 (let loop ((expected-chars (string->list expected-string)) 9 (c (read-char))) 10 (cond 11 ((eof-object? c) 12 (null? expected-chars)) 13 ((null? expected-chars) 14 (unread-char c) 15 #t) 16 ((char=? (car expected-chars) 17 c) 18 (cond 19 ((loop (cdr expected-chars) 20 (read-char)) 21 #t) 22 (else 23 (unread-char c) 24 #f))) 25 (else 26 (unread-char c) 27 #f)))))) 28 29 (define (stack->string stack-of-chars) 30 (list->string (reverse stack-of-chars))) 31 32 (eval-when (eval load compile expand) 33 (define (raw-string-reader char port) 34 (unless (read-expected "(" port) 35 (error "Bad raw string")) 36 37 (cond 38 ((read-expected ")R#" port) 39 "") 40 (else 41 (let loop ((c (read-char port)) 42 (stack '())) 43 (format #t "~s~%" stack) 44 (cond 45 ((eof-object? c) 46 (error "Unexpected end of file" (stack->string stack))) 47 ((read-expected ")R#" port) 48 (stack->string (cons c stack))) 49 (else 50 (loop (read-char port) 51 (cons c stack)))))))) 52 (read-hash-extend #\R raw-string-reader))