forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNames.hs
More file actions
85 lines (70 loc) · 2.14 KB
/
Names.hs
File metadata and controls
85 lines (70 loc) · 2.14 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
-----------------------------------------------------------------------------
--
-- Module : Language.PureScript.Names
-- Copyright : (c) Phil Freeman 2013
-- License : MIT
--
-- Maintainer : Phil Freeman <paf31@cantab.net>
-- Stability : experimental
-- Portability :
--
-- |
-- Data types for names
--
-----------------------------------------------------------------------------
{-# LANGUAGE DeriveDataTypeable, DeriveFunctor #-}
module Language.PureScript.Names where
import Data.List
import Data.Data
-- |
-- Names for value identifiers
--
data Ident
-- |
-- An alphanumeric identifier
--
= Ident String
-- |
-- A symbolic name for an infix operator
--
| Op String deriving (Eq, Ord, Data, Typeable)
runIdent :: Ident -> String
runIdent (Ident i) = i
runIdent (Op op) = op
instance Show Ident where
show (Ident s) = s
show (Op op) = '(':op ++ ")"
-- |
-- Proper names, i.e. capitalized names for e.g. module names, type//data constructors.
--
newtype ProperName = ProperName { runProperName :: String } deriving (Eq, Ord, Data, Typeable)
instance Show ProperName where
show = runProperName
-- |
-- Module names
--
data ModuleName = ModuleName [ProperName] deriving (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'
instance Show ModuleName where
show = runModuleName
-- |
-- A qualified name, i.e. a name with an optional module name
--
data Qualified a = Qualified (Maybe ModuleName) a deriving (Eq, Ord, Data, Typeable, Functor)
instance (Show a) => Show (Qualified a) where
show (Qualified Nothing a) = show a
show (Qualified (Just name) a) = show name ++ "." ++ show 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)