{-# LANGUAGE DeriveFunctor              #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving         #-}
-- | Mid-level parsers for doctype's internal subset, built on top of "Data.XML.InternalSubset.Parser.Low":
--
-- - some formatting details are abstracted away (e.g. quoting, whitespacing), therefore parsers are not reversible
-- - entities delimited by an opening and closing sequence are recognized
-- - token parsers do not overlap, therefore XML document can be tokenized in a stateless way
--
-- All documentation examples assume the following setup:
--
-- > :set -XOverloadedStrings
-- > import Data.Attoparsec.ByteString
module Data.XML.InternalSubset.Parser.Mid
  ( module Data.XML.Parser.Mid.Comment
  , module Data.XML.Parser.Mid.ExternalID
  , module Data.XML.Parser.Mid.Instruction
  , ElementType(..)
  , AttributeList(..)
  , GeneralEntity(..)
  , ParameterEntity(..)
  , Notation(..)
  , Token(..)
  , TokenParser()
  , runTokenParser
  , tokenElementType
  , tokenAttributeList
  , tokenGeneralEntity
  , tokenParameterEntity
  , tokenNotation
  , tokenInstruction
  , tokenComment
  , anyToken
  ) where

import           Control.Applicative
import           Control.Monad.Fail.Compat
import           Data.Functor
import           Data.Text                          (Text)
import qualified Data.Text                          as Text
import           Data.XML.InternalSubset.Parser.Low
import           Data.XML.Parser.Low
import           Data.XML.Parser.Mid.Comment
import           Data.XML.Parser.Mid.ExternalID
import           Data.XML.Parser.Mid.Instruction
import           Text.Parser.Char
import           Text.Parser.Combinators

-- $setup
-- >>> :set -XOverloadedStrings
-- >>> import Data.Attoparsec.ByteString

data Token
  = TokenElementType ElementType
  | TokenAttributeList AttributeList
  | TokenGeneralEntity GeneralEntity
  | TokenParameterEntity ParameterEntity
  | TokenNotation Notation
  | TokenInstruction Instruction
  | TokenComment Text
  deriving (Token -> Token -> Bool
(Token -> Token -> Bool) -> (Token -> Token -> Bool) -> Eq Token
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Token -> Token -> Bool
$c/= :: Token -> Token -> Bool
== :: Token -> Token -> Bool
$c== :: Token -> Token -> Bool
Eq, Eq Token
Eq Token
-> (Token -> Token -> Ordering)
-> (Token -> Token -> Bool)
-> (Token -> Token -> Bool)
-> (Token -> Token -> Bool)
-> (Token -> Token -> Bool)
-> (Token -> Token -> Token)
-> (Token -> Token -> Token)
-> Ord Token
Token -> Token -> Bool
Token -> Token -> Ordering
Token -> Token -> Token
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 :: Token -> Token -> Token
$cmin :: Token -> Token -> Token
max :: Token -> Token -> Token
$cmax :: Token -> Token -> Token
>= :: Token -> Token -> Bool
$c>= :: Token -> Token -> Bool
> :: Token -> Token -> Bool
$c> :: Token -> Token -> Bool
<= :: Token -> Token -> Bool
$c<= :: Token -> Token -> Bool
< :: Token -> Token -> Bool
$c< :: Token -> Token -> Bool
compare :: Token -> Token -> Ordering
$ccompare :: Token -> Token -> Ordering
$cp1Ord :: Eq Token
Ord, Int -> Token -> ShowS
[Token] -> ShowS
Token -> String
(Int -> Token -> ShowS)
-> (Token -> String) -> ([Token] -> ShowS) -> Show Token
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Token] -> ShowS
$cshowList :: [Token] -> ShowS
show :: Token -> String
$cshow :: Token -> String
showsPrec :: Int -> Token -> ShowS
$cshowsPrec :: Int -> Token -> ShowS
Show)

-- | A parser that consumes whole 'Token's.
newtype TokenParser m a = TokenParser { TokenParser m a -> m a
runTokenParser :: m a }

deriving instance Functor m => Functor (TokenParser m)
deriving instance Applicative m => Applicative (TokenParser m)
deriving instance Alternative m => Alternative (TokenParser m)
deriving instance Monad m => Monad (TokenParser m)

instance (Parsing m, Monad m) => MonadFail (TokenParser m) where
  fail :: String -> TokenParser m a
fail = m a -> TokenParser m a
forall (m :: * -> *) a. m a -> TokenParser m a
TokenParser (m a -> TokenParser m a)
-> (String -> m a) -> String -> TokenParser m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m a
forall (m :: * -> *) a. Parsing m => String -> m a
unexpected

-- | <https://www.w3.org/TR/REC-xml/#dt-eldecl>
data ElementType = ElementType Text ElementTypeContent
  deriving (ElementType -> ElementType -> Bool
(ElementType -> ElementType -> Bool)
-> (ElementType -> ElementType -> Bool) -> Eq ElementType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ElementType -> ElementType -> Bool
$c/= :: ElementType -> ElementType -> Bool
== :: ElementType -> ElementType -> Bool
$c== :: ElementType -> ElementType -> Bool
Eq, Eq ElementType
Eq ElementType
-> (ElementType -> ElementType -> Ordering)
-> (ElementType -> ElementType -> Bool)
-> (ElementType -> ElementType -> Bool)
-> (ElementType -> ElementType -> Bool)
-> (ElementType -> ElementType -> Bool)
-> (ElementType -> ElementType -> ElementType)
-> (ElementType -> ElementType -> ElementType)
-> Ord ElementType
ElementType -> ElementType -> Bool
ElementType -> ElementType -> Ordering
ElementType -> ElementType -> ElementType
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 :: ElementType -> ElementType -> ElementType
$cmin :: ElementType -> ElementType -> ElementType
max :: ElementType -> ElementType -> ElementType
$cmax :: ElementType -> ElementType -> ElementType
>= :: ElementType -> ElementType -> Bool
$c>= :: ElementType -> ElementType -> Bool
> :: ElementType -> ElementType -> Bool
$c> :: ElementType -> ElementType -> Bool
<= :: ElementType -> ElementType -> Bool
$c<= :: ElementType -> ElementType -> Bool
< :: ElementType -> ElementType -> Bool
$c< :: ElementType -> ElementType -> Bool
compare :: ElementType -> ElementType -> Ordering
$ccompare :: ElementType -> ElementType -> Ordering
$cp1Ord :: Eq ElementType
Ord, ReadPrec [ElementType]
ReadPrec ElementType
Int -> ReadS ElementType
ReadS [ElementType]
(Int -> ReadS ElementType)
-> ReadS [ElementType]
-> ReadPrec ElementType
-> ReadPrec [ElementType]
-> Read ElementType
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ElementType]
$creadListPrec :: ReadPrec [ElementType]
readPrec :: ReadPrec ElementType
$creadPrec :: ReadPrec ElementType
readList :: ReadS [ElementType]
$creadList :: ReadS [ElementType]
readsPrec :: Int -> ReadS ElementType
$creadsPrec :: Int -> ReadS ElementType
Read, Int -> ElementType -> ShowS
[ElementType] -> ShowS
ElementType -> String
(Int -> ElementType -> ShowS)
-> (ElementType -> String)
-> ([ElementType] -> ShowS)
-> Show ElementType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ElementType] -> ShowS
$cshowList :: [ElementType] -> ShowS
show :: ElementType -> String
$cshow :: ElementType -> String
showsPrec :: Int -> ElementType -> ShowS
$cshowsPrec :: Int -> ElementType -> ShowS
Show)

-- | <https://www.w3.org/TR/REC-xml/#NT-elementdecl>
--
-- Missing: mixed | children
data ElementTypeContent = ContentEmpty | ContentAny
  deriving (ElementTypeContent -> ElementTypeContent -> Bool
(ElementTypeContent -> ElementTypeContent -> Bool)
-> (ElementTypeContent -> ElementTypeContent -> Bool)
-> Eq ElementTypeContent
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ElementTypeContent -> ElementTypeContent -> Bool
$c/= :: ElementTypeContent -> ElementTypeContent -> Bool
== :: ElementTypeContent -> ElementTypeContent -> Bool
$c== :: ElementTypeContent -> ElementTypeContent -> Bool
Eq, Eq ElementTypeContent
Eq ElementTypeContent
-> (ElementTypeContent -> ElementTypeContent -> Ordering)
-> (ElementTypeContent -> ElementTypeContent -> Bool)
-> (ElementTypeContent -> ElementTypeContent -> Bool)
-> (ElementTypeContent -> ElementTypeContent -> Bool)
-> (ElementTypeContent -> ElementTypeContent -> Bool)
-> (ElementTypeContent -> ElementTypeContent -> ElementTypeContent)
-> (ElementTypeContent -> ElementTypeContent -> ElementTypeContent)
-> Ord ElementTypeContent
ElementTypeContent -> ElementTypeContent -> Bool
ElementTypeContent -> ElementTypeContent -> Ordering
ElementTypeContent -> ElementTypeContent -> ElementTypeContent
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 :: ElementTypeContent -> ElementTypeContent -> ElementTypeContent
$cmin :: ElementTypeContent -> ElementTypeContent -> ElementTypeContent
max :: ElementTypeContent -> ElementTypeContent -> ElementTypeContent
$cmax :: ElementTypeContent -> ElementTypeContent -> ElementTypeContent
>= :: ElementTypeContent -> ElementTypeContent -> Bool
$c>= :: ElementTypeContent -> ElementTypeContent -> Bool
> :: ElementTypeContent -> ElementTypeContent -> Bool
$c> :: ElementTypeContent -> ElementTypeContent -> Bool
<= :: ElementTypeContent -> ElementTypeContent -> Bool
$c<= :: ElementTypeContent -> ElementTypeContent -> Bool
< :: ElementTypeContent -> ElementTypeContent -> Bool
$c< :: ElementTypeContent -> ElementTypeContent -> Bool
compare :: ElementTypeContent -> ElementTypeContent -> Ordering
$ccompare :: ElementTypeContent -> ElementTypeContent -> Ordering
$cp1Ord :: Eq ElementTypeContent
Ord, ReadPrec [ElementTypeContent]
ReadPrec ElementTypeContent
Int -> ReadS ElementTypeContent
ReadS [ElementTypeContent]
(Int -> ReadS ElementTypeContent)
-> ReadS [ElementTypeContent]
-> ReadPrec ElementTypeContent
-> ReadPrec [ElementTypeContent]
-> Read ElementTypeContent
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ElementTypeContent]
$creadListPrec :: ReadPrec [ElementTypeContent]
readPrec :: ReadPrec ElementTypeContent
$creadPrec :: ReadPrec ElementTypeContent
readList :: ReadS [ElementTypeContent]
$creadList :: ReadS [ElementTypeContent]
readsPrec :: Int -> ReadS ElementTypeContent
$creadsPrec :: Int -> ReadS ElementTypeContent
Read, Int -> ElementTypeContent -> ShowS
[ElementTypeContent] -> ShowS
ElementTypeContent -> String
(Int -> ElementTypeContent -> ShowS)
-> (ElementTypeContent -> String)
-> ([ElementTypeContent] -> ShowS)
-> Show ElementTypeContent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ElementTypeContent] -> ShowS
$cshowList :: [ElementTypeContent] -> ShowS
show :: ElementTypeContent -> String
$cshow :: ElementTypeContent -> String
showsPrec :: Int -> ElementTypeContent -> ShowS
$cshowsPrec :: Int -> ElementTypeContent -> ShowS
Show)

-- | <https://www.w3.org/TR/REC-xml/#NT-AttlistDecl>
--
-- Missing: attribute definition
newtype AttributeList = AttributeList Text
  deriving (AttributeList -> AttributeList -> Bool
(AttributeList -> AttributeList -> Bool)
-> (AttributeList -> AttributeList -> Bool) -> Eq AttributeList
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AttributeList -> AttributeList -> Bool
$c/= :: AttributeList -> AttributeList -> Bool
== :: AttributeList -> AttributeList -> Bool
$c== :: AttributeList -> AttributeList -> Bool
Eq, Eq AttributeList
Eq AttributeList
-> (AttributeList -> AttributeList -> Ordering)
-> (AttributeList -> AttributeList -> Bool)
-> (AttributeList -> AttributeList -> Bool)
-> (AttributeList -> AttributeList -> Bool)
-> (AttributeList -> AttributeList -> Bool)
-> (AttributeList -> AttributeList -> AttributeList)
-> (AttributeList -> AttributeList -> AttributeList)
-> Ord AttributeList
AttributeList -> AttributeList -> Bool
AttributeList -> AttributeList -> Ordering
AttributeList -> AttributeList -> AttributeList
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 :: AttributeList -> AttributeList -> AttributeList
$cmin :: AttributeList -> AttributeList -> AttributeList
max :: AttributeList -> AttributeList -> AttributeList
$cmax :: AttributeList -> AttributeList -> AttributeList
>= :: AttributeList -> AttributeList -> Bool
$c>= :: AttributeList -> AttributeList -> Bool
> :: AttributeList -> AttributeList -> Bool
$c> :: AttributeList -> AttributeList -> Bool
<= :: AttributeList -> AttributeList -> Bool
$c<= :: AttributeList -> AttributeList -> Bool
< :: AttributeList -> AttributeList -> Bool
$c< :: AttributeList -> AttributeList -> Bool
compare :: AttributeList -> AttributeList -> Ordering
$ccompare :: AttributeList -> AttributeList -> Ordering
$cp1Ord :: Eq AttributeList
Ord, ReadPrec [AttributeList]
ReadPrec AttributeList
Int -> ReadS AttributeList
ReadS [AttributeList]
(Int -> ReadS AttributeList)
-> ReadS [AttributeList]
-> ReadPrec AttributeList
-> ReadPrec [AttributeList]
-> Read AttributeList
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [AttributeList]
$creadListPrec :: ReadPrec [AttributeList]
readPrec :: ReadPrec AttributeList
$creadPrec :: ReadPrec AttributeList
readList :: ReadS [AttributeList]
$creadList :: ReadS [AttributeList]
readsPrec :: Int -> ReadS AttributeList
$creadsPrec :: Int -> ReadS AttributeList
Read, Int -> AttributeList -> ShowS
[AttributeList] -> ShowS
AttributeList -> String
(Int -> AttributeList -> ShowS)
-> (AttributeList -> String)
-> ([AttributeList] -> ShowS)
-> Show AttributeList
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AttributeList] -> ShowS
$cshowList :: [AttributeList] -> ShowS
show :: AttributeList -> String
$cshow :: AttributeList -> String
showsPrec :: Int -> AttributeList -> ShowS
$cshowsPrec :: Int -> AttributeList -> ShowS
Show)

-- | <https://www.w3.org/TR/REC-xml/#gen-entity>
data GeneralEntity = GeneralEntity Text [Content]
  deriving (GeneralEntity -> GeneralEntity -> Bool
(GeneralEntity -> GeneralEntity -> Bool)
-> (GeneralEntity -> GeneralEntity -> Bool) -> Eq GeneralEntity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GeneralEntity -> GeneralEntity -> Bool
$c/= :: GeneralEntity -> GeneralEntity -> Bool
== :: GeneralEntity -> GeneralEntity -> Bool
$c== :: GeneralEntity -> GeneralEntity -> Bool
Eq, Eq GeneralEntity
Eq GeneralEntity
-> (GeneralEntity -> GeneralEntity -> Ordering)
-> (GeneralEntity -> GeneralEntity -> Bool)
-> (GeneralEntity -> GeneralEntity -> Bool)
-> (GeneralEntity -> GeneralEntity -> Bool)
-> (GeneralEntity -> GeneralEntity -> Bool)
-> (GeneralEntity -> GeneralEntity -> GeneralEntity)
-> (GeneralEntity -> GeneralEntity -> GeneralEntity)
-> Ord GeneralEntity
GeneralEntity -> GeneralEntity -> Bool
GeneralEntity -> GeneralEntity -> Ordering
GeneralEntity -> GeneralEntity -> GeneralEntity
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 :: GeneralEntity -> GeneralEntity -> GeneralEntity
$cmin :: GeneralEntity -> GeneralEntity -> GeneralEntity
max :: GeneralEntity -> GeneralEntity -> GeneralEntity
$cmax :: GeneralEntity -> GeneralEntity -> GeneralEntity
>= :: GeneralEntity -> GeneralEntity -> Bool
$c>= :: GeneralEntity -> GeneralEntity -> Bool
> :: GeneralEntity -> GeneralEntity -> Bool
$c> :: GeneralEntity -> GeneralEntity -> Bool
<= :: GeneralEntity -> GeneralEntity -> Bool
$c<= :: GeneralEntity -> GeneralEntity -> Bool
< :: GeneralEntity -> GeneralEntity -> Bool
$c< :: GeneralEntity -> GeneralEntity -> Bool
compare :: GeneralEntity -> GeneralEntity -> Ordering
$ccompare :: GeneralEntity -> GeneralEntity -> Ordering
$cp1Ord :: Eq GeneralEntity
Ord, ReadPrec [GeneralEntity]
ReadPrec GeneralEntity
Int -> ReadS GeneralEntity
ReadS [GeneralEntity]
(Int -> ReadS GeneralEntity)
-> ReadS [GeneralEntity]
-> ReadPrec GeneralEntity
-> ReadPrec [GeneralEntity]
-> Read GeneralEntity
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GeneralEntity]
$creadListPrec :: ReadPrec [GeneralEntity]
readPrec :: ReadPrec GeneralEntity
$creadPrec :: ReadPrec GeneralEntity
readList :: ReadS [GeneralEntity]
$creadList :: ReadS [GeneralEntity]
readsPrec :: Int -> ReadS GeneralEntity
$creadsPrec :: Int -> ReadS GeneralEntity
Read, Int -> GeneralEntity -> ShowS
[GeneralEntity] -> ShowS
GeneralEntity -> String
(Int -> GeneralEntity -> ShowS)
-> (GeneralEntity -> String)
-> ([GeneralEntity] -> ShowS)
-> Show GeneralEntity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GeneralEntity] -> ShowS
$cshowList :: [GeneralEntity] -> ShowS
show :: GeneralEntity -> String
$cshow :: GeneralEntity -> String
showsPrec :: Int -> GeneralEntity -> ShowS
$cshowsPrec :: Int -> GeneralEntity -> ShowS
Show)

-- | <https://www.w3.org/TR/REC-xml/#dt-PE>
data ParameterEntity = ParameterEntity Text [EntityValue]
  deriving (ParameterEntity -> ParameterEntity -> Bool
(ParameterEntity -> ParameterEntity -> Bool)
-> (ParameterEntity -> ParameterEntity -> Bool)
-> Eq ParameterEntity
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParameterEntity -> ParameterEntity -> Bool
$c/= :: ParameterEntity -> ParameterEntity -> Bool
== :: ParameterEntity -> ParameterEntity -> Bool
$c== :: ParameterEntity -> ParameterEntity -> Bool
Eq, Eq ParameterEntity
Eq ParameterEntity
-> (ParameterEntity -> ParameterEntity -> Ordering)
-> (ParameterEntity -> ParameterEntity -> Bool)
-> (ParameterEntity -> ParameterEntity -> Bool)
-> (ParameterEntity -> ParameterEntity -> Bool)
-> (ParameterEntity -> ParameterEntity -> Bool)
-> (ParameterEntity -> ParameterEntity -> ParameterEntity)
-> (ParameterEntity -> ParameterEntity -> ParameterEntity)
-> Ord ParameterEntity
ParameterEntity -> ParameterEntity -> Bool
ParameterEntity -> ParameterEntity -> Ordering
ParameterEntity -> ParameterEntity -> ParameterEntity
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 :: ParameterEntity -> ParameterEntity -> ParameterEntity
$cmin :: ParameterEntity -> ParameterEntity -> ParameterEntity
max :: ParameterEntity -> ParameterEntity -> ParameterEntity
$cmax :: ParameterEntity -> ParameterEntity -> ParameterEntity
>= :: ParameterEntity -> ParameterEntity -> Bool
$c>= :: ParameterEntity -> ParameterEntity -> Bool
> :: ParameterEntity -> ParameterEntity -> Bool
$c> :: ParameterEntity -> ParameterEntity -> Bool
<= :: ParameterEntity -> ParameterEntity -> Bool
$c<= :: ParameterEntity -> ParameterEntity -> Bool
< :: ParameterEntity -> ParameterEntity -> Bool
$c< :: ParameterEntity -> ParameterEntity -> Bool
compare :: ParameterEntity -> ParameterEntity -> Ordering
$ccompare :: ParameterEntity -> ParameterEntity -> Ordering
$cp1Ord :: Eq ParameterEntity
Ord, ReadPrec [ParameterEntity]
ReadPrec ParameterEntity
Int -> ReadS ParameterEntity
ReadS [ParameterEntity]
(Int -> ReadS ParameterEntity)
-> ReadS [ParameterEntity]
-> ReadPrec ParameterEntity
-> ReadPrec [ParameterEntity]
-> Read ParameterEntity
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ParameterEntity]
$creadListPrec :: ReadPrec [ParameterEntity]
readPrec :: ReadPrec ParameterEntity
$creadPrec :: ReadPrec ParameterEntity
readList :: ReadS [ParameterEntity]
$creadList :: ReadS [ParameterEntity]
readsPrec :: Int -> ReadS ParameterEntity
$creadsPrec :: Int -> ReadS ParameterEntity
Read, Int -> ParameterEntity -> ShowS
[ParameterEntity] -> ShowS
ParameterEntity -> String
(Int -> ParameterEntity -> ShowS)
-> (ParameterEntity -> String)
-> ([ParameterEntity] -> ShowS)
-> Show ParameterEntity
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParameterEntity] -> ShowS
$cshowList :: [ParameterEntity] -> ShowS
show :: ParameterEntity -> String
$cshow :: ParameterEntity -> String
showsPrec :: Int -> ParameterEntity -> ShowS
$cshowsPrec :: Int -> ParameterEntity -> ShowS
Show)

-- | <https://www.w3.org/TR/REC-xml/#dt-notation>
data Notation = Notation Text ExternalID
  deriving (Notation -> Notation -> Bool
(Notation -> Notation -> Bool)
-> (Notation -> Notation -> Bool) -> Eq Notation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Notation -> Notation -> Bool
$c/= :: Notation -> Notation -> Bool
== :: Notation -> Notation -> Bool
$c== :: Notation -> Notation -> Bool
Eq, Eq Notation
Eq Notation
-> (Notation -> Notation -> Ordering)
-> (Notation -> Notation -> Bool)
-> (Notation -> Notation -> Bool)
-> (Notation -> Notation -> Bool)
-> (Notation -> Notation -> Bool)
-> (Notation -> Notation -> Notation)
-> (Notation -> Notation -> Notation)
-> Ord Notation
Notation -> Notation -> Bool
Notation -> Notation -> Ordering
Notation -> Notation -> Notation
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 :: Notation -> Notation -> Notation
$cmin :: Notation -> Notation -> Notation
max :: Notation -> Notation -> Notation
$cmax :: Notation -> Notation -> Notation
>= :: Notation -> Notation -> Bool
$c>= :: Notation -> Notation -> Bool
> :: Notation -> Notation -> Bool
$c> :: Notation -> Notation -> Bool
<= :: Notation -> Notation -> Bool
$c<= :: Notation -> Notation -> Bool
< :: Notation -> Notation -> Bool
$c< :: Notation -> Notation -> Bool
compare :: Notation -> Notation -> Ordering
$ccompare :: Notation -> Notation -> Ordering
$cp1Ord :: Eq Notation
Ord, ReadPrec [Notation]
ReadPrec Notation
Int -> ReadS Notation
ReadS [Notation]
(Int -> ReadS Notation)
-> ReadS [Notation]
-> ReadPrec Notation
-> ReadPrec [Notation]
-> Read Notation
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Notation]
$creadListPrec :: ReadPrec [Notation]
readPrec :: ReadPrec Notation
$creadPrec :: ReadPrec Notation
readList :: ReadS [Notation]
$creadList :: ReadS [Notation]
readsPrec :: Int -> ReadS Notation
$creadsPrec :: Int -> ReadS Notation
Read, Int -> Notation -> ShowS
[Notation] -> ShowS
Notation -> String
(Int -> Notation -> ShowS)
-> (Notation -> String) -> ([Notation] -> ShowS) -> Show Notation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Notation] -> ShowS
$cshowList :: [Notation] -> ShowS
show :: Notation -> String
$cshow :: Notation -> String
showsPrec :: Int -> Notation -> ShowS
$cshowsPrec :: Int -> Notation -> ShowS
Show)


-- | <https://www.w3.org/TR/REC-xml/#NT-contentspec>
elementTypeContent :: CharParsing m => m ElementTypeContent
elementTypeContent :: m ElementTypeContent
elementTypeContent = (m ()
forall (m :: * -> *). CharParsing m => m ()
tokenElementTypeContentEmpty m () -> ElementTypeContent -> m ElementTypeContent
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> ElementTypeContent
ContentEmpty)
  m ElementTypeContent
-> m ElementTypeContent -> m ElementTypeContent
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (m ()
forall (m :: * -> *). CharParsing m => m ()
tokenElementTypeContentAny m () -> ElementTypeContent -> m ElementTypeContent
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> ElementTypeContent
ContentAny)

-- | <https://www.w3.org/TR/REC-xml/#NT-elementdecl>
--
-- >>> parseOnly (runTokenParser tokenElementType) "<!ELEMENT br EMPTY>"
-- Right (ElementType "br" ContentEmpty)
-- >>> parseOnly (runTokenParser tokenElementType) "<!ELEMENT container ANY>"
-- Right (ElementType "container" ContentAny)
tokenElementType :: CharParsing m => Monad m => TokenParser m ElementType
tokenElementType :: TokenParser m ElementType
tokenElementType = m ElementType -> TokenParser m ElementType
forall (m :: * -> *) a. m a -> TokenParser m a
TokenParser (m ElementType -> TokenParser m ElementType)
-> m ElementType -> TokenParser m ElementType
forall a b. (a -> b) -> a -> b
$ do
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenElementTypeOpen
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  Text
name <- m Text
forall (m :: * -> *). (CharParsing m, Monad m) => m Text
tokenName
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  ElementTypeContent
content <- m ElementTypeContent
forall (m :: * -> *). CharParsing m => m ElementTypeContent
elementTypeContent
  m String -> m (Maybe String)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenElementClose
  ElementType -> m ElementType
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ElementType -> m ElementType) -> ElementType -> m ElementType
forall a b. (a -> b) -> a -> b
$ Text -> ElementTypeContent -> ElementType
ElementType Text
name ElementTypeContent
content

-- | <https://www.w3.org/TR/REC-xml/#NT-AttlistDecl>
--
-- >>> parseOnly (runTokenParser tokenAttributeList) "<!ATTLIST termdef>"
-- Right (AttributeList "termdef")
tokenAttributeList :: CharParsing m => Monad m => TokenParser m AttributeList
tokenAttributeList :: TokenParser m AttributeList
tokenAttributeList = m AttributeList -> TokenParser m AttributeList
forall (m :: * -> *) a. m a -> TokenParser m a
TokenParser (m AttributeList -> TokenParser m AttributeList)
-> m AttributeList -> TokenParser m AttributeList
forall a b. (a -> b) -> a -> b
$ do
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenAttributeListOpen
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  Text
name <- m Text
forall (m :: * -> *). (CharParsing m, Monad m) => m Text
tokenName
  m String -> m (Maybe String)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenElementClose
  AttributeList -> m AttributeList
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AttributeList -> m AttributeList)
-> AttributeList -> m AttributeList
forall a b. (a -> b) -> a -> b
$ Text -> AttributeList
AttributeList Text
name

-- | <https://www.w3.org/TR/REC-xml/#NT-GEDecl>
--
-- >>> parseOnly (runTokenParser tokenGeneralEntity) "<!ENTITY d '&#xD;'>"
-- Right (GeneralEntity "d" [ContentReference (CharRef '\r')])
-- >>> parseOnly (runTokenParser tokenGeneralEntity) "<!ENTITY da '&#xD;&#xA;'>"
-- Right (GeneralEntity "da" [ContentReference (CharRef '\r'),ContentReference (CharRef '\n')])
-- >>> parseOnly (runTokenParser tokenGeneralEntity) "<!ENTITY Pub-Status 'This is a pre-release of the specification.'>"
-- Right (GeneralEntity "Pub-Status" [ContentText "This is a pre-release of the specification."])
tokenGeneralEntity :: CharParsing m => Monad m => TokenParser m GeneralEntity
tokenGeneralEntity :: TokenParser m GeneralEntity
tokenGeneralEntity = m GeneralEntity -> TokenParser m GeneralEntity
forall (m :: * -> *) a. m a -> TokenParser m a
TokenParser (m GeneralEntity -> TokenParser m GeneralEntity)
-> m GeneralEntity -> TokenParser m GeneralEntity
forall a b. (a -> b) -> a -> b
$ do
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenEntityOpen
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  Text
name <- m Text
forall (m :: * -> *). (CharParsing m, Monad m) => m Text
tokenName
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  Char
quote <- m Char
forall (m :: * -> *). CharParsing m => m Char
tokenQuote
  [Content]
definition <- m Content -> m [Content]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (String -> m Content
forall (m :: * -> *).
(CharParsing m, Monad m) =>
String -> m Content
tokenContent (String -> m Content) -> String -> m Content
forall a b. (a -> b) -> a -> b
$ Char
quoteChar -> ShowS
forall a. a -> [a] -> [a]
:String
"%")
  Char -> m Char
forall (m :: * -> *). CharParsing m => Char -> m Char
char Char
quote
  m String -> m (Maybe String)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenElementClose
  GeneralEntity -> m GeneralEntity
forall (m :: * -> *) a. Monad m => a -> m a
return (GeneralEntity -> m GeneralEntity)
-> GeneralEntity -> m GeneralEntity
forall a b. (a -> b) -> a -> b
$ Text -> [Content] -> GeneralEntity
GeneralEntity Text
name [Content]
definition

-- | <https://www.w3.org/TR/REC-xml/#NT-PEDecl>
--
-- >>> parseOnly (runTokenParser tokenParameterEntity) "<!ENTITY % YN '\"Yes\"' >"
-- Right (ParameterEntity "YN" [ValueText "\"Yes\""])
tokenParameterEntity :: CharParsing m => Monad m => TokenParser m ParameterEntity
tokenParameterEntity :: TokenParser m ParameterEntity
tokenParameterEntity = m ParameterEntity -> TokenParser m ParameterEntity
forall (m :: * -> *) a. m a -> TokenParser m a
TokenParser (m ParameterEntity -> TokenParser m ParameterEntity)
-> m ParameterEntity -> TokenParser m ParameterEntity
forall a b. (a -> b) -> a -> b
$ do
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenEntityOpen
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenPercent
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  Text
name <- m Text
forall (m :: * -> *). (CharParsing m, Monad m) => m Text
tokenName
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  Char
quote <- m Char
forall (m :: * -> *). CharParsing m => m Char
tokenQuote
  [EntityValue]
definition <- m EntityValue -> m [EntityValue]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (String -> m EntityValue
forall (m :: * -> *).
(CharParsing m, Monad m) =>
String -> m EntityValue
tokenEntityValue [Char
quote])
  Char -> m Char
forall (m :: * -> *). CharParsing m => Char -> m Char
char Char
quote
  m String -> m (Maybe String)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenElementClose
  ParameterEntity -> m ParameterEntity
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParameterEntity -> m ParameterEntity)
-> ParameterEntity -> m ParameterEntity
forall a b. (a -> b) -> a -> b
$ Text -> [EntityValue] -> ParameterEntity
ParameterEntity Text
name [EntityValue]
definition

-- | <https://www.w3.org/TR/REC-xml/#NT-NotationDecl>
--
-- >>> parseOnly (runTokenParser tokenNotation) "<!NOTATION foo SYSTEM \"identifier\">"
-- Right (Notation "foo" (SystemID "identifier"))
tokenNotation :: CharParsing m => Monad m => TokenParser m Notation
tokenNotation :: TokenParser m Notation
tokenNotation = m Notation -> TokenParser m Notation
forall (m :: * -> *) a. m a -> TokenParser m a
TokenParser (m Notation -> TokenParser m Notation)
-> m Notation -> TokenParser m Notation
forall a b. (a -> b) -> a -> b
$ do
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenNotationOpen
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  Text
name <- m Text
forall (m :: * -> *). (CharParsing m, Monad m) => m Text
tokenName
  m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  ExternalID
externalEntity <- m ExternalID
forall (m :: * -> *). (CharParsing m, Monad m) => m ExternalID
externalID -- TODO: support PUBLIC ID
  m String -> m (Maybe String)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional m String
forall (m :: * -> *). CharParsing m => m String
tokenWhitespace
  m ()
forall (m :: * -> *). CharParsing m => m ()
tokenElementClose
  Notation -> m Notation
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Notation -> m Notation) -> Notation -> m Notation
forall a b. (a -> b) -> a -> b
$ Text -> ExternalID -> Notation
Notation Text
name ExternalID
externalEntity

-- | <https://www.w3.org/TR/REC-xml/#dt-pi>
tokenInstruction :: CharParsing m => Monad m => TokenParser m Instruction
tokenInstruction :: TokenParser m Instruction
tokenInstruction = m Instruction -> TokenParser m Instruction
forall (m :: * -> *) a. m a -> TokenParser m a
TokenParser m Instruction
forall (m :: * -> *). (CharParsing m, Monad m) => m Instruction
instruction

-- | <https://www.w3.org/TR/REC-xml/#NT-Comment>
tokenComment :: CharParsing m => Monad m => TokenParser m Text
tokenComment :: TokenParser m Text
tokenComment = m Text -> TokenParser m Text
forall (m :: * -> *) a. m a -> TokenParser m a
TokenParser m Text
forall (m :: * -> *). (CharParsing m, Monad m) => m Text
comment

-- | Parse any 'Token'.
anyToken :: CharParsing m => Monad m => TokenParser m Token
anyToken :: TokenParser m Token
anyToken = ElementType -> Token
TokenElementType (ElementType -> Token)
-> TokenParser m ElementType -> TokenParser m Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TokenParser m ElementType
forall (m :: * -> *).
(CharParsing m, Monad m) =>
TokenParser m ElementType
tokenElementType
  TokenParser m Token -> TokenParser m Token -> TokenParser m Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> AttributeList -> Token
TokenAttributeList (AttributeList -> Token)
-> TokenParser m AttributeList -> TokenParser m Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TokenParser m AttributeList
forall (m :: * -> *).
(CharParsing m, Monad m) =>
TokenParser m AttributeList
tokenAttributeList
  TokenParser m Token -> TokenParser m Token -> TokenParser m Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> GeneralEntity -> Token
TokenGeneralEntity (GeneralEntity -> Token)
-> TokenParser m GeneralEntity -> TokenParser m Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TokenParser m GeneralEntity
forall (m :: * -> *).
(CharParsing m, Monad m) =>
TokenParser m GeneralEntity
tokenGeneralEntity
  TokenParser m Token -> TokenParser m Token -> TokenParser m Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParameterEntity -> Token
TokenParameterEntity (ParameterEntity -> Token)
-> TokenParser m ParameterEntity -> TokenParser m Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TokenParser m ParameterEntity
forall (m :: * -> *).
(CharParsing m, Monad m) =>
TokenParser m ParameterEntity
tokenParameterEntity
  TokenParser m Token -> TokenParser m Token -> TokenParser m Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Notation -> Token
TokenNotation (Notation -> Token)
-> TokenParser m Notation -> TokenParser m Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TokenParser m Notation
forall (m :: * -> *).
(CharParsing m, Monad m) =>
TokenParser m Notation
tokenNotation
  TokenParser m Token -> TokenParser m Token -> TokenParser m Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Instruction -> Token
TokenInstruction (Instruction -> Token)
-> TokenParser m Instruction -> TokenParser m Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TokenParser m Instruction
forall (m :: * -> *).
(CharParsing m, Monad m) =>
TokenParser m Instruction
tokenInstruction
  TokenParser m Token -> TokenParser m Token -> TokenParser m Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Token
TokenComment (Text -> Token) -> TokenParser m Text -> TokenParser m Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TokenParser m Text
forall (m :: * -> *).
(CharParsing m, Monad m) =>
TokenParser m Text
tokenComment