Parse_AS_Library.hs revision 9d497e0c39b5934506691f80983ecf1c6ea4c7b5
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseModule : $Header$
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseCopyright : (c) Maciek Makowski, Uni Bremen 2002-2004
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseLicence : similar to LGPL, see HetCATS/LICENCE.txt or LIZENZ.txt
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseMaintainer : hets@tzi.de
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseStability : provisional
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrsePortability : non-portable(Grothendieck)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse Parsing the specification library.
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse Follows Sect. II:3.1.5 of the CASL Reference Manual.
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse from 25 March 2001
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse C.2.4 Specification Libraries
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse - architectural specs
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse - move structured parsing from libItem to Parse_AS_Structured (?)
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rseimport Common.Id(tokPos)
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rseimport Data.List(intersperse)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseimport Data.Maybe(maybeToList)
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse------------------------------------------------------------------------
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse-- * Parsing functions
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse-- | Parse a library of specifications
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rselibrary :: (AnyLogic, LogicGraph) -> AParser AnyLogic LIB_DEFN
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rselibrary l = do (ps, ln) <- option ([], Lib_id $ Indirect_link libraryS [])
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse (do s1 <- asKey libraryS -- 'library' keyword
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse n <- libName -- library name
d86ef5503dcbc38e87c0e03cd3e1f16458cb6323rse return ([tokPos s1], n))
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse an <- annos -- annotations
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ls <- libItems l -- library elements
6ace32dacb8313226eb9019275d0e4fa45a15148rse return (Lib_defn ln ls ps an)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse-- | Parse library name
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrselibName :: AParser st LIB_NAME
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrselibName = do libid <- libId
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse v <- option Nothing (fmap Just version)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (case v of
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse Nothing -> Lib_id libid
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse Just v1 -> Lib_version libid v1)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse-- | Parse the library version
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseversion :: AParser st VERSION_NUMBER
d1bb6e2664788e0437acc18e877562c9a796d7cerseversion = do s <- asKey versionS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse pos <- getPos
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse n <- many1 digit `sepBy1` (string ".")
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (Version_number n ([tokPos s, pos]))
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse-- | Parse library ID
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrselibId :: AParser st LIB_ID
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrselibId = do pos <- getPos
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse path <- scanAnyWords `sepBy1` (string "/")
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (Indirect_link (concat (intersperse "/" path)) [pos])
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse -- ??? URL need to be added
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse-- | Parse the library elements
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrselibItems :: (AnyLogic, LogicGraph) -> AParser AnyLogic [Annoted LIB_ITEM]
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrselibItems l@(_, lG) =
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (eof >> return [])
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (r, newlog) <- libItem l
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse an <- annos
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse is <- libItems (newlog, lG)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return ((Annoted r [] [] an) : is)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse-- | Parse an element of the library
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrselibItem :: (AnyLogic, LogicGraph) -> AParser AnyLogic (LIB_ITEM, AnyLogic)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrselibItem l@(lgc, lG) =
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse -- spec defn
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse do s <- asKey specS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse n <- simpleId
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse g <- generics l
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse e <- asKey equalS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse a <- aSpec l
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse q <- optEnd
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (map tokPos ([s, e] ++ maybeToList q)), lgc)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse <|> -- view defn
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse do s1 <- asKey viewS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse vn <- simpleId
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse g <- generics l
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse s2 <- asKey ":"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse vt <- viewType l
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (symbMap,ps) <- option ([],[])
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (do s <- asKey equalS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (m, _, _) <- parseMapping l
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (m,[s]))
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse q <- optEnd
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (Syntax.AS_Library.View_defn vn g vt symbMap
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (map tokPos ([s1, s2] ++ ps ++ maybeToList q)), lgc)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse <|> -- unit spec
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse do kUnit <- asKey unitS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse kSpec <- asKey specS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse name <- simpleId
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse kEqu <- asKey equalS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse usp <- unitSpec l
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse kEnd <- optEnd
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (map tokPos ([kUnit, kSpec, kEqu] ++ maybeToList kEnd)),
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse <|> -- arch spec
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse do kArch <- asKey archS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse kSpec <- asKey specS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse name <- simpleId
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse kEqu <- asKey equalS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse asp <- annotedArchSpec l
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse kEnd <- optEnd
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (map tokPos ([kArch, kSpec, kEqu] ++ maybeToList kEnd)),
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse <|> -- download
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse do s1 <- asKey fromS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse ln <- libName
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse s2 <- asKey getS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (il,ps) <- itemNameOrMap `separatedBy` anComma
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse q <- optEnd
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (Download_items ln il
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (map tokPos ([s1, s2] ++ ps ++ maybeToList q)), lgc)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse <|> -- logic
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse do s1 <- asKey logicS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse logN@(Logic_name t _) <- logicName
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse newLog <- lookupLogicName logN lG
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (Logic_decl logN (map tokPos [s1,t]), newLog)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse do a <- aSpec l
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (Syntax.AS_Library.Spec_defn (mkSimpleId specS)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse (Genericity (Params []) (Imported []) []) a [], lgc)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse-- | Parse view type
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseviewType :: (AnyLogic, LogicGraph) -> AParser AnyLogic VIEW_TYPE
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseviewType l = do sp1 <- annoParser (groupSpec l)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse s <- asKey toS
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse sp2 <- annoParser (groupSpec l)
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (View_type sp1 sp2 [tokPos s])
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse-- | Parse item name or name map
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseitemNameOrMap :: AParser st ITEM_NAME_OR_MAP
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrseitemNameOrMap = do i1 <- simpleId
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse i' <- option Nothing (do
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse s <- asKey "|->"
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse i <- simpleId
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (Just (i,s)))
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse return (case i' of
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse Nothing -> Item_name i1
cc003103e52ff9d5fe9bed567ef9438613ab4fbfrse Just (i2,s) -> Item_name_map i1 i2 [tokPos s])