module Text.XML.HXT.Parser.XmlDTDTokenParser where
import Text.ParserCombinators.Parsec
import Text.XML.HXT.DOM.Interface
import Text.XML.HXT.DOM.XmlNode ( mkDTDElem'
, mkText'
)
import qualified Text.XML.HXT.Parser.XmlTokenParser as XT
import Text.XML.HXT.Parser.XmlCharParser ( XParser )
dtdDeclTokenizer :: XParser s XmlTree
dtdDeclTokenizer
= do
(dcl, al) <- dtdDeclStart
content <- many1 dtdToken
dtdDeclEnd
return $ mkDTDElem' dcl al content
dtdDeclStart :: XParser s (DTDElem, Attributes)
dtdDeclStart
= foldr1 (<|>) $
map (uncurry dtdStart) $
[ ("ELEMENT", ELEMENT )
, ("ATTLIST", ATTLIST )
, ("ENTITY", ENTITY )
, ("NOTATION", NOTATION)
]
where
dtdStart :: String -> DTDElem -> XParser s (DTDElem, Attributes)
dtdStart dcl element
= try ( do
_ <- string "<!"
_ <- string dcl
pos <- getPosition
return (element, [ (a_source, sourceName pos)
, (a_line, show (sourceLine pos))
, (a_column, show (sourceColumn pos))
]
)
)
dtdDeclEnd :: XParser s ()
dtdDeclEnd
= do
_ <- XT.gt
return ()
dtdToken :: XParser s XmlTree
dtdToken
= dtdChars
<|>
entityValue
<|>
try peReference
<|>
percent
<?> "DTD token"
peReference :: XParser s XmlTree
peReference
= do
r <- XT.peReference
return $! (mkDTDElem' PEREF [(a_peref, r)] [])
entityValue :: XParser s XmlTree
entityValue
= do
v <- XT.entityValue
return $ mkText' v
dtdChars :: XParser s XmlTree
dtdChars
= do
v <- many1 (XT.singleChar "%\"'<>[]")
return $ mkText' v
percent :: XParser s XmlTree
percent
= do
c <- char '%'
return $ mkText' [c]