Skip to content
Merged
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
59 changes: 58 additions & 1 deletion src/Elm/Parser/Expression.elm
Original file line number Diff line number Diff line change
Expand Up @@ -944,12 +944,35 @@ negationOperation =
"," ->
negationAfterMinus

"[" ->
negationAfterMinus

"=" ->
negationAfterMinus

-- from the end of -> in case branches
">" ->
Comment thread
jfmengels marked this conversation as resolved.
negationAfterMinus

-- TODO only for tests
"" ->
negationAfterMinus

_ ->
negationWhitespaceProblem
-- Check if preceded by a keyword that introduces an expression.
-- We check for the keyword AND a space/boundary before it
-- to avoid matching identifiers that end with the keyword
-- (e.g., "within" should not match "in").
if
endsWithKeyword "then" offset source
|| endsWithKeyword "else" offset source
|| endsWithKeyword "of" offset source
|| endsWithKeyword "in" offset source
then
negationAfterMinus

else
negationWhitespaceProblem
)
)

Expand All @@ -959,6 +982,40 @@ negationWhitespaceProblem =
ParserFast.problem "if a negation sign is not preceded by whitespace, it's considered subtraction"


{-| Check if the source immediately before the minus sign at `offset - 1`
ends with the given keyword, and that keyword is preceded by a word boundary
(space, newline, or start of input) so we don't match identifiers like
"within" for keyword "in".
-}
endsWithKeyword : String -> Int -> String -> Bool
endsWithKeyword keyword offset source =
let
keywordLen =
String.length keyword

keywordStart =
offset - 1 - keywordLen
in
(String.slice keywordStart (offset - 1) source == keyword)
&& (keywordStart <= 0 || isWordBoundary (String.slice (keywordStart - 1) keywordStart source))


isWordBoundary : String -> Bool
isWordBoundary char =
case char of
" " ->
True

"\n" ->
True

"\u{000D}" ->
True

_ ->
False


negationAfterMinus : Parser (WithComments (Node Expression))
negationAfterMinus =
ParserFast.map
Expand Down
72 changes: 72 additions & 0 deletions tests/Elm/Parser/ExpressionTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,78 @@ all =
(Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (Integer 1))
)
)
, test "negated expression in list literal without spaces" <|
\() ->
"[-3, 5, -1]"
|> expectAst
(Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } }
(ListExpr
[ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 4 } }
(Negation (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (Integer 3)))
, Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } }
(Integer 5)
, Node { start = { row = 1, column = 9 }, end = { row = 1, column = 11 } }
(Negation (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 11 } } (Integer 1)))
]
)
)
, test "negated expression in record literal without spaces" <|
\() ->
"{a=-1}"
|> expectAst
(Node { start = { row = 1, column = 1 }, end = { row = 1, column = 7 } }
(RecordExpr
[ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 6 } }
( Node { start = { row = 1, column = 2 }, end = { row = 1, column = 3 } } "a"
, Node { start = { row = 1, column = 4 }, end = { row = 1, column = 6 } }
(Negation (Node { start = { row = 1, column = 5 }, end = { row = 1, column = 6 } } (Integer 1)))
)
]
)
)
, test "negated expression after arrow without space" <|
\() ->
"case x of\n _->-1"
|> expectAst
(Node { start = { row = 1, column = 1 }, end = { row = 2, column = 10 } }
(CaseExpression
{ expression =
Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } (FunctionOrValue [] "x")
, cases =
[ ( Node { start = { row = 2, column = 5 }, end = { row = 2, column = 6 } } AllPattern
, Node { start = { row = 2, column = 8 }, end = { row = 2, column = 10 } }
(Negation (Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } (Integer 1)))
)
]
}
)
)
, test "negated expression after then without space" <|
\() ->
"if True then-1 else 0"
|> expectAst
(Node { start = { row = 1, column = 1 }, end = { row = 1, column = 22 } }
(IfBlock
(Node { start = { row = 1, column = 4 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "True"))
(Node { start = { row = 1, column = 13 }, end = { row = 1, column = 15 } }
(Negation (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } (Integer 1)))
)
(Node { start = { row = 1, column = 21 }, end = { row = 1, column = 22 } } (Integer 0))
)
)
, test "negated expression after else without space" <|
\() ->
"if True then 0 else-1"
|> expectAst
(Node { start = { row = 1, column = 1 }, end = { row = 1, column = 22 } }
(IfBlock
(Node { start = { row = 1, column = 4 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "True"))
(Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } (Integer 0))
(Node { start = { row = 1, column = 20 }, end = { row = 1, column = 22 } }
(Negation (Node { start = { row = 1, column = 21 }, end = { row = 1, column = 22 } } (Integer 1)))
)
)
)
, test "negated expression for value" <|
\() ->
"-x"
Expand Down