1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveFunctor #-}
3{-# LANGUAGE TemplateHaskell #-}
4{-# LANGUAGE EmptyDataDecls #-}
5{-# LANGUAGE KindSignatures #-}
10module Language.PureScript.Names where
12import Control.Monad (liftM)
13import Control.Monad.Supply.Class
21-- Names for value identifiers
25 -- An alphanumeric identifier
29 -- A symbolic name for an infix operator
33 -- A generated name for an identifier
35 | GenIdent (Maybe String) Integer deriving (Show, Read, Eq, Ord, Data, Typeable)
37runIdent :: Ident -> String
40runIdent (GenIdent Nothing n) = "$" ++ show n
41runIdent (GenIdent (Just name) n) = "$" ++ name ++ show n
43showIdent :: Ident -> String
44showIdent (Op op) = '(' : op ++ ")"
45showIdent i = runIdent i
47freshIdent :: (MonadSupply m) => String -> m Ident
48freshIdent name = liftM (GenIdent (Just name)) fresh
50freshIdent' :: (MonadSupply m) => m Ident
51freshIdent' = liftM (GenIdent Nothing) fresh
54-- Proper names, i.e. capitalized names for e.g. module names, type//data constructors.
56newtype ProperName (a :: ProperNameType) = ProperName { runProperName :: String }
57 deriving (Show, Read, Eq, Ord, Data, Typeable)
59instance ToJSON (ProperName a) where
60 toJSON = toJSON . runProperName
62instance FromJSON (ProperName a) where
63 parseJSON = fmap ProperName . parseJSON
66-- The closed set of proper name types.
68data ProperNameType = TypeName | ConstructorName | ClassName | Namespace
71-- Coerces a ProperName from one ProperNameType to another. This should be used
72-- with care, and is primarily used to convert ClassNames into TypeNames after
73-- classes have been desugared.
75coerceProperName :: ProperName a -> ProperName b
76coerceProperName = ProperName . runProperName
81newtype ModuleName = ModuleName [ProperName 'Namespace] deriving (Show, Read, Eq, Ord, Data, Typeable)
83runModuleName :: ModuleName -> String
84runModuleName (ModuleName pns) = intercalate "." (runProperName `map` pns)
86moduleNameFromString :: String -> ModuleName
87moduleNameFromString = ModuleName . splitProperNames
89 splitProperNames s = case dropWhile (== '.') s of
91 s' -> ProperName w : splitProperNames s''
92 where (w, s'') = break (== '.') s'
95-- A qualified name, i.e. a name with an optional module name
97data Qualified a = Qualified (Maybe ModuleName) a deriving (Show, Read, Eq, Ord, Data, Typeable, Functor)
99showQualified :: (a -> String) -> Qualified a -> String
100showQualified f (Qualified Nothing a) = f a
101showQualified f (Qualified (Just name) a) = runModuleName name ++ "." ++ f a
104-- Provide a default module name, if a name is unqualified
106qualify :: ModuleName -> Qualified a -> (ModuleName, a)
107qualify m (Qualified Nothing a) = (m, a)
108qualify _ (Qualified (Just m) a) = (m, a)
111-- Makes a qualified value from a name and module name.
113mkQualified :: a -> ModuleName -> Qualified a
114mkQualified name mn = Qualified (Just mn) name
116-- | Remove the module name from a qualified name
117disqualify :: Qualified a -> a
118disqualify (Qualified _ a) = a
121-- Checks whether a qualified value is actually qualified with a module reference
123isQualified :: Qualified a -> Bool
124isQualified (Qualified Nothing _) = False
128-- Checks whether a qualified value is not actually qualified with a module reference
130isUnqualified :: Qualified a -> Bool
131isUnqualified = not . isQualified
134-- Checks whether a qualified value is qualified with a particular module
136isQualifiedWith :: ModuleName -> Qualified a -> Bool
137isQualifiedWith mn (Qualified (Just mn') _) = mn == mn'
138isQualifiedWith _ _ = False
140$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''Qualified)
141$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''Ident)
142$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ModuleName)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list