diff options
-rw-r--r-- | spec/src/Thermoprint/Printout/BBCode.hs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/spec/src/Thermoprint/Printout/BBCode.hs b/spec/src/Thermoprint/Printout/BBCode.hs new file mode 100644 index 0000000..8d98da1 --- /dev/null +++ b/spec/src/Thermoprint/Printout/BBCode.hs | |||
@@ -0,0 +1,45 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | {-# LANGUAGE DeriveGeneric #-} | ||
3 | |||
4 | module Thermoprint.Printout.BBCode | ||
5 | ( bbcode | ||
6 | , TreeError(..) | ||
7 | ) where | ||
8 | |||
9 | import Data.Text (Text) | ||
10 | import qualified Data.Text as T () | ||
11 | |||
12 | import GHC.Generics (Generic) | ||
13 | import Control.Exception (Exception) | ||
14 | import Data.Typeable (Typeable) | ||
15 | |||
16 | import Data.Bifunctor (bimap, first) | ||
17 | import Control.Monad (join) | ||
18 | |||
19 | import Text.BBCode (DomTree(..), TreeError(..)) | ||
20 | import qualified Text.BBCode as Raw (bbcode, BBCodeError(..)) | ||
21 | |||
22 | import Thermoprint.Printout | ||
23 | |||
24 | -- ^ We replicate 'Raw.BBCodeError' but add a new failure mode documenting incompatibly of the parsed syntax tree with our document format | ||
25 | data BBCodeError = LexerError String -- ^ Error while parsing input to stream of tokens | ||
26 | | TreeError TreeError -- ^ Error while parsing stream of tokens to syntax tree | ||
27 | | SemanticError SemanticError -- ^ Error while mapping syntax tree to document format | ||
28 | deriving (Show, Eq, Generic, Typeable) | ||
29 | |||
30 | instance Exception BBCodeError | ||
31 | |||
32 | data SemanticError = Placeholder | ||
33 | deriving (Show, Eq, Generic, Typeable) | ||
34 | |||
35 | instance Exception SemanticError | ||
36 | |||
37 | bbcode :: Text -> Either BBCodeError Printout | ||
38 | bbcode = join . fmap (first SemanticError) . bimap morph' morph . Raw.bbcode | ||
39 | |||
40 | morph' :: Raw.BBCodeError -> BBCodeError | ||
41 | morph' (Raw.LexerError x) = LexerError x | ||
42 | morph' (Raw.TreeError x) = TreeError x | ||
43 | |||
44 | morph :: [DomTree] -> Either SemanticError Printout | ||
45 | morph = undefined | ||