git.iko.soy/rowtype-yoga/purescript-native
on d167155 help sign in
143 lines 4.0 kB history raw
1{-# LANGUAGE DeriveFunctor #-}
2{-# LANGUAGE TemplateHaskell #-}
3{-# LANGUAGE EmptyDataDecls #-}
4{-# LANGUAGE KindSignatures #-}
6-- |
7-- Data types for names
8--
9module Language.PureScript.Names where
11import Control.Monad (liftM)
12import Control.Monad.Supply.Class
14import Data.List
15import Data.Aeson
16import Data.Aeson.TH
18-- |
19-- Names for value identifiers
20--
21data Ident
22 -- |
23 -- An alphanumeric identifier
24 --
25 = Ident String
26 -- |
27 -- A symbolic name for an infix operator
28 --
29 | Op String
30 -- |
31 -- A generated name for an identifier
32 --
33 | GenIdent (Maybe String) Integer
34 deriving (Show, Read, Eq, Ord)
36runIdent :: Ident -> String
37runIdent (Ident i) = i
38runIdent (Op op) = op
39runIdent (GenIdent Nothing n) = "$" ++ show n
40runIdent (GenIdent (Just name) n) = "$" ++ name ++ show n
42showIdent :: Ident -> String
43showIdent (Op op) = '(' : op ++ ")"
44showIdent i = runIdent i
46freshIdent :: (MonadSupply m) => String -> m Ident
47freshIdent name = liftM (GenIdent (Just name)) fresh
49freshIdent' :: (MonadSupply m) => m Ident
50freshIdent' = liftM (GenIdent Nothing) fresh
52-- |
53-- Proper names, i.e. capitalized names for e.g. module names, type//data constructors.
54--
55newtype ProperName (a :: ProperNameType) = ProperName { runProperName :: String }
56 deriving (Show, Read, Eq, Ord)
58instance ToJSON (ProperName a) where
59 toJSON = toJSON . runProperName
61instance FromJSON (ProperName a) where
62 parseJSON = fmap ProperName . parseJSON
64-- |
65-- The closed set of proper name types.
66--
67data ProperNameType = TypeName | ConstructorName | ClassName | Namespace
69-- |
70-- Coerces a ProperName from one ProperNameType to another. This should be used
71-- with care, and is primarily used to convert ClassNames into TypeNames after
72-- classes have been desugared.
73--
74coerceProperName :: ProperName a -> ProperName b
75coerceProperName = ProperName . runProperName
77-- |
78-- Module names
79--
80newtype ModuleName = ModuleName [ProperName 'Namespace]
81 deriving (Show, Read, Eq, Ord)
83runModuleName :: ModuleName -> String
84runModuleName (ModuleName pns) = intercalate "." (runProperName `map` pns)
86moduleNameFromString :: String -> ModuleName
87moduleNameFromString = ModuleName . splitProperNames
88 where
89 splitProperNames s = case dropWhile (== '.') s of
90 "" -> []
91 s' -> ProperName w : splitProperNames s''
92 where (w, s'') = break (== '.') s'
94-- |
95-- A qualified name, i.e. a name with an optional module name
96--
97data Qualified a = Qualified (Maybe ModuleName) a
98 deriving (Show, Read, Eq, Ord, Functor)
100showQualified :: (a -> String) -> Qualified a -> String
101showQualified f (Qualified Nothing a) = f a
102showQualified f (Qualified (Just name) a) = runModuleName name ++ "." ++ f a
104-- |
105-- Provide a default module name, if a name is unqualified
106--
107qualify :: ModuleName -> Qualified a -> (ModuleName, a)
108qualify m (Qualified Nothing a) = (m, a)
109qualify _ (Qualified (Just m) a) = (m, a)
111-- |
112-- Makes a qualified value from a name and module name.
113--
114mkQualified :: a -> ModuleName -> Qualified a
115mkQualified name mn = Qualified (Just mn) name
117-- | Remove the module name from a qualified name
118disqualify :: Qualified a -> a
119disqualify (Qualified _ a) = a
121-- |
122-- Checks whether a qualified value is actually qualified with a module reference
123--
124isQualified :: Qualified a -> Bool
125isQualified (Qualified Nothing _) = False
126isQualified _ = True
128-- |
129-- Checks whether a qualified value is not actually qualified with a module reference
130--
131isUnqualified :: Qualified a -> Bool
132isUnqualified = not . isQualified
134-- |
135-- Checks whether a qualified value is qualified with a particular module
136--
137isQualifiedWith :: ModuleName -> Qualified a -> Bool
138isQualifiedWith mn (Qualified (Just mn') _) = mn == mn'
139isQualifiedWith _ _ = False
141$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''Qualified)
142$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''Ident)
143$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ModuleName)