New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use handwritten JSON instances for Type/Kind #3496
Merged
natefaubion
merged 1 commit into
purescript:master
from
natefaubion:types-kinds-json-instances
Dec 23, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,23 +2,22 @@ | |
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE DeriveFoldable #-} | ||
{-# LANGUAGE DeriveTraversable #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
module Language.PureScript.Kinds where | ||
|
||
import Prelude.Compat | ||
|
||
import GHC.Generics (Generic) | ||
import Control.Applicative ((<|>)) | ||
import Control.DeepSeq (NFData) | ||
import Data.Function (fix) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import Data.Aeson.BetterErrors (Parse, key, asText, asIntegral, nth, fromAesonParser, toAesonParser, throwCustomError, (<|>)) | ||
import Data.Aeson ((.=)) | ||
import Data.Aeson (Value, toJSON, (.=), (.:)) | ||
import Data.Aeson.Types (Parser) | ||
import qualified Data.Aeson as A | ||
|
||
import Language.PureScript.AST.SourcePos | ||
import Language.PureScript.Names | ||
import qualified Language.PureScript.Constants as C | ||
|
||
type SourceKind = Kind SourceAnn | ||
|
||
|
@@ -48,67 +47,65 @@ srcFunKind = FunKind NullSourceAnn | |
srcNamedKind :: Qualified (ProperName 'KindName) -> SourceKind | ||
srcNamedKind = NamedKind NullSourceAnn | ||
|
||
instance A.ToJSON a => A.ToJSON (Kind a) where | ||
toJSON kind = case kind of | ||
kindToJSON :: forall a. (a -> Value) -> Kind a -> Value | ||
kindToJSON annToJSON kind = | ||
case kind of | ||
KUnknown a i -> | ||
obj "KUnknown" a i | ||
variant "KUnknown" a i | ||
Row a k -> | ||
obj "Row" a k | ||
variant "Row" a (go k) | ||
FunKind a k1 k2 -> | ||
obj "FunKind" a [k1, k2] | ||
variant "FunKind" a (go k1, go k2) | ||
NamedKind a n -> | ||
obj "NamedKind" a n | ||
where | ||
obj :: A.ToJSON b => Text -> a -> b -> A.Value | ||
obj tag ann contents = | ||
A.object [ "tag" .= tag, "annotation" .= ann, "contents" .= contents ] | ||
|
||
-- This handles JSON generated by compilers up to 0.10.3 and maps them to the | ||
-- new representations (i.e. NamedKinds which are defined in the Prim module). | ||
kindFromJSON :: Parse Text a -> Parse Text a -> Parse Text (Kind a) | ||
kindFromJSON defaultAnn annFromJSON = fix $ \go -> do | ||
t <- key "tag" asText | ||
let annFromJSON' = key "annotation" annFromJSON <|> defaultAnn | ||
case t of | ||
variant "NamedKind" a n | ||
where | ||
go :: Kind a -> Value | ||
go = kindToJSON annToJSON | ||
|
||
variant :: A.ToJSON b => Text -> a -> b -> A.Value | ||
variant tag ann contents = | ||
A.object | ||
[ "tag" .= tag | ||
, "annotation" .= annToJSON ann | ||
, "contents" .= contents | ||
] | ||
|
||
instance A.ToJSON a => A.ToJSON (Kind a) where | ||
toJSON = kindToJSON toJSON | ||
|
||
kindFromJSON :: forall a. Parser a -> (Value -> Parser a) -> Value -> Parser (Kind a) | ||
kindFromJSON defaultAnn annFromJSON = A.withObject "Kind" $ \o -> do | ||
tag <- o .: "tag" | ||
a <- (o .: "annotation" >>= annFromJSON) <|> defaultAnn | ||
let | ||
contents :: A.FromJSON b => Parser b | ||
contents = o .: "contents" | ||
case tag of | ||
"KUnknown" -> | ||
KUnknown <$> annFromJSON' <*> key "contents" (nth 0 asIntegral) | ||
"Star" -> | ||
kindType <$> defaultAnn | ||
KUnknown a <$> contents | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure this parser was broken. It appears to parse with |
||
"Row" -> | ||
Row <$> annFromJSON' <*> key "contents" go | ||
"FunKind" -> | ||
let | ||
kindAt n = key "contents" (nth n go) | ||
in | ||
FunKind <$> annFromJSON' <*> kindAt 0 <*> kindAt 1 | ||
"Symbol" -> | ||
kindSymbol <$> defaultAnn | ||
Row a <$> (go =<< contents) | ||
"FunKind" -> do | ||
(b, c) <- contents | ||
FunKind a <$> go b <*> go c | ||
"NamedKind" -> | ||
NamedKind <$> annFromJSON' <*> key "contents" fromAesonParser | ||
NamedKind a <$> contents | ||
other -> | ||
throwCustomError (T.append "Unrecognised tag: " other) | ||
|
||
fail $ "Unrecognised tag: " ++ other | ||
where | ||
-- The following are copied from Environment and reimplemented to avoid | ||
-- circular dependencies. | ||
primName :: Text -> Qualified (ProperName b) | ||
primName = Qualified (Just $ ModuleName [ProperName C.prim]) . ProperName | ||
|
||
primKind = flip NamedKind . primName | ||
|
||
kindType = primKind C.typ | ||
kindSymbol = primKind C.symbol | ||
go :: Value -> Parser (Kind a) | ||
go = kindFromJSON defaultAnn annFromJSON | ||
|
||
-- These overlapping instances exist to preserve compatability for common | ||
-- instances which have a sensible default for missing annotations. | ||
instance {-# OVERLAPPING #-} A.FromJSON (Kind SourceAnn) where | ||
parseJSON = toAesonParser id (kindFromJSON (pure NullSourceAnn) fromAesonParser) | ||
parseJSON = kindFromJSON (pure NullSourceAnn) A.parseJSON | ||
|
||
instance {-# OVERLAPPING #-} A.FromJSON (Kind ()) where | ||
parseJSON = toAesonParser id (kindFromJSON (pure ()) fromAesonParser) | ||
parseJSON = kindFromJSON (pure ()) A.parseJSON | ||
|
||
instance {-# OVERLAPPING #-} A.FromJSON a => A.FromJSON (Kind a) where | ||
parseJSON = toAesonParser id (kindFromJSON (fail "Invalid annotation") fromAesonParser) | ||
parseJSON = kindFromJSON (fail "Invalid annotation") A.parseJSON | ||
|
||
everywhereOnKinds :: (Kind a -> Kind a) -> Kind a -> Kind a | ||
everywhereOnKinds f = go | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the old kind parsing from 0.10. I don't think it's necessary to preserve these anymore.