~ chicken-core (chicken-5) e1b27335820dea4908e3e738f96c39f388c1ca01
commit e1b27335820dea4908e3e738f96c39f388c1ca01 Author: felix <felix@z.(none)> AuthorDate: Mon Feb 28 23:17:11 2011 +0100 Commit: felix <felix@z.(none)> CommitDate: Mon Feb 28 23:17:11 2011 +0100 octal escape sequences in strings; documented escape sequences diff --git a/library.scm b/library.scm index 5760774d..4e1aa1fb 100644 --- a/library.scm +++ b/library.scm @@ -2344,19 +2344,20 @@ EOF (##sys#read-char-0 port) (loop (##sys#peek-char-0 port)) ) ) ) ) - (define (r-usequence u n) - (let loop ([seq '()] [n n]) + (define (r-usequence u n base) + (let loop ((seq '()) (n n)) (if (eq? n 0) - (let* ([str (##sys#reverse-list->string seq)] - [n (string->number str 16)]) - (or n - (##sys#read-error - port - (string-append "invalid escape-sequence '\\" u str "\'")) ) ) - (let ([x (##sys#read-char-0 port)]) - (if (or (eof-object? x) (char=? #\" x)) - (##sys#read-error port "unterminated string constant") - (loop (cons x seq) (fx- n 1)) ) ) ) ) ) + (let* ((str (##sys#reverse-list->string seq)) + (n (string->number str base))) + (or n + (##sys#read-error + port + (string-append + "invalid escape-sequence '\\" u str "\'")) ) ) + (let ((x (##sys#read-char-0 port))) + (if (or (eof-object? x) (char=? #\" x)) + (##sys#read-error port "unterminated string constant") + (loop (cons x seq) (fx- n 1)) ) ) ) ) ) (define (r-cons-codepoint cp lst) (let* ((s (##sys#char->utf8-string (integer->char cp))) @@ -2381,10 +2382,10 @@ EOF ((#\v) (loop (##sys#read-char-0 port) (cons (integer->char 11) lst))) ((#\f) (loop (##sys#read-char-0 port) (cons (integer->char 12) lst))) ((#\x) - (let ([ch (integer->char (r-usequence "x" 2))]) + (let ([ch (integer->char (r-usequence "x" 2 16))]) (loop (##sys#read-char-0 port) (cons ch lst)) ) ) ((#\u) - (let ([n (r-usequence "u" 4)]) + (let ([n (r-usequence "u" 4 16)]) (if (##sys#unicode-surrogate? n) (if (and (eqv? #\\ (##sys#read-char-0 port)) (eqv? #\u (##sys#read-char-0 port))) @@ -2404,11 +2405,15 @@ EOF ((#\\ #\' #\" #\|) (loop (##sys#read-char-0 port) (cons c lst))) (else - (##sys#read-warning - port - "undefined escape sequence in string - probably forgot backslash" - c) - (loop (##sys#read-char-0 port) (cons c lst))) ) ) + (cond ((char-numeric? c) + (let ((ch (integer->char (r-usequence "" 2 8)))) + (loop (##sys#read-char-0 port) (cons ch lst)) )) + (else + (##sys#read-warning + port + "undefined escape sequence in string - probably forgot backslash" + c) + (loop (##sys#read-char-0 port) (cons )c lst))) ) )) ((eq? term c) (##sys#reverse-list->string lst)) (else (loop (##sys#read-char-0 port) (cons c lst))) ) )) diff --git a/manual/Non-standard read syntax b/manual/Non-standard read syntax index fa2f63ea..0cecce36 100644 --- a/manual/Non-standard read syntax +++ b/manual/Non-standard read syntax @@ -93,6 +93,27 @@ prints and substituted expressions: (+ three 99) ==> 102 (three is "3") +=== String escape sequences + +String-literals may contain the following of the escape sequences: + +<table> +<tr><th>Escape sequence</th><th>Character</th></tr> +<tr><td>\n</td><td>line feed / newline</td></tr> +<tr><td>\t</td><td>tab</td></tr> +<tr><td>\r</td><td>carriage return</td></tr> +<tr><td>\b</td><td>backspace</td></tr> +<tr><td>\a</td><td>bell</td></tr> +<tr><td>\v</td><td>vertical tab</td></tr> +<tr><td>\f</td><td>form feed</td></tr> +<tr><td>\xXX</td><td>hexadecimal 8-bit character</td></tr> +<tr><td>\uXXXX</td><td>hexadecimal 16-bit UNICODE character</td></tr> +<tr><td>\UXXXXXXXX</td><td>hexadecimal 32-bit UNICODE character</td></tr> +<tr><td>\OOO</td><td>octal 8-bit character</td></tr> +<tr><td>\| \" \\ \'</td><td>represents escaped character</td></tr> +</table> + + === Foreign Declare #> ... <# @@ -126,6 +147,7 @@ Interpretation depends on the directly following characters. Only the following ==== Read Mark Invocation * If a ''read mark'' with the same name as the token is registered, then its procedure is called and the result of the read-mark procedure will be returned +(see {{set-read-syntax!}} for more information about read-marks). === Case Sensitive ExpressionTrap