Skip to content

Commit 8543f48

Browse files
committed
Allow macrocall in function def syntax
Goes with JuliaLang/julia#55040
1 parent ae7d6ac commit 8543f48

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/parser.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,19 +2157,23 @@ function parse_function_signature(ps::ParseState, is_function::Bool)
21572157
end
21582158
end
21592159
end
2160-
if peek(ps, skip_newlines=true) == K"end" && !is_anon_func && !parsed_call
2161-
return false
2162-
end
21632160
if needs_parse_call
21642161
# Parse function argument list
21652162
# function f(x,y) end ==> (function (call f x y) (block))
21662163
# function f{T}() end ==> (function (call (curly f T)) (block))
21672164
# function A.f() end ==> (function (call (. A f)) (block))
21682165
parse_call_chain(ps, mark)
2169-
if peek_behind(ps).kind != K"call"
2166+
sig_kind = peek_behind(ps).kind
2167+
if sig_kind in KSet"Identifier var $" && peek(ps, skip_newlines=true) == K"end"
2168+
# function f end ==> (function f)
2169+
# function $f end ==> (function $f)
2170+
return false
2171+
elseif sig_kind == K"macrocall"
2172+
min_supported_version(v"1.12", ps, mark, "macrocall function sig")
2173+
elseif sig_kind != K"call"
21702174
# function f body end ==> (function (error f) (block body))
21712175
emit(ps, mark, K"error",
2172-
error="Invalid signature in $(is_function ? "function" : "macro") definition")
2176+
error="Invalid signature in $(is_function ? "function" : "macro") definition")
21732177
end
21742178
end
21752179
if is_function && peek(ps) == K"::"
@@ -3494,7 +3498,11 @@ function parse_atom(ps::ParseState, check_identifiers=true)
34943498
# + ==> +
34953499
# .+ ==> (. +)
34963500
# .= ==> (. =)
3497-
bump_dotsplit(ps, emit_dot_node=true)
3501+
if is_dotted(peek_token(ps))
3502+
bump_dotsplit(ps, emit_dot_node=true)
3503+
else
3504+
bump(ps, remap_kind=K"Identifier")
3505+
end
34983506
if check_identifiers && !is_valid_identifier(leading_kind)
34993507
# += ==> (error +=)
35003508
# ? ==> (error ?)

test/parser.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ tests = [
603603
# body
604604
"function f() \n a \n b end" => "(function (call f) (block a b))"
605605
"function f() end" => "(function (call f) (block))"
606+
# Macrocall as sig
607+
((v=v"1.12",), "function @callmemacro(a::Int) \n 1 \n end") => "(function (macrocall-p @callmemacro (::-i a Int)) (block 1))"
608+
((v=v"1.12",), "function @callmemacro(a::T, b::T) where T <: Int64\n3\nend") => "(function (where (macrocall-p @callmemacro (::-i a T) (::-i b T)) (<: T Int64)) (block 3))"
609+
((v=v"1.12",), "function @callmemacro(a::Int, b::Int, c::Int)::Float64\n4\nend") => "(function (::-i (macrocall-p @callmemacro (::-i a Int) (::-i b Int) (::-i c Int)) Float64) (block 4))"
610+
((v=v"1.12",), "function @f()() end") => "(function (call (macrocall-p @f)) (block))"
606611
# Errors
607612
"function" => "(function (error (error)) (block (error)) (error-t))"
608613
],
@@ -963,6 +968,9 @@ tests = [
963968
"public[7] = 5" => "(= (ref public 7) 5)"
964969
"public() = 6" => "(= (call public) 6)"
965970
]),
971+
JuliaSyntax.parse_stmts => [
972+
((v = v"1.12",), "@callmemacro(b::Float64) = 2") => "(= (macrocall-p @callmemacro (::-i b Float64)) 2)"
973+
],
966974
JuliaSyntax.parse_docstring => [
967975
""" "notdoc" ] """ => "(string \"notdoc\")"
968976
""" "notdoc" \n] """ => "(string \"notdoc\")"

0 commit comments

Comments
 (0)