2019-10-21 21:59:42 +03:00
|
|
|
{-# LANGUAGE DerivingStrategies, GeneralizedNewtypeDeriving, StandaloneDeriving, FlexibleInstances, NamedFieldPuns, OverloadedStrings, QuantifiedConstraints, TypeOperators, UndecidableInstances #-}
|
2019-08-27 18:27:53 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2019-09-04 19:47:43 +03:00
|
|
|
module Instances () where
|
2019-08-27 18:27:53 +03:00
|
|
|
|
|
|
|
-- Testing code depends on certain instances that we don't want to
|
|
|
|
-- expose in semantic-core proper, yet are important enough that
|
|
|
|
-- we should keep track of them in a dedicated file.
|
|
|
|
|
2019-10-11 19:37:11 +03:00
|
|
|
import Analysis.File
|
2019-08-27 18:27:53 +03:00
|
|
|
import Analysis.ScopeGraph
|
2019-10-10 22:07:49 +03:00
|
|
|
import Core.Name (Name (..))
|
2019-08-27 18:27:53 +03:00
|
|
|
import Data.Aeson
|
|
|
|
import qualified Data.Map as Map
|
2019-10-11 01:08:25 +03:00
|
|
|
import Data.Text (Text, pack)
|
|
|
|
import qualified System.Path as Path
|
2019-09-04 19:47:43 +03:00
|
|
|
|
2019-10-08 00:20:12 +03:00
|
|
|
deriving newtype instance ToJSON Name
|
|
|
|
deriving newtype instance ToJSONKey Name
|
|
|
|
|
2019-09-04 19:47:43 +03:00
|
|
|
instance ToJSON a => ToJSON (File a) where
|
2019-10-10 21:18:56 +03:00
|
|
|
toJSON File{filePath, fileSpan, fileBody} = object
|
|
|
|
[ "path" .= filePath
|
|
|
|
, "span" .= fileSpan
|
2019-09-04 19:47:43 +03:00
|
|
|
, "body" .= fileBody
|
|
|
|
]
|
2019-08-27 18:27:53 +03:00
|
|
|
|
2019-10-11 01:08:25 +03:00
|
|
|
instance ToJSON Path.AbsRelFile where
|
2019-10-11 19:13:57 +03:00
|
|
|
toJSON p = toJSON (pack (Path.toString p))
|
2019-08-27 18:27:53 +03:00
|
|
|
|
|
|
|
instance ToJSON Ref where
|
2019-10-10 21:18:56 +03:00
|
|
|
toJSON (Ref path span) = object
|
|
|
|
[ "kind" .= ("ref" :: Text)
|
|
|
|
, "path" .= path
|
|
|
|
, "span" .= span
|
|
|
|
]
|
2019-08-27 18:27:53 +03:00
|
|
|
|
2019-10-11 19:23:09 +03:00
|
|
|
instance ToJSON (Decl Name) where
|
2019-10-10 21:18:56 +03:00
|
|
|
toJSON Decl{declSymbol, declPath, declSpan} = object
|
2019-08-27 18:27:53 +03:00
|
|
|
[ "kind" .= ("decl" :: Text)
|
|
|
|
, "symbol" .= declSymbol
|
2019-10-10 21:18:56 +03:00
|
|
|
, "path" .= declPath
|
|
|
|
, "span" .= declSpan
|
2019-08-27 18:27:53 +03:00
|
|
|
]
|
|
|
|
|
2019-10-11 19:23:09 +03:00
|
|
|
instance ToJSON (ScopeGraph Name) where
|
2019-08-27 18:27:53 +03:00
|
|
|
toJSON (ScopeGraph sc) = toJSON . Map.mapKeys declSymbol $ sc
|