Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 17 additions & 4 deletions JuliaSyntax/src/julia/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2218,10 +2218,23 @@ function parse_function_signature(ps::ParseState, is_function::Bool)
opts = parse_brackets(ps, K")") do had_commas, had_splat, num_semis, num_subexprs
_parsed_call = was_eventually_call(ps)
_maybe_grouping_parens = !had_commas && !had_splat && num_semis == 0 && num_subexprs == 1
# Skip intervening newlines only when the parentheses hold a single
# expression, which is the ambiguous case between a name like (::T)
# and an anonymous function parameter list.
next_kind = peek(ps, 2, skip_newlines=_maybe_grouping_parens)
# Check if there's a newline between `)` and the next `(` or `.`.
# We need to find where `)` is and check what immediately follows it.
# If peek(1, skip_newlines=false) is `)`, we're directly before it.
# Otherwise there's whitespace/newline before `)`.
next_token_pos = if peek(ps, 1, skip_newlines=false) == K")"
# Directly before ), token after ) is at 2
2
else
# There's whitespace before ), so ) is at 2
# and what follows ) is at 3
3
end
token_after_paren = peek(ps, next_token_pos, skip_newlines=false)
# If token_after_paren is a newline, this is an anonymous function
has_newline_after_paren = _maybe_grouping_parens && token_after_paren == K"NewlineWs"
# Get the next significant token to determine if we need to parse a call
next_kind = peek(ps, 2, skip_newlines=_maybe_grouping_parens && !has_newline_after_paren)
Comment on lines +2221 to +2237
Copy link

@fogti fogti Dec 7, 2025

Choose a reason for hiding this comment

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

I think it would make a lot of sense to split the logic with a case distinction on _maybe_grouping_parens:

Suggested change
# Check if there's a newline between `)` and the next `(` or `.`.
# We need to find where `)` is and check what immediately follows it.
# If peek(1, skip_newlines=false) is `)`, we're directly before it.
# Otherwise there's whitespace/newline before `)`.
next_token_pos = if peek(ps, 1, skip_newlines=false) == K")"
# Directly before ), token after ) is at 2
2
else
# There's whitespace before ), so ) is at 2
# and what follows ) is at 3
3
end
token_after_paren = peek(ps, next_token_pos, skip_newlines=false)
# If token_after_paren is a newline, this is an anonymous function
has_newline_after_paren = _maybe_grouping_parens && token_after_paren == K"NewlineWs"
# Get the next significant token to determine if we need to parse a call
next_kind = peek(ps, 2, skip_newlines=_maybe_grouping_parens && !has_newline_after_paren)
next_kind = if _maybe_grouping_parens
# Check if there's a newline between `)` and the next `(` or `.`.
# We need to find where `)` is and check what immediately follows it.
# If peek(1, skip_newlines=false) is `)`, we're directly before it.
# Otherwise there's whitespace/newline before `)`.
next_token_pos = if peek(ps, 1, skip_newlines=false) == K")"
# Directly before ), token after ) is at 2
2
else
# There's whitespace before ), so ) is at 2
# and what follows ) is at 3
3
end
token_after_paren = peek(ps, next_token_pos, skip_newlines=false)
# If token_after_paren is a newline, this is an anonymous function
# Get the next significant token to determine if we need to parse a call
peek(ps, 2, skip_newlines= token_after_paren != K"NewlineWs")
else
# Get the next significant token to determine if we need to parse a call
peek(ps, 2, skip_newlines=false)
end

Copy link

Choose a reason for hiding this comment

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

Subsequently, it makes sense to inspect more closely what happens if _maybe_grouping_parens is true: If the next tokens are:

  • K")", K"NewlineWs", then we call peek(ps, 2, skip_newlines=false) = token_after_paren
  • K")", !K"NewlineWs", then we call peek(ps, 2, skip_newlines=true) = token_after_paren
  • !K")", x/*we expect K")" here, but don't check that*/, K"NewlineWs", then we call peek(ps, 2, skip_newlines=false)=token_after_paren (I think (hope I interpret it correctly) that __lookahead_index treats the case with skip_newlines=true such that it also skips newlines entirely, including in the count up to n=2)
  • !K")", x, !K"NewlineWs", then we call peek(ps, 2, skip_newlines=false)=token_after_paren`

So, I think we can omit the peek(ps, 2, skip_newlines= token_after_paren != K"NewlineWs") entirely.

Suggested change
# Check if there's a newline between `)` and the next `(` or `.`.
# We need to find where `)` is and check what immediately follows it.
# If peek(1, skip_newlines=false) is `)`, we're directly before it.
# Otherwise there's whitespace/newline before `)`.
next_token_pos = if peek(ps, 1, skip_newlines=false) == K")"
# Directly before ), token after ) is at 2
2
else
# There's whitespace before ), so ) is at 2
# and what follows ) is at 3
3
end
token_after_paren = peek(ps, next_token_pos, skip_newlines=false)
# If token_after_paren is a newline, this is an anonymous function
has_newline_after_paren = _maybe_grouping_parens && token_after_paren == K"NewlineWs"
# Get the next significant token to determine if we need to parse a call
next_kind = peek(ps, 2, skip_newlines=_maybe_grouping_parens && !has_newline_after_paren)
next_token_pos = if _maybe_grouping_paren
# Check if there's a newline between `)` and the next `(` or `.`.
# We need to find where `)` is and check what immediately follows it.
# If peek(1, skip_newlines=false) is `)`, we're directly before it.
# Otherwise there's whitespace/newline before `)`.
if peek(ps, 1, skip_newlines=false) == K")"
# Directly before ), token after ) is at 2
2
else
# There's whitespace before ), so ) is at 2
# and what follows ) is at 3
3
end
else
2
end
# Get the next significant token to determine if we need to parse a call
next_kind = peek(ps, next_token_pos, skip_newlines=false)

_needs_parse_call = next_kind ∈ KSet"( ."
_is_anon_func = (!_needs_parse_call && !_parsed_call) || had_commas
return (needs_parameters = _is_anon_func,
Expand Down
2 changes: 2 additions & 0 deletions JuliaSyntax/test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ tests = [
"macro \$f() end" => "(macro (call (\$ f)) (block))"
"macro (\$f)() end" => "(macro (call (parens (\$ f))) (block))"
"function (x) body end"=> "(function (tuple-p x) (block body))"
"function (x)\n body\nend"=> "(function (tuple-p x) (block body))"
"function (x)\n() end" => "(function (tuple-p x) (block (tuple-p)))"
"function (x,y) end" => "(function (tuple-p x y) (block))"
"function (x,y,) end" => "(function (tuple-p-, x y) (block))"
"function (x=1) end" => "(function (tuple-p (= x 1)) (block))"
Expand Down