Skip to content

Commit e238fde

Browse files
authored
Merge pull request #10 from c42f/cjf/fix-raw-triple-strings-backslash
Fix raw triple string dedenting with `\` before newline
2 parents 7b560c7 + 3153015 commit e238fde

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

Tokenize/src/lexer.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,9 @@ function lex_string_chunk(l)
523523
c = readchar(l)
524524
if c == '\\'
525525
n = 1
526-
while true
526+
while peekchar(l) == '\\'
527527
readchar(l)
528528
n += 1
529-
if peekchar(l) != '\\'
530-
break
531-
end
532529
end
533530
if peekchar(l) == state.delim && !iseven(n)
534531
readchar(l)

Tokenize/test/lexer.jl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,22 +349,38 @@ end
349349

350350
@testset "string escaped newline whitespace" begin
351351
ts = collect(tokenize("\"x\\\n \ty\""))
352-
@test ts[1] ~ (T.DQUOTE , "\"")
352+
@test ts[1] ~ (T.DQUOTE, "\"")
353353
@test ts[2] ~ (T.STRING, "x")
354354
@test ts[3] ~ (T.WHITESPACE, "\\\n \t")
355355
@test ts[4] ~ (T.STRING, "y")
356-
@test ts[5] ~ (T.DQUOTE , "\"")
356+
@test ts[5] ~ (T.DQUOTE, "\"")
357+
358+
# No newline escape for raw strings
359+
ts = collect(tokenize("r\"x\\\ny\""))
360+
@test ts[1] ~ (T.IDENTIFIER , "r")
361+
@test ts[2] ~ (T.DQUOTE, "\"")
362+
@test ts[3] ~ (T.STRING, "x\\\ny")
363+
@test ts[4] ~ (T.DQUOTE , "\"")
357364
end
358365

359366
@testset "triple quoted string line splitting" begin
360367
ts = collect(tokenize("\"\"\"\nx\r\ny\rz\n\r\"\"\""))
361368
@test ts[1] ~ (T.TRIPLE_DQUOTE , "\"\"\"")
362-
@test ts[2] ~ (T.STRING, "\n")
363-
@test ts[3] ~ (T.STRING, "x\r\n")
364-
@test ts[4] ~ (T.STRING, "y\r")
365-
@test ts[5] ~ (T.STRING, "z\n")
366-
@test ts[6] ~ (T.STRING, "\r")
367-
@test ts[7] ~ (T.TRIPLE_DQUOTE, "\"\"\"")
369+
@test ts[2] ~ (T.STRING , "\n")
370+
@test ts[3] ~ (T.STRING , "x\r\n")
371+
@test ts[4] ~ (T.STRING , "y\r")
372+
@test ts[5] ~ (T.STRING , "z\n")
373+
@test ts[6] ~ (T.STRING , "\r")
374+
@test ts[7] ~ (T.TRIPLE_DQUOTE , "\"\"\"")
375+
376+
# Also for raw strings
377+
ts = collect(tokenize("r\"\"\"\nx\ny\"\"\""))
378+
@test ts[1] ~ (T.IDENTIFIER , "r")
379+
@test ts[2] ~ (T.TRIPLE_DQUOTE , "\"\"\"")
380+
@test ts[3] ~ (T.STRING , "\n")
381+
@test ts[4] ~ (T.STRING , "x\n")
382+
@test ts[5] ~ (T.STRING , "y")
383+
@test ts[6] ~ (T.TRIPLE_DQUOTE , "\"\"\"")
368384
end
369385

370386
@testset "interpolation" begin

src/parser.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,8 @@ function parse_call_chain(ps::ParseState, mark, is_macrocall=false)
16011601
# Triple quoted procesing for custom strings
16021602
# r"""\nx""" ==> (macrocall @r_str "x")
16031603
# r"""\n x\n y""" ==> (macrocall @r_str (string-sr "x\n" "y"))
1604-
1604+
# r"""\n x\\n y""" ==> (macrocall @r_str (string-sr "x\\\n" "y"))
1605+
#
16051606
# Use a special token kind for string and cmd macro names so the
16061607
# names can be expanded later as necessary.
16071608
outk = is_string_delim(k) ? K"StringMacroName" : K"CmdMacroName"

test/parser.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ tests = [
301301
"x\"\"" => """(macrocall @x_str "")"""
302302
"x``" => """(macrocall @x_cmd "")"""
303303
# Triple quoted procesing for custom strings
304-
"r\"\"\"\nx\"\"\"" => raw"""(macrocall @r_str "x")"""
305-
"r\"\"\"\n x\n y\"\"\"" => raw"""(macrocall @r_str (string-sr "x\n" "y"))"""
304+
"r\"\"\"\nx\"\"\"" => raw"""(macrocall @r_str "x")"""
305+
"r\"\"\"\n x\n y\"\"\"" => raw"""(macrocall @r_str (string-sr "x\n" "y"))"""
306+
"r\"\"\"\n x\\\n y\"\"\"" => raw"""(macrocall @r_str (string-sr "x\\\n" "y"))"""
306307
# Macro sufficies can include keywords and numbers
307308
"x\"s\"y" => """(macrocall @x_str "s" "y")"""
308309
"x\"s\"end" => """(macrocall @x_str "s" "end")"""

0 commit comments

Comments
 (0)