module Elm.Ast
( ElmDefinition (..)
, ElmRecord (..)
, ElmType (..)
, ElmPrim (..)
, ElmRecordField (..)
, ElmConstructor (..)
, isEnum
, getConstructorNames
, TypeName (..)
, TypeRef (..)
, definitionToRef
) where
import Data.List.NonEmpty (NonEmpty, toList)
import Data.Text (Text)
data ElmDefinition
= DefRecord !ElmRecord
| DefType !ElmType
| DefPrim !ElmPrim
deriving (Int -> ElmDefinition -> ShowS
[ElmDefinition] -> ShowS
ElmDefinition -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ElmDefinition] -> ShowS
$cshowList :: [ElmDefinition] -> ShowS
show :: ElmDefinition -> String
$cshow :: ElmDefinition -> String
showsPrec :: Int -> ElmDefinition -> ShowS
$cshowsPrec :: Int -> ElmDefinition -> ShowS
Show)
data ElmRecord = ElmRecord
{ ElmRecord -> Text
elmRecordName :: !Text
, ElmRecord -> NonEmpty ElmRecordField
elmRecordFields :: !(NonEmpty ElmRecordField)
, ElmRecord -> Bool
elmRecordIsNewtype :: !Bool
} deriving (Int -> ElmRecord -> ShowS
[ElmRecord] -> ShowS
ElmRecord -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ElmRecord] -> ShowS
$cshowList :: [ElmRecord] -> ShowS
show :: ElmRecord -> String
$cshow :: ElmRecord -> String
showsPrec :: Int -> ElmRecord -> ShowS
$cshowsPrec :: Int -> ElmRecord -> ShowS
Show)
data ElmRecordField = ElmRecordField
{ ElmRecordField -> TypeRef
elmRecordFieldType :: !TypeRef
, ElmRecordField -> Text
elmRecordFieldName :: !Text
} deriving (Int -> ElmRecordField -> ShowS
[ElmRecordField] -> ShowS
ElmRecordField -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ElmRecordField] -> ShowS
$cshowList :: [ElmRecordField] -> ShowS
show :: ElmRecordField -> String
$cshow :: ElmRecordField -> String
showsPrec :: Int -> ElmRecordField -> ShowS
$cshowsPrec :: Int -> ElmRecordField -> ShowS
Show)
newtype TypeName = TypeName
{ TypeName -> Text
unTypeName :: Text
} deriving (Int -> TypeName -> ShowS
[TypeName] -> ShowS
TypeName -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TypeName] -> ShowS
$cshowList :: [TypeName] -> ShowS
show :: TypeName -> String
$cshow :: TypeName -> String
showsPrec :: Int -> TypeName -> ShowS
$cshowsPrec :: Int -> TypeName -> ShowS
Show)
data ElmType = ElmType
{ ElmType -> Text
elmTypeName :: !Text
, ElmType -> [Text]
elmTypeVars :: ![Text]
, ElmType -> Bool
elmTypeIsNewtype :: !Bool
, ElmType -> NonEmpty ElmConstructor
elmTypeConstructors :: !(NonEmpty ElmConstructor)
} deriving (Int -> ElmType -> ShowS
[ElmType] -> ShowS
ElmType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ElmType] -> ShowS
$cshowList :: [ElmType] -> ShowS
show :: ElmType -> String
$cshow :: ElmType -> String
showsPrec :: Int -> ElmType -> ShowS
$cshowsPrec :: Int -> ElmType -> ShowS
Show)
data ElmConstructor = ElmConstructor
{ ElmConstructor -> Text
elmConstructorName :: !Text
, ElmConstructor -> [TypeRef]
elmConstructorFields :: ![TypeRef]
} deriving (Int -> ElmConstructor -> ShowS
[ElmConstructor] -> ShowS
ElmConstructor -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ElmConstructor] -> ShowS
$cshowList :: [ElmConstructor] -> ShowS
show :: ElmConstructor -> String
$cshow :: ElmConstructor -> String
showsPrec :: Int -> ElmConstructor -> ShowS
$cshowsPrec :: Int -> ElmConstructor -> ShowS
Show)
isEnum :: ElmType -> Bool
isEnum :: ElmType -> Bool
isEnum ElmType{Bool
[Text]
NonEmpty ElmConstructor
Text
elmTypeConstructors :: NonEmpty ElmConstructor
elmTypeIsNewtype :: Bool
elmTypeVars :: [Text]
elmTypeName :: Text
elmTypeConstructors :: ElmType -> NonEmpty ElmConstructor
elmTypeIsNewtype :: ElmType -> Bool
elmTypeVars :: ElmType -> [Text]
elmTypeName :: ElmType -> Text
..} = forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
elmTypeVars Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => t a -> Bool
null (forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ElmConstructor -> [TypeRef]
elmConstructorFields NonEmpty ElmConstructor
elmTypeConstructors)
getConstructorNames :: ElmType -> [Text]
getConstructorNames :: ElmType -> [Text]
getConstructorNames ElmType{Bool
[Text]
NonEmpty ElmConstructor
Text
elmTypeConstructors :: NonEmpty ElmConstructor
elmTypeIsNewtype :: Bool
elmTypeVars :: [Text]
elmTypeName :: Text
elmTypeConstructors :: ElmType -> NonEmpty ElmConstructor
elmTypeIsNewtype :: ElmType -> Bool
elmTypeVars :: ElmType -> [Text]
elmTypeName :: ElmType -> Text
..} = forall a b. (a -> b) -> [a] -> [b]
map ElmConstructor -> Text
elmConstructorName forall a b. (a -> b) -> a -> b
$ forall a. NonEmpty a -> [a]
toList NonEmpty ElmConstructor
elmTypeConstructors
data ElmPrim
= ElmUnit
| ElmNever
| ElmBool
| ElmChar
| ElmInt
| ElmFloat
| ElmString
| ElmTime
| ElmValue
| ElmMaybe !TypeRef
| ElmResult !TypeRef !TypeRef
| ElmPair !TypeRef !TypeRef
| ElmTriple !TypeRef !TypeRef !TypeRef
| ElmList !TypeRef
| ElmNonEmptyPair !TypeRef
deriving (Int -> ElmPrim -> ShowS
[ElmPrim] -> ShowS
ElmPrim -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ElmPrim] -> ShowS
$cshowList :: [ElmPrim] -> ShowS
show :: ElmPrim -> String
$cshow :: ElmPrim -> String
showsPrec :: Int -> ElmPrim -> ShowS
$cshowsPrec :: Int -> ElmPrim -> ShowS
Show)
data TypeRef
= RefPrim !ElmPrim
| RefCustom !TypeName
deriving (Int -> TypeRef -> ShowS
[TypeRef] -> ShowS
TypeRef -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TypeRef] -> ShowS
$cshowList :: [TypeRef] -> ShowS
show :: TypeRef -> String
$cshow :: TypeRef -> String
showsPrec :: Int -> TypeRef -> ShowS
$cshowsPrec :: Int -> TypeRef -> ShowS
Show)
definitionToRef :: ElmDefinition -> TypeRef
definitionToRef :: ElmDefinition -> TypeRef
definitionToRef = \case
DefRecord ElmRecord{Bool
NonEmpty ElmRecordField
Text
elmRecordIsNewtype :: Bool
elmRecordFields :: NonEmpty ElmRecordField
elmRecordName :: Text
elmRecordIsNewtype :: ElmRecord -> Bool
elmRecordFields :: ElmRecord -> NonEmpty ElmRecordField
elmRecordName :: ElmRecord -> Text
..} -> TypeName -> TypeRef
RefCustom forall a b. (a -> b) -> a -> b
$ Text -> TypeName
TypeName Text
elmRecordName
DefType ElmType{Bool
[Text]
NonEmpty ElmConstructor
Text
elmTypeConstructors :: NonEmpty ElmConstructor
elmTypeIsNewtype :: Bool
elmTypeVars :: [Text]
elmTypeName :: Text
elmTypeConstructors :: ElmType -> NonEmpty ElmConstructor
elmTypeIsNewtype :: ElmType -> Bool
elmTypeVars :: ElmType -> [Text]
elmTypeName :: ElmType -> Text
..} -> TypeName -> TypeRef
RefCustom forall a b. (a -> b) -> a -> b
$ Text -> TypeName
TypeName Text
elmTypeName
DefPrim ElmPrim
elmPrim -> ElmPrim -> TypeRef
RefPrim ElmPrim
elmPrim