Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version = "1.0.0-DEV"
JuliaSyntax = "70703baa-626e-46a2-a12c-08ffd08c73b4"

[sources]
JuliaSyntax = {rev = "e02f29f", url = "https://github.com/JuliaLang/JuliaSyntax.jl"}
JuliaSyntax = {rev = "f6ae138", url = "https://github.com/JuliaLang/JuliaSyntax.jl"}

[compat]
julia = "1"
Expand Down
4 changes: 2 additions & 2 deletions src/ast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ end
function makeleaf(ctx, srcref, k::Kind, value; kws...)
graph = syntax_graph(ctx)
if k == K"Identifier" || k == K"core" || k == K"top" || k == K"Symbol" ||
k == K"globalref" || k == K"Placeholder" || k == K"MacroName" ||
k == K"StringMacroName" || k == K"CmdMacroName"
k == K"globalref" || k == K"Placeholder" ||
k == K"StrMacroName" || k == K"CmdMacroName"
makeleaf(graph, srcref, k; name_val=value, kws...)
elseif k == K"BindingId"
makeleaf(graph, srcref, k; var_id=value, kws...)
Expand Down
25 changes: 16 additions & 9 deletions src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
end
deleteat!(child_exprs, 2)
if a1 isa Symbol
child_exprs[1] = a1_esc(Expr(:MacroName, a1))
child_exprs[1] = a1_esc(Expr(:macro_name, a1))
elseif a1 isa Expr && a1.head === :(.)
a12,a12_esc = unwrap_esc(a1.args[2])
if a12 isa QuoteNode
child_exprs[1] = a1_esc(Expr(:(.), a1.args[1],
Expr(:MacroName, a12_esc(a12.value))))
Expr(:macro_name, a12_esc(a12.value))))
end
elseif a1 isa GlobalRef && a1.mod === Core
# TODO (maybe): syntax-introduced macrocalls are listed here for
Expand Down Expand Up @@ -400,7 +400,7 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
if e.args[1].head === :macrocall
st_k = K"macrocall"
c1,c1_esc = unwrap_esc(callargs[1])
callargs[1] = c1_esc(Expr(:MacroName, c1))
callargs[1] = c1_esc(Expr(:macro_name, c1))
else
st_k = K"call"
end
Expand Down Expand Up @@ -514,13 +514,20 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
end

#---------------------------------------------------------------------------
# Temporary heads introduced by us converting the parent expr
if e.head === :MacroName
# Possibly-temporary heads introduced by us converting the parent expr
if e.head === :macro_name
@assert nargs === 1
mac_name = string(e.args[1])
mac_name = mac_name == "@__dot__" ? "@." : mac_name
st_id = _insert_tree_node(graph, K"MacroName", src, st_flags; name_val=mac_name)
return st_id, src
# Trim `@` for a correct SyntaxTree, although we need to add it back
# later for finding the macro
if e.args[1] === :(.)
mac_name = string(e.args[1][2])
mac_name = mac_name == "@__dot__" ? "." : mac_name[2:end]
child_exprs[1] = Expr(:(.), e.args[1][1], Symbol(mac_name))
else
mac_name = string(e.args[1])
mac_name = mac_name == "@__dot__" ? "." : mac_name[2:end]
child_exprs[1] = Symbol(mac_name)
end
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, this back and forth conversion is ugly ... at face value it makes me worry the upstream changes weren't a good tradeoff.

I guess we could delete all this if we gave up on trying to reproduce all the macro name kinds. They are mainly present to represent the source text as it is parsed ... it might be fine if they're normalized to K"Identifier" when converting Expr back to SyntaxTree?

On the other hand, it seems bad to have more cases when SyntaxTree->Expr->SyntaxTree gives a different expression given that macros can observe the difference. Hmm.

elseif e.head === :catch_var_placeholder
st_k = K"Placeholder"
st_attrs[:name_val] = ""
Expand Down
31 changes: 26 additions & 5 deletions src/macro_expansion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,31 @@ function Base.showerror(io::IO, exc::MacroExpansionError)
end
end

function fixup_macro_name(ctx::MacroExpansionContext, ex::SyntaxTree)
k = kind(ex)
if k == K"StrMacroName" || k == K"CmdMacroName"
layerid = get(ex, :scope_layer, current_layer_id(ctx))
newname = JuliaSyntax.lower_identifier_name(ex.name_val, k)
makeleaf(ctx, ex, ex, kind=K"Identifier", scope_layer=layerid, name_val=newname)
elseif k == K"macro_name"
@chk numchildren(ex) === 1
if kind(ex[1]) === K"."
@ast ctx ex [K"." ex[1][1] [K"macro_name" ex[1][2]]]
else
layerid = get(ex, :scope_layer, current_layer_id(ctx))
newname = JuliaSyntax.lower_identifier_name(ex[1].name_val, K"macro_name")
makeleaf(ctx, ex[1], ex[1], kind=kind(ex[1]), name_val=newname)
end
else
mapchildren(e->fixup_macro_name(ctx,e), ctx, ex)
end
end

function eval_macro_name(ctx::MacroExpansionContext, mctx::MacroContext, ex::SyntaxTree)
# `ex1` might contain a nontrivial mix of scope layers so we can't just
# `eval()` it, as it's already been partially lowered by this point.
# Instead, we repeat the latter parts of `lower()` here.
ex1 = expand_forms_1(ctx, ex)
ex1 = expand_forms_1(ctx, fixup_macro_name(ctx, ex))
ctx2, ex2 = expand_forms_2(ctx, ex1)
ctx3, ex3 = resolve_scopes(ctx2, ex2)
ctx4, ex4 = convert_closures(ctx3, ex3)
Expand Down Expand Up @@ -334,9 +354,10 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
layerid = get(ex, :scope_layer, current_layer_id(ctx))
makeleaf(ctx, ex, ex, kind=K"Identifier", scope_layer=layerid)
end
elseif k == K"Identifier" || k == K"MacroName" || k == K"StringMacroName" || k == K"CmdMacroName"
layerid = get(ex, :scope_layer, current_layer_id(ctx))
makeleaf(ctx, ex, ex, kind=K"Identifier", scope_layer=layerid)
elseif k == K"StrMacroName" || k == K"CmdMacroName" || k == K"macro_name"
# These can appear outside of a macrocall, e.g. in `import`
e2 = fixup_macro_name(ctx, ex)
expand_forms_1(ctx, e2)
elseif k == K"var" || k == K"char" || k == K"parens"
# Strip "container" nodes
@chk numchildren(ex) == 1
Expand Down Expand Up @@ -397,7 +418,7 @@ function expand_forms_1(ctx::MacroExpansionContext, ex::SyntaxTree)
@ast ctx ex [K"." expand_forms_1(ctx, ex[1]) e2]
elseif k == K"cmdstring"
@chk numchildren(ex) == 1
e2 = @ast ctx ex [K"macrocall" "@cmd"::K"core" ex[1]]
e2 = @ast ctx ex [K"macrocall" [K"macro_name" "cmd"::K"core"] ex[1]]
expand_macro(ctx, e2)
elseif (k == K"call" || k == K"dotcall")
# Do some initial desugaring of call and dotcall here to simplify
Expand Down
12 changes: 9 additions & 3 deletions src/syntax_graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ attrsummary(name, value::Number) = "$name=$value"

function _value_string(ex)
k = kind(ex)
str = k in KSet"Identifier MacroName StringMacroName CmdMacroName" || is_operator(k) ? ex.name_val :
str = k in KSet"Identifier StrMacroName CmdMacroName" || is_operator(k) ? ex.name_val :
k == K"Placeholder" ? ex.name_val :
k == K"SSAValue" ? "%" :
k == K"BindingId" ? "#" :
Expand Down Expand Up @@ -609,11 +609,17 @@ function _find_SyntaxTree_macro(ex, line)
# We're in the line range. Either
if firstline == line && kind(c) == K"macrocall" && begin
name = c[1]
if kind(name) == K"macro_name"
name = name[1]
end
if kind(name) == K"."
name = name[2]
if kind(name) == K"macro_name"
name = name[1]
end
end
@assert kind(name) == K"MacroName"
name.name_val == "@SyntaxTree"
@assert kind(name) == K"Identifier"
name.name_val == "SyntaxTree"
end
# We find the node we're looking for. NB: Currently assuming a max
# of one @SyntaxTree invocation per line. Though we could relax
Expand Down
10 changes: 5 additions & 5 deletions test/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ const JL = JuliaLowering
# `@mac x` with macro name escaped
@test JuliaLowering.expr_to_syntaxtree(Expr(:macrocall, esc(Symbol("@mac")), nothing, :x)) ≈
@ast_ [K"macrocall"
[K"escape" "@mac"::K"MacroName"]
[K"escape" [K"macro_name" "mac"::K"Identifier"]]
"x"::K"Identifier"
]

Expand All @@ -505,7 +505,7 @@ const JL = JuliaLowering
[K"escape"
[K"."
"A"::K"Identifier"
"@mac"::K"MacroName"
[K"macro_name" "mac"::K"Identifier"]
]
]
"x"::K"Identifier"
Expand Down Expand Up @@ -577,7 +577,7 @@ const JL = JuliaLowering
Expr(:macrocall, Expr(:var"hygienic-scope", Symbol("@mac"), :other, :args), nothing, :x)) ≈
@ast_ [K"macrocall"
[K"hygienic_scope"
"@mac"::K"MacroName"
[K"macro_name" "mac"::K"Identifier"]
"other"::K"Identifier" # (<- normally a Module)
"args"::K"Identifier" # (<- normally a LineNumberNode)
]
Expand All @@ -587,7 +587,7 @@ const JL = JuliaLowering
# One example of double escaping
@test JuliaLowering.expr_to_syntaxtree(Expr(:macrocall, esc(esc(Symbol("@mac"))), nothing, :x)) ≈
@ast_ [K"macrocall"
[K"escape" [K"escape" "@mac"::K"MacroName"]]
[K"escape" [K"escape" [K"macro_name" "mac"::K"Identifier"]]]
"x"::K"Identifier"
]

Expand All @@ -600,7 +600,7 @@ const JL = JuliaLowering
@ast_ [K"macrocall"
[K"hygienic_scope"
[K"escape"
"@mac"::K"MacroName"
[K"macro_name" "mac"::K"Identifier"]
]
"other"::K"Identifier" # (<- normally a Module)
"args"::K"Identifier" # (<- normally a LineNumberNode)
Expand Down
Loading
Loading