git.iko.soy/rowtype-yoga/purescript-native
on f08bedf help sign in
85 lines 2.2 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, DeriveFunctor #-}
18module Language.PureScript.Names where
20import Data.List
21import Data.Data
23-- |
24-- Names for value identifiers
25--
26data Ident
27 -- |
28 -- An alphanumeric identifier
29 --
30 = Ident String
31 -- |
32 -- A symbolic name for an infix operator
33 --
34 | Op String deriving (Eq, Ord, Data, Typeable)
36runIdent :: Ident -> String
37runIdent (Ident i) = i
38runIdent (Op op) = op
40instance Show Ident where
41 show (Ident s) = s
42 show (Op op) = '(':op ++ ")"
44-- |
45-- Proper names, i.e. capitalized names for e.g. module names, type//data constructors.
46--
47newtype ProperName = ProperName { runProperName :: String } deriving (Eq, Ord, Data, Typeable)
49instance Show ProperName where
50 show = runProperName
52-- |
53-- Module names
54--
55data ModuleName = ModuleName [ProperName] deriving (Eq, Ord, Data, Typeable)
57runModuleName :: ModuleName -> String
58runModuleName (ModuleName pns) = intercalate "." (runProperName `map` pns)
60moduleNameFromString :: String -> ModuleName
61moduleNameFromString = ModuleName . splitProperNames
62 where
63 splitProperNames s = case dropWhile (== '.') s of
64 "" -> []
65 s' -> ProperName w : splitProperNames s''
66 where (w, s'') = break (== '.') s'
68instance Show ModuleName where
69 show = runModuleName
71-- |
72-- A qualified name, i.e. a name with an optional module name
73--
74data Qualified a = Qualified (Maybe ModuleName) a deriving (Eq, Ord, Data, Typeable, Functor)
76instance (Show a) => Show (Qualified a) where
77 show (Qualified Nothing a) = show a
78 show (Qualified (Just name) a) = show name ++ "." ++ show a
80-- |
81-- Provide a default module name, if a name is unqualified
82--
83qualify :: ModuleName -> Qualified a -> (ModuleName, a)
84qualify m (Qualified Nothing a) = (m, a)
85qualify _ (Qualified (Just m) a) = (m, a)