{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Swish.RDF.Parser.N3
( ParseResult
, parseN3
, parseN3fromText
, parseAnyfromText
, parseTextFromText, parseAltFromText
, parseNameFromText
, parseAbsURIrefFromText, parseLexURIrefFromText, parseURIref2FromText
, N3Parser, N3State(..), SpecialMap
, getPrefix
, n3symbol
, quickVariable
, lexUriRef
, document, subgraph
, newBlankNode
)
where
import Swish.GraphClass (arc)
import Swish.Namespace
( Namespace
, ScopedName
, makeNamespace
, getNamespaceTuple
, getScopeNamespace
, getScopedNameURI
, getScopeNamespace
, makeURIScopedName
, makeQNameScopedName
, makeNSScopedName
, nullScopedName
)
import Swish.QName (QName, newLName)
import Swish.RDF.Graph
( RDFGraph, RDFLabel(..)
, ToRDFLabel(..)
, NamespaceMap
, LookupFormula(..)
, addArc
, setFormula
, setNamespaces
, emptyRDFGraph
)
import Swish.RDF.Datatype (makeDatatypedLiteral)
import Swish.RDF.Vocabulary
( LanguageTag
, toLangTag
, rdfType
, rdfFirst, rdfRest, rdfNil
, owlSameAs, logImplies
, xsdBoolean, xsdInteger, xsdDecimal, xsdDouble
)
import Swish.RDF.Parser.Utils
( SpecialMap
, ParseResult
, runParserWithError
, prefixTable
, specialTable
, ignore
, notFollowedBy
, endBy
, sepEndBy
, noneOf
, char
, ichar
, string
, stringT
, symbol
, lexeme
, whiteSpace
, hex4
, hex8
, appendURIs
)
import Control.Applicative
import Control.Monad (forM_, foldM)
import Data.Char (isSpace, isDigit, ord, isAsciiLower)
#if MIN_VERSION_base(4, 7, 0)
import Data.Functor (($>))
#endif
import Data.Maybe (fromMaybe, fromJust)
import Data.Word (Word32)
import Network.URI (URI(..), parseURIReference)
import Text.ParserCombinators.Poly.StateText
import qualified Data.Map as M
import qualified Data.Text as T
import qualified Data.Text.Lazy as L
#if !MIN_VERSION_base(4, 7, 0)
($>) :: Functor f => f a -> b -> f b
($>) = flip (<$)
#endif
data N3State = N3State
{ N3State -> RDFGraph
graphState :: RDFGraph
, N3State -> RDFLabel
thisNode :: RDFLabel
, N3State -> NamespaceMap
prefixUris :: NamespaceMap
, N3State -> SpecialMap
syntaxUris :: SpecialMap
, N3State -> Word32
nodeGen :: Word32
, N3State -> [Text]
keywordsList :: [T.Text]
, N3State -> Bool
allowLocalNames :: Bool
}
setPrefix :: Maybe T.Text -> URI -> N3State -> N3State
setPrefix :: Maybe Text -> URI -> N3State -> N3State
setPrefix Maybe Text
pre URI
uri N3State
st = N3State
st { prefixUris :: NamespaceMap
prefixUris=NamespaceMap
p' }
where
p' :: NamespaceMap
p' = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Maybe Text
pre URI
uri (N3State -> NamespaceMap
prefixUris N3State
st)
setSName :: String -> ScopedName -> N3State -> N3State
setSName :: [Char] -> ScopedName -> N3State -> N3State
setSName [Char]
nam ScopedName
snam N3State
st = N3State
st { syntaxUris :: SpecialMap
syntaxUris=SpecialMap
s' }
where
s' :: SpecialMap
s' = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert [Char]
nam ScopedName
snam (N3State -> SpecialMap
syntaxUris N3State
st)
setSUri :: String -> URI -> N3State -> N3State
setSUri :: [Char] -> URI -> N3State -> N3State
setSUri [Char]
nam = [Char] -> ScopedName -> N3State -> N3State
setSName [Char]
nam forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> ScopedName
makeURIScopedName
setKeywordsList :: [T.Text] -> N3State -> N3State
setKeywordsList :: [Text] -> N3State -> N3State
setKeywordsList [Text]
ks N3State
st = N3State
st { keywordsList :: [Text]
keywordsList = [Text]
ks, allowLocalNames :: Bool
allowLocalNames = Bool
True }
getSName :: N3State -> String -> ScopedName
getSName :: N3State -> [Char] -> ScopedName
getSName N3State
st [Char]
nam = forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault ScopedName
nullScopedName [Char]
nam forall a b. (a -> b) -> a -> b
$ N3State -> SpecialMap
syntaxUris N3State
st
getSUri :: N3State -> String -> URI
getSUri :: N3State -> [Char] -> URI
getSUri N3State
st [Char]
nam = ScopedName -> URI
getScopedNameURI forall a b. (a -> b) -> a -> b
$ N3State -> [Char] -> ScopedName
getSName N3State
st [Char]
nam
getPrefixURI :: N3State -> Maybe T.Text -> Maybe URI
getPrefixURI :: N3State -> Maybe Text -> Maybe URI
getPrefixURI N3State
st Maybe Text
pre = forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Maybe Text
pre (N3State -> NamespaceMap
prefixUris N3State
st)
getKeywordsList :: N3State -> [T.Text]
getKeywordsList :: N3State -> [Text]
getKeywordsList = N3State -> [Text]
keywordsList
getAllowLocalNames :: N3State -> Bool
getAllowLocalNames :: N3State -> Bool
getAllowLocalNames = N3State -> Bool
allowLocalNames
updateGraph :: (RDFGraph -> RDFGraph) -> N3State -> N3State
updateGraph :: (RDFGraph -> RDFGraph) -> N3State -> N3State
updateGraph RDFGraph -> RDFGraph
f N3State
s = N3State
s { graphState :: RDFGraph
graphState = RDFGraph -> RDFGraph
f (N3State -> RDFGraph
graphState N3State
s) }
type N3Parser a = Parser N3State a
parseN3fromText ::
L.Text
-> ParseResult
parseN3fromText :: Text -> ParseResult
parseN3fromText = forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Maybe QName -> ParseResult
parseN3 forall a. Maybe a
Nothing
parseN3 ::
L.Text
-> Maybe QName
-> ParseResult
parseN3 :: Text -> Maybe QName -> ParseResult
parseN3 Text
txt Maybe QName
mbase = forall a. N3Parser a -> Maybe QName -> Text -> Either [Char] a
parseAnyfromText N3Parser RDFGraph
document Maybe QName
mbase Text
txt
hashURI :: URI
hashURI :: URI
hashURI = forall a. HasCallStack => Maybe a -> a
fromJust forall a b. (a -> b) -> a -> b
$ [Char] -> Maybe URI
parseURIReference [Char]
"#"
emptyState ::
Maybe QName
-> N3State
emptyState :: Maybe QName -> N3State
emptyState Maybe QName
mbase =
let pmap :: Map (Maybe a) URI
pmap = forall k a. k -> a -> Map k a
M.singleton forall a. Maybe a
Nothing URI
hashURI
muri :: Maybe ScopedName
muri = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text -> QName -> ScopedName
makeQNameScopedName forall a. Maybe a
Nothing) Maybe QName
mbase
smap :: SpecialMap
smap = forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall a b. (a -> b) -> a -> b
$ Maybe ScopedName -> [([Char], ScopedName)]
specialTable Maybe ScopedName
muri
in N3State
{ graphState :: RDFGraph
graphState = RDFGraph
emptyRDFGraph
, thisNode :: RDFLabel
thisNode = RDFLabel
NoNode
, prefixUris :: NamespaceMap
prefixUris = forall {a}. Map (Maybe a) URI
pmap
, syntaxUris :: SpecialMap
syntaxUris = SpecialMap
smap
, nodeGen :: Word32
nodeGen = Word32
0
, keywordsList :: [Text]
keywordsList = [Text
"a", Text
"is", Text
"of", Text
"true", Text
"false"]
, allowLocalNames :: Bool
allowLocalNames = Bool
False
}
parseAnyfromText :: N3Parser a
-> Maybe QName
-> L.Text
-> Either String a
parseAnyfromText :: forall a. N3Parser a -> Maybe QName -> Text -> Either [Char] a
parseAnyfromText N3Parser a
parser Maybe QName
mbase = forall a b. Parser a b -> a -> Text -> Either [Char] b
runParserWithError N3Parser a
parser (Maybe QName -> N3State
emptyState Maybe QName
mbase)
newBlankNode :: N3Parser RDFLabel
newBlankNode :: N3Parser RDFLabel
newBlankNode = do
Word32
n <- forall s a. (s -> a) -> Parser s a
stQuery (forall a. Enum a => a -> a
succ forall b c a. (b -> c) -> (a -> b) -> a -> c
. N3State -> Word32
nodeGen)
forall s. (s -> s) -> Parser s ()
stUpdate forall a b. (a -> b) -> a -> b
$ \N3State
s -> N3State
s { nodeGen :: Word32
nodeGen = Word32
n }
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Char] -> RDFLabel
Blank (forall a. Show a => a -> [Char]
show Word32
n)
parseTextFromText :: String -> L.Text -> Either String String
parseTextFromText :: [Char] -> Text -> Either [Char] [Char]
parseTextFromText [Char]
s =
forall a. N3Parser a -> Maybe QName -> Text -> Either [Char] a
parseAnyfromText (forall s. [Char] -> Parser s [Char]
string [Char]
s) forall a. Maybe a
Nothing
parseAltFromText :: String -> String -> L.Text -> Either String String
parseAltFromText :: [Char] -> [Char] -> Text -> Either [Char] [Char]
parseAltFromText [Char]
s1 [Char]
s2 =
forall a. N3Parser a -> Maybe QName -> Text -> Either [Char] a
parseAnyfromText (forall s. [Char] -> Parser s [Char]
string [Char]
s1 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall s. [Char] -> Parser s [Char]
string [Char]
s2) forall a. Maybe a
Nothing
parseNameFromText :: L.Text -> Either String String
parseNameFromText :: Text -> Either [Char] [Char]
parseNameFromText =
forall a. N3Parser a -> Maybe QName -> Text -> Either [Char] a
parseAnyfromText Parser N3State [Char]
n3NameStr forall a. Maybe a
Nothing
addTestPrefixes :: N3Parser ()
addTestPrefixes :: Parser N3State ()
addTestPrefixes = forall s. (s -> s) -> Parser s ()
stUpdate forall a b. (a -> b) -> a -> b
$ \N3State
st -> N3State
st { prefixUris :: NamespaceMap
prefixUris =
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Namespace -> (Maybe Text, URI)
getNamespaceTuple [Namespace]
prefixTable
}
parseAbsURIrefFromText :: L.Text -> Either String URI
parseAbsURIrefFromText :: Text -> Either [Char] URI
parseAbsURIrefFromText =
forall a. N3Parser a -> Maybe QName -> Text -> Either [Char] a
parseAnyfromText N3Parser URI
explicitURI forall a. Maybe a
Nothing
parseLexURIrefFromText :: L.Text -> Either String URI
parseLexURIrefFromText :: Text -> Either [Char] URI
parseLexURIrefFromText =
forall a. N3Parser a -> Maybe QName -> Text -> Either [Char] a
parseAnyfromText N3Parser URI
lexUriRef forall a. Maybe a
Nothing
parseURIref2FromText :: L.Text -> Either String ScopedName
parseURIref2FromText :: Text -> Either [Char] ScopedName
parseURIref2FromText =
forall a. N3Parser a -> Maybe QName -> Text -> Either [Char] a
parseAnyfromText (Parser N3State ()
addTestPrefixes forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser N3State ScopedName
n3symbol) forall a. Maybe a
Nothing
comma, semiColon , fullStop :: N3Parser ()
comma :: Parser N3State ()
comma = forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall s. [Char] -> Parser s [Char]
symbol [Char]
","
semiColon :: Parser N3State ()
semiColon = forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall s. [Char] -> Parser s [Char]
symbol [Char]
";"
fullStop :: Parser N3State ()
fullStop = forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall s. [Char] -> Parser s [Char]
symbol [Char]
"."
br :: String -> String -> N3Parser a -> N3Parser a
br :: forall a. [Char] -> [Char] -> N3Parser a -> N3Parser a
br [Char]
lsym [Char]
rsym = forall (p :: * -> *) bra ket a.
PolyParse p =>
p bra -> p ket -> p a -> p a
bracket (forall s. [Char] -> Parser s [Char]
symbol [Char]
lsym) (forall s. [Char] -> Parser s [Char]
symbol [Char]
rsym)
between :: Parser s lbr -> Parser s rbr -> Parser s a -> Parser s a
between :: forall s lbr rbr a.
Parser s lbr -> Parser s rbr -> Parser s a -> Parser s a
between = forall (p :: * -> *) bra ket a.
PolyParse p =>
p bra -> p ket -> p a -> p a
bracket
atSign :: T.Text -> N3Parser ()
atSign :: Text -> Parser N3State ()
atSign Text
s = do
N3State
st <- forall s. Parser s s
stGet
let p :: Parser s ()
p = forall s. Char -> Parser s ()
ichar Char
'@'
if Text
s forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` N3State -> [Text]
getKeywordsList N3State
st
then forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall {s}. Parser s ()
p
else forall {s}. Parser s ()
p
atWord :: T.Text -> N3Parser T.Text
atWord :: Text -> N3Parser Text
atWord Text
s = do
Text -> Parser N3State ()
atSign Text
s
forall s a. Parser s a -> Parser s a
lexeme forall a b. (a -> b) -> a -> b
$ forall s. Text -> Parser s Text
stringT Text
s forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall s. (Char -> Bool) -> Parser s ()
notFollowedBy (forall a. Eq a => a -> a -> Bool
== Char
':')
forall (m :: * -> *) a. Monad m => a -> m a
return Text
s
operatorLabel :: ScopedName -> N3Parser RDFLabel
operatorLabel :: ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
snam = do
N3State
st <- forall s. Parser s s
stGet
let (Maybe Text
pkey, URI
pval) = Namespace -> (Maybe Text, URI)
getNamespaceTuple forall a b. (a -> b) -> a -> b
$ ScopedName -> Namespace
getScopeNamespace ScopedName
snam
opmap :: NamespaceMap
opmap = N3State -> NamespaceMap
prefixUris N3State
st
rval :: RDFLabel
rval = ScopedName -> RDFLabel
Res ScopedName
snam
case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Maybe Text
pkey NamespaceMap
opmap of
Just URI
val | URI
val forall a. Eq a => a -> a -> Bool
== URI
pval -> forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
rval
| Bool
otherwise -> do
forall s. (s -> s) -> Parser s ()
stUpdate forall a b. (a -> b) -> a -> b
$ \N3State
s -> N3State
s { prefixUris :: NamespaceMap
prefixUris = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Maybe Text
pkey URI
pval NamespaceMap
opmap }
forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
rval
Maybe URI
_ -> do
forall s. (s -> s) -> Parser s ()
stUpdate forall a b. (a -> b) -> a -> b
$ \N3State
s -> N3State
s { prefixUris :: NamespaceMap
prefixUris = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Maybe Text
pkey URI
pval NamespaceMap
opmap }
forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
rval
type AddStatement = RDFLabel -> N3Parser ()
addStatement :: RDFLabel -> RDFLabel -> AddStatement
addStatement :: RDFLabel -> RDFLabel -> AddStatement
addStatement RDFLabel
s RDFLabel
p o :: RDFLabel
o@(TypedLit Text
_ ScopedName
dtype) | ScopedName
dtype forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ScopedName
xsdBoolean, ScopedName
xsdInteger, ScopedName
xsdDecimal, ScopedName
xsdDouble] = do
N3State
ost <- forall s. Parser s s
stGet
let stmt :: Arc RDFLabel
stmt = forall lb. lb -> lb -> lb -> Arc lb
arc RDFLabel
s RDFLabel
p RDFLabel
o
oldp :: NamespaceMap
oldp = N3State -> NamespaceMap
prefixUris N3State
ost
ogs :: RDFGraph
ogs = N3State -> RDFGraph
graphState N3State
ost
(Maybe Text
ns, URI
uri) = Namespace -> (Maybe Text, URI)
getNamespaceTuple forall a b. (a -> b) -> a -> b
$ ScopedName -> Namespace
getScopeNamespace ScopedName
dtype
newp :: NamespaceMap
newp = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Maybe Text
ns URI
uri NamespaceMap
oldp
forall s. (s -> s) -> Parser s ()
stUpdate forall a b. (a -> b) -> a -> b
$ \N3State
st -> N3State
st { prefixUris :: NamespaceMap
prefixUris = NamespaceMap
newp, graphState :: RDFGraph
graphState = forall lb. Label lb => Arc lb -> NSGraph lb -> NSGraph lb
addArc Arc RDFLabel
stmt RDFGraph
ogs }
addStatement RDFLabel
s RDFLabel
p RDFLabel
o = forall s. (s -> s) -> Parser s ()
stUpdate ((RDFGraph -> RDFGraph) -> N3State -> N3State
updateGraph (forall lb. Label lb => Arc lb -> NSGraph lb -> NSGraph lb
addArc (forall lb. lb -> lb -> lb -> Arc lb
arc RDFLabel
s RDFLabel
p RDFLabel
o) ))
addStatementRev :: RDFLabel -> RDFLabel -> AddStatement
addStatementRev :: RDFLabel -> RDFLabel -> AddStatement
addStatementRev RDFLabel
o RDFLabel
p RDFLabel
s = RDFLabel -> RDFLabel -> AddStatement
addStatement RDFLabel
s RDFLabel
p RDFLabel
o
isaz, is09, isaz09 :: Char -> Bool
isaz :: Char -> Bool
isaz = Char -> Bool
isAsciiLower
is09 :: Char -> Bool
is09 = Char -> Bool
isDigit
isaz09 :: Char -> Bool
isaz09 Char
c = Char -> Bool
isaz Char
c Bool -> Bool -> Bool
|| Char -> Bool
is09 Char
c
match :: (Ord a) => a -> [(a,a)] -> Bool
match :: forall a. Ord a => a -> [(a, a)] -> Bool
match a
v = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\(a
l,a
h) -> a
v forall a. Ord a => a -> a -> Bool
>= a
l Bool -> Bool -> Bool
&& a
v forall a. Ord a => a -> a -> Bool
<= a
h)
startChar :: Char -> Bool
startChar :: Char -> Bool
startChar Char
c = let i :: Int
i = Char -> Int
ord Char
c
in Char
c forall a. Eq a => a -> a -> Bool
== Char
'_' Bool -> Bool -> Bool
||
forall a. Ord a => a -> [(a, a)] -> Bool
match Char
c [(Char
'A', Char
'Z'), (Char
'a', Char
'z')] Bool -> Bool -> Bool
||
forall a. Ord a => a -> [(a, a)] -> Bool
match Int
i [(Int
0x00c0, Int
0x00d6), (Int
0x00d8, Int
0x00f6), (Int
0x00f8, Int
0x02ff),
(Int
0x0370, Int
0x037d),
(Int
0x037f, Int
0x1fff), (Int
0x200c, Int
0x200d),
(Int
0x2070, Int
0x218f), (Int
0x2c00, Int
0x2fef), (Int
0x3001, Int
0xd7ff),
(Int
0xf900, Int
0xfdcf), (Int
0xfdf0, Int
0xfffd),
(Int
0x00010000, Int
0x000effff)]
inBody :: Char -> Bool
inBody :: Char -> Bool
inBody Char
c = let i :: Int
i = Char -> Int
ord Char
c
in Char
c forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ([Char]
"-_"::String) Bool -> Bool -> Bool
|| Int
i forall a. Eq a => a -> a -> Bool
== Int
0x007 Bool -> Bool -> Bool
||
forall a. Ord a => a -> [(a, a)] -> Bool
match Char
c [(Char
'0', Char
'9'), (Char
'A', Char
'Z'), (Char
'a', Char
'z')] Bool -> Bool -> Bool
||
forall a. Ord a => a -> [(a, a)] -> Bool
match Int
i [(Int
0x00c0, Int
0x00d6), (Int
0x00d8, Int
0x00f6), (Int
0x00f8, Int
0x037d),
(Int
0x037f, Int
0x1fff), (Int
0x200c, Int
0x200d), (Int
0x203f, Int
0x2040),
(Int
0x2070, Int
0x218f), (Int
0x2c00, Int
0x2fef), (Int
0x3001, Int
0xd7ff),
(Int
0xf900, Int
0xfdcf), (Int
0xfdf0, Int
0xfffd),
(Int
0x00010000, Int
0x000effff)]
n3Name :: N3Parser T.Text
n3Name :: N3Parser Text
n3Name = Char -> Text -> Text
T.cons forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall {s}. Parser s Char
n3Init forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall {s}. Parser s Text
n3Body
where
n3Init :: Parser s Char
n3Init = forall s. (Char -> Bool) -> Parser s Char
satisfy Char -> Bool
startChar
n3Body :: Parser s Text
n3Body = Text -> Text
L.toStrict forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. (Char -> Bool) -> Parser s Text
manySatisfy Char -> Bool
inBody
n3NameStr :: N3Parser String
n3NameStr :: Parser N3State [Char]
n3NameStr = Text -> [Char]
T.unpack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> N3Parser Text
n3Name
quickVariable :: N3Parser RDFLabel
quickVariable :: N3Parser RDFLabel
quickVariable = forall s. Char -> Parser s Char
char Char
'?' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ([Char] -> RDFLabel
Var forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser N3State [Char]
n3NameStr)
n3string :: N3Parser T.Text
n3string :: N3Parser Text
n3string = N3Parser Text
tripleQuoted forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> N3Parser Text
singleQuoted
digit :: N3Parser Char
digit :: N3Parser Char
digit = forall s. (Char -> Bool) -> Parser s Char
satisfy Char -> Bool
isDigit
protectedChar :: N3Parser Char
protectedChar :: N3Parser Char
protectedChar =
(forall s. Char -> Parser s Char
char Char
't' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\t')
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall s. Char -> Parser s Char
char Char
'n' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\n')
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall s. Char -> Parser s Char
char Char
'r' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\r')
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall s. Char -> Parser s Char
char Char
'"' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'"')
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall s. Char -> Parser s Char
char Char
'\'' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\'')
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall s. Char -> Parser s Char
char Char
'\\' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\\')
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall s. Char -> Parser s Char
char Char
'u' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall {s}. Parser s Char
hex4)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall s. Char -> Parser s Char
char Char
'U' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall {s}. Parser s Char
hex8)
n3Character :: N3Parser Char
n3Character :: N3Parser Char
n3Character =
(forall s. Char -> Parser s Char
char Char
'\\' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (N3Parser Char
protectedChar forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *) a. Monad m => a -> m a
return Char
'\\'))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall s. [Char] -> Parser s Char
noneOf [Char]
"\"\n"
sQuot :: N3Parser Char
sQuot :: N3Parser Char
sQuot = forall s. Char -> Parser s Char
char Char
'"'
singleQuoted :: N3Parser T.Text
singleQuoted :: N3Parser Text
singleQuoted = [Char] -> Text
T.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (p :: * -> *) bra ket a.
PolyParse p =>
p bra -> p ket -> p a -> p a
bracket N3Parser Char
sQuot N3Parser Char
sQuot (forall (f :: * -> *) a. Alternative f => f a -> f [a]
many N3Parser Char
n3Character)
tripleQuoted :: N3Parser T.Text
tripleQuoted :: N3Parser Text
tripleQuoted =
let sep :: Parser N3State [Char]
sep = forall (p :: * -> *) a. PolyParse p => Int -> p a -> p [a]
exactly Int
3 N3Parser Char
sQuot
in [Text] -> Text
T.concat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (p :: * -> *) bra ket a.
PolyParse p =>
p bra -> p ket -> p a -> p a
bracket Parser N3State [Char]
sep Parser N3State [Char]
sep (forall (f :: * -> *) a. Alternative f => f a -> f [a]
many N3Parser Text
_tCharsLong)
oneOrTwo :: N3Parser T.Text
oneOrTwo :: N3Parser Text
oneOrTwo = do
forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall s. Char -> Parser s Char
char Char
'"'
Maybe Char
mb <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (forall s. Char -> Parser s Char
char Char
'"')
case Maybe Char
mb of
Just Char
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Text
"\"\""
Maybe Char
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Text
"\""
_multiQuote :: N3Parser T.Text
_multiQuote :: N3Parser Text
_multiQuote = do
Maybe Text
mq <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional N3Parser Text
oneOrTwo
Char
r <- forall s. [Char] -> Parser s Char
noneOf [Char]
"\"\\"
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a -> a
fromMaybe Text
T.empty Maybe Text
mq Text -> Char -> Text
`T.snoc` Char
r
_tCharsLong :: N3Parser T.Text
_tCharsLong :: N3Parser Text
_tCharsLong =
Char -> Text
T.singleton forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> N3Parser Char
_protChar
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> N3Parser Text
_multiQuote
_protChar :: N3Parser Char
_protChar :: N3Parser Char
_protChar = forall s. Char -> Parser s Char
char Char
'\\' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (N3Parser Char
_echar' forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> N3Parser Char
_uchar')
_echar' :: N3Parser Char
_echar' :: N3Parser Char
_echar' =
(forall s. Char -> Parser s Char
char Char
't' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\t') forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(forall s. Char -> Parser s Char
char Char
'b' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\b') forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(forall s. Char -> Parser s Char
char Char
'n' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\n') forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(forall s. Char -> Parser s Char
char Char
'r' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\r') forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(forall s. Char -> Parser s Char
char Char
'f' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\f') forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(forall s. Char -> Parser s Char
char Char
'\\' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\\') forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(forall s. Char -> Parser s Char
char Char
'"' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'"') forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(forall s. Char -> Parser s Char
char Char
'\'' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Char
'\'')
_uchar' :: N3Parser Char
_uchar' :: N3Parser Char
_uchar' =
(forall s. Char -> Parser s Char
char Char
'u' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (p :: * -> *) a. Commitment p => p a -> p a
commit forall {s}. Parser s Char
hex4)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(forall s. Char -> Parser s Char
char Char
'U' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (p :: * -> *) a. Commitment p => p a -> p a
commit forall {s}. Parser s Char
hex8)
getDefaultPrefix :: N3Parser Namespace
getDefaultPrefix :: N3Parser Namespace
getDefaultPrefix = do
N3State
s <- forall s. Parser s s
stGet
case N3State -> Maybe Text -> Maybe URI
getPrefixURI N3State
s forall a. Maybe a
Nothing of
Just URI
uri -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Maybe Text -> URI -> Namespace
makeNamespace forall a. Maybe a
Nothing URI
uri
Maybe URI
_ -> forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail [Char]
"No default prefix defined; how unexpected!"
addBase :: URI -> N3Parser ()
addBase :: URI -> Parser N3State ()
addBase = forall s. (s -> s) -> Parser s ()
stUpdate forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> URI -> N3State -> N3State
setSUri [Char]
"base"
addPrefix :: Maybe T.Text -> URI -> N3Parser ()
addPrefix :: Maybe Text -> URI -> Parser N3State ()
addPrefix Maybe Text
p = forall s. (s -> s) -> Parser s ()
stUpdate forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Text -> URI -> N3State -> N3State
setPrefix Maybe Text
p
updateKeywordsList :: [T.Text] -> N3Parser ()
updateKeywordsList :: [Text] -> Parser N3State ()
updateKeywordsList = forall s. (s -> s) -> Parser s ()
stUpdate forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> N3State -> N3State
setKeywordsList
document :: N3Parser RDFGraph
document :: N3Parser RDFGraph
document = N3State -> RDFGraph
mkGr forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall {s}. Parser s ()
whiteSpace forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser N3State ()
statementsOptional forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall {s}. Parser s ()
eof forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall s. Parser s s
stGet)
where
mkGr :: N3State -> RDFGraph
mkGr N3State
s = forall lb. NamespaceMap -> NSGraph lb -> NSGraph lb
setNamespaces (N3State -> NamespaceMap
prefixUris N3State
s) (N3State -> RDFGraph
graphState N3State
s)
statementsOptional :: N3Parser ()
statementsOptional :: Parser N3State ()
statementsOptional = forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall s a b. Parser s a -> Parser s b -> Parser s [a]
endBy (forall s a. Parser s a -> Parser s a
lexeme Parser N3State ()
statement) Parser N3State ()
fullStop
statement :: N3Parser ()
statement :: Parser N3State ()
statement =
Parser N3State ()
declaration
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser N3State ()
existential
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser N3State ()
universal
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser N3State ()
simpleStatement
declaration :: N3Parser ()
declaration :: Parser N3State ()
declaration = forall (p :: * -> *) a. PolyParse p => [p a] -> p a
oneOf [
Text -> N3Parser Text
atWord Text
"base" forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> N3Parser URI
explicitURI forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= URI -> Parser N3State ()
addBase,
Text -> N3Parser Text
atWord Text
"keywords" forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Parser N3State [Text]
bareNameCsl forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [Text] -> Parser N3State ()
updateKeywordsList,
Text -> N3Parser Text
atWord Text
"prefix" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser N3State ()
getPrefix
]
getPrefix :: N3Parser ()
getPrefix :: Parser N3State ()
getPrefix = do
Maybe Text
p <- forall s a. Parser s a -> Parser s a
lexeme N3Parser (Maybe Text)
prefix
URI
u <- N3Parser URI
explicitURI
Maybe Text -> URI -> Parser N3State ()
addPrefix Maybe Text
p URI
u
explicitURI :: N3Parser URI
explicitURI :: N3Parser URI
explicitURI = do
forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall s. Char -> Parser s Char
char Char
'<'
[Char]
ustr <- forall (p :: * -> *) a z.
(PolyParse p, Show a) =>
p a -> p z -> p [a]
manyFinally' ((forall s. (Char -> Bool) -> Parser s Char
satisfy Char -> Bool
isSpace forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall {s}. Parser s Char
next) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall {s}. Parser s Char
next) (forall s. Char -> Parser s Char
char Char
'>')
case [Char] -> Maybe URI
parseURIReference [Char]
ustr of
Maybe URI
Nothing -> forall (p :: * -> *) a. PolyParse p => [Char] -> p a
failBad forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid URI: <" forall a. [a] -> [a] -> [a]
++ [Char]
ustr forall a. [a] -> [a] -> [a]
++ [Char]
">"
Just URI
uref -> do
N3State
s <- forall s. Parser s s
stGet
let base :: URI
base = N3State -> [Char] -> URI
getSUri N3State
s [Char]
"base"
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ URI -> URI -> Either [Char] URI
appendURIs URI
base URI
uref
lexUriRef :: N3Parser URI
lexUriRef :: N3Parser URI
lexUriRef = forall s a. Parser s a -> Parser s a
lexeme N3Parser URI
explicitURI
bareNameCsl :: N3Parser [T.Text]
bareNameCsl :: Parser N3State [Text]
bareNameCsl = forall (p :: * -> *) a sep. PolyParse p => p a -> p sep -> p [a]
sepBy (forall s a. Parser s a -> Parser s a
lexeme N3Parser Text
bareName) Parser N3State ()
comma
bareName :: N3Parser T.Text
bareName :: N3Parser Text
bareName = N3Parser Text
n3Name
prefix :: N3Parser (Maybe T.Text)
prefix :: N3Parser (Maybe Text)
prefix = forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (forall s a. Parser s a -> Parser s a
lexeme N3Parser Text
n3Name) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall s. Char -> Parser s Char
char Char
':'
n3symbol :: N3Parser ScopedName
n3symbol :: Parser N3State ScopedName
n3symbol =
(URI -> ScopedName
makeURIScopedName forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> N3Parser URI
explicitURI)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser N3State ScopedName
qname
symbolCsl :: N3Parser [ScopedName]
symbolCsl :: N3Parser [ScopedName]
symbolCsl = forall (p :: * -> *) a sep. PolyParse p => p a -> p sep -> p [a]
sepBy (forall s a. Parser s a -> Parser s a
lexeme Parser N3State ScopedName
n3symbol) Parser N3State ()
comma
toName :: Namespace -> T.Text -> ScopedName
toName :: Namespace -> Text -> ScopedName
toName Namespace
ns Text
l =
case Text -> Maybe LName
newLName Text
l of
Just LName
local -> Namespace -> LName -> ScopedName
makeNSScopedName Namespace
ns LName
local
Maybe LName
_ -> forall a. HasCallStack => [Char] -> a
error forall a b. (a -> b) -> a -> b
$ [Char]
"Invalid local name: " forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
l
qname :: N3Parser ScopedName
qname :: Parser N3State ScopedName
qname = Parser N3State ScopedName
qname1 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser N3State ScopedName
qname2
qname1 :: N3Parser ScopedName
qname1 :: Parser N3State ScopedName
qname1 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Namespace -> Text -> ScopedName
toName) (forall s. Char -> Parser s Char
char Char
':' forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Parser N3State (Namespace, Text)
g)
where
g :: Parser N3State (Namespace, Text)
g = (,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> N3Parser Namespace
getDefaultPrefix forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (N3Parser Text
n3Name forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *) a. Monad m => a -> m a
return Text
"")
qname2 :: N3Parser ScopedName
qname2 :: Parser N3State ScopedName
qname2 = N3Parser Text
n3Name forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Parser N3State ScopedName
fullOrLocalQName
fullOrLocalQName :: T.Text -> N3Parser ScopedName
fullOrLocalQName :: Text -> Parser N3State ScopedName
fullOrLocalQName Text
name =
(forall s. Char -> Parser s Char
char Char
':' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Text -> Parser N3State ScopedName
fullQName Text
name)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Parser N3State ScopedName
localQName Text
name
fullQName :: T.Text -> N3Parser ScopedName
fullQName :: Text -> Parser N3State ScopedName
fullQName Text
name = Namespace -> Text -> ScopedName
toName forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> N3Parser Namespace
findPrefix Text
name forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (N3Parser Text
n3Name forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"")
findPrefix :: T.Text -> N3Parser Namespace
findPrefix :: Text -> N3Parser Namespace
findPrefix Text
pre = do
N3State
st <- forall s. Parser s s
stGet
case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (forall a. a -> Maybe a
Just Text
pre) (N3State -> NamespaceMap
prefixUris N3State
st) of
Just URI
uri -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Maybe Text -> URI -> Namespace
makeNamespace (forall a. a -> Maybe a
Just Text
pre) URI
uri
Maybe URI
Nothing -> forall (p :: * -> *) a. PolyParse p => [Char] -> p a
failBad forall a b. (a -> b) -> a -> b
$ [Char]
"Prefix '" forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
pre forall a. [a] -> [a] -> [a]
++ [Char]
":' not bound."
localQName :: T.Text -> N3Parser ScopedName
localQName :: Text -> Parser N3State ScopedName
localQName Text
name = do
N3State
st <- forall s. Parser s s
stGet
if N3State -> Bool
getAllowLocalNames N3State
st
then let g :: Parser N3State (Namespace, Text)
g = (,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> N3Parser Namespace
getDefaultPrefix forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
name
in forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Namespace -> Text -> ScopedName
toName forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser N3State (Namespace, Text)
g
else forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char]
"Invalid 'bare' word: " forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
name)
existential :: N3Parser ()
existential :: Parser N3State ()
existential = (Text -> N3Parser Text
atWord Text
"forSome" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> N3Parser [ScopedName]
symbolCsl) forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> ()
simpleStatement :: N3Parser ()
simpleStatement :: Parser N3State ()
simpleStatement = N3Parser RDFLabel
subject forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= AddStatement
propertyListWith
subject :: N3Parser RDFLabel
subject :: N3Parser RDFLabel
subject = forall s a. Parser s a -> Parser s a
lexeme N3Parser RDFLabel
expression
expression :: N3Parser RDFLabel
expression :: N3Parser RDFLabel
expression = do
RDFLabel
i <- N3Parser RDFLabel
pathItem
let backwardExpr :: Parser s (RDFLabel -> RDFLabel -> AddStatement)
backwardExpr = forall s. Char -> Parser s Char
char Char
'!' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> RDFLabel -> RDFLabel -> AddStatement
addStatementRev
forwardExpr :: Parser s (RDFLabel -> RDFLabel -> AddStatement)
forwardExpr = forall s. Char -> Parser s Char
char Char
'^' forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> RDFLabel -> RDFLabel -> AddStatement
addStatement
Maybe (RDFLabel -> RDFLabel -> AddStatement, RDFLabel)
mpt <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional
( (,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Parser s a -> Parser s a
lexeme (forall {s}. Parser s (RDFLabel -> RDFLabel -> AddStatement)
forwardExpr forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall {s}. Parser s (RDFLabel -> RDFLabel -> AddStatement)
backwardExpr) forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s a. Parser s a -> Parser s a
lexeme N3Parser RDFLabel
expression )
case Maybe (RDFLabel -> RDFLabel -> AddStatement, RDFLabel)
mpt of
Maybe (RDFLabel -> RDFLabel -> AddStatement, RDFLabel)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
i
Just (RDFLabel -> RDFLabel -> AddStatement
addFunc, RDFLabel
pt) -> do
RDFLabel
bNode <- N3Parser RDFLabel
newBlankNode
RDFLabel -> RDFLabel -> AddStatement
addFunc RDFLabel
bNode RDFLabel
pt RDFLabel
i
forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
bNode
pathItem :: N3Parser RDFLabel
pathItem :: N3Parser RDFLabel
pathItem =
forall a. [Char] -> [Char] -> N3Parser a -> N3Parser a
br [Char]
"(" [Char]
")" N3Parser RDFLabel
pathList
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. [Char] -> [Char] -> N3Parser a -> N3Parser a
br [Char]
"[" [Char]
"]" N3Parser RDFLabel
propertyListBNode
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. [Char] -> [Char] -> N3Parser a -> N3Parser a
br [Char]
"{" [Char]
"}" N3Parser RDFLabel
formulaContent
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> N3Parser RDFLabel
boolean
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> N3Parser RDFLabel
literal
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> N3Parser RDFLabel
numericLiteral
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> N3Parser RDFLabel
quickVariable
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Char] -> RDFLabel
Blank forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall s. [Char] -> Parser s [Char]
string [Char]
"_:" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser N3State [Char]
n3NameStr)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ScopedName -> RDFLabel
Res forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser N3State ScopedName
n3symbol
pathList :: N3Parser RDFLabel
pathList :: N3Parser RDFLabel
pathList = do
[RDFLabel]
cts <- forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (forall s a. Parser s a -> Parser s a
lexeme N3Parser RDFLabel
expression)
RDFLabel
eNode <- ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
rdfNil
case [RDFLabel]
cts of
[] -> forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
eNode
(RDFLabel
c:[RDFLabel]
cs) -> do
RDFLabel
sNode <- N3Parser RDFLabel
newBlankNode
RDFLabel
first <- ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
rdfFirst
RDFLabel -> RDFLabel -> AddStatement
addStatement RDFLabel
sNode RDFLabel
first RDFLabel
c
RDFLabel
lNode <- forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM RDFLabel -> RDFLabel -> N3Parser RDFLabel
addElem RDFLabel
sNode [RDFLabel]
cs
RDFLabel
rest <- ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
rdfRest
RDFLabel -> RDFLabel -> AddStatement
addStatement RDFLabel
lNode RDFLabel
rest RDFLabel
eNode
forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
sNode
where
addElem :: RDFLabel -> RDFLabel -> N3Parser RDFLabel
addElem RDFLabel
prevNode RDFLabel
curElem = do
RDFLabel
bNode <- N3Parser RDFLabel
newBlankNode
RDFLabel
first <- ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
rdfFirst
RDFLabel
rest <- ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
rdfRest
RDFLabel -> RDFLabel -> AddStatement
addStatement RDFLabel
prevNode RDFLabel
rest RDFLabel
bNode
RDFLabel -> RDFLabel -> AddStatement
addStatement RDFLabel
bNode RDFLabel
first RDFLabel
curElem
forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
bNode
restoreState :: N3State -> N3Parser N3State
restoreState :: N3State -> Parser N3State N3State
restoreState N3State
origState = do
N3State
oldState <- forall s. Parser s s
stGet
forall s. (s -> s) -> Parser s ()
stUpdate forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const N3State
origState { nodeGen :: Word32
nodeGen = N3State -> Word32
nodeGen N3State
oldState }
forall (m :: * -> *) a. Monad m => a -> m a
return N3State
oldState
formulaContent :: N3Parser RDFLabel
formulaContent :: N3Parser RDFLabel
formulaContent = do
RDFLabel
bNode <- N3Parser RDFLabel
newBlankNode
N3State
pstate <- forall s. Parser s s
stGet
forall s. (s -> s) -> Parser s ()
stUpdate forall a b. (a -> b) -> a -> b
$ \N3State
st -> N3State
st { graphState :: RDFGraph
graphState = RDFGraph
emptyRDFGraph, thisNode :: RDFLabel
thisNode = RDFLabel
bNode }
Parser N3State ()
statementList
N3State
oldState <- N3State -> Parser N3State N3State
restoreState N3State
pstate
forall s. (s -> s) -> Parser s ()
stUpdate forall a b. (a -> b) -> a -> b
$ (RDFGraph -> RDFGraph) -> N3State -> N3State
updateGraph forall a b. (a -> b) -> a -> b
$ forall lb. Label lb => Formula lb -> NSGraph lb -> NSGraph lb
setFormula (forall lb gr. lb -> gr -> LookupFormula lb gr
Formula RDFLabel
bNode (N3State -> RDFGraph
graphState N3State
oldState))
forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
bNode
subgraph :: RDFLabel -> N3Parser RDFGraph
subgraph :: RDFLabel -> N3Parser RDFGraph
subgraph RDFLabel
this = do
N3State
pstate <- forall s. Parser s s
stGet
forall s. (s -> s) -> Parser s ()
stUpdate forall a b. (a -> b) -> a -> b
$ \N3State
st -> N3State
st { graphState :: RDFGraph
graphState = RDFGraph
emptyRDFGraph, thisNode :: RDFLabel
thisNode = RDFLabel
this }
Parser N3State ()
statementsOptional
N3State
oldState <- N3State -> Parser N3State N3State
restoreState N3State
pstate
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ N3State -> RDFGraph
graphState N3State
oldState
statementList :: N3Parser ()
statementList :: Parser N3State ()
statementList = forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall s a b. Parser s a -> Parser s b -> Parser s [a]
sepEndBy (forall s a. Parser s a -> Parser s a
lexeme Parser N3State ()
statement) Parser N3State ()
fullStop
boolean :: N3Parser RDFLabel
boolean :: N3Parser RDFLabel
boolean = ScopedName -> Text -> RDFLabel
makeDatatypedLiteral ScopedName
xsdBoolean forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
(Text -> N3Parser Text
atWord Text
"false" forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> N3Parser Text
atWord Text
"true")
literal :: N3Parser RDFLabel
literal :: N3Parser RDFLabel
literal = do
Text
lit <- N3Parser Text
n3string
Maybe (Either LanguageTag ScopedName)
opt <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional N3Parser (Either LanguageTag ScopedName)
dtlang
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (Either LanguageTag ScopedName)
opt of
Just (Left LanguageTag
lcode) -> Text -> LanguageTag -> RDFLabel
LangLit Text
lit LanguageTag
lcode
Just (Right ScopedName
dtype) -> Text -> ScopedName -> RDFLabel
TypedLit Text
lit ScopedName
dtype
Maybe (Either LanguageTag ScopedName)
_ -> Text -> RDFLabel
Lit Text
lit
dtlang :: N3Parser (Either LanguageTag ScopedName)
dtlang :: N3Parser (Either LanguageTag ScopedName)
dtlang =
(forall s. Char -> Parser s Char
char Char
'@' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> N3Parser LanguageTag
langcode))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall s. [Char] -> Parser s [Char]
string [Char]
"^^" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser N3State ScopedName
n3symbol)
langcode :: N3Parser LanguageTag
langcode :: N3Parser LanguageTag
langcode = do
Text
h <- forall s. (Char -> Bool) -> Parser s Text
many1Satisfy Char -> Bool
isaz
Maybe Text
mt <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Char -> Text -> Text
L.cons forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Char -> Parser s Char
char Char
'-' forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall s. (Char -> Bool) -> Parser s Text
many1Satisfy Char -> Bool
isaz09)
let lbl :: Text
lbl = Text -> Text
L.toStrict forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text
L.append Text
h forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a -> a
fromMaybe Text
L.empty Maybe Text
mt
case Text -> Maybe LanguageTag
toLangTag Text
lbl of
Just LanguageTag
lt -> forall (m :: * -> *) a. Monad m => a -> m a
return LanguageTag
lt
Maybe LanguageTag
_ -> forall (m :: * -> *) a. MonadFail m => [Char] -> m a
fail ([Char]
"Invalid language tag: " forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
lbl)
numericLiteral :: N3Parser RDFLabel
numericLiteral :: N3Parser RDFLabel
numericLiteral =
[Char] -> RDFLabel
d2s forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser N3State [Char]
n3double
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ScopedName -> Text -> RDFLabel
makeDatatypedLiteral ScopedName
xsdDecimal forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser N3State [Char]
n3decimal
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ScopedName -> Text -> RDFLabel
makeDatatypedLiteral ScopedName
xsdInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text
T.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser N3State [Char]
n3integer
n3sign :: N3Parser Char
n3sign :: N3Parser Char
n3sign = forall s. Char -> Parser s Char
char Char
'+' forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall s. Char -> Parser s Char
char Char
'-'
n3integer :: N3Parser String
n3integer :: Parser N3State [Char]
n3integer = do
Maybe Char
ms <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional N3Parser Char
n3sign
[Char]
ds <- forall (p :: * -> *) a. PolyParse p => p a -> p [a]
many1 N3Parser Char
digit
case Maybe Char
ms of
Just Char
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Char
s forall a. a -> [a] -> [a]
: [Char]
ds
Maybe Char
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
ds
n3decimal :: N3Parser String
n3decimal :: Parser N3State [Char]
n3decimal = forall a. [a] -> [a] -> [a]
(++) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser N3State [Char]
n3integer forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ( (:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. Char -> Parser s Char
char Char
'.' forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f [a]
many N3Parser Char
digit )
n3double :: N3Parser String
n3double :: Parser N3State [Char]
n3double = forall a. [a] -> [a] -> [a]
(++)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser N3State [Char]
n3decimal
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ( (:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. (Char -> Bool) -> Parser s Char
satisfy (forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ([Char]
"eE"::String)) forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser N3State [Char]
n3integer )
d2s :: String -> RDFLabel
d2s :: [Char] -> RDFLabel
d2s [Char]
s = forall a. ToRDFLabel a => a -> RDFLabel
toRDFLabel (forall a. Read a => [Char] -> a
read [Char]
s :: Double)
propertyListBNode :: N3Parser RDFLabel
propertyListBNode :: N3Parser RDFLabel
propertyListBNode = do
[((RDFLabel -> RDFLabel -> AddStatement, RDFLabel), [RDFLabel])]
plist <- forall s a b. Parser s a -> Parser s b -> Parser s [a]
sepEndBy ((,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s a. Parser s a -> Parser s a
lexeme Parser N3State (RDFLabel -> RDFLabel -> AddStatement, RDFLabel)
verb forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> N3Parser [RDFLabel]
objectList) Parser N3State ()
semiColon
RDFLabel
bNode <- N3Parser RDFLabel
newBlankNode
let addList :: ((RDFLabel -> t -> a -> m b, t), t a) -> m ()
addList ((RDFLabel -> t -> a -> m b
addFunc,t
vrb),t a
items) = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (RDFLabel -> t -> a -> m b
addFunc RDFLabel
bNode t
vrb) t a
items
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [((RDFLabel -> RDFLabel -> AddStatement, RDFLabel), [RDFLabel])]
plist forall {t :: * -> *} {m :: * -> *} {t} {a} {b}.
(Foldable t, Monad m) =>
((RDFLabel -> t -> a -> m b, t), t a) -> m ()
addList
forall (m :: * -> *) a. Monad m => a -> m a
return RDFLabel
bNode
propertyListWith :: RDFLabel -> N3Parser ()
propertyListWith :: AddStatement
propertyListWith RDFLabel
subj =
let
term :: Parser N3State ()
term = forall s a. Parser s a -> Parser s a
lexeme Parser N3State (RDFLabel -> RDFLabel -> AddStatement, RDFLabel)
verb forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(RDFLabel -> RDFLabel -> AddStatement
addFunc, RDFLabel
vrb) -> AddStatement -> Parser N3State ()
objectListWith (RDFLabel -> RDFLabel -> AddStatement
addFunc RDFLabel
subj RDFLabel
vrb)
in forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall s a b. Parser s a -> Parser s b -> Parser s [a]
sepEndBy Parser N3State ()
term Parser N3State ()
semiColon
object :: N3Parser RDFLabel
object :: N3Parser RDFLabel
object = forall s a. Parser s a -> Parser s a
lexeme N3Parser RDFLabel
expression
objectList :: N3Parser [RDFLabel]
objectList :: N3Parser [RDFLabel]
objectList = forall (p :: * -> *) a sep. PolyParse p => p a -> p sep -> p [a]
sepBy1 N3Parser RDFLabel
object Parser N3State ()
comma
objectWith :: AddStatement -> N3Parser ()
objectWith :: AddStatement -> Parser N3State ()
objectWith AddStatement
addFunc = N3Parser RDFLabel
object forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= AddStatement
addFunc
objectListWith :: AddStatement -> N3Parser ()
objectListWith :: AddStatement -> Parser N3State ()
objectListWith AddStatement
addFunc =
forall (f :: * -> *) a. Applicative f => f a -> f ()
ignore forall a b. (a -> b) -> a -> b
$ forall (p :: * -> *) a sep. PolyParse p => p a -> p sep -> p [a]
sepBy1 (AddStatement -> Parser N3State ()
objectWith AddStatement
addFunc) Parser N3State ()
comma
verb :: N3Parser (RDFLabel -> RDFLabel -> AddStatement, RDFLabel)
verb :: Parser N3State (RDFLabel -> RDFLabel -> AddStatement, RDFLabel)
verb =
(,) RDFLabel -> RDFLabel -> AddStatement
addStatementRev forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> N3Parser RDFLabel
verbReverse
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (,) RDFLabel -> RDFLabel -> AddStatement
addStatement forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> N3Parser RDFLabel
verbForward
verbReverse :: N3Parser RDFLabel
verbReverse :: N3Parser RDFLabel
verbReverse =
forall s. [Char] -> Parser s [Char]
string [Char]
"<=" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
logImplies
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall s lbr rbr a.
Parser s lbr -> Parser s rbr -> Parser s a -> Parser s a
between (Text -> N3Parser Text
atWord Text
"is") (Text -> N3Parser Text
atWord Text
"of") (forall s a. Parser s a -> Parser s a
lexeme N3Parser RDFLabel
expression)
verbForward :: N3Parser RDFLabel
verbForward :: N3Parser RDFLabel
verbForward =
(forall s. [Char] -> Parser s [Char]
string [Char]
"=>" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
logImplies)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall s. [Char] -> Parser s [Char]
string [Char]
"=" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
owlSameAs)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Text -> N3Parser Text
atWord Text
"a" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ScopedName -> N3Parser RDFLabel
operatorLabel ScopedName
rdfType)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Text -> N3Parser Text
atWord Text
"has" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall s a. Parser s a -> Parser s a
lexeme N3Parser RDFLabel
expression)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall s a. Parser s a -> Parser s a
lexeme N3Parser RDFLabel
expression
universal :: N3Parser ()
universal :: Parser N3State ()
universal =
Text -> N3Parser Text
atWord Text
"forAll" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
forall (p :: * -> *) a. PolyParse p => [Char] -> p a
failBad [Char]
"universal (@forAll) currently unsupported."