From 731ee2e8c9f8ede9f4d619789a1bb6cdbfe82033 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Fri, 16 Oct 2026 17:30:47 +0300 Subject: [PATCH] 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. --- reader.scm | 63 ++++++++++++++++++++++++++++++++++++++++-------- tests/reader.scm | 23 ++++++++++++++++++ 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/reader.scm b/reader.scm index 5f75024..f1e15fe 100644 --- a/reader.scm +++ b/reader.scm @@ -468,22 +468,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) diff --git a/tests/reader.scm b/tests/reader.scm index 82944a1..86412a6 100644 --- a/tests/reader.scm +++ b/tests/reader.scm @@ -92,6 +92,29 @@ ;; a name that only begins like a number is still a name (reader-test '(3dmath.h) "3dmath.h") + ;; A string is text, and an escape is one of its characters, spelled + ;; the way C spells it. An escape the reader did not know lost its + ;; backslash, so "\x41" read as "x41" and "\012" as NUL and "12" + (reader-test '("A" "A" "A" "A") "\"\\x41\" \"\\101\" \"\\u0041\" \"\\U00000041\"") + (reader-test '("\n" "'?" "\x7f;") "\"\\012\" \"\\'\\?\" \"\\x7f\"") + (reader-test '("é" "😀") "\"\\u00e9\" \"\\U0001F600\"") + ;; a backslash ending a line joins it to the next, as in C + (reader-test '("ab") "\"a\\\nb\"") + ;; C reads \xff and \377 as a byte, which text has none of: what is + ;; past ASCII is written as the character, or its \u + (test "a byte past ASCII is not text" + "a byte past ASCII is not text; write the character or \\u, or a [u8 N] array for bytes" + (read-error "\"\\xff\"")) + (test "in octal neither" #t (string? (read-error "\"\\377\""))) + (test "nor is a run of hex digits, which \\x takes all of" + #t (string? (read-error "\"\\x41BC\""))) + (test "an escape C has not is an error" "unknown escape" (read-error "\"\\q\"")) + (test "\\x needs a digit" "\\x needs a hex digit" (read-error "\"\\xg\"")) + (test "\\u takes four" + "\\u takes four hex digits and \\U eight" (read-error "\"\\u41\"")) + (test "a surrogate is no character" + "no character has this code point" (read-error "\"\\uD800\"")) + ;; `;' comments are preserved as (comment "...") forms (reader-test '((comment " hi")) "; hi") (reader-test '((comment ";; Prototypes")) ";;; Prototypes")