Skip to content

Commit 0df34c9

Browse files
authored
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 1351d70 commit 0df34c9

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

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
@@ -986,6 +986,9 @@ end
986986
parsestmt_test_specs = [
987987
# whitespace before keywords in space-insensitive mode
988988
"(y::\nif x z end)" => "(parens (::-i y (if x (block z))))"
989+
# Contextual keyword pairs inside parentheses
990+
"(abstract type X end)" => "(parens (abstract X))"
991+
"(mutable struct X end)" => "(parens (struct-mut X (block)))"
989992
# parsing of tricky primes
990993
"x in'c'" => "(call-i x in (char 'c'))"
991994
"1where'c'" => "(where 1 (char 'c'))"
@@ -1001,6 +1004,9 @@ parsestmt_test_specs = [
10011004
"|(&\nfunction" => "(call | (& (function (error (error)) (block (error)) (error-t))) (error-t))"
10021005
"@(" => "(macrocall (parens (error-t)))"
10031006
"x = @(" => "(= x (macrocall (parens (error-t))))"
1007+
# Contextual keyword pairs must not be separated by newlines even within parens
1008+
"(abstract\ntype X end)" => "(wrapper (parens abstract (error-t type X)) (error-t end ✘))"
1009+
"(mutable\nstruct X end)" => "(wrapper (parens mutable (error-t struct X)) (error-t end ✘))"
10041010

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

0 commit comments

Comments
 (0)