Skip to content

Commit a41be1a

Browse files
EggBaconAndSpammergify[bot]
authored andcommitted
Fix parser swallowing whitespace in Src annotations (#1563)
* Fix swallowed whitespace in Src annotations The parser annotated application expressions incorrectly: whitespace between function and argument was swallowed when constructing the Src annotation. This bug resulted in strange behaviour in the hovering functionality in dhall-lsp-server. As an example, previously the Dhall expression `0 0` would be parsed as ``` Note (Src {..., srcText = "00"}) (App (Note (Src {..., srcText = "0"}) (NaturalLit 0)) (Note (Src {..., srcText = "0"}) (NaturalLit 0))) ``` while we now get ``` Note (Src {..., srcText = "0 0"}) (App (Note (Src {..., srcText = "0"}) (NaturalLit 0)) (Note (Src {..., srcText = "0"}) (NaturalLit 0))) ``` as expected. * Use foldl' over foldl As suggested by @sjakobi. Note that there are quite a few more occurences of foldl where we don't actually need laziness, but I leave that for someone else to fix :) * Revert "Use foldl' over foldl" This reverts commit 1bd9a54.
1 parent 5f15c86 commit a41be1a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

dhall/src/Dhall/Parser/Expression.hs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,18 @@ parsers embedded = Parsers {..}
323323
f <- (Some <$ _Some <* nonemptyWhitespace)
324324
<|> return id
325325
a <- noted importExpression_
326-
b <- Text.Megaparsec.many (try (nonemptyWhitespace *> noted importExpression_))
327-
return (foldl app (f a) b)
326+
bs <- Text.Megaparsec.many . try $ do
327+
(sep, _) <- Text.Megaparsec.match nonemptyWhitespace
328+
b <- importExpression_
329+
return (sep, b)
330+
return (foldl app (f a) bs)
328331
where
329-
app nL@(Note (Src before _ bytesL) _) nR@(Note (Src _ after bytesR) _) =
330-
Note (Src before after (bytesL <> bytesR)) (App nL nR)
331-
app nL nR =
332-
App nL nR
332+
app a (sep, b)
333+
| Note (Src left _ bytesL) _ <- a
334+
, Note (Src _ right bytesR) _ <- b
335+
= Note (Src left right (bytesL <> sep <> bytesR)) (App a b)
336+
app a (_, b) =
337+
App a b
333338

334339
importExpression_ = noted (choice [ alternative0, alternative1 ])
335340
where

0 commit comments

Comments
 (0)