Skip to content

Commit 7d01d46

Browse files
Gabriella439mergify[bot]
authored andcommitted
Fix parsing of variables with keyword prefixes (#1584)
* Fix parsing of variables with keyword prefixes Fixes #1583 Note that one prefix is not yet supported: `missing`, since fixing that is not easy for the current parsing implementation (as far as I can tell). * Add regression test ... as suggested by @sjakobi I'm not upstreaming this into the standard test suite yet because the current parser cannot handle `missing`, which would be necessary to generate the corresponding CBOR
1 parent de51daf commit 7d01d46

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

dhall/src/Dhall/Parser/Expression.hs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,19 @@ parsers embedded = Parsers {..}
162162
return (Lam a b c)
163163

164164
alternative1 = do
165-
_if
166-
nonemptyWhitespace
165+
try (_if *> nonemptyWhitespace)
167166
a <- expression
168167
whitespace
169-
_then
170-
nonemptyWhitespace
168+
try (_then *> nonemptyWhitespace)
171169
b <- expression
172170
whitespace
173-
_else
174-
nonemptyWhitespace
171+
try (_else *> nonemptyWhitespace)
175172
c <- expression
176173
return (BoolIf a b c)
177174

178175
alternative2 = do
179176
let binding = do
180-
_let
181-
182-
src0 <- src nonemptyWhitespace
177+
src0 <- try (_let *> src nonemptyWhitespace)
183178

184179
c <- label
185180

@@ -208,9 +203,7 @@ parsers embedded = Parsers {..}
208203

209204
as <- Data.List.NonEmpty.some1 binding
210205

211-
_in
212-
213-
nonemptyWhitespace
206+
try (_in *> nonemptyWhitespace)
214207

215208
b <- expression
216209

@@ -234,9 +227,7 @@ parsers embedded = Parsers {..}
234227
return (Dhall.Syntax.wrapInLets as b)
235228

236229
alternative3 = do
237-
_forall
238-
whitespace
239-
_openParens
230+
try (_forall *> whitespace *> _openParens)
240231
whitespace
241232
a <- label
242233
whitespace
@@ -252,9 +243,7 @@ parsers embedded = Parsers {..}
252243
return (Pi a b c)
253244

254245
alternative4 = do
255-
_assert
256-
whitespace
257-
_colon
246+
try (_assert *> whitespace *> _colon)
258247
nonemptyWhitespace
259248
a <- expression
260249
return (Assert a)
@@ -321,7 +310,7 @@ parsers embedded = Parsers {..}
321310
]
322311

323312
applicationExpression = do
324-
f <- (Some <$ _Some <* nonemptyWhitespace)
313+
f <- (Some <$ try (_Some <* nonemptyWhitespace))
325314
<|> return id
326315
a <- noted importExpression_
327316
bs <- Text.Megaparsec.many . try $ do
@@ -433,16 +422,14 @@ parsers embedded = Parsers {..}
433422
alternative06 = listLiteral
434423

435424
alternative07 = do
436-
_merge
437-
nonemptyWhitespace
425+
try (_merge *> nonemptyWhitespace)
438426
a <- importExpression_
439427
nonemptyWhitespace
440428
b <- importExpression_ <?> "second argument to ❰merge❱"
441429
return (Merge a b Nothing)
442430

443431
alternative08 = do
444-
_toMap
445-
nonemptyWhitespace
432+
try (_toMap *> nonemptyWhitespace)
446433
a <- importExpression_
447434
return (ToMap a Nothing)
448435

dhall/tests/Dhall/Test/Regression.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ tests =
4242
, issue1131a
4343
, issue1131b
4444
, issue1341
45+
, issue1584
4546
, parsing0
4647
, typeChecking0
4748
, typeChecking1
@@ -180,6 +181,13 @@ issue1341 = Test.Tasty.HUnit.testCase "Issue #1341" (do
180181
let msg = "NaN shouldn't contain any free variables"
181182
Test.Tasty.HUnit.assertEqual msg False actual)
182183

184+
issue1584 :: TestTree
185+
issue1584 = Test.Tasty.HUnit.testCase "Issue #1584" (do
186+
-- This test ensures that we can parse variables with keyword prefixes
187+
-- (e.g. `ifX`)
188+
_ <- Util.code "./tests/regression/issue1584.dhall"
189+
return () )
190+
183191
parsing0 :: TestTree
184192
parsing0 = Test.Tasty.HUnit.testCase "Parsing regression #0" (do
185193
-- Verify that parsing should not fail
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[ let ifX = 1 in ifX
2+
, let thenX = 1 in thenX
3+
, let elseX = 1 in elseX
4+
, let letX = 1 in letX
5+
, let inX = 1 in inX
6+
, let usingX = 1 in usingX
7+
{- Fixing this case appears to be difficult for the current parser
8+
implementation
9+
-}
10+
-- , let missingX = 1 in missingX
11+
, let asX = 1 in asX
12+
, let InfinityX = 1 in InfinityX
13+
, let NaNX = 1 in NaNX
14+
, let mergeX = 1 in mergeX
15+
, let SomeX = 1 in SomeX
16+
, let toMapX = 1 in toMapX
17+
, let assertX = 1 in assertX
18+
, let forallX = 1 in forallX
19+
]

0 commit comments

Comments
 (0)