Skip to content

Commit ef894e2

Browse files
committed
Fix tokenization of consecutive \r\n line endings (#460)
Without this patch `\r\n\r\n` would be tokenized as `\r\n\r` and `\n` instead of `\r\n` and `\r\n`. Fixes fredrikekre/Runic.jl#15. (cherry picked from commit a11e664, PR #460)
1 parent db9a9c4 commit ef894e2

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/tokenize.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -733,9 +733,10 @@ function lex_whitespace(l::Lexer, c)
733733
if c == '\n'
734734
k = K"NewlineWs"
735735
end
736-
pc = peekchar(l)
736+
pc, ppc = dpeekchar(l)
737737
# stop on non whitespace and limit to a single newline in a token
738-
if !iswhitespace(pc) || (k == K"NewlineWs" && pc == '\n')
738+
if !iswhitespace(pc) ||
739+
(k == K"NewlineWs" && (pc == '\n' || (pc == '\r' && ppc == '\n')))
739740
break
740741
end
741742
c = readchar(l)

test/tokenize.jl

+10
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ end
161161
@test untokenize(tok(str), str)==">>"
162162
end
163163

164+
@testset "tokenize newlines" begin
165+
n = "\n"
166+
rn = "\r\n"
167+
nl = K"NewlineWs"
168+
for i in 0:5
169+
j = 5 - i
170+
@test toks(n^i * rn^j) == vcat(fill(n => nl, i), fill(rn => nl, j))
171+
@test toks(rn^i * n^j) == vcat(fill(rn => nl, i), fill(n => nl, j))
172+
end
173+
end
164174

165175
@testset "test added operators" begin
166176
@test tok("1+=2", 2).kind == K"+="

0 commit comments

Comments
 (0)