Parse.hs revision 5ed4f9033ede7f17c91c0cc95ac37274038f5281
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance{- |
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceModule : $Header$
81ec673ac5ab1493568d9ef7798b752ab8ee0e61Felix Gabriel ManceCopyright : (c) Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceLicense : GPLv2 or higher, see LICENSE.txt
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceMaintainer : f.mance@jacobs-university.de
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceStability : provisional
5d801400993c9671010d244646936d8fd435638cChristian MaederPortability : portable
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel ManceRDF syntax parser
aa0ca44e856c87db27e61687cbb630f270976da1Felix Gabriel Mance
5d801400993c9671010d244646936d8fd435638cChristian Maeder-}
5d801400993c9671010d244646936d8fd435638cChristian Maeder
5d801400993c9671010d244646936d8fd435638cChristian Maedermodule RDF.Parse where
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceimport Common.Parsec
097bc9f18b722812d480df0f5c634d09cbca8e21Felix Gabriel Manceimport Common.Lexer
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
852bd6145634dc2832b61c44678fe539bc1682d5Christian Maederimport OWL2.AS
090c663fcc1593c66f39a0972326799a672760d5Christian Maederimport OWL2.Parse
f8c3d045dda224e92bf6bcb6288e1ee75ab54d1eChristian Maederimport RDF.AS
feab1106bbee4f2ea2fd48bca7106dd041e4211dFelix Gabriel Manceimport RDF.Symbols
18ff56829e5e99383ee6106584d55bcbd8ed45e7Felix Gabriel Mance
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Manceimport Data.List
b1162cc13e8371724e3382ae6d1cfdeb43891fbbChristian Maederimport qualified Data.Map as Map
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Manceimport Text.ParserCombinators.Parsec
863fa65ac095659c6da1cde7fe7b839f1e7f60f9Felix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance-- * hets symbols parser
f8c3d045dda224e92bf6bcb6288e1ee75ab54d1eChristian Maeder
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancerdfEntityType :: CharParser st RDFEntityType
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel MancerdfEntityType = choice $ map (\ f -> keyword (show f) >> return f)
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Mance rdfEntityTypes
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance{- | parses an entity type (subject, predicate or object) followed by a
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mancecomma separated list of IRIs -}
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancerdfSymbItems :: GenParser Char st SymbItems
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancerdfSymbItems = do
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance ext <- optionMaybe rdfEntityType
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance iris <- rdfSymbs
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance return $ SymbItems ext iris
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Mance
0ec1551231bc5dfdcb3f2bd68fec7457fade7bfdFelix Gabriel Mance-- | parse a comma separated list of uris
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancerdfSymbs :: GenParser Char st [IRI]
852bd6145634dc2832b61c44678fe539bc1682d5Christian MaederrdfSymbs = uriP >>= \ u -> do
968930c7674ae3b63d308bf4fa651400aa263054Christian Maeder commaP `followedWith` uriP
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance us <- rdfSymbs
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance return $ u : us
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance <|> return [u]
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance-- | parse a possibly kinded list of comma separated symbol pairs
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel MancerdfSymbMapItems :: GenParser Char st SymbMapItems
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel MancerdfSymbMapItems = do
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance ext <- optionMaybe rdfEntityType
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance iris <- rdfSymbPairs
68de80eb2800338cbd16512106fcadab79325d8bChristian Maeder return $ SymbMapItems ext iris
68de80eb2800338cbd16512106fcadab79325d8bChristian Maeder
68de80eb2800338cbd16512106fcadab79325d8bChristian Maeder-- | parse a comma separated list of uri pairs
68de80eb2800338cbd16512106fcadab79325d8bChristian MaederrdfSymbPairs :: GenParser Char st [(IRI, Maybe IRI)]
68de80eb2800338cbd16512106fcadab79325d8bChristian MaederrdfSymbPairs = uriPair >>= \ u -> do
68de80eb2800338cbd16512106fcadab79325d8bChristian Maeder commaP `followedWith` uriP
68de80eb2800338cbd16512106fcadab79325d8bChristian Maeder us <- rdfSymbPairs
fc7bd98aabe1bc26058660085e8c77d60a97bcecChristian Maeder return $ u : us
fc7bd98aabe1bc26058660085e8c77d60a97bcecChristian Maeder <|> return [u]
68de80eb2800338cbd16512106fcadab79325d8bChristian Maeder
fc7bd98aabe1bc26058660085e8c77d60a97bcecChristian Maederparse1Base :: CharParser st BaseIRI
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceparse1Base = do
68de80eb2800338cbd16512106fcadab79325d8bChristian Maeder pkeyword "@base"
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance base <- skips uriQ
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance skips $ string "."
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance return $ BaseIRI base
852bd6145634dc2832b61c44678fe539bc1682d5Christian Maeder
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceparse1Prefix :: CharParser st Prefix
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceparse1Prefix = do
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance pkeyword "@prefix"
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance p <- skips (option "" prefix << char ':')
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance i <- skips fullIri
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance skips $ string "."
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance return $ Prefix p i
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Mance
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Manceparse1BaseOrPrefix :: CharParser st (Either BaseIRI Prefix)
544989bc1f6ed4bc0813334ffd934db0fb0010eaFelix Gabriel Manceparse1BaseOrPrefix = do
544989bc1f6ed4bc0813334ffd934db0fb0010eaFelix Gabriel Mance b <- parse1Base
544989bc1f6ed4bc0813334ffd934db0fb0010eaFelix Gabriel Mance return $ Left b
544989bc1f6ed4bc0813334ffd934db0fb0010eaFelix Gabriel Mance <|> do
544989bc1f6ed4bc0813334ffd934db0fb0010eaFelix Gabriel Mance p <- parse1Prefix
f8c3d045dda224e92bf6bcb6288e1ee75ab54d1eChristian Maeder return $ Right p
6856a07e36551ed6fadd7c01e7152a3a28878a6fChristian Maeder
6856a07e36551ed6fadd7c01e7152a3a28878a6fChristian MaederstartsWithScheme :: IRI -> Bool
6856a07e36551ed6fadd7c01e7152a3a28878a6fChristian MaederstartsWithScheme iri = isPrefixOf "//" $ localPart iri
6856a07e36551ed6fadd7c01e7152a3a28878a6fChristian Maeder
f8c3d045dda224e92bf6bcb6288e1ee75ab54d1eChristian MaederbaseStartsWithScheme :: BaseIRI -> Bool
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel MancebaseStartsWithScheme (BaseIRI iri) = startsWithScheme iri
f8c3d045dda224e92bf6bcb6288e1ee75ab54d1eChristian Maeder
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceappendTwoBases :: BaseIRI -> BaseIRI -> BaseIRI
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel ManceappendTwoBases (BaseIRI b1) (BaseIRI b2) =
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance let lpb1 = localPart b1
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance (lp1, ok) = if last lpb1 == '/' then (lpb1, 1) else (lpb1 ++ "/", 0)
852bd6145634dc2832b61c44678fe539bc1682d5Christian Maeder lp = lp1 ++ localPart b2
852bd6145634dc2832b61c44678fe539bc1682d5Christian Maeder exp = if ok == 0 then expandedIRI b1 ++ "/" else expandedIRI b1
852bd6145634dc2832b61c44678fe539bc1682d5Christian Maeder in BaseIRI $ b1 {localPart = lp, expandedIRI = exp ++ localPart b2}
ea3f858eb531d981df3ed00beeadd99cf025adecChristian Maeder
ea3f858eb531d981df3ed00beeadd99cf025adecChristian MaederresolveIRI :: BaseIRI -> IRI -> IRI
ea3f858eb531d981df3ed00beeadd99cf025adecChristian MaederresolveIRI b iri = extractIRI $ appendTwoBases b $ BaseIRI iri
75aaf82c430ad2a5cf159962b1c5c09255010fb4Felix Gabriel Mance
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel ManceextractIRI :: BaseIRI -> IRI
75aaf82c430ad2a5cf159962b1c5c09255010fb4Felix Gabriel ManceextractIRI (BaseIRI b) = b
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel ManceparseBases :: BaseIRI -> TurtlePrefixMap -> CharParser st (BaseIRI, TurtlePrefixMap)
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel ManceparseBases base pm = do
1b1144abf7f95a4b23405b8d5604813cfe7b036aFelix Gabriel Mance e <- parse1BaseOrPrefix
19e01e1a7e319063434bd86c8ecbc5f241ef9993Felix Gabriel Mance case e of
1b1144abf7f95a4b23405b8d5604813cfe7b036aFelix Gabriel Mance Left b ->
5a3ae0a9224276de25e709ef8788c1b9716cd206Christian Maeder if baseStartsWithScheme b || iriType (extractIRI b) /= Full then parseBases b pm
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Mance else parseBases (appendTwoBases base b) pm
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Mance Right p@(Prefix s iri) ->
668c9c725a11c0f77057152148570af853a1bc0dFelix Gabriel Mance if startsWithScheme iri then parseBases base $ Map.insert s iri pm
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance else parseBases base $ Map.insert s (resolveIRI base iri) pm
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance <|> return (base, pm)
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance
1b90322eaf59ded3de24fc891bd67bbd73ec2bfaFelix Gabriel ManceparseIRI :: BaseIRI -> CharParser st IRI
1b90322eaf59ded3de24fc891bd67bbd73ec2bfaFelix Gabriel ManceparseIRI b = do
1b90322eaf59ded3de24fc891bd67bbd73ec2bfaFelix Gabriel Mance iri <- uriQ
1b90322eaf59ded3de24fc891bd67bbd73ec2bfaFelix Gabriel Mance return $ if iriType iri == Full && not (startsWithScheme iri)
1b90322eaf59ded3de24fc891bd67bbd73ec2bfaFelix Gabriel Mance then resolveIRI b iri else iri
1b90322eaf59ded3de24fc891bd67bbd73ec2bfaFelix Gabriel Mance
1b90322eaf59ded3de24fc891bd67bbd73ec2bfaFelix Gabriel ManceparseTerm :: BaseIRI -> CharParser st Term
1b90322eaf59ded3de24fc891bd67bbd73ec2bfaFelix Gabriel ManceparseTerm b = fmap LiteralTerm literal <|> fmap IRITerm (parseIRI b)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance <|> fmap Collection (parensP $ many $ skips $ parseIRI b)
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel ManceparseTriples :: BaseIRI -> TurtlePrefixMap -> String -> CharParser st [Triple]
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel ManceparseTriples base tpm end = do
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance (b, pm) <- parseBases base tpm
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance t <- case end of
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance "." -> do
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance s <- skips $ parseTerm b
9cb6af1a7632f12b60f592ce5eb2ac51e6bd33bbFelix Gabriel Mance p <- skips $ parseTerm b
8af00c8930672188ae80c8829428859160d329d0Felix Gabriel Mance o <- skips $ parseTerm b
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance return $ NTriple s p o
968930c7674ae3b63d308bf4fa651400aa263054Christian Maeder "," -> do
c298a419605037f5352b5ad0f67b3e06db094051Felix Gabriel Mance o <- skips $ parseTerm b
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance return $ AbbreviatedTriple Nothing o
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance ";" -> do
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance p <- skips $ parseTerm b
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance o <- skips $ parseTerm b
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance return $ AbbreviatedTriple (Just p) o
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance sep <- choice $ map (\ s -> skips $ string s >> return s) [".", ",", ";"]
ffa6044b04fa0e31242141ff56a5d80c4233b676Felix Gabriel Mance tl <- parseTriples b pm sep
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance return $ t : tl
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance <|> return []
c77c0efe19dc6556ac872828bfb4cfc5fbca5ac5Felix Gabriel Mance