Skip to content

Commit

Permalink
Improve localization of parsing errors
Browse files Browse the repository at this point in the history
The `exprD` parser for function application was unnecessarily backtracking when
parsing the first value in the chain.  Removing the `try` for the first element
greatly improves parsing error localization.

For example, before this change the following Dhall expression:

```
{ foo = 1, bar = '2' }
```

... would give the following parse error:

```
(stdin):1:1: error: expected: "[",
    "\8704", "\955", "\\", "forall",
    "if", "let", "merge"
{ foo = 1, bar = '2' }
^
```

After this change, you now get a much better error:

```
$ dist/build/Dhall/dhall
{ foo = 1, bar = '2' }
(stdin):1:18: error: expected: "''",
    "(", "+", "-", "[", "\8704",
    "\955", "\\", "forall", "if",
    "let", "merge", built-in value,
    double, import, integer, label,
    list literal, natural,
    record literal, record type,
    string, union literal,
    union type
{ foo = 1, bar = '2' }
                 ^
```
  • Loading branch information
Gabriella439 committed Mar 3, 2017
1 parent 505a786 commit baa08c6
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Dhall/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,13 @@ exprC embedded = exprC0
-- arguments
exprD :: Show a => Parser a -> Parser (Expr Src a)
exprD embedded = do
es <- some (noted (try (exprE embedded)))
e <- noted (exprE embedded)
es <- many (noted (try (exprE embedded)))
let app nL@(Note (Src before _ bytesL) _) nR@(Note (Src _ after bytesR) _) =
Note (Src before after (bytesL <> bytesR)) (App nL nR)
app _ _ = Dhall.Core.internalError
("Dhall.Parser.exprD: foldl1 app (" <> Data.Text.pack (show es) <> ")")
return (Data.List.foldl1 app es)
return (Data.List.foldl1 app (e:es))

exprE :: Show a => Parser a -> Parser (Expr Src a)
exprE embedded = noted (do
Expand Down

0 comments on commit baa08c6

Please sign in to comment.