Skip to content

Commit 91a16ad

Browse files
committed
Ignore trailing trivia in Core parser hook
For compatibility with the flisp parser, we must consume any trailing whitespace at the end of a string. Otherwise Meta.parse() will fail unnecessarily when parsing a single statement.
1 parent 9606d96 commit 91a16ad

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/hooks.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ function core_parser_hook(code, filename, lineno, offset, options)
2525
stream = ParseStream(io)
2626
rule = options === :all ? :toplevel : options
2727
if rule !== :toplevel
28-
# To copy the flisp parser driver, we ignore leading trivia when
29-
# parsing statements or atoms
28+
# To copy the flisp parser driver, we ignore leading and trailing
29+
# trivia when parsing statements or atoms
3030
bump_trivia(stream)
3131
end
3232
JuliaSyntax.parse(stream; rule=rule)
33+
if rule !== :toplevel
34+
bump_trivia(stream)
35+
end
3336

3437
if any_error(stream)
3538
e = Expr(:error, ParseError(SourceFile(code), stream.diagnostics))

test/hooks.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
@test Meta.parse("x + 1") == :(x + 1)
55
@test Meta.parse("x + 1", 1) == (:(x + 1), 6)
66

7-
# Test that parsing statements incrementally works
8-
@test Meta.parse("x + 1\n(y)", 1) == (:(x + 1), 6)
9-
@test Meta.parse("x + 1\n(y)", 6) == (:y, 10)
7+
# Test that parsing statements incrementally works and stops after
8+
# whitespace / comment trivia
9+
@test Meta.parse("x + 1\n(y)\n", 1) == (:(x + 1), 7)
10+
@test Meta.parse("x + 1\n(y)\n", 7) == (:y, 11)
11+
@test Meta.parse(" x#==#", 1) == (:x, 7)
1012

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

0 commit comments

Comments
 (0)