@@ -28,28 +28,30 @@ import Data.Bifunctor (first)
28
28
import Data.List.NonEmpty (NonEmpty ((:|) ))
29
29
import Data.Text (Text )
30
30
import Data.Void (Void )
31
+ import Dhall (EvaluateSettings )
31
32
import Network.URI (URI )
32
33
import System.FilePath
33
34
( splitDirectories
34
35
, takeDirectory
35
36
, takeFileName
36
37
)
37
38
38
- import qualified Data.Graph as Graph
39
- import qualified Data.Map.Strict as Map
40
- import qualified Data.Set as Set
41
- import qualified Data.Text as Text
42
- import qualified Dhall.Core as Dhall
43
- import qualified Dhall.Import as Dhall
39
+ import qualified Data.Graph as Graph
40
+ import qualified Data.Map.Strict as Map
41
+ import qualified Data.Set as Set
42
+ import qualified Data.Text as Text
43
+ import qualified Dhall
44
+ import qualified Dhall.Core as Dhall
45
+ import qualified Dhall.Import as Import
44
46
import qualified Dhall.Map
45
- import qualified Dhall.Parser as Dhall
46
- import qualified Dhall.TypeCheck as Dhall
47
+ import qualified Dhall.Parser as Dhall
48
+ import qualified Dhall.TypeCheck as Dhall
47
49
import qualified Language.LSP.Protocol.Types as LSP.Types
48
- import qualified Network.URI as URI
50
+ import qualified Network.URI as URI
49
51
50
52
51
53
-- | A @FileIdentifier@ represents either a local file or a remote url.
52
- newtype FileIdentifier = FileIdentifier Dhall . Chained
54
+ newtype FileIdentifier = FileIdentifier Import . Chained
53
55
54
56
-- | Construct a FileIdentifier from a local file path.
55
57
fileIdentifierFromFilePath :: FilePath -> FileIdentifier
@@ -58,7 +60,7 @@ fileIdentifierFromFilePath path =
58
60
directory = takeDirectory path
59
61
components = map Text. pack . reverse . splitDirectories $ directory
60
62
file = Dhall. File (Dhall. Directory components) filename
61
- in FileIdentifier $ Dhall . chainedFromLocalHere Dhall. Absolute file Dhall. Code
63
+ in FileIdentifier $ Import . chainedFromLocalHere Dhall. Absolute file Dhall. Code
62
64
63
65
-- | Construct a FileIdentifier from a given URI. Supports only "file:" URIs.
64
66
fileIdentifierFromURI :: URI -> Maybe FileIdentifier
@@ -76,11 +78,11 @@ newtype WellTyped = WellTyped {fromWellTyped :: Expr Src Void}
76
78
newtype Normal = Normal { fromNormal :: Expr Src Void }
77
79
78
80
-- An import graph, represented by list of import dependencies.
79
- type ImportGraph = [Dhall . Depends ]
81
+ type ImportGraph = [Import . Depends ]
80
82
81
83
-- | A cache maps Dhall imports to fully normalised expressions. By reusing
82
84
-- caches we can speeds up diagnostics etc. significantly!
83
- data Cache = Cache ImportGraph (Dhall.Map. Map Dhall . Chained Dhall . ImportSemantics )
85
+ data Cache = Cache ImportGraph (Dhall.Map. Map Import . Chained Import . ImportSemantics )
84
86
85
87
-- | The initial cache.
86
88
emptyCache :: Cache
@@ -94,11 +96,11 @@ invalidate :: FileIdentifier -> Cache -> Cache
94
96
invalidate (FileIdentifier chained) (Cache dependencies cache) =
95
97
Cache dependencies' $ Dhall.Map. withoutKeys cache invalidImports
96
98
where
97
- imports = map Dhall . parent dependencies ++ map Dhall . child dependencies
99
+ imports = map Import . parent dependencies ++ map Import . child dependencies
98
100
99
101
adjacencyLists = foldr
100
102
-- add reversed edges to adjacency lists
101
- (\ (Dhall . Depends parent child) -> Map. adjust (parent : ) child)
103
+ (\ (Import . Depends parent child) -> Map. adjust (parent : ) child)
102
104
-- starting from the discrete graph
103
105
(Map. fromList [ (i,[] ) | i <- imports])
104
106
dependencies
@@ -112,18 +114,18 @@ invalidate (FileIdentifier chained) (Cache dependencies cache) =
112
114
do vertex <- vertexFromImport import_
113
115
return (Graph. reachable graph vertex)
114
116
115
- codeImport = Dhall . chainedChangeMode Dhall. Code chained
116
- textImport = Dhall . chainedChangeMode Dhall. RawText chained
117
+ codeImport = Import . chainedChangeMode Dhall. Code chained
118
+ textImport = Import . chainedChangeMode Dhall. RawText chained
117
119
invalidImports = Set. fromList $ codeImport : reachableImports codeImport
118
120
++ textImport : reachableImports textImport
119
121
120
- dependencies' = filter (\ (Dhall . Depends parent child) -> Set. notMember parent invalidImports
122
+ dependencies' = filter (\ (Import . Depends parent child) -> Set. notMember parent invalidImports
121
123
&& Set. notMember child invalidImports) dependencies
122
124
123
125
-- | A Dhall error. Covers parsing, resolving of imports, typechecking and
124
126
-- normalisation.
125
127
data DhallError = ErrorInternal SomeException
126
- | ErrorImportSourced (Dhall. SourcedException Dhall . MissingImports )
128
+ | ErrorImportSourced (Dhall. SourcedException Import . MissingImports )
127
129
| ErrorTypecheck (Dhall. TypeError Src Void )
128
130
| ErrorParse Dhall. ParseError
129
131
@@ -137,38 +139,50 @@ parseWithHeader :: Text -> Either DhallError (Dhall.Header, Expr Src Dhall.Impor
137
139
parseWithHeader = first ErrorParse . Dhall. exprAndHeaderFromText " "
138
140
139
141
-- | Resolve all imports in an expression.
140
- load :: FileIdentifier -> Expr Src Dhall. Import -> Cache ->
141
- IO (Either DhallError (Cache , Expr Src Void ))
142
- load (FileIdentifier chained) expr (Cache graph cache) = do
143
- let emptyStatus = Dhall. emptyStatus " "
144
- status = -- reuse cache and import graph
145
- set Dhall. cache cache .
146
- set Dhall. graph graph .
142
+ load
143
+ :: EvaluateSettings
144
+ -> FileIdentifier
145
+ -> Expr Src Dhall. Import
146
+ -> Cache
147
+ -> IO (Either DhallError (Cache , Expr Src Void ))
148
+ load settings (FileIdentifier chained) expr (Cache graph cache) = do
149
+ let emptyStatus =
150
+ set Import. substitutions (view Dhall. substitutions settings)
151
+ . set Import. normalizer (view Dhall. normalizer settings)
152
+ . set Import. startingContext (view Dhall. startingContext settings)
153
+ $ Import. emptyStatusWithManager (view Dhall. newManager settings) " "
154
+
155
+ let status = -- reuse cache and import graph
156
+ set Import. cache cache .
157
+ set Import. graph graph .
147
158
-- set "root import"
148
- set Dhall . stack (chained :| [] )
159
+ set Import . stack (chained :| [] )
149
160
$ emptyStatus
150
- (do (expr', status') <- runStateT (Dhall . loadWith expr) status
151
- let cache' = view Dhall . cache status'
152
- graph' = view Dhall . graph status'
161
+ (do (expr', status') <- runStateT (Import . loadWith expr) status
162
+ let cache' = view Import . cache status'
163
+ graph' = view Import . graph status'
153
164
return . Right $ (Cache graph' cache', expr'))
154
165
`catch` (\ e -> return . Left $ ErrorImportSourced e)
155
166
`catch` (\ e -> return . Left $ ErrorInternal e)
156
167
157
168
-- | Typecheck a fully resolved expression. Returns a certification that the
158
169
-- input was well-typed along with its (well-typed) type.
159
- typecheck :: Expr Src Void -> Either DhallError (WellTyped , WellTyped )
160
- typecheck expr = case Dhall. typeOf expr of
170
+ typecheck
171
+ :: EvaluateSettings
172
+ -> Expr Src Void
173
+ -> Either DhallError (WellTyped , WellTyped )
174
+ typecheck settings expr = case Dhall. typeWith (view Dhall. startingContext settings) expr of
161
175
Left err -> Left $ ErrorTypecheck err
162
176
Right typ -> Right (WellTyped expr, WellTyped typ)
163
177
164
178
-- | Normalise a well-typed expression.
165
- normalize :: WellTyped -> Normal
166
- normalize (WellTyped expr) = Normal $ Dhall. normalize expr
179
+ normalize :: EvaluateSettings -> WellTyped -> Normal
180
+ normalize settings (WellTyped expr) = Normal $ Dhall. normalizeWith (view Dhall. normalizer settings) expr
167
181
168
182
-- | Given a normal expression compute the hash (using the default standard
169
183
-- version) of its alpha-normal form. Returns the hash in the format used in
170
184
-- Dhall's hash annotations (prefixed by "sha256:" and base-64 encoded).
171
185
hashNormalToCode :: Normal -> Text
172
186
hashNormalToCode (Normal expr) =
173
- Dhall . hashExpressionToCode (Dhall. denote alphaNormal)
187
+ Import . hashExpressionToCode (Dhall. denote alphaNormal)
174
188
where alphaNormal = Dhall. alphaNormalize expr
0 commit comments