Skip to content

Commit a11e664

Browse files
authored
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.
1 parent ebcc56a commit a11e664

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
@@ -163,6 +163,16 @@ end
163163
@test untokenize(tok(str), str)==">>"
164164
end
165165

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

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

0 commit comments

Comments
 (0)