@@ -20,12 +20,14 @@ module Dhall.TH
20
20
, defaultGenerateOptions
21
21
) where
22
22
23
+ import Control.Monad (forM_ )
23
24
import Data.Bifunctor (first )
24
25
import Data.Text (Text )
25
26
import Dhall (FromDhall , ToDhall )
26
27
import Dhall.Syntax (Expr (.. ), FunctionBinding (.. ), Var (.. ))
27
28
import GHC.Generics (Generic )
28
29
import Language.Haskell.TH.Quote (QuasiQuoter (.. ), dataToExpQ )
30
+ import Lens.Family (view )
29
31
import Prettyprinter (Pretty )
30
32
31
33
import Language.Haskell.TH.Syntax
@@ -52,11 +54,12 @@ import qualified Data.Time as Time
52
54
import qualified Data.Typeable as Typeable
53
55
import qualified Dhall
54
56
import qualified Dhall.Core as Core
57
+ import qualified Dhall.Import
55
58
import qualified Dhall.Map
56
59
import qualified Dhall.Pretty
57
60
import qualified Dhall.Util
58
61
import qualified GHC.IO.Encoding
59
- import qualified Language.Haskell.TH.Syntax as Syntax
62
+ import qualified Language.Haskell.TH.Syntax as TH
60
63
import qualified Numeric.Natural
61
64
import qualified Prettyprinter.Render.String as Pretty
62
65
import qualified System.IO
@@ -88,15 +91,35 @@ import qualified System.IO
88
91
-}
89
92
staticDhallExpression :: Text -> Q Exp
90
93
staticDhallExpression text = do
91
- Syntax . runIO (GHC.IO.Encoding. setLocaleEncoding System.IO. utf8)
94
+ TH . runIO (GHC.IO.Encoding. setLocaleEncoding System.IO. utf8)
92
95
93
- expression <- Syntax. runIO (Dhall. inputExpr text)
96
+ (expression, status) <- TH. runIO $ do
97
+ parsed <- Dhall. parseWithSettings Dhall. defaultInputSettings text
98
+
99
+ (resolved, status) <- Dhall. resolveAndStatusWithSettings Dhall. defaultInputSettings parsed
100
+
101
+ _ <- Dhall. typecheckWithSettings Dhall. defaultInputSettings resolved
102
+
103
+ let normalized = Dhall. normalizeWithSettings Dhall. defaultInputSettings resolved
104
+
105
+ pure (normalized, status)
106
+
107
+ forM_ (Dhall.Map. keys (view Dhall.Import. cache status)) $ \ chained ->
108
+ case Dhall.Import. chainedImport chained of
109
+ Core. Import
110
+ { importHashed = Core. ImportHashed
111
+ { importType = Core. Local prefix file
112
+ }
113
+ } -> do
114
+ fp <- Dhall.Import. localToPath prefix file
115
+ TH. addDependentFile fp
116
+ _ -> return ()
94
117
95
118
dataToExpQ (fmap liftText . Typeable. cast) expression
96
119
where
97
120
-- A workaround for a problem in TemplateHaskell (see
98
121
-- https://stackoverflow.com/questions/38143464/cant-find-inerface-file-declaration-for-variable)
99
- liftText = fmap (AppE (VarE 'Text. pack)) . Syntax . lift . Text. unpack
122
+ liftText = fmap (AppE (VarE 'Text. pack)) . TH . lift . Text. unpack
100
123
101
124
{-| A quasi-quoter for Dhall expressions.
102
125
@@ -207,14 +230,14 @@ toNestedHaskellType typeParams haskellTypes = loop
207
230
208
231
Var v
209
232
| Just (V param index) <- List. find (v == ) typeParams -> do
210
- let name = Syntax . mkName $ (Text. unpack param) ++ (show index)
233
+ let name = TH . mkName $ (Text. unpack param) ++ (show index)
211
234
212
235
return (VarT name)
213
236
214
237
| otherwise -> fail $ message v
215
238
216
239
_ | Just haskellType <- List. find (predicate dhallType) haskellTypes -> do
217
- let name = Syntax . mkName (Text. unpack (typeName haskellType))
240
+ let name = TH . mkName (Text. unpack (typeName haskellType))
218
241
219
242
return (ConT name)
220
243
| otherwise -> fail $ message dhallType
@@ -225,7 +248,7 @@ derivingGenericClause = DerivClause (Just StockStrategy) [ ConT ''Generic ]
225
248
226
249
-- | Generates a `FromDhall` instances.
227
250
fromDhallInstance
228
- :: Syntax . Name -- ^ The name of the type the instances is for
251
+ :: TH . Name -- ^ The name of the type the instances is for
229
252
-> Q Exp -- ^ A TH splice generating some `Dhall.InterpretOptions`
230
253
-> Q [Dec ]
231
254
fromDhallInstance n interpretOptions = [d |
@@ -235,7 +258,7 @@ fromDhallInstance n interpretOptions = [d|
235
258
236
259
-- | Generates a `ToDhall` instances.
237
260
toDhallInstance
238
- :: Syntax . Name -- ^ The name of the type the instances is for
261
+ :: TH . Name -- ^ The name of the type the instances is for
239
262
-> Q Exp -- ^ A TH splice generating some `Dhall.InterpretOptions`
240
263
-> Q [Dec ]
241
264
toDhallInstance n interpretOptions = [d |
@@ -265,15 +288,15 @@ toDeclaration generateOptions@GenerateOptions{..} haskellTypes typ =
265
288
interpretOptions = generateToInterpretOptions generateOptions typ
266
289
267
290
#if MIN_VERSION_template_haskell(2,21,0)
268
- toTypeVar (V n i) = Syntax . PlainTV (Syntax . mkName (Text. unpack n ++ show i)) Syntax . BndrReq
291
+ toTypeVar (V n i) = TH . PlainTV (TH . mkName (Text. unpack n ++ show i)) TH . BndrReq
269
292
#elif MIN_VERSION_template_haskell(2,17,0)
270
- toTypeVar (V n i) = Syntax . PlainTV (Syntax . mkName (Text. unpack n ++ show i)) ()
293
+ toTypeVar (V n i) = TH . PlainTV (TH . mkName (Text. unpack n ++ show i)) ()
271
294
#else
272
- toTypeVar (V n i) = Syntax . PlainTV (Syntax . mkName (Text. unpack n ++ show i))
295
+ toTypeVar (V n i) = TH . PlainTV (TH . mkName (Text. unpack n ++ show i))
273
296
#endif
274
297
275
298
toDataD typeName typeParams constructors = do
276
- let name = Syntax . mkName (Text. unpack typeName)
299
+ let name = TH . mkName (Text. unpack typeName)
277
300
278
301
let params = fmap toTypeVar typeParams
279
302
@@ -355,7 +378,7 @@ toConstructor
355
378
-- ^ @(constructorName, fieldType)@
356
379
-> Q Con
357
380
toConstructor typeParams GenerateOptions {.. } haskellTypes outerTypeName (constructorName, maybeAlternativeType) = do
358
- let name = Syntax . mkName (Text. unpack $ constructorModifier constructorName)
381
+ let name = TH . mkName (Text. unpack $ constructorModifier constructorName)
359
382
360
383
let strictness = if makeStrict then SourceStrict else NoSourceStrictness
361
384
@@ -368,15 +391,15 @@ toConstructor typeParams GenerateOptions{..} haskellTypes outerTypeName (constru
368
391
&& typeName haskellType /= outerTypeName
369
392
, Just haskellType <- List. find predicate haskellTypes -> do
370
393
let innerName =
371
- Syntax . mkName (Text. unpack (typeName haskellType))
394
+ TH . mkName (Text. unpack (typeName haskellType))
372
395
373
396
return (NormalC name [ (bang, ConT innerName) ])
374
397
375
398
Just (Record kts) -> do
376
399
let process (key, dhallFieldType) = do
377
400
haskellFieldType <- toNestedHaskellType typeParams haskellTypes dhallFieldType
378
401
379
- return (Syntax . mkName (Text. unpack $ fieldModifier key), bang, haskellFieldType)
402
+ return (TH . mkName (Text. unpack $ fieldModifier key), bang, haskellFieldType)
380
403
381
404
varBangTypes <- traverse process (Dhall.Map. toList $ Core. recordFieldValue <$> kts)
382
405
@@ -508,16 +531,16 @@ generateToInterpretOptions GenerateOptions{..} haskellType = [| Dhall.InterpretO
508
531
mkMatch n = Match (textToPat $ f n) (NormalB $ textToExp n) []
509
532
510
533
nameE :: Exp
511
- nameE = Syntax . VarE $ Syntax . mkName " n"
534
+ nameE = TH . VarE $ TH . mkName " n"
512
535
513
536
nameP :: Pat
514
- nameP = Syntax . VarP $ Syntax . mkName " n"
537
+ nameP = TH . VarP $ TH . mkName " n"
515
538
516
539
textToExp :: Text -> Exp
517
- textToExp = Syntax . LitE . Syntax . StringL . Text. unpack
540
+ textToExp = TH . LitE . TH . StringL . Text. unpack
518
541
519
542
textToPat :: Text -> Pat
520
- textToPat = Syntax . LitP . Syntax . StringL . Text. unpack
543
+ textToPat = TH . LitP . TH . StringL . Text. unpack
521
544
522
545
-- | Generate a Haskell datatype declaration with one constructor from a Dhall
523
546
-- type.
@@ -605,8 +628,8 @@ makeHaskellTypes = makeHaskellTypesWith defaultGenerateOptions
605
628
-- > makeHaskellTypes = makeHaskellTypesWith defaultGenerateOptions
606
629
makeHaskellTypesWith :: GenerateOptions -> [HaskellType Text ] -> Q [Dec ]
607
630
makeHaskellTypesWith generateOptions haskellTypes = do
608
- Syntax . runIO (GHC.IO.Encoding. setLocaleEncoding System.IO. utf8)
631
+ TH . runIO (GHC.IO.Encoding. setLocaleEncoding System.IO. utf8)
609
632
610
- haskellTypes' <- traverse (traverse (Syntax . runIO . Dhall. inputExpr)) haskellTypes
633
+ haskellTypes' <- traverse (traverse (TH . runIO . Dhall. inputExpr)) haskellTypes
611
634
612
635
concat <$> traverse (toDeclaration generateOptions haskellTypes') haskellTypes'
0 commit comments