Skip to content

Commit a6ccaee

Browse files
authored
Fix core hook when parsing only whitespace. (#45)
core_parser_hook() should return `nothing` when attempting to parse statements or atoms, but when there's nothing but whitespace remaining.
1 parent 70fec80 commit a6ccaee

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/hooks.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ function core_parser_hook(code, filename, lineno, offset, options)
2828
# To copy the flisp parser driver, we ignore leading and trailing
2929
# trivia when parsing statements or atoms
3030
bump_trivia(stream)
31+
if peek(stream) == K"EndMarker"
32+
# If we're at the end of stream after skipping whitespace, just
33+
# return `nothing` to indicate this rather than attempting to
34+
# parse a statement or atom and failing.
35+
return Core.svec(nothing, last_byte(stream))
36+
end
3137
end
3238
JuliaSyntax.parse(stream; rule=rule)
3339
if rule !== :toplevel

test/hooks.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
@testset "Hooks for Core integration" begin
2+
@testset "parsing empty strings" begin
3+
@test JuliaSyntax.core_parser_hook("", "somefile", 0, :statement) == Core.svec(nothing, 0)
4+
@test JuliaSyntax.core_parser_hook("", "somefile", 0, :statement) == Core.svec(nothing, 0)
5+
6+
@test JuliaSyntax.core_parser_hook(" ", "somefile", 2, :statement) == Core.svec(nothing,2)
7+
@test JuliaSyntax.core_parser_hook(" #==# ", "somefile", 6, :statement) == Core.svec(nothing,6)
8+
end
9+
210
@testset "filename is used" begin
311
ex = JuliaSyntax.core_parser_hook("@a", "somefile", 0, :statement)[1]
412
@test Meta.isexpr(ex, :macrocall)
@@ -16,6 +24,7 @@
1624
@test Meta.parse("x + 1\n(y)\n", 1) == (:(x + 1), 7)
1725
@test Meta.parse("x + 1\n(y)\n", 7) == (:y, 11)
1826
@test Meta.parse(" x#==#", 1) == (:x, 7)
27+
@test Meta.parse(" #==# ", 1) == (nothing, 7)
1928

2029
# Check that Meta.parse throws the JuliaSyntax.ParseError rather than
2130
# Meta.ParseError when Core integration is enabled.

0 commit comments

Comments
 (0)