1module AST.Variable where
3import Control.Applicative ((<$>), (<*>))
5import qualified Data.List as List
6import qualified Data.Maybe as Maybe
7import Text.PrettyPrint as P
8import qualified AST.Helpers as Help
14newtype Raw = Raw String
15 deriving (Eq,Ord,Show)
22 deriving (Eq,Ord,Show)
25data Canonical = Canonical
29 deriving (Eq,Ord,Show)
32local :: String -> Canonical
33local x = Canonical Local x
36builtin :: String -> Canonical
37builtin x = Canonical BuiltIn x
40fromModule :: [String] -> String -> Canonical
42 Canonical (Module home) name
45-- VARIABLE RECOGNIZERS
47is :: [String] -> String -> Canonical -> Bool
49 var == Canonical (Module home) name
52isJson :: Canonical -> Bool
54 is ["Json", "Encode"] "Value"
57isMaybe :: Canonical -> Bool
61isArray :: Canonical -> Bool
66isList :: Canonical -> Bool
68 v == Canonical BuiltIn "List"
71isTuple :: Canonical -> Bool
74 Canonical BuiltIn name -> Help.isTuple name
78isPrimitive :: Canonical -> Bool
81 Canonical BuiltIn name -> name `elem` ["Int","Float","String","Bool"]
85isPrim :: String -> Canonical -> Bool
88 Canonical BuiltIn name -> name == prim
92isText :: Canonical -> Bool
100 toString :: a -> String
103instance ToString Raw where
104 toString (Raw name) =
108instance ToString Canonical where
109 toString (Canonical home name) =
112 Module path -> List.intercalate "." (path ++ [name])
118-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
119data Listing a = Listing
122 } deriving (Eq,Ord,Show)
125openListing :: Listing a
130closedListing :: Listing a
135listing :: [a] -> Listing a
140-- | A value that can be imported or exported
144 | Union !String !(Listing String)
145 deriving (Eq,Ord,Show)
148-- CATEGORIZING VALUES
150getValues :: [Value] -> [String]
152 Maybe.mapMaybe getValue values
155getValue :: Value -> Maybe String
158 Value name -> Just name
163getAliases :: [Value] -> [String]
165 Maybe.mapMaybe getAlias values
168getAlias :: Value -> Maybe String
172 Alias name -> Just name
176getUnions :: [Value] -> [(String, Listing String)]
178 Maybe.mapMaybe getUnion values
181getUnion :: Value -> Maybe (String, Listing String)
186 Union name ctors -> Just (name, ctors)
191instance Pretty Raw where
196instance Pretty Canonical where
198 P.text (toString var)
201instance Pretty a => Pretty (Listing a) where
202 pretty (Listing explicits open) =
203 P.parens (commaCat (map pretty explicits ++ dots))
205 dots = [if open then P.text ".." else P.empty]
208instance Pretty Value where
211 Value name -> P.text name
212 Alias name -> P.text name
214 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
217-- BINARY SERIALIZATION
219instance Binary Canonical where
220 put (Canonical home name) =
222 BuiltIn -> putWord8 0 >> put name
223 Module path -> putWord8 1 >> put path >> put name
224 Local -> putWord8 2 >> put name
226 get = do tag <- getWord8
228 0 -> Canonical BuiltIn <$> get
229 1 -> Canonical . Module <$> get <*> get
230 2 -> Canonical Local <$> get
231 _ -> error "Unexpected tag when deserializing canonical variable"
234instance Binary Value where
237 Value name -> putWord8 0 >> put name
238 Alias name -> putWord8 1 >> put name
239 Union name ctors -> putWord8 2 >> put name >> put ctors
241 get = do tag <- getWord8
245 2 -> Union <$> get <*> get
246 _ -> error "Error reading valid import/export information from serialized string"
249instance (Binary a) => Binary (Listing a) where
250 put (Listing explicits open) =
251 put explicits >> put open
253 get = Listing <$> get <*> get
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list