diff --git a/src/julia-parser.scm b/src/julia-parser.scm index 18380262f2fa2..a33368e4bce63 100644 --- a/src/julia-parser.scm +++ b/src/julia-parser.scm @@ -2114,35 +2114,35 @@ (define (unescape-parsed-string-literal strs) (map-at even? unescape-string strs)) +;; remove `\` followed by a newline +(define (strip-escaped-newline s) + (let ((in (open-input-string s)) + (out (open-output-string))) + (define (loop preceding-backslash?) + (let ((c (read-char in))) + (cond ((eof-object? c)) + (preceding-backslash? + (if (not (eqv? c #\newline)) + (begin (write-char #\\ out) (write-char c out))) + (loop #f)) + ((eqv? c #\\) (loop #t)) + (else (write-char c out) (loop #f))))) + (loop #f) + (io.tostring! out))) + (define (parse-string-literal s delim raw) - (let ((p (ts:port s))) - ((if raw identity unescape-parsed-string-literal) - (map (lambda (s) - (if (and (not raw) (string? s)) - ;; remove `\` followed by a newline - (let ((spl (string-split s "\\\n"))) - (foldl (lambda (line s) - ;; if there is an odd number of backslashes before the backslash - ;; preceding the newline, keep the backslash and newline since - ;; the backslash is actually escaped - (define (odd-backslashes? (i (length s))) - (and (> i 0) - (let ((i (string.dec s i))) - (and (eqv? (string.char s i) #\\) - (not (odd-backslashes? i)))))) - (if (odd-backslashes?) - (string s "\\\n" line) - (string s line))) - "" - spl)) - s)) - (if (eqv? (peek-char p) delim) - (if (eqv? (peek-char (take-char p)) delim) - (map-first strip-leading-newline - (dedent-triplequoted-string - (parse-string-literal- 2 (take-char p) s delim raw))) - (list "")) - (parse-string-literal- 0 p s delim raw)))))) + (let* ((p (ts:port s)) + (str (if (eqv? (peek-char p) delim) + (if (eqv? (peek-char (take-char p)) delim) + (map-first strip-leading-newline + (dedent-triplequoted-string + (parse-string-literal- 2 (take-char p) s delim raw))) + (list "")) + (parse-string-literal- 0 p s delim raw)))) + (if raw str (unescape-parsed-string-literal + (map (lambda (s) + (if (string? s) (strip-escaped-newline s) s)) + str))))) (define (strip-leading-newline s) (let ((n (sizeof s)))