read a string's escapes the way C spells them
An escape the reader did not know lost its backslash, so "\x41" read as "x41" and "\012" as NUL and "12". A string is text, so \x and octal reach only as far as ASCII; past it, \u and \U name the character.
This commit is contained in:
63
reader.scm
63
reader.scm
@@ -471,22 +471,65 @@
|
||||
(cond
|
||||
((eof-object? c) (error "Unterminated string literal"))
|
||||
((char=? c #\") (list->string (reverse chars)))
|
||||
((char=? c #\\) (loop (cons (read-escape port) chars)))
|
||||
((char=? c #\\) (let ((escaped (read-escape port)))
|
||||
(loop (if escaped (cons escaped chars) chars))))
|
||||
(else (loop (cons c chars)))))))
|
||||
|
||||
;;; C's escapes, each one character of the string, or #f for a backslash
|
||||
;;; ending a line, which joins it to the next. A string is text, so \x
|
||||
;;; and octal, bytes to C, reach only as far as ASCII: past it the
|
||||
;;; character is written as itself or its \u.
|
||||
;;;
|
||||
;;; \x41 \101 A -> A \012 -> newline é -> é
|
||||
(define (read-escape port)
|
||||
(let ((c (get-ch port)))
|
||||
(cond
|
||||
((eof-object? c) (error "Unterminated string literal"))
|
||||
((char=? c #\n) #\newline)
|
||||
((char=? c #\t) #\tab)
|
||||
((char=? c #\r) #\return)
|
||||
((char=? c #\a) #\alarm)
|
||||
((char=? c #\b) #\backspace)
|
||||
((char=? c #\f) (integer->char 12))
|
||||
((char=? c #\v) (integer->char 11))
|
||||
((char=? c #\0) #\nul)
|
||||
(else c)))) ; \" \\ and anything else: literal
|
||||
((assv c simple-escapes) => cdr)
|
||||
((char=? c #\newline) #f)
|
||||
((digit-in 8 c) => (lambda (d) (ascii (more-digits port 8 2 d))))
|
||||
((char=? c #\x)
|
||||
(let ((d (digit-in 16 (peek port))))
|
||||
(unless d (error "\\x needs a hex digit"))
|
||||
(get-ch port)
|
||||
(ascii (more-digits port 16 #f d))))
|
||||
((char=? c #\u) (code-point port 4))
|
||||
((char=? c #\U) (code-point port 8))
|
||||
(else (error "unknown escape" (string #\\ c))))))
|
||||
|
||||
(define simple-escapes
|
||||
(list (cons #\n #\newline) (cons #\t #\tab) (cons #\r #\return)
|
||||
(cons #\a #\alarm) (cons #\b #\backspace)
|
||||
(cons #\f (integer->char 12)) (cons #\v (integer->char 11))
|
||||
(cons #\\ #\\) (cons #\" #\") (cons #\' #\') (cons #\? #\?)))
|
||||
|
||||
(define (digit-in radix c)
|
||||
(and (char? c) (string->number (string c) radix)))
|
||||
|
||||
;;; N, with up to MOST more digits of RADIX read onto it; any number of
|
||||
;;; them for MOST #f
|
||||
(define (more-digits port radix most n)
|
||||
(let loop ((n n) (left most))
|
||||
(let ((d (and (or (not left) (> left 0)) (digit-in radix (peek port)))))
|
||||
(if d
|
||||
(begin (get-ch port) (loop (+ (* n radix) d) (and left (- left 1))))
|
||||
n))))
|
||||
|
||||
(define (ascii n)
|
||||
(if (> n 127)
|
||||
(error "a byte past ASCII is not text; write the character or \\u, or a [u8 N] array for bytes")
|
||||
(integer->char n)))
|
||||
|
||||
(define (code-point port digits)
|
||||
(let loop ((n 0) (left digits))
|
||||
(if (= left 0)
|
||||
(if (or (<= #xD800 n #xDFFF) (> n #x10FFFF))
|
||||
(error "no character has this code point")
|
||||
(integer->char n))
|
||||
(let ((d (digit-in 16 (peek port))))
|
||||
(unless d (error "\\u takes four hex digits and \\U eight"))
|
||||
(get-ch port)
|
||||
(loop (+ (* n 16) d) (- left 1))))))
|
||||
|
||||
(define (skip-block-comment port depth)
|
||||
(if (= depth 0)
|
||||
|
||||
Reference in New Issue
Block a user