Skip to content

Commit 2294163

Browse files
committed
Disallow newline between contextual keyword pairs in parentheses (#386)
Ensure that we never treat things like `"mutable\nstruct"` as a mutable struct definition, even within parentheses where newline whitespace is insignificant.
1 parent 4682487 commit 2294163

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

src/parser.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function peek_initial_reserved_words(ps::ParseState)
253253
if is_initial_reserved_word(ps, k)
254254
return true
255255
elseif is_contextual_keyword(k)
256-
k2 = peek(ps,2)
256+
k2 = peek(ps, 2, skip_newlines=false)
257257
return (k == K"mutable" && k2 == K"struct") ||
258258
(k == K"primitive" && k2 == K"type") ||
259259
(k == K"abstract" && k2 == K"type")

test/parser.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,9 @@ end
956956
parsestmt_test_specs = [
957957
# whitespace before keywords in space-insensitive mode
958958
"(y::\nif x z end)" => "(parens (::-i y (if x (block z))))"
959+
# Contextual keyword pairs inside parentheses
960+
"(abstract type X end)" => "(parens (abstract X))"
961+
"(mutable struct X end)" => "(parens (struct-mut X (block)))"
959962
# parsing of tricky primes
960963
"x in'c'" => "(call-i x in (char 'c'))"
961964
"1where'c'" => "(where 1 (char 'c'))"
@@ -971,6 +974,9 @@ parsestmt_test_specs = [
971974
"|(&\nfunction" => "(call | (& (function (error (error)) (block (error)) (error-t))) (error-t))"
972975
"@(" => "(macrocall (parens (error-t)))"
973976
"x = @(" => "(= x (macrocall (parens (error-t))))"
977+
# Contextual keyword pairs must not be separated by newlines even within parens
978+
"(abstract\ntype X end)" => "(wrapper (parens abstract (error-t type X)) (error-t end ✘))"
979+
"(mutable\nstruct X end)" => "(wrapper (parens mutable (error-t struct X)) (error-t end ✘))"
974980

975981
# The following is currently broken but at least the parser shouldn't
976982
# crash.

0 commit comments

Comments
 (0)