Skip to content

Commit

Permalink
Allow macrocall in function def syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed Jul 26, 2024
1 parent ae7d6ac commit 8543f48
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2157,19 +2157,23 @@ function parse_function_signature(ps::ParseState, is_function::Bool)
end
end
end
if peek(ps, skip_newlines=true) == K"end" && !is_anon_func && !parsed_call
return false
end
if needs_parse_call
# Parse function argument list
# function f(x,y) end ==> (function (call f x y) (block))
# function f{T}() end ==> (function (call (curly f T)) (block))
# function A.f() end ==> (function (call (. A f)) (block))
parse_call_chain(ps, mark)
if peek_behind(ps).kind != K"call"
sig_kind = peek_behind(ps).kind
if sig_kind in KSet"Identifier var $" && peek(ps, skip_newlines=true) == K"end"
# function f end ==> (function f)
# function $f end ==> (function $f)
return false
elseif sig_kind == K"macrocall"
min_supported_version(v"1.12", ps, mark, "macrocall function sig")
elseif sig_kind != K"call"
# function f body end ==> (function (error f) (block body))
emit(ps, mark, K"error",
error="Invalid signature in $(is_function ? "function" : "macro") definition")
error="Invalid signature in $(is_function ? "function" : "macro") definition")
end
end
if is_function && peek(ps) == K"::"
Expand Down Expand Up @@ -3494,7 +3498,11 @@ function parse_atom(ps::ParseState, check_identifiers=true)
# + ==> +
# .+ ==> (. +)
# .= ==> (. =)
bump_dotsplit(ps, emit_dot_node=true)
if is_dotted(peek_token(ps))
bump_dotsplit(ps, emit_dot_node=true)
else
bump(ps, remap_kind=K"Identifier")
end
if check_identifiers && !is_valid_identifier(leading_kind)
# += ==> (error +=)
# ? ==> (error ?)
Expand Down
8 changes: 8 additions & 0 deletions test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ tests = [
# body
"function f() \n a \n b end" => "(function (call f) (block a b))"
"function f() end" => "(function (call f) (block))"
# Macrocall as sig
((v=v"1.12",), "function @callmemacro(a::Int) \n 1 \n end") => "(function (macrocall-p @callmemacro (::-i a Int)) (block 1))"
((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))"
((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))"
((v=v"1.12",), "function @f()() end") => "(function (call (macrocall-p @f)) (block))"
# Errors
"function" => "(function (error (error)) (block (error)) (error-t))"
],
Expand Down Expand Up @@ -963,6 +968,9 @@ tests = [
"public[7] = 5" => "(= (ref public 7) 5)"
"public() = 6" => "(= (call public) 6)"
]),
JuliaSyntax.parse_stmts => [
((v = v"1.12",), "@callmemacro(b::Float64) = 2") => "(= (macrocall-p @callmemacro (::-i b Float64)) 2)"
],
JuliaSyntax.parse_docstring => [
""" "notdoc" ] """ => "(string \"notdoc\")"
""" "notdoc" \n] """ => "(string \"notdoc\")"
Expand Down

0 comments on commit 8543f48

Please sign in to comment.