git.iko.soy/rowtype-yoga/purescript-native
on 5df930c help sign in
144 lines 3.5 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Names
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Data types for names
13--
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
18module Language.PureScript.Names where
20import Data.List
21import Data.Data
22import Data.Function (on)
24-- |
25-- The type of a foreign import
26--
27data ForeignImportType
28 -- |
29 -- A regular foreign import
30 --
31 = ForeignImport
32 -- |
33 -- A foreign import which contains inline Javascript as a string literal
34 --
35 | InlineJavascript
36 -- |
37 -- A type class dictionary member accessor import, generated during desugaring of type class declarations
38 --
39 | TypeClassAccessorImport deriving (Show, Eq, Data, Typeable)
41-- |
42-- The type of a name
43--
44data NameKind
45 -- |
46 -- A value introduced as a binding in a module
47 --
48 = Value
49 -- |
50 -- A foreign import
51 --
52 | Extern ForeignImportType
53 -- |
54 -- A local name introduced using a lambda abstraction, variable introduction or binder
55 --
56 | LocalVariable
57 -- |
58 -- A data constructor
59 --
60 | DataConstructor
61 -- |
62 -- A type class dictionary, generated during desugaring of type class declarations
63 --
64 | TypeInstanceDictionaryValue
65 -- |
66 -- A type instance member, generated during desugaring of type class declarations
67 --
68 | TypeInstanceMember deriving (Show, Eq, Data, Typeable)
70-- |
71-- Names for value identifiers
72--
73data Ident
74 -- |
75 -- An alphanumeric identifier
76 --
77 = Ident String
78 -- |
79 -- A symbolic name for an infix operator
80 --
81 | Op String
82 -- |
83 -- An escaped name
84 --
85 | Escaped String deriving (Data, Typeable)
87instance Show Ident where
88 show (Ident s) = s
89 show (Op op) = '(':op ++ ")"
90 show (Escaped s) = s
92instance Eq Ident where
93 Ident s1 == Ident s2 = s1 == s2
94 Op s1 == Op s2 = s1 == s2
95 Escaped s1 == Escaped s2 = s1 == s2
96 Ident s1 == Escaped s2 = s1 == s2
97 Escaped s1 == Ident s2 = s1 == s2
98 _ == _ = False
100instance Ord Ident where
101 compare = compare `on` show
103-- |
104-- Proper names, i.e. capitalized names for e.g. module names, type//data constructors.
105--
106newtype ProperName = ProperName { runProperName :: String } deriving (Eq, Ord, Data, Typeable)
108instance Show ProperName where
109 show = runProperName
111-- |
112-- Module names
113--
114data ModuleName = ModuleName [ProperName] deriving (Eq, Ord, Data, Typeable)
116runModuleName :: ModuleName -> String
117runModuleName (ModuleName pns) = intercalate "." (runProperName `map` pns)
119moduleNameFromString :: String -> ModuleName
120moduleNameFromString = ModuleName . splitProperNames
121 where
122 splitProperNames s = case dropWhile (== '.') s of
123 "" -> []
124 s' -> ProperName w : splitProperNames s''
125 where (w, s'') = break (== '.') s'
127instance Show ModuleName where
128 show = runModuleName
130-- |
131-- A qualified name, i.e. a name with an optional module name
132--
133data Qualified a = Qualified (Maybe ModuleName) a deriving (Eq, Ord, Data, Typeable)
135instance (Show a) => Show (Qualified a) where
136 show (Qualified Nothing a) = show a
137 show (Qualified (Just name) a) = show name ++ "." ++ show a
139-- |
140-- Provide a default module name, if a name is unqualified
141--
142qualify :: ModuleName -> Qualified a -> (ModuleName, a)
143qualify m (Qualified Nothing a) = (m, a)
144qualify _ (Qualified (Just m) a) = (m, a)