summaryrefslogtreecommitdiff
path: root/provider/posts/thermoprint/4.md
diff options
context:
space:
mode:
Diffstat (limited to 'provider/posts/thermoprint/4.md')
-rw-r--r--provider/posts/thermoprint/4.md116
1 files changed, 116 insertions, 0 deletions
diff --git a/provider/posts/thermoprint/4.md b/provider/posts/thermoprint/4.md
new file mode 100644
index 0000000..756c166
--- /dev/null
+++ b/provider/posts/thermoprint/4.md
@@ -0,0 +1,116 @@
1---
2title: On the Design of a Parser
3published: 2016-01-12
4tags: Thermoprint
5---
6
7The concrete application we’ll be walking through is a naive parser for [bbcode](https://en.wikipedia.org/wiki/BBCode)
8-- more specifically the contents of the directory `bbcode` in the
9[git repo](https://git.yggdrasil.li/thermoprint/tree/bbcode?h=rewrite&id=dc99dae).
10
11In a manner consistent with designing software as
12[compositions of simple morphisms](https://en.wikipedia.org/wiki/Tacit_programming) we start by determining the type of
13our solution (as illustrated by the following mockup):
14
15~~~ {.haskell}
16-- | Our target structure -- a rose tree with an explicit terminal constructor
17data DomTree = Element Text (Map Text Text) [DomTree]
18 | Content Text
19 deriving (Show, Eq)
20
21bbcode :: Text -> Maybe DomTree
22-- ^ Parse BBCode
23~~~
24
25Writing a parser capable of dealing with `Text` directly from scratch would be unnecessarily abstruse, we’ll be using
26the [attoparsec](https://hackage.haskell.org/package/attoparsec/docs/Data-Attoparsec-Text.html) family of parser
27combinators instead.
28
29We reproduce an incomplete version of the lexer below (it’s missing tag attributes and self-closing tags).
30
31We introduce `escapedText`, a helper function for extracting text until we reach one of a set of delimiting characters
32(exclusive).
33While doing this we also parse any delimiting character iff it's prefixed with an escape character (we use `\`) -- the
34escape character itself needs only be escaped if encountered directly before one of the delimiting characters.
35
36~~~ {.haskell}
37data Token = BBOpen Text -- ^ "[open]"
38 | BBClose Text -- ^ "[/close]"
39 | BBStr Text -- ^ "text"
40
41token :: Parser [Token]
42token = BBClose <$ "[/" <*> escapedText' [']'] <* "]"
43 <|> BBOpen <$ "[" <*> escapedText' [']'] <* "]"
44 <|> BBStr <$> escapedText ['[']
45
46escapedText' :: [Char] -> Parser Text
47escapedText' = option "" . escapedText
48
49escapedText :: [Char] -> Parser Text
50escapedText [] = takeText -- No delimiting characters -- parse all remaining input
51escapedText cs = recurse $ choice [ takeWhile1 (not . special) -- a series of characters we don't treat as special
52 , escapeSeq -- an escaped delimiter
53 , escapeChar' -- the escape character
54 ]
55 where
56 escapeChar = '\\'
57 special = inClass $ escapeChar : cs
58 escapeChar' = string $ T.singleton escapeChar
59 escapeSeq = escapeChar' *> (T.singleton <$> satisfy special) -- escape character followed by a special character (which encludes the escape character)
60 recurse p = mappend <$> p <*> escapedText' cs -- parse a prefix and optionally append another chunk of escapedText
61
62runTokenizer :: Text -> Maybe [Token]
63runTokenizer = either (const Nothing) Just . parseOnly (many token <* endOfInput)
64~~~
65
66We have now reduced the Problem to `[Token] -> DomTree`.
67We quickly see that the structure of the problem is that of a
68[fold](https://hackage.haskell.org/package/base/docs/Data-Foldable.html).
69
70Having realised this we require a function of type `Token -> DomTree -> DomTree` to recursively build up our target
71structure.
72
73In general we’ll want to not only keep track of the `DomTree` during recursion but also maintain a reference to the
74position at which we’ll be inserting new tokens.
75This kind of problem is well understood and solved idiomatically by using a
76[zipper](https://en.wikipedia.org/wiki/Zipper_(data_structure))
77([a cursory introduction](http://learnyouahaskell.com/zippers)).
78
79Writing zippers tends to be tedious. We’ll therefore introduce an
80[additional intermediate structure](https://hackage.haskell.org/package/containers/docs/Data-Tree.html) for which an
81[implementation](https://hackage.haskell.org/package/rosezipper) is available readily.
82The morphism from this new structure (`Forest BBLabel`) to our `DomTree` will be almost trivial.
83
84~~~ {.haskell}
85import Data.Tree.Zipper (TreePos, Empty, Full)
86import qualified Data.Tree.Zipper as Z
87
88data BBLabel = BBTag Text
89 | BBPlain Text
90
91rose :: [BBToken] -> Maybe (Forest BBLabel)
92rose = Z.toForest <$> foldM (flip rose') (Z.fromForest [])
93
94rose' :: BBToken -> TreePos Empty BBLabel -> Maybe (TreePos Empty BBLabel)
95rose' (BBStr t) = return . Z.nextSpace . Z.insert (Node (BBPlain t) []) -- insert a node with no children and move one step to the right in the forest we’re currently viewing
96rose' (BBOpen t) = return . Z.children . Z.insert (Node (BBTag t) []) -- insert the node and move into position to insert it's first child
97rose' (BBClose t) = close t -- haskell complains if multiple equations for the same function have a differing number of arguments, therefore: 'close'
98 where
99 close :: Text -> TreePos Empty BBLabel -> Maybe (TreePos Empty BBLabel)
100 close tag pos = do
101 pos' <- Z.parent pos -- fail if we're trying to close a tag that does not have a parent (this indicates imbalanced tags)
102 let
103 pTag = (\(BBTag t) -> t) $ Z.label pos' -- yes, this will fail unceremoniously if the parent is not a tag, this poses no problem since we're constructing the structure ourselves. The proof that this failure mode does not occur is left as an exercise for the reader.
104 guard (pTag == tag) -- The structure shows that this mode of failure (opening tags content does not match the closing tags) is not logically required -- it only serves as a *notification* to the user
105 return $ Z.nextSpace pos' -- move one level up and to point at the next sibling of the parent
106~~~
107
108All that is left to do now is present our final morphism:
109
110~~~ {.haskell}
111dom :: Forest BBLabel -> [DomTree]
112dom = map dom'
113 where
114 dom' (Node (BBPlain t) []) = Content t
115 dom' (Node (BBTag t) ts = Element t $ map dom' ts
116~~~