Skip to content

Commit a78aa6e

Browse files
authored
Better conversion to Expr with parse(..., ignore_errors=true) (#224)
This isn't a complete solution, but it improves the situation somewhat. (It's unclear whether we should spend a lot of effort here as converting to Expr is not a particularly expressive path for error propagation. We probably need to plumb diagnostic support into the Julia runtime in a more general way.)
1 parent 70c908f commit a78aa6e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/expr.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ end
3232

3333
function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
3434
eq_to_kw=false, map_kw_in_params=false)
35+
nodekind = kind(node)
3536
if !haschildren(node)
3637
val = node.val
3738
if val isa Union{Int128,UInt128,BigInt}
@@ -44,15 +45,21 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
4445
val isa UInt128 ? Symbol("@uint128_str") :
4546
Symbol("@big_str")
4647
return Expr(:macrocall, GlobalRef(Core, macname), nothing, str)
47-
elseif kind(node) == K"core_@cmd"
48+
elseif nodekind == K"core_@cmd"
4849
return GlobalRef(Core, Symbol("@cmd"))
49-
elseif kind(node) == K"MacroName" && val === Symbol("@.")
50+
elseif nodekind == K"MacroName" && val === Symbol("@.")
5051
return Symbol("@__dot__")
52+
elseif is_error(nodekind)
53+
# TODO: Get non-token error messages in here as well, somehow?
54+
# There's an awkward mismatch between the out-of-tree
55+
# `Vector{Diagnostic}` vs Expr(:error) being part of the tree.
56+
return Expr(:error,
57+
"$(_token_error_descriptions[nodekind]): `$(sourcetext(node))`"
58+
)
5159
else
5260
return val
5361
end
5462
end
55-
nodekind = kind(node)
5663
node_args = children(node)
5764
if nodekind == K"var"
5865
@check length(node_args) == 1

test/expr.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,12 @@
326326
@test parse(Expr, "baremodule A end") ==
327327
Expr(:module, false, :A, Expr(:block, LineNumberNode(1), LineNumberNode(1)))
328328
end
329+
330+
@testset "errors" begin
331+
@test parse(Expr, "--", ignore_errors=true) ==
332+
Expr(:error, "invalid operator: `--`")
333+
@test parseall(Expr, "a b", ignore_errors=true) ==
334+
Expr(:toplevel, LineNumberNode(1), :a,
335+
LineNumberNode(1), Expr(:error, :b))
336+
end
329337
end

0 commit comments

Comments
 (0)