Description : library names for HetCASL and development graphs
Copyright : (c) Christian Maeder, DFKI GmbH 2008
License : GPLv2 or higher
Maintainer : Christian.Maeder@dfki.de
Abstract syntax of HetCASL specification libraries
Follows Sect. II:2.2.5 of the CASL Reference Manual.
mkQualName :: SIMPLE_ID -> LibId -> Id -> Id
mkQualName nodeId libId i =
Id omTs [i, simpleIdToId nodeId, libIdToId libId] $ posOfId i
isQualNameFrom :: SIMPLE_ID -> LibId -> Id -> Bool
isQualNameFrom nodeId libId i@(Id _ cs _) = case cs of
_ : n : l : _ | isQualName i ->
n == simpleIdToId nodeId && libIdToId libId == l
isQualName (Id ts cs _) = case cs of
libIdOfQualName :: Id -> Id
libIdOfQualName j@(Id _ cs _) = case cs of
[_, _, i] | isQualName j -> i
_ -> error "libIdOfQualName: Check by isQualName before calling getLibId!"
getNodeId j@(Id _ cs _) = case cs of
[_, i, _] | isQualName j -> i
_ -> error "Check by isQualName before calling getNodeId!"
unQualName j@(Id _ cs _) = case cs of
i : _ | isQualName j -> i
path = splitOn '/' $ show li
toTok s = Token s $ getRange li
in mkId $ map toTok $ intersperse "/" path
, libVersion :: Maybe VersionNumber }
emptyLibName :: String -> LibName
emptyLibName s = LibName (IndirectLink s nullRange "" noTime) Nothing
| IndirectLink PATH Range FilePath ClockTime
-- | Returns the LibId of a LibName
getModTime :: LibId -> ClockTime
getModTime li = case li of
IndirectLink _ _ _ m -> m
updFilePathOfLibId :: FilePath -> ClockTime -> LibId -> LibId
updFilePathOfLibId fp mt li = case li of
IndirectLink p r _ _ -> IndirectLink p r fp mt
setFilePath :: FilePath -> ClockTime -> LibName -> LibName
ln { getLibId = updFilePathOfLibId fp mt $ getLibId ln }
getFilePath :: LibName -> FilePath
if null fp then error "getFilePath: empty fp in " ++ n else fp
_ -> error "getFilePath: No IndirectLink"
data VersionNumber = VersionNumber [String] Range
-- pos: "version", start of first string
instance GetRange LibId where
IndirectLink _ r _ _ -> r
instance Show LibId where
IndirectLink s1 _ _ _ -> s1
instance GetRange LibName where
getRange = getRange . getLibId
instance Show LibName where
show = show . hsep . prettyLibName
prettyVersionNumber :: VersionNumber -> [Doc]
prettyVersionNumber (VersionNumber v _) =
[keyword versionS, hcat $ punctuate dot $ map codeToken v]
prettyLibName :: LibName -> [Doc]
prettyLibName (LibName i mv) = pretty i : case mv of
Just v -> prettyVersionNumber v
DirectLink s1 _ == DirectLink s2 _ = s1 == s2
IndirectLink s1 _ _ _ == IndirectLink s2 _ _ _ = s1 == s2
DirectLink s1 _ <= DirectLink s2 _ = s1 <= s2
IndirectLink s1 _ _ _ <= IndirectLink s2 _ _ _ = s1 <= s2
DirectLink _ _ <= _ = True
IndirectLink _ _ _ _ <= _ = False
instance Eq LibName where
ln1 == ln2 = compare ln1 ln2 == EQ
instance Ord LibName where
compare ln1 ln2 = compare (getLibId ln1) $ getLibId ln2
instance Pretty LibName where
pretty = fsep . prettyLibName
instance Pretty LibId where
-- The Int type is used to represent Node, which is a typesynonym for Int.
-- We can't use Node here
data LinkPath a = LinkPath a [(LibId, Node)] deriving (Ord, Eq)
type SLinkPath = LinkPath String
showSLinkPath :: SLinkPath -> String
showSLinkPath (LinkPath x l) = s l where
s ((_, n) : l1) = show n ++ "/" ++ s l1
instance Show a => Show (LinkPath a) where
show (LinkPath x l) = showSLinkPath $ LinkPath (show x) l
instance Functor LinkPath where
fmap f (LinkPath x l) = LinkPath (f x) l
addToPath :: LibId -> Node -> LinkPath a -> LinkPath a
addToPath libid n (LinkPath x l) = LinkPath x $ (libid, n) : l
initPath :: LibId -> Node -> a -> LinkPath a
initPath libid n x = LinkPath x [(libid, n)]