Skip to content

Commit 4b17592

Browse files
authored
Avoid adding tuple in parsing of $(x) -> rhs (#534)
1 parent 2e965a1 commit 4b17592

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/parser.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ end
14611461
# $a ==> ($ a)
14621462
#
14631463
# flisp: parse-unary-prefix
1464-
function parse_unary_prefix(ps::ParseState)
1464+
function parse_unary_prefix(ps::ParseState, has_unary_prefix=false)
14651465
mark = position(ps)
14661466
t = peek_token(ps)
14671467
k = kind(t)
@@ -1480,15 +1480,15 @@ function parse_unary_prefix(ps::ParseState)
14801480
# $a ==> ($ a)
14811481
# $$a ==> ($ ($ a))
14821482
# $&a ==> ($ (& a))
1483-
parse_unary_prefix(ps)
1483+
parse_unary_prefix(ps, true)
14841484
end
14851485
# Only need PREFIX_OP_FLAG for ::
14861486
f = k == K"::" ? PREFIX_OP_FLAG : EMPTY_FLAGS
14871487
emit(ps, mark, k, f)
14881488
end
14891489
else
14901490
# .&(x,y) ==> (call .& x y)
1491-
parse_atom(ps)
1491+
parse_atom(ps, true, has_unary_prefix)
14921492
end
14931493
end
14941494

@@ -3066,7 +3066,7 @@ end
30663066
# *very* overloaded!
30673067
#
30683068
# flisp: parse-paren / parse-paren-
3069-
function parse_paren(ps::ParseState, check_identifiers=true)
3069+
function parse_paren(ps::ParseState, check_identifiers=true, has_unary_prefix=false)
30703070
ps = ParseState(ps, range_colon_enabled=true,
30713071
space_sensitive=false,
30723072
where_enabled=true,
@@ -3100,7 +3100,7 @@ function parse_paren(ps::ParseState, check_identifiers=true)
31003100
opts = parse_brackets(ps, K")") do had_commas, had_splat, num_semis, num_subexprs
31013101
is_tuple = had_commas || (had_splat && num_semis >= 1) ||
31023102
(initial_semi && (num_semis == 1 || num_subexprs > 0)) ||
3103-
(peek(ps, 2) == K"->" && peek_behind(ps).kind != K"where")
3103+
(peek(ps, 2) == K"->" && (peek_behind(ps).kind != K"where" && !has_unary_prefix))
31043104
return (needs_parameters=is_tuple,
31053105
is_tuple=is_tuple,
31063106
is_block=num_semis > 0)
@@ -3475,7 +3475,7 @@ end
34753475
# the syntactic operators or closing tokens.
34763476
#
34773477
# flisp: parse-atom
3478-
function parse_atom(ps::ParseState, check_identifiers=true)
3478+
function parse_atom(ps::ParseState, check_identifiers=true, has_unary_prefix=false)
34793479
bump_trivia(ps)
34803480
mark = position(ps)
34813481
leading_kind = peek(ps)
@@ -3634,7 +3634,7 @@ function parse_atom(ps::ParseState, check_identifiers=true)
36343634
bump(ps, remap_kind=K"Identifier")
36353635
end
36363636
elseif leading_kind == K"(" # parens or tuple
3637-
parse_paren(ps, check_identifiers)
3637+
parse_paren(ps, check_identifiers, has_unary_prefix)
36383638
elseif leading_kind == K"[" # cat expression
36393639
bump(ps, TRIVIA_FLAG)
36403640
ckind, cflags = parse_cat(ps, K"]", ps.end_symbol)

test/parser.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ tests = [
293293
"(a,b)->c" => "(-> (tuple-p a b) c)"
294294
"(a;b=1)->c" => "(-> (tuple-p a (parameters (= b 1))) c)"
295295
"x::T->c" => "(-> (tuple (::-i x T)) c)"
296+
"\$a->b" => "(-> (tuple (\$ a)) b)"
297+
"\$(a)->b" => "(-> (tuple (\$ (parens a))) b)"
298+
# FIXME "&(a)->b" => "(-> (tuple-p (& (parens a))) b)"
299+
# FIXME "::(a)->b" => "(-> (tuple-p (:: (parens a))) b)"
296300
# `where` combined with `->` still parses strangely. However:
297301
# * It's extra hard to add a tuple around the `x` in this syntax corner case.
298302
# * The user already needs to add additional, ugly, parens to get this

0 commit comments

Comments
 (0)