module Text.XML.HaXml.SAX
( SaxElement(..)
, saxParse
) where
import Text.XML.HaXml.Types
import Text.XML.HaXml.Parse
import Text.XML.HaXml.Lex
import Text.ParserCombinators.Poly.State
data SaxElement
= SaxDocTypeDecl DocTypeDecl
| SaxProcessingInstruction ProcessingInstruction
| String
| SaxElementOpen Name [Attribute]
| SaxElementClose Name
| SaxElementTag Name [Attribute]
| SaxCharData CharData
| SaxReference Reference
saxParse :: String
-> String
-> ([SaxElement],Maybe String)
saxParse :: String -> String -> ([SaxElement], Maybe String)
saxParse String
file String
cntnt = Parser SymTabs (Posn, TokenT) SaxElement
-> SymTabs -> [(Posn, TokenT)] -> ([SaxElement], Maybe String)
forall s t a. Parser s t a -> s -> [t] -> ([a], Maybe String)
parseStream Parser SymTabs (Posn, TokenT) SaxElement
sax SymTabs
emptySTs
(String -> String -> [(Posn, TokenT)]
xmlLex String
file String
cntnt)
parseStream :: Parser s t a -> s -> [t] -> ([a], Maybe String)
parseStream :: Parser s t a -> s -> [t] -> ([a], Maybe String)
parseStream Parser s t a
_ s
_ [] = ([],Maybe String
forall a. Maybe a
Nothing)
parseStream Parser s t a
p s
state [t]
toks = case Parser s t a -> s -> [t] -> (Either String a, s, [t])
forall s t a. Parser s t a -> s -> [t] -> (Either String a, s, [t])
runParser Parser s t a
p s
state [t]
toks of
(Left String
err, s
_, [t]
_) -> ([],String -> Maybe String
forall a. a -> Maybe a
Just String
err)
(Right a
res, s
nstate, [t]
rest) -> (a
resa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
moreres, Maybe String
err)
where ([a]
moreres,Maybe String
err) = Parser s t a -> s -> [t] -> ([a], Maybe String)
forall s t a. Parser s t a -> s -> [t] -> ([a], Maybe String)
parseStream Parser s t a
p s
nstate [t]
rest
sax :: XParser SaxElement
sax :: Parser SymTabs (Posn, TokenT) SaxElement
sax = [Parser SymTabs (Posn, TokenT) SaxElement]
-> Parser SymTabs (Posn, TokenT) SaxElement
forall (p :: * -> *) a. PolyParse p => [p a] -> p a
oneOf [ Parser SymTabs (Posn, TokenT) SaxElement
saxelementopen
, Parser SymTabs (Posn, TokenT) SaxElement
saxelementclose
, Parser SymTabs (Posn, TokenT) SaxElement
saxprocessinginstruction
, Parser SymTabs (Posn, TokenT) SaxElement
saxcomment
, Parser SymTabs (Posn, TokenT) SaxElement
saxdoctypedecl
, Parser SymTabs (Posn, TokenT) SaxElement
saxreference
, Parser SymTabs (Posn, TokenT) SaxElement
saxchardata
]
Parser SymTabs (Posn, TokenT) SaxElement
-> (String -> String) -> Parser SymTabs (Posn, TokenT) SaxElement
forall (p :: * -> *) a.
Commitment p =>
p a -> (String -> String) -> p a
`adjustErr` (String -> String -> String
forall a. [a] -> [a] -> [a]
++(String
"\nLooking for a SAX event:\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++String
" elem-open, elem-close, PI, comment, DTD, ref, or chardata"))
saxelementopen :: XParser SaxElement
saxelementopen :: Parser SymTabs (Posn, TokenT) SaxElement
saxelementopen = do
TokenT -> XParser TokenT
tok TokenT
TokAnyOpen
(ElemTag (N String
n) [Attribute]
as) <- XParser ElemTag
elemtag
(( do TokenT -> XParser TokenT
tok TokenT
TokEndClose
SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> [Attribute] -> SaxElement
SaxElementTag String
n [Attribute]
as)) Parser SymTabs (Posn, TokenT) SaxElement
-> Parser SymTabs (Posn, TokenT) SaxElement
-> Parser SymTabs (Posn, TokenT) SaxElement
forall s t a. Parser s t a -> Parser s t a -> Parser s t a
`onFail`
( do TokenT -> XParser TokenT
tok TokenT
TokAnyClose
SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> [Attribute] -> SaxElement
SaxElementOpen String
n [Attribute]
as))
Parser SymTabs (Posn, TokenT) SaxElement
-> Parser SymTabs (Posn, TokenT) SaxElement
-> Parser SymTabs (Posn, TokenT) SaxElement
forall s t a. Parser s t a -> Parser s t a -> Parser s t a
`onFail` String -> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"missing > or /> in element tag")
saxelementclose :: XParser SaxElement
saxelementclose :: Parser SymTabs (Posn, TokenT) SaxElement
saxelementclose = do
TokenT -> XParser TokenT
tok TokenT
TokEndOpen
String
n <- XParser String
name
TokenT -> XParser TokenT
tok TokenT
TokAnyClose
SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> SaxElement
SaxElementClose String
n)
saxcomment :: XParser SaxElement
= XParser String
comment XParser String
-> (String -> Parser SymTabs (Posn, TokenT) SaxElement)
-> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a. Monad m => a -> m a
return (SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement)
-> (String -> SaxElement)
-> String
-> Parser SymTabs (Posn, TokenT) SaxElement
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> SaxElement
SaxComment
saxchardata :: XParser SaxElement
saxchardata :: Parser SymTabs (Posn, TokenT) SaxElement
saxchardata =
(XParser String
cdsect XParser String
-> (String -> Parser SymTabs (Posn, TokenT) SaxElement)
-> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a. Monad m => a -> m a
return (SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement)
-> (String -> SaxElement)
-> String
-> Parser SymTabs (Posn, TokenT) SaxElement
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> SaxElement
SaxCharData)
Parser SymTabs (Posn, TokenT) SaxElement
-> Parser SymTabs (Posn, TokenT) SaxElement
-> Parser SymTabs (Posn, TokenT) SaxElement
forall s t a. Parser s t a -> Parser s t a -> Parser s t a
`onFail`
(XParser String
chardata XParser String
-> (String -> Parser SymTabs (Posn, TokenT) SaxElement)
-> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a. Monad m => a -> m a
return (SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement)
-> (String -> SaxElement)
-> String
-> Parser SymTabs (Posn, TokenT) SaxElement
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> SaxElement
SaxCharData)
saxreference :: XParser SaxElement
saxreference :: Parser SymTabs (Posn, TokenT) SaxElement
saxreference = XParser Reference
reference XParser Reference
-> (Reference -> Parser SymTabs (Posn, TokenT) SaxElement)
-> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a. Monad m => a -> m a
return (SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement)
-> (Reference -> SaxElement)
-> Reference
-> Parser SymTabs (Posn, TokenT) SaxElement
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Reference -> SaxElement
SaxReference
saxdoctypedecl :: XParser SaxElement
saxdoctypedecl :: Parser SymTabs (Posn, TokenT) SaxElement
saxdoctypedecl = XParser DocTypeDecl
doctypedecl XParser DocTypeDecl
-> (DocTypeDecl -> Parser SymTabs (Posn, TokenT) SaxElement)
-> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement
forall (m :: * -> *) a. Monad m => a -> m a
return (SaxElement -> Parser SymTabs (Posn, TokenT) SaxElement)
-> (DocTypeDecl -> SaxElement)
-> DocTypeDecl
-> Parser SymTabs (Posn, TokenT) SaxElement
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DocTypeDecl -> SaxElement
SaxDocTypeDecl
saxprocessinginstruction :: XParser SaxElement
saxprocessinginstruction :: Parser SymTabs (Posn, TokenT) SaxElement
saxprocessinginstruction = (ProcessingInstruction -> SaxElement)
-> Parser SymTabs (Posn, TokenT) ProcessingInstruction
-> Parser SymTabs (Posn, TokenT) SaxElement
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ProcessingInstruction -> SaxElement
SaxProcessingInstruction Parser SymTabs (Posn, TokenT) ProcessingInstruction
processinginstruction