Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for recovering parser #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions xml-conduit/src/Text/XML/Stream/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module Text.XML.Stream.Parse
, psRetainNamespaces
, psEntityExpansionSizeLimit
, psIgnoreInternalEntityDeclarations
, psRecoverFromParseFailure
-- *** Entity decoding
, decodeXmlEntities
, decodeHtmlEntities
Expand Down Expand Up @@ -470,6 +471,7 @@ data ParseSettings = ParseSettings
-- ^ Whether to resolve any but the predefined entities.
--
-- Default: @False@
, psRecoverFromParseFailure :: Bool
}

instance Default ParseSettings where
Expand All @@ -479,6 +481,7 @@ instance Default ParseSettings where
, psDecodeIllegalCharacters = const Nothing
, psEntityExpansionSizeLimit = 8192
, psIgnoreInternalEntityDeclarations = False
, psRecoverFromParseFailure = False
}

conduitToken :: MonadThrow m => ParseSettings -> ConduitT T.Text (PositionRange, Token) m ()
Expand All @@ -488,7 +491,9 @@ parseToken :: ParseSettings -> Parser Token
parseToken settings = do
mbc <- peekChar
case mbc of
Just '<' -> char '<' >> parseLt
Just '<' -> if psRecoverFromParseFailure settings
then (char '<' >> parseLt) <|> (TokenContent <$> parseContent settings False False)
else char '<' >> parseLt
_ -> TokenContent <$> parseContent settings False False
where
parseLt = do
Expand Down Expand Up @@ -643,7 +648,14 @@ parseContent :: ParseSettings
-> Bool -- break on double quote
-> Bool -- break on single quote
-> Parser Content
parseContent (ParseSettings decodeEntities _ decodeIllegalCharacters _ _) breakDouble breakSingle = parseReference <|> (parseTextContent <?> "text content") where
parseContent (ParseSettings decodeEntities _ decodeIllegalCharacters _ _ recover) breakDouble breakSingle = parseReference <|> contentParser where
contentParser =
if recover
then (parseTextContent <?> "text content") <|> parseFailingChar
else parseTextContent <?> "text content"
parseFailingChar = do
c <- char '&' <|> char '<' <|> char '>'
return $ ContentText $ T.singleton c
parseReference = do
char' '&'
t <- parseEntityRef <|> parseHexCharRef <|> parseDecCharRef
Expand Down