diff options
Diffstat (limited to 'bbcode/src/Text/BBCode.hs')
| -rw-r--r-- | bbcode/src/Text/BBCode.hs | 17 | 
1 files changed, 12 insertions, 5 deletions
| diff --git a/bbcode/src/Text/BBCode.hs b/bbcode/src/Text/BBCode.hs index 32f74df..75fa219 100644 --- a/bbcode/src/Text/BBCode.hs +++ b/bbcode/src/Text/BBCode.hs | |||
| @@ -3,12 +3,14 @@ | |||
| 3 | 3 | ||
| 4 | -- | An implementation of BBcode parsing 'Text' to a syntax tree | 4 | -- | An implementation of BBcode parsing 'Text' to a syntax tree | 
| 5 | module Text.BBCode | 5 | module Text.BBCode | 
| 6 | ( TreeError(..) | 6 | ( bbcode | 
| 7 | , BBCodeError(..) | ||
| 8 | , TreeError(..) | ||
| 7 | , DomTree(..) | 9 | , DomTree(..) | 
| 8 | , dom | 10 | -- , dom | 
| 9 | , BBLabel | 11 | -- , BBLabel | 
| 10 | , rose | 12 | -- , rose | 
| 11 | , matches | 13 | -- , matches | 
| 12 | ) where | 14 | ) where | 
| 13 | 15 | ||
| 14 | import Data.Text (Text) | 16 | import Data.Text (Text) | 
| @@ -36,11 +38,15 @@ import qualified Data.CaseInsensitive as CI | |||
| 36 | 38 | ||
| 37 | import Data.Bifunctor (Bifunctor(first)) | 39 | import Data.Bifunctor (Bifunctor(first)) | 
| 38 | 40 | ||
| 41 | -- | Our target structure -- a rose tree with an explicit terminal constructor | ||
| 39 | data DomTree = Element Text (Map Text Text) [DomTree] | 42 | data DomTree = Element Text (Map Text Text) [DomTree] | 
| 40 | | Content Text | 43 | | Content Text | 
| 41 | deriving (Show, Eq) | 44 | deriving (Show, Eq) | 
| 42 | 45 | ||
| 43 | dom :: Forest BBLabel -> [DomTree] | 46 | dom :: Forest BBLabel -> [DomTree] | 
| 47 | -- ^ Parse semantically constrained rose tree to syntactically constrained version | ||
| 48 | -- | ||
| 49 | -- Silently drops children of semantically terminal nodes ('BBPlain') | ||
| 44 | dom = map dom' | 50 | dom = map dom' | 
| 45 | where | 51 | where | 
| 46 | dom' (Node (BBPlain t) _) = Content t | 52 | dom' (Node (BBPlain t) _) = Content t | 
| @@ -54,6 +60,7 @@ data BBCodeError = LexerError String -- ^ Error while parsing input to stream of | |||
| 54 | instance Exception BBCodeError | 60 | instance Exception BBCodeError | 
| 55 | 61 | ||
| 56 | bbcode :: Text -> Either BBCodeError [DomTree] | 62 | bbcode :: Text -> Either BBCodeError [DomTree] | 
| 63 | -- ^ Parse BBCode | ||
| 57 | bbcode t = fmap dom $ first LexerError (parseOnly (many token <* endOfInput) t) >>= first TreeError . rose | 64 | bbcode t = fmap dom $ first LexerError (parseOnly (many token <* endOfInput) t) >>= first TreeError . rose | 
| 58 | 65 | ||
| 59 | -- | Errors in input encountered during parsing of lexed token-stream | 66 | -- | Errors in input encountered during parsing of lexed token-stream | 
