Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/core/parse_stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,14 @@ function peek_behind_pos(stream::ParseStream; skip_trivia::Bool=true,
while node_idx > 0
node = stream.output[node_idx]
if kind(node) == K"TOMBSTONE" || (skip_trivia && is_trivia(node))
node_idx -= 1
byte_idx -= node.byte_span
# If this is a non-terminal node, skip its children without
# subtracting their byte_spans, as they're already included in the parent
if is_non_terminal(node)
node_idx -= (1 + node.node_span)
else
node_idx -= 1
end
else
break
end
Expand Down
10 changes: 10 additions & 0 deletions test/parse_stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,13 @@ end
@test ParseStream(y) isa ParseStream
@test parsestmt(Expr, y) == parsestmt(Expr, "1")
end

@testset "peek_behind_pos with negative byte index" begin
# Test that peek_behind_pos doesn't cause InexactError when byte_idx goes negative
# This can happen when parsing certain incomplete keywords like "do"
# where trivia skipping walks back past the beginning of the stream
@test_throws JuliaSyntax.ParseError parseall(GreenNode, "do")
@test_throws JuliaSyntax.ParseError parseall(GreenNode, "do ")
@test_throws JuliaSyntax.ParseError parseall(GreenNode, " do")
@test_throws JuliaSyntax.ParseError parseall(GreenNode, "do\n")
end
Loading