Skip to content

Commit 48c0170

Browse files
authored
Report input name in parsing errors (#2195)
Fixes #2194
1 parent c445742 commit 48c0170

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

dhall/src/Dhall/Format.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,18 @@ format (Format { inputs = inputs0, transitivity = transitivity0, ..}) =
7272
<> Dhall.Pretty.prettyCharacterSet characterSet expr
7373
<> "\n")
7474

75-
(originalText, transitivity) <- case input of
75+
(inputName, originalText, transitivity) <- case input of
7676
InputFile file -> do
7777
text <- Data.Text.IO.readFile file
7878

79-
return (text, transitivity0)
79+
return (file, text, transitivity0)
8080
StandardInput -> do
8181
text <- Data.Text.IO.getContents
8282

83-
return (text, NonTransitive)
83+
return ("(input)", text, NonTransitive)
8484

8585

86-
headerAndExpr@(_, parsedExpression) <- Dhall.Util.getExpressionAndHeaderFromStdinText censor originalText
86+
headerAndExpr@(_, parsedExpression) <- Dhall.Util.getExpressionAndHeaderFromStdinText censor inputName originalText
8787

8888
case transitivity of
8989
Transitive ->

dhall/src/Dhall/Freeze.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,18 @@ freezeWithManager newManager outputMode transitivity0 inputs scope intent chosen
174174

175175
let status = Dhall.Import.emptyStatusWithManager newManager directory
176176

177-
(originalText, transitivity) <- case input of
177+
(inputName, originalText, transitivity) <- case input of
178178
InputFile file -> do
179179
text <- Text.IO.readFile file
180180

181-
return (text, transitivity0)
181+
return (file, text, transitivity0)
182182

183183
StandardInput -> do
184184
text <- Text.IO.getContents
185185

186-
return (text, NonTransitive)
186+
return ("(input)", text, NonTransitive)
187187

188-
(Header header, parsedExpression) <- Util.getExpressionAndHeaderFromStdinText censor originalText
188+
(Header header, parsedExpression) <- Util.getExpressionAndHeaderFromStdinText censor inputName originalText
189189

190190
let characterSet = fromMaybe (detectCharacterSet parsedExpression) chosenCharacterSet
191191

dhall/src/Dhall/Main.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,18 +848,18 @@ command (Options {..}) = do
848848

849849
let status = Dhall.Import.emptyStatus directory
850850

851-
(originalText, transitivity) <- case input of
851+
(inputName, originalText, transitivity) <- case input of
852852
InputFile file -> do
853853
text <- Data.Text.IO.readFile file
854854

855-
return (text, transitivity0)
855+
return (file, text, transitivity0)
856856
StandardInput -> do
857857
text <- Data.Text.IO.getContents
858858

859-
return (text, NonTransitive)
859+
return ("(input)", text, NonTransitive)
860860

861861
(Header header, parsedExpression) <-
862-
Dhall.Util.getExpressionAndHeaderFromStdinText censor originalText
862+
Dhall.Util.getExpressionAndHeaderFromStdinText censor inputName originalText
863863

864864
let characterSet = fromMaybe (detectCharacterSet parsedExpression) chosenCharacterSet
865865

dhall/src/Dhall/Schemas.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ data Schemas = Schemas
6868
-- | Implementation of the @dhall rewrite-with-schemas@ subcommand
6969
schemasCommand :: Schemas -> IO ()
7070
schemasCommand Schemas{..} = do
71-
originalText <- case input of
72-
InputFile file -> Text.IO.readFile file
73-
StandardInput -> Text.IO.getContents
71+
(inputName, originalText) <- case input of
72+
InputFile file -> (,) file <$> Text.IO.readFile file
73+
StandardInput -> (,) "(input)" <$> Text.IO.getContents
7474

75-
(Header header, expression) <- Util.getExpressionAndHeaderFromStdinText censor originalText
75+
(Header header, expression) <- Util.getExpressionAndHeaderFromStdinText censor inputName originalText
7676

7777
let characterSet = fromMaybe (detectCharacterSet expression) chosenCharacterSet
7878

dhall/src/Dhall/Util.hs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ get parser censor input = do
118118
case input of
119119
Input_ (InputFile file) -> Data.Text.IO.readFile file
120120
Input_ StandardInput -> Data.Text.IO.getContents
121-
StdinText text -> pure text
121+
StdinText _ text -> pure text
122122

123123
let name =
124124
case input of
125125
Input_ (InputFile file) -> file
126126
Input_ StandardInput -> "(input)"
127-
StdinText _ -> "(input)"
127+
StdinText inputName _ -> inputName
128128

129129
let result = parser name inText
130130

@@ -149,7 +149,11 @@ data Censor = NoCensor | Censor
149149
data Input = StandardInput | InputFile FilePath deriving (Eq)
150150

151151
-- | Path to input or raw input text, necessary since we can't read STDIN twice
152-
data InputOrTextFromStdin = Input_ Input | StdinText Text
152+
data InputOrTextFromStdin
153+
= Input_ Input
154+
| StdinText String Text
155+
-- ^ @StdinText name text@ where name is a user-friendly name describing the
156+
-- input expression, used in parsing error messages
153157

154158
{-| Specifies whether or not an input's transitive dependencies should also be
155159
processed. Transitive dependencies are restricted to relative file imports.
@@ -234,6 +238,6 @@ getExpressionAndHeader censor =
234238
-- | Convenient utility for retrieving an expression along with its header from
235239
-- | text already read from STDIN (so it's not re-read)
236240
getExpressionAndHeaderFromStdinText
237-
:: Censor -> Text -> IO (Header, Expr Src Import)
238-
getExpressionAndHeaderFromStdinText censor =
239-
get Dhall.Parser.exprAndHeaderFromText censor . StdinText
241+
:: Censor -> String -> Text -> IO (Header, Expr Src Import)
242+
getExpressionAndHeaderFromStdinText censor inputName =
243+
get Dhall.Parser.exprAndHeaderFromText censor . StdinText inputName

0 commit comments

Comments
 (0)