git.iko.soy/rowtype-yoga/purescript-native
on 7e26d58 help sign in
117 lines 3.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 #-}
17{-# LANGUAGE DeriveFunctor #-}
18{-# LANGUAGE GADTs #-}
20module Language.PureScript.Names where
22import Data.List
23import Data.Data
24import Data.List.Split (splitOn)
25import qualified Data.Aeson as A
26import qualified Data.Text as T
28-- |
29-- Names for value identifiers
30--
31data Ident
32 -- |
33 -- An alphanumeric identifier
34 --
35 = Ident String
36 -- |
37 -- A symbolic name for an infix operator
38 --
39 | Op String deriving (Eq, Ord, Data, Typeable)
41runIdent :: Ident -> String
42runIdent (Ident i) = i
43runIdent (Op op) = op
45instance Show Ident where
46 show (Ident s) = s
47 show (Op op) = '(':op ++ ")"
49-- |
50-- Proper names, i.e. capitalized names for e.g. module names, type//data constructors.
51--
52newtype ProperName = ProperName { runProperName :: String } deriving (Eq, Ord, Data, Typeable)
54instance Show ProperName where
55 show = runProperName
57-- |
58-- Module names
59--
60data ModuleName = ModuleName [ProperName] deriving (Eq, Ord, Data, Typeable)
62runModuleName :: ModuleName -> String
63runModuleName (ModuleName pns) = intercalate "." (runProperName `map` pns)
65moduleNameFromString :: String -> ModuleName
66moduleNameFromString = ModuleName . splitProperNames
67 where
68 splitProperNames s = case dropWhile (== '.') s of
69 "" -> []
70 s' -> ProperName w : splitProperNames s''
71 where (w, s'') = break (== '.') s'
73instance Show ModuleName where
74 show = runModuleName
76-- |
77-- A qualified name, i.e. a name with an optional module name
78--
79data Qualified a = Qualified (Maybe ModuleName) a deriving (Eq, Ord, Data, Typeable, Functor)
81instance (Show a) => Show (Qualified a) where
82 show (Qualified Nothing a) = show a
83 show (Qualified (Just name) a) = show name ++ "." ++ show a
85instance (a ~ ProperName) => A.ToJSON (Qualified a) where
86 toJSON = A.toJSON . show
88instance (a ~ ProperName) => A.FromJSON (Qualified a) where
89 parseJSON =
90 A.withText "Qualified ProperName" $ \str ->
91 return $ case reverse (splitOn "." (T.unpack str)) of
92 [name] -> Qualified Nothing (ProperName name)
93 (name:rest) -> Qualified (Just (reconstructModuleName rest)) (ProperName name)
94 _ -> Qualified Nothing (ProperName "")
95 where
96 reconstructModuleName = moduleNameFromString . intercalate "." . reverse
99-- |
100-- Provide a default module name, if a name is unqualified
101--
102qualify :: ModuleName -> Qualified a -> (ModuleName, a)
103qualify m (Qualified Nothing a) = (m, a)
104qualify _ (Qualified (Just m) a) = (m, a)
106-- |
107-- Makes a qualified value from a name and module name.
108--
109mkQualified :: a -> ModuleName -> Qualified a
110mkQualified name mn = Qualified (Just mn) name
112-- |
113-- Checks whether a qualified value is actually qualified with a module reference
114--
115isUnqualified :: Qualified a -> Bool
116isUnqualified (Qualified Nothing _) = True
117isUnqualified _ = False