diff --git a/src/expr.jl b/src/expr.jl index 4d0093af..56ac0f89 100644 --- a/src/expr.jl +++ b/src/expr.jl @@ -215,7 +215,7 @@ function _internal_node_to_Expr(source, srcrange, head, childranges, childheads, elseif k == K"macrocall" _reorder_parameters!(args, 2) insert!(args, 2, loc) - elseif k == K"block" || k == K"toplevel" + elseif k == K"block" || (k == K"toplevel" && !has_flags(head, TOPLEVEL_SEMICOLONS_FLAG)) if isempty(args) push!(args, loc) else @@ -225,6 +225,9 @@ function _internal_node_to_Expr(source, srcrange, head, childranges, childheads, args[2*i-1] = source_location(LineNumberNode, source, first(childranges[i])) end end + if k == K"block" && has_flags(head, PARENS_FLAG) + popfirst!(args) + end elseif k == K"doc" headsym = :macrocall args = [GlobalRef(Core, Symbol("@doc")), loc, args...] diff --git a/src/parse_stream.jl b/src/parse_stream.jl index 74789c30..3862991f 100644 --- a/src/parse_stream.jl +++ b/src/parse_stream.jl @@ -33,6 +33,8 @@ const RAW_STRING_FLAG = RawFlags(1<<6) const PARENS_FLAG = RawFlags(1<<5) # Set for K"quote" for the short form `:x` as oppsed to long form `quote x end` const COLON_QUOTE = RawFlags(1<<5) +# Set for K"toplevel" which is delimited by parentheses +const TOPLEVEL_SEMICOLONS_FLAG = RawFlags(1<<5) # Set for K"struct" when mutable const MUTABLE_FLAG = RawFlags(1<<5) @@ -99,6 +101,8 @@ function untokenize(head::SyntaxHead; unique=true, include_flag_suff=true) has_flags(head, PARENS_FLAG) && (str = str*"-p") elseif kind(head) == K"quote" has_flags(head, COLON_QUOTE) && (str = str*"-:") + elseif kind(head) == K"toplevel" + has_flags(head, TOPLEVEL_SEMICOLONS_FLAG) && (str = str*"-;") elseif kind(head) == K"struct" has_flags(head, MUTABLE_FLAG) && (str = str*"-mut") elseif kind(head) == K"module" diff --git a/src/parser.jl b/src/parser.jl index acf2fe26..efdf917d 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -495,7 +495,7 @@ function parse_stmts(ps::ParseState) error="extra tokens after end of expression") end if do_emit - emit(ps, mark, K"toplevel") + emit(ps, mark, K"toplevel", TOPLEVEL_SEMICOLONS_FLAG) end end diff --git a/test/expr.jl b/test/expr.jl index 9c9088b3..3d785643 100644 --- a/test/expr.jl +++ b/test/expr.jl @@ -33,10 +33,20 @@ LineNumberNode(4), :c, ) + @test parsestmt("(a;b;c)") == + Expr(:block, + :a, + LineNumberNode(1), + :b, + LineNumberNode(1), + :c, + ) @test parsestmt("begin end") == Expr(:block, LineNumberNode(1) ) + @test parsestmt("(;;)") == + Expr(:block) @test parseall("a\n\nb") == Expr(:toplevel, @@ -45,6 +55,8 @@ LineNumberNode(3), :b, ) + @test parsestmt("a;b") == + Expr(:toplevel, :a, :b) @test parsestmt("module A\n\nbody\nend") == Expr(:module,