Skip to content

Commit ffd3e47

Browse files
authored
Fix hooks: whitespace parsing with Meta.parseatom() (#48)
Previously, `Meta.parseatom()` would consume trailing whitespace which breaks usages like in `Base.shell_parse()`
1 parent 253d879 commit ffd3e47

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/hooks.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ function core_parser_hook(code, filename, lineno, offset, options)
2424

2525
stream = ParseStream(io)
2626
rule = options === :all ? :toplevel : options
27-
if rule !== :toplevel
28-
# To copy the flisp parser driver, we ignore leading and trailing
29-
# trivia when parsing statements or atoms
27+
if rule === :statement || rule === :atom
28+
# To copy the flisp parser driver:
29+
# * Parsing atoms consumes leading trivia
30+
# * Parsing statements consumes leading+trailing trivia
3031
bump_trivia(stream)
3132
if peek(stream) == K"EndMarker"
3233
# If we're at the end of stream after skipping whitespace, just
@@ -36,7 +37,7 @@ function core_parser_hook(code, filename, lineno, offset, options)
3637
end
3738
end
3839
JuliaSyntax.parse(stream; rule=rule)
39-
if rule !== :toplevel
40+
if rule === :statement
4041
bump_trivia(stream)
4142
end
4243

test/hooks.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
@testset "Hooks for Core integration" begin
2-
@testset "parsing empty strings" begin
2+
@testset "whitespace parsing" begin
33
@test JuliaSyntax.core_parser_hook("", "somefile", 0, :statement) == Core.svec(nothing, 0)
44
@test JuliaSyntax.core_parser_hook("", "somefile", 0, :statement) == Core.svec(nothing, 0)
55

66
@test JuliaSyntax.core_parser_hook(" ", "somefile", 2, :statement) == Core.svec(nothing,2)
77
@test JuliaSyntax.core_parser_hook(" #==# ", "somefile", 6, :statement) == Core.svec(nothing,6)
8+
9+
@test JuliaSyntax.core_parser_hook(" x \n", "somefile", 0, :statement) == Core.svec(:x,4)
10+
@test JuliaSyntax.core_parser_hook(" x \n", "somefile", 0, :atom) == Core.svec(:x,2)
811
end
912

1013
@testset "filename is used" begin

0 commit comments

Comments
 (0)