Skip to content

Commit 7eec31d

Browse files
sjakobiGabriella439
authored andcommitted
Parse whitespace more precisely (#1483)
This is preparatory work for #1454. This also fixes some cases where dhall would previously accept malformatted inputs. The changes to dhall-lsp-server are mostly untested. See #1510. Co-authored-by: Gabriel Gonzalez <Gabriel439@gmail.com>
1 parent cc1814b commit 7eec31d

File tree

4 files changed

+165
-146
lines changed

4 files changed

+165
-146
lines changed

dhall-lsp-server/src/Dhall/LSP/Backend/Parsing.hs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ getLetIdentifier src@(Src left _ text) =
7373
where parseLetIdentifier = do
7474
setSourcePos left
7575
_let
76+
whitespace
7677
begin <- getSourcePos
7778
(tokens, _) <- Megaparsec.match label
7879
end <- getSourcePos
@@ -86,7 +87,9 @@ getLamIdentifier (Src left _ text) =
8687
where parseLetIdentifier = do
8788
setSourcePos left
8889
_lambda
90+
whitespace
8991
_openParens
92+
whitespace
9093
begin <- getSourcePos
9194
(tokens, _) <- Megaparsec.match label
9295
end <- getSourcePos
@@ -100,7 +103,9 @@ getForallIdentifier (Src left _ text) =
100103
where parseLetIdentifier = do
101104
setSourcePos left
102105
_forall
106+
whitespace
103107
_openParens
108+
whitespace
104109
begin <- getSourcePos
105110
(tokens, _) <- Megaparsec.match label
106111
end <- getSourcePos
@@ -116,6 +121,7 @@ getImportHash (Src left _ text) =
116121
where parseImportHashPosition = do
117122
setSourcePos left
118123
_ <- importType_
124+
whitespace
119125
begin <- getSourcePos
120126
(tokens, _) <- Megaparsec.match $ optional importHash_
121127
end <- getSourcePos
@@ -166,37 +172,57 @@ binderExprFromText txt =
166172

167173
closedLet = do
168174
_let
175+
nonemptyWhitespace
169176
_ <- label
177+
whitespace
170178
_ <- optional (do
171179
_colon
180+
nonemptyWhitespace
172181
expr)
173182
_equal
183+
whitespace
174184
_ <- expr
185+
whitespace
175186
(do
176187
_in
188+
nonemptyWhitespace
177189
_ <- expr
178190
return ())
179191
<|> closedLet
180192

181193
closedLambda = do
182194
_lambda
195+
whitespace
183196
_openParens
197+
whitespace
184198
_ <- label
199+
whitespace
185200
_colon
201+
nonemptyWhitespace
186202
_ <- expr
203+
whitespace
187204
_closeParens
205+
whitespace
188206
_arrow
207+
whitespace
189208
_ <- expr
190209
return ()
191210

192211
closedPi = do
193212
_forall
213+
whitespace
194214
_openParens
215+
whitespace
195216
_ <- label
217+
whitespace
196218
_colon
219+
nonemptyWhitespace
197220
_ <- expr
221+
whitespace
198222
_closeParens
223+
whitespace
199224
_arrow
225+
whitespace
200226
_ <- expr
201227
return ()
202228

@@ -219,33 +245,45 @@ binderExprFromText txt =
219245

220246
letBinder = do
221247
_let
248+
nonemptyWhitespace
222249
name <- label
223-
mType <- optional (do _colon; _type <- expr; return (Nothing, _type))
250+
whitespace
251+
mType <- optional (do _colon; nonemptyWhitespace; _type <- expr; whitespace; return (Nothing, _type))
224252

225253
-- if the bound value does not parse, skip and replace with 'hole'
226-
value <- try (do _equal; expr)
254+
value <- try (do _equal; whitespace; expr <* whitespace)
227255
<|> (do skipManyTill anySingle (lookAhead boundary <|> _in); return holeExpr)
228256
inner <- parseBinderExpr
229257
return (Let (Binding Nothing name Nothing mType Nothing value) inner)
230258

231259
forallBinder = do
232260
_forall
261+
whitespace
233262
_openParens
263+
whitespace
234264
name <- label
265+
whitespace
235266
_colon
267+
nonemptyWhitespace
236268
-- if the bound type does not parse, skip and replace with 'hole'
237-
typ <- try (do e <- expr; _closeParens; _arrow; return e)
269+
typ <- try (do e <- expr; whitespace; _closeParens; whitespace; _arrow; return e)
238270
<|> (do skipManyTill anySingle _arrow; return holeExpr)
271+
whitespace
239272
inner <- parseBinderExpr
240273
return (Pi name typ inner)
241274

242275
lambdaBinder = do
243276
_lambda
277+
whitespace
244278
_openParens
279+
whitespace
245280
name <- label
281+
whitespace
246282
_colon
283+
nonemptyWhitespace
247284
-- if the bound type does not parse, skip and replace with 'hole'
248-
typ <- try (do e <- expr; _closeParens; _arrow; return e)
285+
typ <- try (do e <- expr; whitespace; _closeParens; whitespace; _arrow; return e)
249286
<|> (do skipManyTill anySingle _arrow; return holeExpr)
287+
whitespace
250288
inner <- parseBinderExpr
251289
return (Lam name typ inner)

0 commit comments

Comments
 (0)