forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNames.hs
More file actions
108 lines (89 loc) · 3.07 KB
/
Names.hs
File metadata and controls
108 lines (89 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE GADTs #-}
-- |
-- Data types for names
--
module Language.PureScript.Names where
import Control.Monad (liftM)
import Control.Monad.Supply.Class
import Data.List
import Data.Data
import Data.Aeson.TH
-- |
-- Names for value identifiers
--
data Ident
-- |
-- An alphanumeric identifier
--
= Ident String
-- |
-- A symbolic name for an infix operator
--
| Op String
-- |
-- A generated name for an identifier
--
| GenIdent (Maybe String) Integer deriving (Show, Read, Eq, Ord, Data, Typeable)
runIdent :: Ident -> String
runIdent (Ident i) = i
runIdent (Op op) = op
runIdent (GenIdent Nothing n) = "$" ++ show n
runIdent (GenIdent (Just name) n) = "$" ++ name ++ show n
showIdent :: Ident -> String
showIdent (Op op) = '(' : op ++ ")"
showIdent i = runIdent i
freshIdent :: (MonadSupply m) => String -> m Ident
freshIdent name = liftM (GenIdent (Just name)) fresh
freshIdent' :: (MonadSupply m) => m Ident
freshIdent' = liftM (GenIdent Nothing) fresh
-- |
-- Proper names, i.e. capitalized names for e.g. module names, type//data constructors.
--
newtype ProperName = ProperName { runProperName :: String } deriving (Show, Read, Eq, Ord, Data, Typeable)
-- |
-- Module names
--
newtype ModuleName = ModuleName [ProperName] deriving (Show, Read, Eq, Ord, Data, Typeable)
runModuleName :: ModuleName -> String
runModuleName (ModuleName pns) = intercalate "." (runProperName `map` pns)
moduleNameFromString :: String -> ModuleName
moduleNameFromString = ModuleName . splitProperNames
where
splitProperNames s = case dropWhile (== '.') s of
"" -> []
s' -> ProperName w : splitProperNames s''
where (w, s'') = break (== '.') s'
-- |
-- A qualified name, i.e. a name with an optional module name
--
data Qualified a = Qualified (Maybe ModuleName) a deriving (Show, Read, Eq, Ord, Data, Typeable, Functor)
showQualified :: (a -> String) -> Qualified a -> String
showQualified f (Qualified Nothing a) = f a
showQualified f (Qualified (Just name) a) = runModuleName name ++ "." ++ f a
-- |
-- Provide a default module name, if a name is unqualified
--
qualify :: ModuleName -> Qualified a -> (ModuleName, a)
qualify m (Qualified Nothing a) = (m, a)
qualify _ (Qualified (Just m) a) = (m, a)
-- |
-- Makes a qualified value from a name and module name.
--
mkQualified :: a -> ModuleName -> Qualified a
mkQualified name mn = Qualified (Just mn) name
-- | Remove the module name from a qualified name
disqualify :: Qualified a -> a
disqualify (Qualified _ a) = a
-- |
-- Checks whether a qualified value is actually qualified with a module reference
--
isUnqualified :: Qualified a -> Bool
isUnqualified (Qualified Nothing _) = True
isUnqualified _ = False
$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''Qualified)
$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''Ident)
$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ProperName)
$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ModuleName)