-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONTop.hs
431 lines (393 loc) · 15.1 KB
/
JSONTop.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
module Agda.Interaction.JSONTop
( jsonREPL
) where
import Control.Monad.State
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy.Char8 as BS
import qualified Data.Text as T
import qualified Data.Set as Set
import Agda.Interaction.AgdaTop
import Agda.Interaction.Base
(CommandState(..), CurrentFile(..), ComputeMode(..), Rewrite(..), OutputForm(..), OutputConstraint(..))
import qualified Agda.Interaction.BasicOps as B
import Agda.Interaction.EmacsTop
import Agda.Interaction.JSON
import Agda.Interaction.Response as R
import Agda.Interaction.Highlighting.JSON
import Agda.Syntax.Abstract.Pretty (prettyATop)
import Agda.Syntax.Common
import qualified Agda.Syntax.Concrete as C
import Agda.Syntax.Concrete.Name (NameInScope(..), Name)
import Agda.Syntax.Internal (telToList, Dom'(..), Dom, MetaId(..), ProblemId(..), Blocker(..))
import Agda.Syntax.Position (Range, rangeIntervals, Interval'(..), Position'(..))
import Agda.VersionCommit
import Agda.TypeChecking.Errors (getAllWarningsOfTCErr)
import Agda.TypeChecking.Monad (Comparison(..), inTopContext, TCM, TCErr, TCWarning, NamedMeta(..))
import Agda.TypeChecking.Monad.MetaVars (getInteractionRange, getMetaRange, withMetaId)
import Agda.TypeChecking.Pretty (PrettyTCM(..), prettyTCM)
-- borrowed from EmacsTop, for temporarily serialising stuff
import Agda.TypeChecking.Pretty.Warning (filterTCWarnings)
import Agda.TypeChecking.Warnings (WarningsAndNonFatalErrors(..))
import Agda.Utils.Pretty (Pretty(..))
import qualified Agda.Utils.Pretty as P
import Agda.Utils.Time (CPUTime(..))
--------------------------------------------------------------------------------
-- | 'jsonREPL' is a interpreter like 'mimicGHCi', but outputs JSON-encoded strings.
--
-- 'jsonREPL' reads Haskell values (that starts from 'IOTCM' ...) from stdin,
-- interprets them, and outputs JSON-encoded strings. into stdout.
jsonREPL :: TCM () -> TCM ()
jsonREPL = repl (liftIO . BS.putStrLn <=< jsonifyResponse) "JSON> "
instance EncodeTCM NameInScope where
instance ToJSON NameInScope where
toJSON InScope = toJSON True
toJSON NotInScope = toJSON False
instance EncodeTCM Status where
instance ToJSON Status where
toJSON status = object
[ "showImplicitArguments" .= sShowImplicitArguments status
, "showIrrelevantArguments" .= sShowIrrelevantArguments status
, "checked" .= sChecked status
]
instance EncodeTCM CommandState where
instance ToJSON CommandState where
toJSON commandState = object
[ "interactionPoints" .= theInteractionPoints commandState
, "currentFile" .= theCurrentFile commandState
-- more?
]
instance EncodeTCM CurrentFile where
instance ToJSON CurrentFile where
toJSON (CurrentFile path _ time) = toJSON (path, time) -- backwards compat.
instance EncodeTCM ResponseContextEntry where
encodeTCM entry = obj
[ "originalName" @= encodePretty (respOrigName entry)
, "reifiedName" @= encodePretty (respReifName entry)
, "binding" #= prettyATop (unArg (respType entry))
, "inScope" @= respInScope entry
]
instance EncodeTCM (Position' ()) where
instance ToJSON (Position' ()) where
toJSON p = object
[ "pos" .= toJSON (posPos p)
, "line" .= toJSON (posLine p)
, "col" .= toJSON (posCol p)
]
instance EncodeTCM Range where
instance ToJSON Range where
toJSON = toJSON . map prettyInterval . rangeIntervals
where prettyInterval i = object [ "start" .= iStart i, "end" .= iEnd i ]
instance EncodeTCM ProblemId where
instance EncodeTCM MetaId where
instance ToJSON ProblemId where toJSON (ProblemId i) = toJSON i
instance ToJSON MetaId where toJSON (MetaId i) = toJSON i
instance EncodeTCM InteractionId where
encodeTCM ii@(InteractionId i) = obj
[ "id" @= toJSON i
, "range" #= intervalsTCM
]
where
intervalsTCM = toJSON <$> getInteractionRange ii
instance ToJSON InteractionId where
toJSON (InteractionId i) = toJSON i
instance EncodeTCM NamedMeta where
encodeTCM m = obj
[ "name" #= nameTCM
, "range" #= intervalsTCM
]
where
nameTCM = encodeShow <$> withMetaId (nmid m) (prettyATop m)
intervalsTCM = toJSON <$> getMetaRange (nmid m)
instance EncodeTCM GiveResult where
instance ToJSON GiveResult where
toJSON (Give_String s) = object [ "str" .= s ]
toJSON Give_Paren = object [ "paren" .= True ]
toJSON Give_NoParen = object [ "paren" .= False ]
instance EncodeTCM MakeCaseVariant where
instance ToJSON MakeCaseVariant where
toJSON R.Function = String "Function"
toJSON R.ExtendedLambda = String "ExtendedLambda"
encodePretty :: Pretty a => a -> Value
encodePretty = encodeShow . pretty
encodeShow :: Show a => a -> Value
encodeShow = String . T.pack . show
encodePrettyTCM :: PrettyTCM a => a -> TCM Value
encodePrettyTCM = (encodeShow <$>) . prettyTCM
instance EncodeTCM Rewrite where
instance ToJSON Rewrite where toJSON = encodeShow
instance EncodeTCM CPUTime where
instance ToJSON CPUTime where toJSON = encodePretty
instance EncodeTCM ComputeMode where
instance ToJSON ComputeMode where toJSON = encodeShow
encodeOCCmp :: (a -> TCM Value)
-> Comparison -> a -> a -> T.Text
-> TCM Value
encodeOCCmp f c i j k = kind k
[ "comparison" @= encodeShow c
, "constraintObjs" #= traverse f [i, j]
]
-- Goals
encodeOC :: (a -> TCM Value)
-> (b -> TCM Value)
-> OutputConstraint b a
-> TCM Value
encodeOC f encPrettyTCM = \case
OfType i a -> kind "OfType"
[ "constraintObj" #= f i
, "type" #= encPrettyTCM a
]
CmpInType c a i j -> kind "CmpInType"
[ "comparison" @= encodeShow c
, "type" #= encPrettyTCM a
, "constraintObjs" #= traverse f [i, j]
]
CmpElim ps a is js -> kind "CmpElim"
[ "polarities" @= map encodeShow ps
, "type" #= encPrettyTCM a
, "constraintObjs" #= traverse (traverse f) [is, js]
]
JustType a -> kind "JustType"
[ "constraintObj" #= f a
]
JustSort a -> kind "JustSort"
[ "constraintObj" #= f a
]
CmpTypes c i j -> encodeOCCmp f c i j "CmpTypes"
CmpLevels c i j -> encodeOCCmp f c i j "CmpLevels"
CmpTeles c i j -> encodeOCCmp f c i j "CmpTeles"
CmpSorts c i j -> encodeOCCmp f c i j "CmpSorts"
Assign i a -> kind "Assign"
[ "constraintObj" #= f i
, "value" #= encPrettyTCM a
]
TypedAssign i v t -> kind "TypedAssign"
[ "constraintObj" #= f i
, "value" #= encPrettyTCM v
, "type" #= encPrettyTCM t
]
PostponedCheckArgs i es t0 t1 -> kind "PostponedCheckArgs"
[ "constraintObj" #= f i
, "ofType" #= encPrettyTCM t0
, "arguments" #= forM es encPrettyTCM
, "type" #= encPrettyTCM t1
]
IsEmptyType a -> kind "IsEmptyType"
[ "type" #= encPrettyTCM a
]
SizeLtSat a -> kind "SizeLtSat"
[ "type" #= encPrettyTCM a
]
FindInstanceOF i t cs -> kind "FindInstanceOF"
[ "constraintObj" #= f i
, "candidates" #= forM cs encodeKVPairs
, "type" #= encPrettyTCM t
]
where encodeKVPairs (_, v, t) = obj -- TODO: encode kind
[ "value" #= encPrettyTCM v
, "type" #= encPrettyTCM t
]
PTSInstance a b -> kind "PTSInstance"
[ "constraintObjs" #= traverse f [a, b]
]
PostponedCheckFunDef name a err -> kind "PostponedCheckFunDef"
[ "name" @= encodePretty name
, "type" #= encPrettyTCM a
, "error" #= encodeTCM err
]
CheckLock t lk -> kind "CheckLock"
[ "head" #= f t
, "lock" #= f lk
]
UsableAtMod mod t -> kind "UsableAtMod"
[ "mod" @= encodePretty mod
, "term" #= f t
]
encodeNamedPretty :: PrettyTCM a => (Name, a) -> TCM Value
encodeNamedPretty (name, a) = obj
[ "name" @= encodePretty name
, "term" #= encodePrettyTCM a
]
instance EncodeTCM (OutputForm C.Expr C.Expr) where
encodeTCM (OutputForm range problems unblock oc) = obj
[ "range" @= range
, "problems" @= problems
, "unblocker" @= unblock
, "constraint" #= encodeOC (pure . encodeShow) (pure . encodeShow) oc
]
instance EncodeTCM Blocker where
encodeTCM (UnblockOnMeta x) = kind "UnblockOnMeta" [ "meta" @= x ]
encodeTCM (UnblockOnProblem p) = kind "UnblockOnProblem" [ "id" @= p ]
encodeTCM (UnblockOnAll us) = kind "UnblockOnAll" [ "blockers" @= Set.toList us ]
encodeTCM (UnblockOnAny us) = kind "UnblockOnAny" [ "blockers" @= Set.toList us ]
instance EncodeTCM DisplayInfo where
encodeTCM (Info_CompilationOk backend wes) = kind "CompilationOk"
[ "backend" @= encodePretty backend
, "warnings" #= encodeTCM (filterTCWarnings (tcWarnings wes))
, "errors" #= encodeTCM (filterTCWarnings (nonFatalErrors wes))
]
encodeTCM (Info_Constraints constraints) = kind "Constraints"
[ "constraints" #= forM constraints encodeTCM
]
encodeTCM (Info_AllGoalsWarnings (vis, invis) wes) = kind "AllGoalsWarnings"
[ "visibleGoals" #= forM vis (encodeOC encodeTCM encodePrettyTCM)
, "invisibleGoals" #= forM invis (encodeOC encodeTCM encodePrettyTCM)
, "warnings" #= encodeTCM (filterTCWarnings (tcWarnings wes))
, "errors" #= encodeTCM (filterTCWarnings (nonFatalErrors wes))
]
encodeTCM (Info_Time time) = kind "Time"
[ "time" @= time
]
encodeTCM (Info_Error err) = encodeTCM err
encodeTCM Info_Intro_NotFound = kind "IntroNotFound" []
encodeTCM (Info_Intro_ConstructorUnknown introductions) = kind "IntroConstructorUnknown"
[ "constructors" @= map toJSON introductions
]
encodeTCM (Info_Auto info) = kind "Auto"
[ "info" @= toJSON info
]
encodeTCM (Info_ModuleContents names tele contents) = kind "ModuleContents"
[ "contents" #= forM contents encodeNamedPretty
, "telescope" #= forM (telToList tele) encodeDomType
, "names" @= map encodePretty names
]
where
encodeDomType :: PrettyTCM a => Dom (ArgName, a) -> TCM Value
encodeDomType dom = obj
[ "dom" #= encodePrettyTCM (unDom dom)
, "name" @= fmap encodePretty (bareNameOf dom)
, "finite" @= toJSON (domFinite dom)
, "cohesion" @= encodeShow (modCohesion . argInfoModality $ domInfo dom)
, "relevance" @= encodeShow (modRelevance . argInfoModality $ domInfo dom)
, "hiding" @= case argInfoHiding $ domInfo dom of
Instance o -> show o
o -> show o
]
encodeTCM (Info_SearchAbout results search) = kind "SearchAbout"
[ "results" #= forM results encodeNamedPretty
, "search" @= toJSON search
]
encodeTCM (Info_WhyInScope thing path v xs ms) = kind "WhyInScope"
[ "thing" @= thing
, "filepath" @= toJSON path
-- use Emacs message first
, "message" #= explainWhyInScope thing path v xs ms
]
encodeTCM (Info_NormalForm commandState computeMode time expr) = kind "NormalForm"
[ "commandState" @= commandState
, "computeMode" @= computeMode
, "time" @= time
, "expr" #= encodePrettyTCM expr
]
encodeTCM (Info_InferredType commandState time expr) = kind "InferredType"
[ "commandState" @= commandState
, "time" @= time
, "expr" #= encodePrettyTCM expr
]
encodeTCM (Info_Context ii ctx) = kind "Context"
[ "interactionPoint" @= ii
, "context" @= ctx
]
encodeTCM Info_Version = kind "Version"
[ "version" @= (versionWithCommitInfo :: String)
]
encodeTCM (Info_GoalSpecific ii info) = kind "GoalSpecific"
[ "interactionPoint" @= ii
, "goalInfo" #= encodeGoalSpecific ii info
]
instance EncodeTCM GoalTypeAux where
encodeTCM GoalOnly = kind "GoalOnly" []
encodeTCM (GoalAndHave expr) = kind "GoalAndHave"
[ "expr" #= encodePrettyTCM expr ]
encodeTCM (GoalAndElaboration term) = kind "GoalAndElaboration"
[ "term" #= encodePrettyTCM term ]
encodeGoalSpecific :: InteractionId -> GoalDisplayInfo -> TCM Value
encodeGoalSpecific ii = go
where
go (Goal_HelperFunction helperType) = kind "HelperFunction"
[ "signature" #= inTopContext (prettyATop helperType)
]
go (Goal_NormalForm computeMode expr) = kind "NormalForm"
[ "computeMode" @= computeMode
, "expr" #= B.showComputed computeMode expr
]
go (Goal_GoalType rewrite goalType entries boundary outputForms) = kind "GoalType"
[ "rewrite" @= rewrite
, "typeAux" @= goalType
, "type" #= prettyTypeOfMeta rewrite ii
, "entries" @= entries
, "boundary" @= map encodePretty boundary
, "outputForms" @= map encodePretty outputForms
]
go (Goal_CurrentGoal rewrite) = kind "CurrentGoal"
[ "rewrite" @= rewrite
, "type" #= prettyTypeOfMeta rewrite ii
]
go (Goal_InferredType expr) = kind "InferredType"
[ "expr" #= prettyATop expr
]
instance EncodeTCM Info_Error where
encodeTCM (Info_GenericError err) = kind "Error"
[ "warnings" #= (getAllWarningsOfTCErr err
>>= encodeTCM . filterTCWarnings)
, "error" #= encodeTCM err
]
encodeTCM err = kind "Error"
[ "warnings" @= ([] :: [String])
, "error" #= obj
[ "message" #= showInfoError err
]
]
instance EncodeTCM TCErr where
encodeTCM err = obj
[ "message" #= encodePrettyTCM err
]
instance EncodeTCM TCWarning where
encodeTCM w = obj
[ "message" #= (P.render <$> prettyTCM w)
]
instance EncodeTCM Response where
encodeTCM (Resp_HighlightingInfo info remove method modFile) =
liftIO $ jsonifyHighlightingInfo info remove method modFile
encodeTCM (Resp_DisplayInfo info) = kind "DisplayInfo"
[ "info" @= info
]
encodeTCM (Resp_ClearHighlighting tokenBased) = kind "ClearHighlighting"
[ "tokenBased" @= tokenBased
]
encodeTCM Resp_DoneAborting = kind "DoneAborting" []
encodeTCM Resp_DoneExiting = kind "DoneExiting" []
encodeTCM Resp_ClearRunningInfo = kind "ClearRunningInfo" []
encodeTCM (Resp_RunningInfo debugLevel msg) = kind "RunningInfo"
[ "debugLevel" @= debugLevel
, "message" @= msg
]
encodeTCM (Resp_Status status) = kind "Status"
[ "status" @= status
]
encodeTCM (Resp_JumpToError filepath position) = kind "JumpToError"
[ "filepath" @= filepath
, "position" @= position
]
encodeTCM (Resp_InteractionPoints interactionPoints) = kind "InteractionPoints"
[ "interactionPoints" @= interactionPoints
]
encodeTCM (Resp_GiveAction i giveResult) = kind "GiveAction"
[ "interactionPoint" @= i
, "giveResult" @= giveResult
]
encodeTCM (Resp_MakeCase id variant clauses) = kind "MakeCase"
[ "interactionPoint" @= id
, "variant" @= variant
, "clauses" @= clauses
]
encodeTCM (Resp_SolveAll solutions) = kind "SolveAll"
[ "solutions" @= map encodeSolution solutions
]
where
encodeSolution (i, expr) = object
[ "interactionPoint" .= i
, "expression" .= P.prettyShow expr
]
-- | Convert Response to an JSON value for interactive editor frontends.
jsonifyResponse :: Response -> TCM ByteString
jsonifyResponse = pure . encode <=< encodeTCM