Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data ParserData = ParserData {}
- pdCursor :: Lens' ParserData Cursor
- pdAxis :: Lens' ParserData Axis
- pdPath :: Lens' ParserData [Text]
- data ParserError
- = PENotFound { }
- | PEWrongFormat {
- _pePath :: ![Text]
- _peDetails :: Text
- | PEWrongTagName {
- _pePath :: ![Text]
- _peDetails :: !Text
- | PENotElement { }
- | PEContentNotFound { }
- | PEOther {
- _pePath :: ![Text]
- _peDetails :: !Text
- pePath :: Lens' ParserError [Text]
- peDetails :: Traversal' ParserError Text
- newtype ParserErrors = ParserErrors {}
- throwParserError :: ([Text] -> ParserError) -> DomParser a
- renderPath :: [Text] -> String
- type DomParser = ExceptT [ParserError] (Reader ParserData)
- runDomParser :: Document -> DomParser a -> Either [ParserError] a
- type ContentParser a = Text -> Either Text a
- unitFromDom :: DomParser ()
- voidFromDom :: DomParser Void
- textFromContent :: ContentParser Text
- stringFromContent :: ContentParser String
- charFromContent :: ContentParser Char
- intFromContent :: ContentParser Int
- integerFromContent :: ContentParser Integer
- doubleFromContent :: ContentParser Double
- fixedFromContent :: (HasResolution a, Typeable a) => ContentParser (Fixed a)
- boolFromContent :: ContentParser Bool
- class FromDom a where
- class FromContent a where
- fromContent :: ContentParser a
- inElem :: Text -> DomParser a -> DomParser a
- inElems :: Text -> DomParser a -> DomParser [a]
- nonEmptyInElems :: Text -> DomParser a -> DomParser (NonEmpty a)
- maybeInElem :: Text -> DomParser a -> DomParser (Maybe a)
- inElemsPred :: Traversable f => ([Cursor] -> f Cursor) -> Text -> DomParser a -> DomParser (f a)
- inAxis :: [Text] -> Axis -> DomParser a -> DomParser a
- inDescendants :: DomParser a -> DomParser a
- inTags :: [Text] -> DomParser a -> DomParser a
- tryCurrentContent :: ContentParser a -> DomParser (Maybe a)
- tryCurrentContentText :: DomParser (Maybe Text)
- currentContent :: ContentParser a -> DomParser a
- tryCurrentName :: DomParser (Maybe Name)
- currentName :: DomParser Name
- tryCurrentAttr :: Text -> DomParser (Maybe Text)
- currentAttr :: Text -> DomParser Text
- elemContent :: Text -> ContentParser a -> DomParser a
- nonEmptyElemsContent :: Text -> ContentParser a -> DomParser (NonEmpty a)
- elemsContent :: Text -> ContentParser a -> DomParser [a]
- maybeElemContent :: Text -> ContentParser a -> DomParser (Maybe a)
- fromContentR :: forall a. (Read a, Typeable a) => Text -> Either Text a
- newtype CurrentContent a = CurrentContent {
- unCurrentContent :: a
- currentNodes :: DomParser [Node]
- checkCurrentLaxName :: Text -> DomParser ()
- checkCurrentName :: (Name -> Maybe Text) -> DomParser ()
Parser internals
pdPath :: Lens' ParserData [Text] Source
data ParserError Source
DOM parser error description.
PENotFound | Tag not found which should be. |
PEWrongFormat | Tag contents has wrong format, (could not read text to value) |
| |
PEWrongTagName | Such tag name is not expected in this place |
| |
PENotElement | Node is not an element but should be |
PEContentNotFound | Node should have text content, but it does not. |
PEOther | Any other error |
|
pePath :: Lens' ParserError [Text] Source
newtype ParserErrors Source
throwParserError :: ([Text] -> ParserError) -> DomParser a Source
renderPath :: [Text] -> String Source
Render path for showing error
Parser itself
type DomParser = ExceptT [ParserError] (Reader ParserData) Source
Parser monad where all parsing actions live
runDomParser :: Document -> DomParser a -> Either [ParserError] a Source
Run parser on root element of Document.
type ContentParser a = Text -> Either Text a Source
Content parser type. Parser is just a function taking Text and returning either error description or successfully parsed value.
Common parsers
unitFromDom :: DomParser () Source
Always successfully parses any DOM to ()
voidFromDom :: DomParser Void Source
Never parses successfully. It is just mzero
textFromContent :: ContentParser Text Source
Does not strip content. Returns content unmodified.
stringFromContent :: ContentParser String Source
Does not strip content. Returns content unmodified.
charFromContent :: ContentParser Char Source
Expects content to be a singe non-blank character. Blank characters are stripped to parse pretty-printed XML files.
fixedFromContent :: (HasResolution a, Typeable a) => ContentParser (Fixed a) Source
boolFromContent :: ContentParser Bool Source
Expects content to be y, yes, t, true or 1 for True value. n, no, f, false or 0 for False value. Case is not significant, blank characters are striped.
Parser classes
Typeclass for structures which may be parsed from XML
DOM. Usually you should pass parsing function explicitly to
combinators like inElem
, maybeInElem
or inTags
, but
sometimes you need term search. Especially when you try to parse
polymorphic types. Or you maybe generate parser with TH for your
types, so typeclass would be convenient also.
FromDom () Source | |
FromContent a => FromDom (CurrentContent a) Source |
class FromContent a where Source
Usually you should pass ContentParser
to combinators like
elemContent
or maybeElemContent
explicitly. But sometimes you
need term search. Especially for code generated with TH.
fromContent :: ContentParser a Source
Should return either error message (what was wrong) or parsed value
FromContent Bool Source | This isntance might be not very obvious but anyway |
FromContent Char Source | |
FromContent Double Source | |
FromContent Int Source | |
FromContent Integer Source | |
FromContent String Source | |
FromContent Text Source | |
(HasResolution a, Typeable * a) => FromContent (Fixed a) Source |
Combinators
inElem :: Text -> DomParser a -> DomParser a Source
Find first element with given name in current element and run parser inside of found element. Throws PENotFound error if element not found.
inElems :: Text -> DomParser a -> DomParser [a] Source
Find all elements with gievn name in current element and run parser inside of this elements.
maybeInElem :: Text -> DomParser a -> DomParser (Maybe a) Source
Try to find element with given name and run parser inside of it. If not found return Nothing
:: Traversable f | |
=> ([Cursor] -> f Cursor) | Some predicate like |
-> Text | Name of tags to find in current tag |
-> DomParser a | Parser to run inside found cursors |
-> DomParser (f a) |
Generic elements combinator. Takes predicate filtering/converting list of cursors to some traversable (with posible filtering and/or reordering)
:: [Text] | Path suffix to append to path before run parser |
-> Axis | Axis to append to context |
-> DomParser a | Parser to run |
-> DomParser a |
Run parser within axis context. Expected to not use directly.
inDescendants :: DomParser a -> DomParser a Source
Given parser will match tag in arbitrary deepness
Given parser will match inside specific
Content getters
tryCurrentContent :: ContentParser a -> DomParser (Maybe a) Source
tryCurrentContentText :: DomParser (Maybe Text) Source
Get concatenated text from current parser's node(s). If current
context have no Content
nodes then return Nothing.
currentContent :: ContentParser a -> DomParser a Source
tryCurrentName :: DomParser (Maybe Name) Source
Return the name of current cursor we staying in. Return Nothing
if we are not staying on element node
currentName :: DomParser Name Source
Return name of current element the parser in.
currentAttr :: Text -> DomParser Text Source
Take attribute from current node (if it is an element). Throws
PENotFound
or PENotElement
elemContent :: Text -> ContentParser a -> DomParser a Source
nonEmptyElemsContent :: Text -> ContentParser a -> DomParser (NonEmpty a) Source
elemsContent :: Text -> ContentParser a -> DomParser [a] Source
maybeElemContent :: Text -> ContentParser a -> DomParser (Maybe a) Source
Helpers
newtype CurrentContent a Source
Helper newtype returning currentContent
for any type with
instance FromContent
Eq a => Eq (CurrentContent a) Source | |
Ord a => Ord (CurrentContent a) Source | |
Show a => Show (CurrentContent a) Source | |
Generic (CurrentContent a) Source | |
FromContent a => FromDom (CurrentContent a) Source | |
type Rep (CurrentContent a) Source |
Raw node getters
currentNodes :: DomParser [Node] Source
Get children nodes from current parser's node.
Checkers
checkCurrentLaxName :: Text -> DomParser () Source
Throw PEWrongTagName
if name of current element does not match
with given.