Skip to content

Commit

Permalink
strip spaces after backslash-escaped newline (#41245)
Browse files Browse the repository at this point in the history
This was discussed on Slack with @StefanKarpinski and @BioTurboNick and
the general consensus was that stripping all spaces following a
backslash-escaped newline would make more sense than the current
behavior. Stripping the newline and spaces after a backslash now happens
before detenting for triple-quoted srings, so in cases like

```julia
"""
  a\
b
  c"""
```

the `b` doesn't affect detenting and the spaces before `a` and `c` are
removed.
  • Loading branch information
simeonschaub authored Jun 29, 2021
1 parent d1145d4 commit be08627
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
4 changes: 4 additions & 0 deletions base/shell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ function shell_parse(str::AbstractString, interpolate::Bool=true;
i += 1
popfirst!(st)
end
while !isempty(st) && peek(st)[2] in (' ', '\t')
i = nextind(str, i)
_ = popfirst!(st)
end
elseif in_double_quotes
isempty(st) && error("unterminated double quote")
k, c′ = peek(st)
Expand Down
25 changes: 17 additions & 8 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2188,16 +2188,24 @@
(define (unescape-parsed-string-literal strs)
(map-at even? unescape-string strs))

(define (strip-escaped-newline s raw)
(if raw s (map (lambda (s)
(if (string? s) (strip-escaped-newline- s) s))
s)))

;; remove `\` followed by a newline
(define (strip-escaped-newline s)
(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)))
(begin (write-char #\\ out) (write-char c out))
((define (loop-)
(if (memv (peek-char in) '(#\space #\tab))
(begin (take-char in) (loop-))))))
(loop #f))
((eqv? c #\\) (loop #t))
(else (write-char c out) (loop #f)))))
Expand All @@ -2210,13 +2218,14 @@
(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)))
(strip-escaped-newline
(parse-string-literal- 2 (take-char p) s delim raw)
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)))))
(strip-escaped-newline
(parse-string-literal- 0 p s delim raw)
raw))))
(if raw str (unescape-parsed-string-literal str))))

(define (strip-leading-newline s)
(let ((n (sizeof s)))
Expand Down
10 changes: 5 additions & 5 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2837,7 +2837,7 @@ end
@test "a\
b" == "ab"
@test "a\
b" == "a b"
b" == "ab"
@test raw"a\
b" == "a\\\nb"
@test "a$c\
Expand All @@ -2851,10 +2851,10 @@ b" == "acb"
b""" == "ab"
@test """
a\
b""" == "a b"
b""" == "ab"
@test """
a\
b""" == " ab"
b""" == "ab"
@test raw"""
a\
b""" == "a\\\nb"
Expand Down Expand Up @@ -2894,7 +2894,7 @@ b" == "acb"
@test `a\
b` == `ab`
@test `a\
b` == `a b`
b` == `ab`
@test `a$c\
b` == `acb`
@test `"a\
Expand All @@ -2910,7 +2910,7 @@ b'` == `$("a\\\nb")`
b``` == `ab`
@test ```
a\
b``` == `a b`
b``` == `ab`
@test ```
a\
b``` == ` ab`
Expand Down

0 comments on commit be08627

Please sign in to comment.