{-|
Module      : Data.RDF.Internal
Description : Representation and Incremental Processing of RDF Data
Copyright   : Travis Whitaker 2016
License     : MIT
Maintainer  : pi.boy.travis@gmail.com
Stability   : Provisional
Portability : Portable

Internal module.
-}

{-# LANGUAGE DeriveGeneric
           , DeriveAnyClass
           , OverloadedStrings
           #-}

module Data.RDF.Internal where

import Control.Applicative

import Control.DeepSeq

import qualified Data.Attoparsec.Combinator as A
import qualified Data.Attoparsec.Text       as A

import Data.Char

import Data.String

import GHC.Generics

import qualified Data.Text as T

-- | A contiguous RDF graph with optional label. Note that a contiguous graph
--   within an RDF data set will not appear as a single contiguous graph to this
--   library if the graph's constituent triples are not contiguous in the
--   original data set. This strategy allows for incremental processing of RDF
--   data in constant space.
data RDFGraph = RDFGraph {
    -- | A named RDF graph includes an 'IRI'.
    RDFGraph -> Maybe IRI
rdfLabel :: !(Maybe IRI)
    -- | The constituent triples. A proper graph is a strict set of triples
    --   (i.e. no duplicate nodes or edges), but this guarantee cannot be made
    --   if the triples are to be processed incrementally in constant space.
    --   Programs using this type for interpreting RDF graphs should ignore any
    --   supernumerary triples in this list.
  , RDFGraph -> [Triple]
rdfTriples    :: [Triple]
  } deriving ( RDFGraph -> RDFGraph -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RDFGraph -> RDFGraph -> Bool
$c/= :: RDFGraph -> RDFGraph -> Bool
== :: RDFGraph -> RDFGraph -> Bool
$c== :: RDFGraph -> RDFGraph -> Bool
Eq
             , Eq RDFGraph
RDFGraph -> RDFGraph -> Bool
RDFGraph -> RDFGraph -> Ordering
RDFGraph -> RDFGraph -> RDFGraph
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: RDFGraph -> RDFGraph -> RDFGraph
$cmin :: RDFGraph -> RDFGraph -> RDFGraph
max :: RDFGraph -> RDFGraph -> RDFGraph
$cmax :: RDFGraph -> RDFGraph -> RDFGraph
>= :: RDFGraph -> RDFGraph -> Bool
$c>= :: RDFGraph -> RDFGraph -> Bool
> :: RDFGraph -> RDFGraph -> Bool
$c> :: RDFGraph -> RDFGraph -> Bool
<= :: RDFGraph -> RDFGraph -> Bool
$c<= :: RDFGraph -> RDFGraph -> Bool
< :: RDFGraph -> RDFGraph -> Bool
$c< :: RDFGraph -> RDFGraph -> Bool
compare :: RDFGraph -> RDFGraph -> Ordering
$ccompare :: RDFGraph -> RDFGraph -> Ordering
Ord
             , ReadPrec [RDFGraph]
ReadPrec RDFGraph
Int -> ReadS RDFGraph
ReadS [RDFGraph]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [RDFGraph]
$creadListPrec :: ReadPrec [RDFGraph]
readPrec :: ReadPrec RDFGraph
$creadPrec :: ReadPrec RDFGraph
readList :: ReadS [RDFGraph]
$creadList :: ReadS [RDFGraph]
readsPrec :: Int -> ReadS RDFGraph
$creadsPrec :: Int -> ReadS RDFGraph
Read
             , Int -> RDFGraph -> ShowS
[RDFGraph] -> ShowS
RDFGraph -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RDFGraph] -> ShowS
$cshowList :: [RDFGraph] -> ShowS
show :: RDFGraph -> String
$cshow :: RDFGraph -> String
showsPrec :: Int -> RDFGraph -> ShowS
$cshowsPrec :: Int -> RDFGraph -> ShowS
Show
             , forall x. Rep RDFGraph x -> RDFGraph
forall x. RDFGraph -> Rep RDFGraph x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RDFGraph x -> RDFGraph
$cfrom :: forall x. RDFGraph -> Rep RDFGraph x
Generic
             , RDFGraph -> ()
forall a. (a -> ()) -> NFData a
rnf :: RDFGraph -> ()
$crnf :: RDFGraph -> ()
NFData
             )

-- | An RDF quad, i.e. a triple belonging to a named graph.
data Quad = Quad {
    Quad -> Triple
quadTriple :: !Triple
  , Quad -> Maybe IRI
quadGraph  :: !(Maybe IRI)
  } deriving ( Quad -> Quad -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Quad -> Quad -> Bool
$c/= :: Quad -> Quad -> Bool
== :: Quad -> Quad -> Bool
$c== :: Quad -> Quad -> Bool
Eq
             , Eq Quad
Quad -> Quad -> Bool
Quad -> Quad -> Ordering
Quad -> Quad -> Quad
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Quad -> Quad -> Quad
$cmin :: Quad -> Quad -> Quad
max :: Quad -> Quad -> Quad
$cmax :: Quad -> Quad -> Quad
>= :: Quad -> Quad -> Bool
$c>= :: Quad -> Quad -> Bool
> :: Quad -> Quad -> Bool
$c> :: Quad -> Quad -> Bool
<= :: Quad -> Quad -> Bool
$c<= :: Quad -> Quad -> Bool
< :: Quad -> Quad -> Bool
$c< :: Quad -> Quad -> Bool
compare :: Quad -> Quad -> Ordering
$ccompare :: Quad -> Quad -> Ordering
Ord
             , ReadPrec [Quad]
ReadPrec Quad
Int -> ReadS Quad
ReadS [Quad]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Quad]
$creadListPrec :: ReadPrec [Quad]
readPrec :: ReadPrec Quad
$creadPrec :: ReadPrec Quad
readList :: ReadS [Quad]
$creadList :: ReadS [Quad]
readsPrec :: Int -> ReadS Quad
$creadsPrec :: Int -> ReadS Quad
Read
             , Int -> Quad -> ShowS
[Quad] -> ShowS
Quad -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Quad] -> ShowS
$cshowList :: [Quad] -> ShowS
show :: Quad -> String
$cshow :: Quad -> String
showsPrec :: Int -> Quad -> ShowS
$cshowsPrec :: Int -> Quad -> ShowS
Show
             , forall x. Rep Quad x -> Quad
forall x. Quad -> Rep Quad x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Quad x -> Quad
$cfrom :: forall x. Quad -> Rep Quad x
Generic
             , Quad -> ()
forall a. (a -> ()) -> NFData a
rnf :: Quad -> ()
$crnf :: Quad -> ()
NFData
             )

-- | An RDF triple.
data Triple = Triple !Subject !Predicate !Object
            deriving ( Triple -> Triple -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Triple -> Triple -> Bool
$c/= :: Triple -> Triple -> Bool
== :: Triple -> Triple -> Bool
$c== :: Triple -> Triple -> Bool
Eq
                     , Eq Triple
Triple -> Triple -> Bool
Triple -> Triple -> Ordering
Triple -> Triple -> Triple
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Triple -> Triple -> Triple
$cmin :: Triple -> Triple -> Triple
max :: Triple -> Triple -> Triple
$cmax :: Triple -> Triple -> Triple
>= :: Triple -> Triple -> Bool
$c>= :: Triple -> Triple -> Bool
> :: Triple -> Triple -> Bool
$c> :: Triple -> Triple -> Bool
<= :: Triple -> Triple -> Bool
$c<= :: Triple -> Triple -> Bool
< :: Triple -> Triple -> Bool
$c< :: Triple -> Triple -> Bool
compare :: Triple -> Triple -> Ordering
$ccompare :: Triple -> Triple -> Ordering
Ord
                     , ReadPrec [Triple]
ReadPrec Triple
Int -> ReadS Triple
ReadS [Triple]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Triple]
$creadListPrec :: ReadPrec [Triple]
readPrec :: ReadPrec Triple
$creadPrec :: ReadPrec Triple
readList :: ReadS [Triple]
$creadList :: ReadS [Triple]
readsPrec :: Int -> ReadS Triple
$creadsPrec :: Int -> ReadS Triple
Read
                     , Int -> Triple -> ShowS
[Triple] -> ShowS
Triple -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Triple] -> ShowS
$cshowList :: [Triple] -> ShowS
show :: Triple -> String
$cshow :: Triple -> String
showsPrec :: Int -> Triple -> ShowS
$cshowsPrec :: Int -> Triple -> ShowS
Show
                     , forall x. Rep Triple x -> Triple
forall x. Triple -> Rep Triple x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Triple x -> Triple
$cfrom :: forall x. Triple -> Rep Triple x
Generic
                     , Triple -> ()
forall a. (a -> ()) -> NFData a
rnf :: Triple -> ()
$crnf :: Triple -> ()
NFData
                     )

-- | An RDF subject, i.e. either an 'IRI' or a 'BlankNode'.
--
--   This type has an 'IsString' instance, allowing string literals to be
--   interpreted as 'Subject's with @-XOverloadedStrings@, like so:
--
--   >>> "<http://example.com> :: Subject
--   IRISubject (IRI (...))
--   >>> "_:some-node" :: Subject
--   BlankSubject (BlankNode {unBlankNode = "some-node"})
data Subject = IRISubject   !IRI
             | BlankSubject !BlankNode
             deriving ( Subject -> Subject -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Subject -> Subject -> Bool
$c/= :: Subject -> Subject -> Bool
== :: Subject -> Subject -> Bool
$c== :: Subject -> Subject -> Bool
Eq
                      , Eq Subject
Subject -> Subject -> Bool
Subject -> Subject -> Ordering
Subject -> Subject -> Subject
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Subject -> Subject -> Subject
$cmin :: Subject -> Subject -> Subject
max :: Subject -> Subject -> Subject
$cmax :: Subject -> Subject -> Subject
>= :: Subject -> Subject -> Bool
$c>= :: Subject -> Subject -> Bool
> :: Subject -> Subject -> Bool
$c> :: Subject -> Subject -> Bool
<= :: Subject -> Subject -> Bool
$c<= :: Subject -> Subject -> Bool
< :: Subject -> Subject -> Bool
$c< :: Subject -> Subject -> Bool
compare :: Subject -> Subject -> Ordering
$ccompare :: Subject -> Subject -> Ordering
Ord
                      , ReadPrec [Subject]
ReadPrec Subject
Int -> ReadS Subject
ReadS [Subject]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Subject]
$creadListPrec :: ReadPrec [Subject]
readPrec :: ReadPrec Subject
$creadPrec :: ReadPrec Subject
readList :: ReadS [Subject]
$creadList :: ReadS [Subject]
readsPrec :: Int -> ReadS Subject
$creadsPrec :: Int -> ReadS Subject
Read
                      , Int -> Subject -> ShowS
[Subject] -> ShowS
Subject -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Subject] -> ShowS
$cshowList :: [Subject] -> ShowS
show :: Subject -> String
$cshow :: Subject -> String
showsPrec :: Int -> Subject -> ShowS
$cshowsPrec :: Int -> Subject -> ShowS
Show
                      , forall x. Rep Subject x -> Subject
forall x. Subject -> Rep Subject x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Subject x -> Subject
$cfrom :: forall x. Subject -> Rep Subject x
Generic
                      , Subject -> ()
forall a. (a -> ()) -> NFData a
rnf :: Subject -> ()
$crnf :: Subject -> ()
NFData
                      )

-- | An RDF predicate.
--
--   This type has an 'IsString' instance, allowing string literals to be
--   interpreted as 'Predicate's with @-XOverloadedStrings@, like so:
--
--   >>> "<http://example.com>" :: Predicate
--   Predicate {unPredicate = IRI (...)}
newtype Predicate = Predicate { Predicate -> IRI
unPredicate :: IRI }
                  deriving ( Predicate -> Predicate -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Predicate -> Predicate -> Bool
$c/= :: Predicate -> Predicate -> Bool
== :: Predicate -> Predicate -> Bool
$c== :: Predicate -> Predicate -> Bool
Eq
                           , Eq Predicate
Predicate -> Predicate -> Bool
Predicate -> Predicate -> Ordering
Predicate -> Predicate -> Predicate
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Predicate -> Predicate -> Predicate
$cmin :: Predicate -> Predicate -> Predicate
max :: Predicate -> Predicate -> Predicate
$cmax :: Predicate -> Predicate -> Predicate
>= :: Predicate -> Predicate -> Bool
$c>= :: Predicate -> Predicate -> Bool
> :: Predicate -> Predicate -> Bool
$c> :: Predicate -> Predicate -> Bool
<= :: Predicate -> Predicate -> Bool
$c<= :: Predicate -> Predicate -> Bool
< :: Predicate -> Predicate -> Bool
$c< :: Predicate -> Predicate -> Bool
compare :: Predicate -> Predicate -> Ordering
$ccompare :: Predicate -> Predicate -> Ordering
Ord
                           , ReadPrec [Predicate]
ReadPrec Predicate
Int -> ReadS Predicate
ReadS [Predicate]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Predicate]
$creadListPrec :: ReadPrec [Predicate]
readPrec :: ReadPrec Predicate
$creadPrec :: ReadPrec Predicate
readList :: ReadS [Predicate]
$creadList :: ReadS [Predicate]
readsPrec :: Int -> ReadS Predicate
$creadsPrec :: Int -> ReadS Predicate
Read
                           , Int -> Predicate -> ShowS
[Predicate] -> ShowS
Predicate -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Predicate] -> ShowS
$cshowList :: [Predicate] -> ShowS
show :: Predicate -> String
$cshow :: Predicate -> String
showsPrec :: Int -> Predicate -> ShowS
$cshowsPrec :: Int -> Predicate -> ShowS
Show
                           , forall x. Rep Predicate x -> Predicate
forall x. Predicate -> Rep Predicate x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Predicate x -> Predicate
$cfrom :: forall x. Predicate -> Rep Predicate x
Generic
                           , Predicate -> ()
forall a. (a -> ()) -> NFData a
rnf :: Predicate -> ()
$crnf :: Predicate -> ()
NFData
                           )

-- | An RDF object, i.e. either an 'IRI', a 'Literal', or a 'BlankNode'.
--
--   This type has an 'IsString' instance, allowing string literals to be
--   interpreted as 'Object's with @-XOverloadedStrings@, like so:
--
--   >>> "<http://example.com>" :: Object
--   IRIObject (IRI (...))
--   >>> "_:some-node" :: Object
--   BlankObject (BlankNode {unBlankNode = "some-node"})
--   >>> "computer" :: Object
--   LiteralObject (Literal {litString = "computer", litType = LiteralUntyped})
--
--   The precedence for literal interpretation is IRI > BlankNode > Literal. To
--   force a literal that is also a valid blank node label or IRI to be
--   interpreted as a 'LiteralObject', wrap it in an extra set of double quotes:
--
--   >>> "\"_:some-node\"" :: Object
--   LiteralObject (Literal {litString = "_:some-node", litType = LiteralUntyped})
data Object = IRIObject     !IRI
            | BlankObject   !BlankNode
            | LiteralObject !Literal
            deriving ( Object -> Object -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Object -> Object -> Bool
$c/= :: Object -> Object -> Bool
== :: Object -> Object -> Bool
$c== :: Object -> Object -> Bool
Eq
                     , Eq Object
Object -> Object -> Bool
Object -> Object -> Ordering
Object -> Object -> Object
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Object -> Object -> Object
$cmin :: Object -> Object -> Object
max :: Object -> Object -> Object
$cmax :: Object -> Object -> Object
>= :: Object -> Object -> Bool
$c>= :: Object -> Object -> Bool
> :: Object -> Object -> Bool
$c> :: Object -> Object -> Bool
<= :: Object -> Object -> Bool
$c<= :: Object -> Object -> Bool
< :: Object -> Object -> Bool
$c< :: Object -> Object -> Bool
compare :: Object -> Object -> Ordering
$ccompare :: Object -> Object -> Ordering
Ord
                     , ReadPrec [Object]
ReadPrec Object
Int -> ReadS Object
ReadS [Object]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Object]
$creadListPrec :: ReadPrec [Object]
readPrec :: ReadPrec Object
$creadPrec :: ReadPrec Object
readList :: ReadS [Object]
$creadList :: ReadS [Object]
readsPrec :: Int -> ReadS Object
$creadsPrec :: Int -> ReadS Object
Read
                     , Int -> Object -> ShowS
[Object] -> ShowS
Object -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Object] -> ShowS
$cshowList :: [Object] -> ShowS
show :: Object -> String
$cshow :: Object -> String
showsPrec :: Int -> Object -> ShowS
$cshowsPrec :: Int -> Object -> ShowS
Show
                     , forall x. Rep Object x -> Object
forall x. Object -> Rep Object x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Object x -> Object
$cfrom :: forall x. Object -> Rep Object x
Generic
                     , Object -> ()
forall a. (a -> ()) -> NFData a
rnf :: Object -> ()
$crnf :: Object -> ()
NFData
                     )

-- | A blank node with its local label, without the preceeding "_:". Other
--   programs processing RDF are permitted to discard these node labels, i.e.
--   all blank node labels are local to a specific representation of an RDF data
--   set.
--
--   This type has an 'IsString' instance, allowing string literals to be
--   interpreted as 'BlankNode's with @-XOverloadedStrings@, like so:
--
--   >>> "_:some-node" :: BlankNode
--   BlankNode {unBlankNode = "some-node"}
newtype BlankNode = BlankNode { BlankNode -> Text
unBlankNode :: T.Text }
                  deriving ( BlankNode -> BlankNode -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BlankNode -> BlankNode -> Bool
$c/= :: BlankNode -> BlankNode -> Bool
== :: BlankNode -> BlankNode -> Bool
$c== :: BlankNode -> BlankNode -> Bool
Eq
                           , Eq BlankNode
BlankNode -> BlankNode -> Bool
BlankNode -> BlankNode -> Ordering
BlankNode -> BlankNode -> BlankNode
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BlankNode -> BlankNode -> BlankNode
$cmin :: BlankNode -> BlankNode -> BlankNode
max :: BlankNode -> BlankNode -> BlankNode
$cmax :: BlankNode -> BlankNode -> BlankNode
>= :: BlankNode -> BlankNode -> Bool
$c>= :: BlankNode -> BlankNode -> Bool
> :: BlankNode -> BlankNode -> Bool
$c> :: BlankNode -> BlankNode -> Bool
<= :: BlankNode -> BlankNode -> Bool
$c<= :: BlankNode -> BlankNode -> Bool
< :: BlankNode -> BlankNode -> Bool
$c< :: BlankNode -> BlankNode -> Bool
compare :: BlankNode -> BlankNode -> Ordering
$ccompare :: BlankNode -> BlankNode -> Ordering
Ord
                           , ReadPrec [BlankNode]
ReadPrec BlankNode
Int -> ReadS BlankNode
ReadS [BlankNode]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BlankNode]
$creadListPrec :: ReadPrec [BlankNode]
readPrec :: ReadPrec BlankNode
$creadPrec :: ReadPrec BlankNode
readList :: ReadS [BlankNode]
$creadList :: ReadS [BlankNode]
readsPrec :: Int -> ReadS BlankNode
$creadsPrec :: Int -> ReadS BlankNode
Read
                           , Int -> BlankNode -> ShowS
[BlankNode] -> ShowS
BlankNode -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BlankNode] -> ShowS
$cshowList :: [BlankNode] -> ShowS
show :: BlankNode -> String
$cshow :: BlankNode -> String
showsPrec :: Int -> BlankNode -> ShowS
$cshowsPrec :: Int -> BlankNode -> ShowS
Show
                           , forall x. Rep BlankNode x -> BlankNode
forall x. BlankNode -> Rep BlankNode x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BlankNode x -> BlankNode
$cfrom :: forall x. BlankNode -> Rep BlankNode x
Generic
                           , BlankNode -> ()
forall a. (a -> ()) -> NFData a
rnf :: BlankNode -> ()
$crnf :: BlankNode -> ()
NFData
                           )

-- | An RDF literal. As stipulated by the RDF standard, the 'litType' is merely
--   metadata; all RDF processing programs must try to handle literals that are
--   ill-typed.
--
--   This type has an 'IsString' instance, allowing string literals to be
--   interpreted as 'Literal's with @-XOverloadedStrings@, like so:
--
--   >>> "computer" :: Literal
--   Literal {litString = "computer", litType = LiteralUntyped}
--
--   For untyped literals the extra double quotes are not required. They are
--   required for typed literals:
--
--   >>> "\"computer\"@en" :: Literal
--   Literal {litString = "computer", litType = LiteralLangType "en"}
--
--   >>> "\"computer\"^^<http://computer.machine/machine>" :: Literal
--   Literal { litString = "computer", litType = LiteralIRIType (...)}
data Literal = Literal {
    Literal -> Text
litString :: !T.Text
  , Literal -> LiteralType
litType   :: !LiteralType
  } deriving ( Literal -> Literal -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Literal -> Literal -> Bool
$c/= :: Literal -> Literal -> Bool
== :: Literal -> Literal -> Bool
$c== :: Literal -> Literal -> Bool
Eq
             , Eq Literal
Literal -> Literal -> Bool
Literal -> Literal -> Ordering
Literal -> Literal -> Literal
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Literal -> Literal -> Literal
$cmin :: Literal -> Literal -> Literal
max :: Literal -> Literal -> Literal
$cmax :: Literal -> Literal -> Literal
>= :: Literal -> Literal -> Bool
$c>= :: Literal -> Literal -> Bool
> :: Literal -> Literal -> Bool
$c> :: Literal -> Literal -> Bool
<= :: Literal -> Literal -> Bool
$c<= :: Literal -> Literal -> Bool
< :: Literal -> Literal -> Bool
$c< :: Literal -> Literal -> Bool
compare :: Literal -> Literal -> Ordering
$ccompare :: Literal -> Literal -> Ordering
Ord
             , ReadPrec [Literal]
ReadPrec Literal
Int -> ReadS Literal
ReadS [Literal]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Literal]
$creadListPrec :: ReadPrec [Literal]
readPrec :: ReadPrec Literal
$creadPrec :: ReadPrec Literal
readList :: ReadS [Literal]
$creadList :: ReadS [Literal]
readsPrec :: Int -> ReadS Literal
$creadsPrec :: Int -> ReadS Literal
Read
             , Int -> Literal -> ShowS
[Literal] -> ShowS
Literal -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Literal] -> ShowS
$cshowList :: [Literal] -> ShowS
show :: Literal -> String
$cshow :: Literal -> String
showsPrec :: Int -> Literal -> ShowS
$cshowsPrec :: Int -> Literal -> ShowS
Show
             , forall x. Rep Literal x -> Literal
forall x. Literal -> Rep Literal x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Literal x -> Literal
$cfrom :: forall x. Literal -> Rep Literal x
Generic
             , Literal -> ()
forall a. (a -> ()) -> NFData a
rnf :: Literal -> ()
$crnf :: Literal -> ()
NFData
             )

-- | An RDF literal type. As stipulated by the RDF standard, this is merely
--   metadata; all RDF processing programs must try to handle literals that are
--   ill-typed.
data LiteralType = LiteralIRIType  !IRI
                 | LiteralLangType !T.Text
                 | LiteralUntyped
                 deriving ( LiteralType -> LiteralType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LiteralType -> LiteralType -> Bool
$c/= :: LiteralType -> LiteralType -> Bool
== :: LiteralType -> LiteralType -> Bool
$c== :: LiteralType -> LiteralType -> Bool
Eq
                          , Eq LiteralType
LiteralType -> LiteralType -> Bool
LiteralType -> LiteralType -> Ordering
LiteralType -> LiteralType -> LiteralType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: LiteralType -> LiteralType -> LiteralType
$cmin :: LiteralType -> LiteralType -> LiteralType
max :: LiteralType -> LiteralType -> LiteralType
$cmax :: LiteralType -> LiteralType -> LiteralType
>= :: LiteralType -> LiteralType -> Bool
$c>= :: LiteralType -> LiteralType -> Bool
> :: LiteralType -> LiteralType -> Bool
$c> :: LiteralType -> LiteralType -> Bool
<= :: LiteralType -> LiteralType -> Bool
$c<= :: LiteralType -> LiteralType -> Bool
< :: LiteralType -> LiteralType -> Bool
$c< :: LiteralType -> LiteralType -> Bool
compare :: LiteralType -> LiteralType -> Ordering
$ccompare :: LiteralType -> LiteralType -> Ordering
Ord
                          , ReadPrec [LiteralType]
ReadPrec LiteralType
Int -> ReadS LiteralType
ReadS [LiteralType]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [LiteralType]
$creadListPrec :: ReadPrec [LiteralType]
readPrec :: ReadPrec LiteralType
$creadPrec :: ReadPrec LiteralType
readList :: ReadS [LiteralType]
$creadList :: ReadS [LiteralType]
readsPrec :: Int -> ReadS LiteralType
$creadsPrec :: Int -> ReadS LiteralType
Read
                          , Int -> LiteralType -> ShowS
[LiteralType] -> ShowS
LiteralType -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LiteralType] -> ShowS
$cshowList :: [LiteralType] -> ShowS
show :: LiteralType -> String
$cshow :: LiteralType -> String
showsPrec :: Int -> LiteralType -> ShowS
$cshowsPrec :: Int -> LiteralType -> ShowS
Show
                          , forall x. Rep LiteralType x -> LiteralType
forall x. LiteralType -> Rep LiteralType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep LiteralType x -> LiteralType
$cfrom :: forall x. LiteralType -> Rep LiteralType x
Generic
                          , LiteralType -> ()
forall a. (a -> ()) -> NFData a
rnf :: LiteralType -> ()
$crnf :: LiteralType -> ()
NFData
                          )

-- | An Internationalized Resource Identifier. This library preferentially
--   follows RFC 3987 over the RDF 1.1 specification, as the two standards
--   disagree about precisely what constitutes an IRI. A notable exception is
--   the handling of IRI fragments; this library follows the RDF 1.1
--   specification, allowing IRI fragments to occur in absolute IRIs, even
--   though this is expressly prohibited by RFC 3987.
--
--   Unlike the @network-uri@ package's behavior with URI fields, this library
--   does not include the sentinel tokens in the parsed fields. For example,
--   when parsing @http://example.com@, @network-uri@ will provide the string
--   @http:@ as the scheme, while this library will provide @http@ as the
--   scheme.
--
--   This type has an 'IsString' instnace, allowing string literals to be
--   interpreted as 'IRI's with @-XOverloadedStrings@, like so:
--
--   >>> "http://example.com" :: IRI
--   IRI { iriScheme = "http"
--       , iriAuth = Just (IRIAuth { iriUser = Nothing
--                                 , iriHost = "example.com"
--                                 , iriPort = Nothing
--                                 })
--       , iriPath = ""
--       , iriQuery = Nothing
--       , iriFragment = Nothing
--       }
data IRI = IRI {
    -- | The IRI scheme, e.g. @http@
    IRI -> Text
iriScheme   :: !T.Text
    -- | The IRI authority, e.g. @example.com@
  , IRI -> Maybe IRIAuth
iriAuth     :: !(Maybe IRIAuth)
    -- | The IRI path, e.g. @/posts//index.html@
  , IRI -> Text
iriPath     :: !T.Text
    -- | The IRI query, i.e. the component after the @?@ if present.
  , IRI -> Maybe Text
iriQuery    :: !(Maybe T.Text)
    -- | The IRI fragment, i.e. the component after the @#@ if present.
  , IRI -> Maybe Text
iriFragment :: !(Maybe T.Text)
  } deriving ( IRI -> IRI -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IRI -> IRI -> Bool
$c/= :: IRI -> IRI -> Bool
== :: IRI -> IRI -> Bool
$c== :: IRI -> IRI -> Bool
Eq
             , Eq IRI
IRI -> IRI -> Bool
IRI -> IRI -> Ordering
IRI -> IRI -> IRI
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: IRI -> IRI -> IRI
$cmin :: IRI -> IRI -> IRI
max :: IRI -> IRI -> IRI
$cmax :: IRI -> IRI -> IRI
>= :: IRI -> IRI -> Bool
$c>= :: IRI -> IRI -> Bool
> :: IRI -> IRI -> Bool
$c> :: IRI -> IRI -> Bool
<= :: IRI -> IRI -> Bool
$c<= :: IRI -> IRI -> Bool
< :: IRI -> IRI -> Bool
$c< :: IRI -> IRI -> Bool
compare :: IRI -> IRI -> Ordering
$ccompare :: IRI -> IRI -> Ordering
Ord
             , ReadPrec [IRI]
ReadPrec IRI
Int -> ReadS IRI
ReadS [IRI]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [IRI]
$creadListPrec :: ReadPrec [IRI]
readPrec :: ReadPrec IRI
$creadPrec :: ReadPrec IRI
readList :: ReadS [IRI]
$creadList :: ReadS [IRI]
readsPrec :: Int -> ReadS IRI
$creadsPrec :: Int -> ReadS IRI
Read
             , Int -> IRI -> ShowS
[IRI] -> ShowS
IRI -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IRI] -> ShowS
$cshowList :: [IRI] -> ShowS
show :: IRI -> String
$cshow :: IRI -> String
showsPrec :: Int -> IRI -> ShowS
$cshowsPrec :: Int -> IRI -> ShowS
Show
             , forall x. Rep IRI x -> IRI
forall x. IRI -> Rep IRI x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep IRI x -> IRI
$cfrom :: forall x. IRI -> Rep IRI x
Generic
             , IRI -> ()
forall a. (a -> ()) -> NFData a
rnf :: IRI -> ()
$crnf :: IRI -> ()
NFData
             )

-- | An IRI Authority, as described by RFC 3987.
data IRIAuth = IRIAuth {
    -- | The IRI user, i.e. the component before the @\@@ if present.
    IRIAuth -> Maybe Text
iriUser :: !(Maybe T.Text)
    -- | The IRI host, e.g. @example.com@.
  , IRIAuth -> Text
iriHost :: T.Text
    -- | The IRI port, i.e. the numeral after the @:@ if present.
  , IRIAuth -> Maybe Text
iriPort :: !(Maybe T.Text)
  } deriving ( IRIAuth -> IRIAuth -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IRIAuth -> IRIAuth -> Bool
$c/= :: IRIAuth -> IRIAuth -> Bool
== :: IRIAuth -> IRIAuth -> Bool
$c== :: IRIAuth -> IRIAuth -> Bool
Eq
             , Eq IRIAuth
IRIAuth -> IRIAuth -> Bool
IRIAuth -> IRIAuth -> Ordering
IRIAuth -> IRIAuth -> IRIAuth
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: IRIAuth -> IRIAuth -> IRIAuth
$cmin :: IRIAuth -> IRIAuth -> IRIAuth
max :: IRIAuth -> IRIAuth -> IRIAuth
$cmax :: IRIAuth -> IRIAuth -> IRIAuth
>= :: IRIAuth -> IRIAuth -> Bool
$c>= :: IRIAuth -> IRIAuth -> Bool
> :: IRIAuth -> IRIAuth -> Bool
$c> :: IRIAuth -> IRIAuth -> Bool
<= :: IRIAuth -> IRIAuth -> Bool
$c<= :: IRIAuth -> IRIAuth -> Bool
< :: IRIAuth -> IRIAuth -> Bool
$c< :: IRIAuth -> IRIAuth -> Bool
compare :: IRIAuth -> IRIAuth -> Ordering
$ccompare :: IRIAuth -> IRIAuth -> Ordering
Ord
             , ReadPrec [IRIAuth]
ReadPrec IRIAuth
Int -> ReadS IRIAuth
ReadS [IRIAuth]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [IRIAuth]
$creadListPrec :: ReadPrec [IRIAuth]
readPrec :: ReadPrec IRIAuth
$creadPrec :: ReadPrec IRIAuth
readList :: ReadS [IRIAuth]
$creadList :: ReadS [IRIAuth]
readsPrec :: Int -> ReadS IRIAuth
$creadsPrec :: Int -> ReadS IRIAuth
Read
             , Int -> IRIAuth -> ShowS
[IRIAuth] -> ShowS
IRIAuth -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [IRIAuth] -> ShowS
$cshowList :: [IRIAuth] -> ShowS
show :: IRIAuth -> String
$cshow :: IRIAuth -> String
showsPrec :: Int -> IRIAuth -> ShowS
$cshowsPrec :: Int -> IRIAuth -> ShowS
Show
             , forall x. Rep IRIAuth x -> IRIAuth
forall x. IRIAuth -> Rep IRIAuth x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep IRIAuth x -> IRIAuth
$cfrom :: forall x. IRIAuth -> Rep IRIAuth x
Generic
             , IRIAuth -> ()
forall a. (a -> ()) -> NFData a
rnf :: IRIAuth -> ()
$crnf :: IRIAuth -> ()
NFData
             )

-- | Predicate on 'Char's for acceptability for inclusion in an 'IRI'.
isIRI :: Char -> Bool
isIRI :: Char -> Bool
isIRI Char
c = (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'<')
       Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'>')
       Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'"')
       Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'{')
       Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'}')
       Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'|')
       Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'^')
       Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'`')
       Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'\\')

-- | 'IRI' parser.
parseIRI :: A.Parser IRI
parseIRI :: Parser IRI
parseIRI = Text -> Maybe IRIAuth -> Text -> Maybe Text -> Maybe Text -> IRI
IRI forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser Text Text
parseScheme forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
A.char Char
':')
               forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe IRIAuth)
parseAuth
               forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text Text
parsePath
               forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe Text)
parseQuery
               forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe Text)
parseFragment

-- | 'IRI' scheme parser.
parseScheme :: A.Parser T.Text
parseScheme :: Parser Text Text
parseScheme = (Char -> Bool) -> Parser Text Text
A.takeWhile1 Char -> Bool
isScheme forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall {f :: * -> *}. MonadFail f => Text -> f Text
check
    where check :: Text -> f Text
check Text
t
            | Char -> Bool
isAlpha (Text -> Char
T.head Text
t) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
t
            | Bool
otherwise          = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"parseScheme: must start with letter."
          isScheme :: Char -> Bool
isScheme Char
c = Char -> Bool
isAlphaNum Char
c
                    Bool -> Bool -> Bool
|| (Char
c forall a. Eq a => a -> a -> Bool
== Char
'+')
                    Bool -> Bool -> Bool
|| (Char
c forall a. Eq a => a -> a -> Bool
== Char
'-')
                    Bool -> Bool -> Bool
|| (Char
c forall a. Eq a => a -> a -> Bool
== Char
'.')

-- | 'IRIAuth' parser.
parseAuth :: A.Parser (Maybe IRIAuth)
parseAuth :: Parser (Maybe IRIAuth)
parseAuth = forall (f :: * -> *) a. Alternative f => a -> f a -> f a
A.option forall a. Maybe a
Nothing (Text -> Parser Text Text
A.string Text
"//" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text IRIAuth
parseIRIAuth))
    where parseIRIAuth :: Parser Text IRIAuth
parseIRIAuth = Maybe Text -> Text -> Maybe Text -> IRIAuth
IRIAuth forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (Maybe Text)
parseUser
                                 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text Text
parseHost
                                 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser (Maybe Text)
parsePort

-- | 'IRIAuth' user parser.
parseUser :: A.Parser (Maybe T.Text)
parseUser :: Parser (Maybe Text)
parseUser = forall (f :: * -> *) a. Alternative f => a -> f a -> f a
A.option forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Char -> Bool) -> Parser Text Text
A.takeWhile1 Char -> Bool
isUser forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
A.char Char
'@'))
    where isUser :: Char -> Bool
isUser Char
c = Char -> Bool
isIRI Char
c Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'@')

-- | 'IRIAuth' host parser.
parseHost :: A.Parser T.Text
parseHost :: Parser Text Text
parseHost = (Char -> Bool) -> Parser Text Text
A.takeWhile1 Char -> Bool
isHost
    where isHost :: Char -> Bool
isHost Char
c = Char -> Bool
isIRI Char
c Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'/') Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
':')

-- | 'IRIAuth' port parser.
parsePort :: A.Parser (Maybe T.Text)
parsePort :: Parser (Maybe Text)
parsePort = forall (f :: * -> *) a. Alternative f => a -> f a -> f a
A.option forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Parser Char
A.char Char
':' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Char -> Bool) -> Parser Text Text
A.takeWhile1 Char -> Bool
isDigit))

-- | 'IRI' path parser.
parsePath :: A.Parser T.Text
parsePath :: Parser Text Text
parsePath = forall (f :: * -> *) a. Alternative f => a -> f a -> f a
A.option Text
"" (Char -> Parser Char
A.char Char
'/' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Char -> Bool) -> Parser Text Text
A.takeWhile1 Char -> Bool
isPath)
    where isPath :: Char -> Bool
isPath Char
c = Char -> Bool
isIRI Char
c Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'?') Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'#')

-- | 'IRI' query parser.
parseQuery :: A.Parser (Maybe T.Text)
parseQuery :: Parser (Maybe Text)
parseQuery = forall (f :: * -> *) a. Alternative f => a -> f a -> f a
A.option forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Parser Char
A.char Char
'?' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Char -> Bool) -> Parser Text Text
A.takeWhile1 Char -> Bool
isQuery))
    where isQuery :: Char -> Bool
isQuery Char
c = Char -> Bool
isIRI Char
c Bool -> Bool -> Bool
&& (Char
cforall a. Eq a => a -> a -> Bool
/= Char
'#')

-- | 'IRI' fragment parser.
parseFragment :: A.Parser (Maybe T.Text)
parseFragment :: Parser (Maybe Text)
parseFragment = forall (f :: * -> *) a. Alternative f => a -> f a -> f a
A.option forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Parser Char
A.char Char
'#' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Char -> Bool) -> Parser Text Text
A.takeWhile1 Char -> Bool
isIRI))

-- | Parser for graph labels, i.e. either an escaped 'IRI' or the empty string.
parseGraphLabel :: A.Parser (Maybe IRI)
parseGraphLabel :: Parser (Maybe IRI)
parseGraphLabel = forall (f :: * -> *) a. Alternative f => a -> f a -> f a
A.option forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser IRI
parseEscapedIRI)

-- | 'Subject' parser.
parseSubject :: A.Parser Subject
parseSubject :: Parser Subject
parseSubject = do
    Char
c <- Parser Char
A.anyChar
    case Char
c of Char
'<' -> IRI -> Subject
IRISubject forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser IRI
parseIRI forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
A.char Char
'>')
              Char
'_' -> BlankNode -> Subject
BlankSubject forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Parser Char
A.char Char
':' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Text BlankNode
parseBlankNodeLabel)
              Char
_   -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"parseSubject: must be blank node or IRI."

-- | 'Predicate' parser.
parsePredicate :: A.Parser Predicate
parsePredicate :: Parser Predicate
parsePredicate = IRI -> Predicate
Predicate forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser IRI
parseEscapedIRI

-- | 'Object' parser.
parseObject :: A.Parser Object
parseObject :: Parser Object
parseObject = do
    Char
c <- Parser Char
A.anyChar
    case Char
c of Char
'<' -> IRI -> Object
IRIObject forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Parser IRI
parseIRI forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
A.char Char
'>')
              Char
'_' -> BlankNode -> Object
BlankObject forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Parser Char
A.char Char
':' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Text BlankNode
parseBlankNodeLabel)
              Char
_   -> Literal -> Object
LiteralObject forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Literal
parseLiteralBody

-- | Parse an escaped 'IRI', i.e. an IRI enclosed in angle brackets.
parseEscapedIRI :: A.Parser IRI
parseEscapedIRI :: Parser IRI
parseEscapedIRI = Char -> Parser Char
A.char Char
'<' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser IRI
parseIRI forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
A.char Char
'>'

-- | Parse a blank node label.
parseBlankNodeLabel :: A.Parser BlankNode
parseBlankNodeLabel :: Parser Text BlankNode
parseBlankNodeLabel = Text -> BlankNode
BlankNode forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Char -> Bool) -> Parser Text Text
A.takeWhile1 Char -> Bool
isLabel forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall {f :: * -> *}. MonadFail f => Text -> f Text
check)
    where check :: Text -> f Text
check Text
t
            | Char -> Bool
isHead (Text -> Char
T.head Text
t) Bool -> Bool -> Bool
&& Char -> Bool
isTail (Text -> Char
T.last Text
t) = forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
t
            | Bool
otherwise                              = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"parseBlankNode"
          isLabel :: Char -> Bool
isLabel  = Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
isSpace
          isHead :: Char -> Bool
isHead Char
c = Char -> Bool
isLabel Char
c
                  Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'-')
                  Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'.')
          isTail :: Char -> Bool
isTail Char
c = Char -> Bool
isLabel Char
c
                  Bool -> Bool -> Bool
&& (Char
c forall a. Eq a => a -> a -> Bool
/= Char
'.')

-- | Parse a blank node label, with the preceeding @_:@.
parseBlankNode :: A.Parser BlankNode
parseBlankNode :: Parser Text BlankNode
parseBlankNode = Text -> Parser Text Text
A.string Text
"_:" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Text BlankNode
parseBlankNodeLabel

-- | Like 'parseLiteral', but without the leading double quote.
parseLiteralBody :: A.Parser Literal
parseLiteralBody :: Parser Literal
parseLiteralBody = Text -> LiteralType -> Literal
Literal forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text Text
escString forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Text LiteralType
valType
    where valType :: Parser Text LiteralType
valType     = Parser Text LiteralType
valIRIType forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Text LiteralType
valLangType forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure LiteralType
LiteralUntyped
          valIRIType :: Parser Text LiteralType
valIRIType  = IRI -> LiteralType
LiteralIRIType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Parser Text Text
A.string Text
"^^" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser IRI
parseEscapedIRI)
          valLangType :: Parser Text LiteralType
valLangType = Text -> LiteralType
LiteralLangType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Parser Char
A.char Char
'@' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Char -> Bool) -> Parser Text Text
A.takeWhile1 Char -> Bool
isLang)
          isLang :: Char -> Bool
isLang Char
c    = Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| (Char
c forall a. Eq a => a -> a -> Bool
== Char
'-')
          escString :: Parser Text Text
escString = Text -> Text
unescapeAll forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. s -> (s -> Char -> Maybe s) -> Parser Text Text
A.scan Bool
False Bool -> Char -> Maybe Bool
machine
          machine :: Bool -> Char -> Maybe Bool
machine Bool
False Char
'\\' = forall a. a -> Maybe a
Just Bool
True
          machine Bool
False Char
'"'  = forall a. Maybe a
Nothing
          machine Bool
False Char
_    = forall a. a -> Maybe a
Just Bool
False
          machine Bool
True Char
_     = forall a. a -> Maybe a
Just Bool
False
          unescapeAll :: Text -> Text
unescapeAll = [Text] -> Text
T.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> [Text]
unescapeFrag forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [Text]
T.splitOn Text
"\\"
          unescapeFrag :: [Text] -> [Text]
unescapeFrag []     = []
          unescapeFrag (Text
f:[Text]
fs) = case Text -> Maybe (Char, Text)
T.uncons Text
f of
                Maybe (Char, Text)
Nothing        -> Text
f forall a. a -> [a] -> [a]
: [Text] -> [Text]
unescapeFrag [Text]
fs
                (Just (Char
e, Text
f')) -> Char -> Text
T.singleton (Char -> Char
unescape Char
e) forall a. a -> [a] -> [a]
: Text
f' forall a. a -> [a] -> [a]
: [Text] -> [Text]
unescapeFrag [Text]
fs
          unescape :: Char -> Char
unescape Char
't' = Char
'\t'
          unescape Char
'b' = Char
'\b'
          unescape Char
'n' = Char
'\n'
          unescape Char
'r' = Char
'\r'
          unescape Char
'f' = Char
'\f'
          unescape Char
c   = Char
c

-- | Parse an RDF 'Literal', including the 'LiteralType' if present.
parseLiteral :: A.Parser Literal
parseLiteral :: Parser Literal
parseLiteral = Char -> Parser Char
A.char Char
'"' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser Literal
parseLiteralBody

-- | Parse an unescaped untyped RDF 'Literal'.
parseUnescapedLiteral :: A.Parser Literal
parseUnescapedLiteral :: Parser Literal
parseUnescapedLiteral = Text -> LiteralType -> Literal
Literal forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text Text
A.takeText forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => a -> f a
pure LiteralType
LiteralUntyped

-- | Make implementations for 'fromString' from a 'A.Parser'.
fromStringParser :: A.Parser a    -- ^ The literal parser.
                 -> String        -- ^ The literal type name for error messages.
                 -> (String -> a) -- ^ The 'fromString' implementation.
fromStringParser :: forall a. Parser a -> String -> String -> a
fromStringParser Parser a
p String
n String
s = let t :: Text
t = String -> Text
T.pack String
s
                             r :: Either String a
r = forall a. Parser a -> Text -> Either String a
A.parseOnly Parser a
p Text
t
                         in case Either String a
r of (Left String
e)  -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat
                                                      [ String
"Invalid "
                                                      , String
n
                                                      , String
" literal ("
                                                      , String
s
                                                      , String
") "
                                                      , String
e
                                                      ]
                                      (Right a
x) -> a
x

-- | This instance uses 'parseIRI' and calls 'error' if the literal is invalid.
--   It is not clear exactly when 'fromString' is evaluated so this error is
--   difficult to explictly catch. This can be solved by ensuring that your
--   'IRI' literals are eagerly evaluated so any malformed literals can be
--   caught immediately. It would be nicer if this happened at compile time.
instance IsString IRI where
    fromString :: String -> IRI
fromString = forall a. Parser a -> String -> String -> a
fromStringParser Parser IRI
parseIRI String
"IRI"

-- | This instance uses 'parseLiteral' and calls 'error' if the literal is
--   invalid. It is not clear exactly when 'fromString' is evaluated so this
--   error is difficult to explictly catch. This can be solved by ensuring that
--   your 'Literal' literals are eagerly evaluated so any malformed literals can
--   be caught immediately. It would be nicer if this happened at compile time.
instance IsString Literal where
    fromString :: String -> Literal
fromString = forall a. Parser a -> String -> String -> a
fromStringParser Parser Literal
p String
"Literal"
        where p :: Parser Literal
p = Parser Literal
parseLiteral forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Literal
parseUnescapedLiteral

-- | This instance uses 'parseBlankNode' and calls 'error' if the literal is
--   invalid. It is not clear exactly when 'fromString' is evaluated so this
--   error is difficult to explictly catch. This can be solved by ensuring that
--   your 'BlankNode' literals are eagerly evaluated so any malformed literals
--   can be caught immediately. It would be nicer if this happened at compile
--   time.
instance IsString BlankNode  where
    fromString :: String -> BlankNode
fromString = forall a. Parser a -> String -> String -> a
fromStringParser Parser Text BlankNode
parseBlankNode String
"BlankNode"

-- | This instance uses 'parseSubject' and calls 'error' if the literal
--   is invalid. It is not clear exactly when 'fromString' is evaluated so this
--   error is difficult to explictly catch. This can be solved by ensuring that
--   your 'Subject' literals are eagerly evaluated so any malformed literals can
--   be caught immediately. It would be nicer if this happened at compile time.
instance IsString Subject where
    fromString :: String -> Subject
fromString = forall a. Parser a -> String -> String -> a
fromStringParser Parser Subject
parseSubject String
"Subject"

-- | This instance uses 'parsePredicate' and calls 'error' if the literal is
--   invalid. It is not clear exactly when 'fromString' is evaluated so this
--   error is difficult to explictly catch. This can be solved by ensuring that
--   your 'Predicate' literals are eagerly evaluated so any malformed literals
--   can be caught immediately. It would be nicer if this happened at compile
--   time.
instance IsString Predicate where
    fromString :: String -> Predicate
fromString = forall a. Parser a -> String -> String -> a
fromStringParser Parser Predicate
parsePredicate String
"Predicate"

-- | This instance uses 'parseObject' and calls 'error' if the literal is
--   invalid. It is not clear exactly when 'fromString' is evaluated so this
--   error is difficult to explictly catch. This can be solved by ensuring that
--   your 'Object' literals are eagerly evaluated so any malformed literals can
--   be caught immediately. It would be nicer if this happened at compile time.
instance IsString Object where
    fromString :: String -> Object
fromString = forall a. Parser a -> String -> String -> a
fromStringParser Parser Object
p String
"Object"
        where p :: Parser Object
p = Parser Object
parseObject forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Literal -> Object
LiteralObject forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Literal
parseUnescapedLiteral)