Parse.hs revision 9c3f6477a95da46a907326206673b4a5c2164164
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett{- |
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettModule : $Header$
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettCopyright : (c) Felix Gabriel Mance
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettLicense : GPLv2 or higher, see LICENSE.txt
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettMaintainer : f.mance@jacobs-university.de
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettStability : provisional
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettPortability : portable
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettRDF syntax parser
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett-}
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblettmodule RDF.Parse where
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport Common.Parsec
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport Common.Lexer
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport OWL2.AS
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport OWL2.Parse
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport RDF.AS
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett-- import RDF.Symbols
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport Network.URI
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport Data.Maybe
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport Data.List
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport qualified Data.Map as Map
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblettimport Text.ParserCombinators.Parsec
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett{-
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett-- * hets symbols parser
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettrdfEntityType :: CharParser st RDFEntityType
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy GimblettrdfEntityType = choice $ map (\ f -> keyword (show f) >> return f)
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett rdfEntityTypes
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett{- | parses an entity type (subject, predicate or object) followed by a
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblettcomma separated list of IRIs -}
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy GimblettrdfSymbItems :: GenParser Char st SymbItems
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettrdfSymbItems = do
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett ext <- optionMaybe rdfEntityType
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett iris <- rdfSymbs
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett return $ SymbItems ext iris
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett-- | parse a comma separated list of uris
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettrdfSymbs :: GenParser Char st [IRI]
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettrdfSymbs = uriP >>= \ u -> do
aa0d5f8be9950e788884f7431cf4cb7bee74788cAndy Gimblett commaP `followedWith` uriP
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett us <- rdfSymbs
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett return $ u : us
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett <|> return [u]
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett-- | parse a possibly kinded list of comma separated symbol pairs
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettrdfSymbMapItems :: GenParser Char st SymbMapItems
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettrdfSymbMapItems = do
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett ext <- optionMaybe rdfEntityType
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett iris <- rdfSymbPairs
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett return $ SymbMapItems ext iris
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett-- | parse a comma separated list of uri pairs
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettrdfSymbPairs :: GenParser Char st [(IRI, Maybe IRI)]
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettrdfSymbPairs = uriPair >>= \ u -> do
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett commaP `followedWith` uriP
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett us <- rdfSymbPairs
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett return $ u : us
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett <|> return [u]
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett-}
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparseBase :: CharParser st Statement
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparseBase = do
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett pkeyword "@base"
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett base <- skips uriQ
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy Gimblett skips $ string "."
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy Gimblett return $ Base base
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy Gimblett
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy GimblettparsePrefix :: CharParser st Statement
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy GimblettparsePrefix = do
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy Gimblett pkeyword "@prefix"
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy Gimblett p <- skips (option "" prefix << char ':')
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy Gimblett i <- skips uriQ
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy Gimblett skips $ string "."
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy Gimblett return $ Prefix p i
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparsePredicate :: CharParser st Predicate
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparsePredicate = fmap Predicate $ skips uriQ
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparseSubject :: CharParser st Subject
b34e5090387d45b3a35f88eaa23477a83d2a2962Andy GimblettparseSubject =
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett fmap Subject (skips uriQ)
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett <|> fmap SubjectList
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett (between (skips $ char '[') (skips $ char ']') $ skips parsePredObjList)
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett <|> fmap SubjectCollection
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett (between (skips $ char '(') (skips $ char ')') $ many parseObject)
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparseObject :: CharParser st Object
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparseObject = fmap ObjectLiteral literal <|> fmap Object parseSubject
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparsePredObjects :: CharParser st PredicateObjectList
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparsePredObjects = do
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett pred <- parsePredicate
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett objs <- sepBy parseObject $ skips $ char ','
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett return $ PredicateObjectList pred objs
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparsePredObjList :: CharParser st [PredicateObjectList]
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettparsePredObjList = sepEndBy parsePredObjects $ skips $ char ';'
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett