|
| 1 | +««« |
| 2 | +title: Rewriting the parser: Challenges and opportunities, |
| 3 | +author: Estevão Soares dos Santos, |
| 4 | +author_avatar: img/avatars/tivie.jpg, |
| 5 | +date: 2018-09-25, |
| 6 | +language: en, |
| 7 | +image: img/blog/2018.09.25.jpg, |
| 8 | +summary: There comes a time when tough (and exciting) choices need to be made. With a version 2.0 in the oven |
| 9 | + (and the exciting reverse parser already completed), it seemed an excellent opportunity for doing something |
| 10 | + that I wished for a long time: rewriting showdown's parser from scratch. But maybe that challenge was a lot |
| 11 | + more tough than I first anticipated! |
| 12 | +»»» |
| 13 | + |
| 14 | +***There comes a time when tough (and exciting) choices need to be made. With a version 2.0 in the oven |
| 15 | +(and the exciting reverse parser already completed), it seemed an excellent opportunity for doing something that |
| 16 | +I wished for a long time: rewriting showdown's parser from scratch.*** |
| 17 | + |
| 18 | +But maybe that challenge was a lot more tough than I first anticipated! |
| 19 | + |
| 20 | +## The old parser |
| 21 | + |
| 22 | +The old parser was based on replacing markdown text *in loco* and in the original string with HTML, through a series of |
| 23 | +Regular Expression. |
| 24 | +While this was true to showdown's origins in John Gruber's Markdown.pl, during the last 3 years it became clear that |
| 25 | +Regex, at least by itself, was not suitable for the parsing job. |
| 26 | + |
| 27 | +Don't get me wrong, Regex is a great tool: *not only does it works for 99% of use cases*, it also makes it extremely easy |
| 28 | +to develop extensions that complement showdown's features, even for beginner programmers. |
| 29 | + |
| 30 | +The problem, however, are those pesky edge cases... that 1% of weird scenarios that ended up making showdown's Regular Expressions |
| 31 | +increasingly complex over time |
| 32 | + |
| 33 | +In fact, when you compare v 1.0 to v. 1.8.6, it's plain to see that almost all bug fixes (with notable exceptions) |
| 34 | +were, in fact, edge case fixes. |
| 35 | + |
| 36 | +## Regex Madness |
| 37 | + |
| 38 | +Some regexes grew so weirdly that I ended up needing to split them in chunks and do some convoluted stuff to keep them minimally sane. |
| 39 | +For instance, it takes 6 RegExps just to parse `__foo__bar__baz__`. |
| 40 | +```javascript |
| 41 | + if (options.literalMidWordUnderscores) { |
| 42 | + text = text.replace(/\b___(\S[\s\S]*)___\b/g, function (wm, txt) { |
| 43 | + return parseInside (txt, '<strong><em>', '</em></strong>'); |
| 44 | + }); |
| 45 | + text = text.replace(/\b__(\S[\s\S]*)__\b/g, function (wm, txt) { |
| 46 | + return parseInside (txt, '<strong>', '</strong>'); |
| 47 | + }); |
| 48 | + text = text.replace(/\b_(\S[\s\S]*?)_\b/g, function (wm, txt) { |
| 49 | + return parseInside (txt, '<em>', '</em>'); |
| 50 | + }); |
| 51 | + } else { |
| 52 | + text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) { |
| 53 | + return (/\S$/.test(m)) ? parseInside (m, '<strong><em>', '</em></strong>') : wm; |
| 54 | + }); |
| 55 | + text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) { |
| 56 | + return (/\S$/.test(m)) ? parseInside (m, '<strong>', '</strong>') : wm; |
| 57 | + }); |
| 58 | + text = text.replace(/_([^\s_][\s\S]*?)_/g, function (wm, m) { |
| 59 | + // !/^_[^_]/.test(m) - test if it doesn't start with __ (since it seems redundant, we removed it) |
| 60 | + return (/\S$/.test(m)) ? parseInside (m, '<em>', '</em>') : wm; |
| 61 | + }); |
| 62 | + } |
| 63 | +``` |
| 64 | + |
| 65 | +Others, which were inherited from when I took over the project, were just plainly poorly coded and |
| 66 | +[hid some nasty bugs within](https://codereview.stackexchange.com/questions/150277/regex-to-parse-horizontal-rules-in-markdown). |
| 67 | + |
| 68 | +Javascript's regex limitations (such as no support for Lookbehind or atomic and possessive grouping) meant that I needed to rely |
| 69 | +on the language's quirks and look for hacks to overcome those limitations. |
| 70 | +At some point I even had to adapt a [recursive RegExp extension](https://github.com/showdownjs/showdown/blob/version_1.x/src/helpers.js#L210) |
| 71 | +which seems an overkill for a syntax that doesn't really care about balanced stuff. |
| 72 | + |
| 73 | +And some stuff was just []impossible to fix](https://github.com/showdownjs/showdown/issues/549) without making the parser |
| 74 | +very VERY slow for everyone. |
| 75 | + |
| 76 | + |
| 77 | +## Enter the new parser |
| 78 | + |
| 79 | +For all those reasons, and some more, I felt that version 2.0 should have a shinny and **proper new parser** that read |
| 80 | +the code sequentially and was aware of context (use more code logic and less RexExp wizardry :tada:). The new parser is, |
| 81 | +in fact, a lot more similar to a PEG parser than a RegExp converter with in place substitutions. |
| 82 | + |
| 83 | +Some key features include: |
| 84 | + |
| 85 | + - **Sequential** |
| 86 | + |
| 87 | + The new parser reads the input sequentially. When a syntax element match is found, it "stores" the element without |
| 88 | + changing the original string. This means that Showdown no longer relies on (nor needs) a specific order in which |
| 89 | + sub parsers are invoked. |
| 90 | + |
| 91 | + - **Full separation of the *parsing* and *conversion* steps** |
| 92 | + |
| 93 | + Instead of making in place substitutions of the original input, the new parser creates an intermediary abstract layer, |
| 94 | + an object that is a *node tree* of elements, similar to the DOM Tree in the browser, which makes it easy to manipulate |
| 95 | + each node before outputting it as a string again, in other format. |
| 96 | + |
| 97 | + |
| 98 | + - **Output manipulation is extremely easy and customizable through templates** |
| 99 | + |
| 100 | + The node tree (and each individual node) can be manipulated as you see fit since it's, in practice, an agnostic |
| 101 | + representation of an element. Each node has a template for each supported format: HTML and Markdown. But you can |
| 102 | + even add more formats if you wish. |
| 103 | + |
| 104 | + What is cool is that, **not only can you can manipulate the tree (add, delete or swap nodes)**, you can also **tweak |
| 105 | + or completely change the output of all nodes of a type or even a specific, individual node**. |
| 106 | + |
| 107 | + - **Extensions are a lot more powerful now** |
| 108 | + |
| 109 | + The extension system is being completely redone and will tie in really really well with the new parser and |
| 110 | + **Reverse Converter**. |
| 111 | + |
| 112 | + - **Faster (it seems)** |
| 113 | + |
| 114 | + RegExps, specially the complex ones, are slow. So, moving away from them, and only using RegExp for quick checks, |
| 115 | + should speed up the parser a lot. Well, at least in theory. |
| 116 | + |
| 117 | + |
| 118 | +## Not everything are roses though |
| 119 | + |
| 120 | +Unfortunately, to accomplish this and successfully move away from RexExp, 3 things were needed: |
| 121 | + |
| 122 | +1. Keep the extension logic simple (while making it more powerful) |
| 123 | +2. Time |
| 124 | +3. Accurately estimate number 2 (time needed vs free time) |
| 125 | + |
| 126 | + |
| 127 | +While I feel that the first one is being accomplished quite nicely (if I may say so myself), **I might have over estimated |
| 128 | +my free time** and I definitely failed at number 3. Which meant I had to keep delaying the alpha release for v2.0, which |
| 129 | +I intended to release in mid 2018. |
| 130 | + |
| 131 | +Between work and family, the little free time I get, I dedicate it to this library. I do feel that things |
| 132 | +are still going on the right track, albeit a lot more slowly than expected (and than I wished for). |
| 133 | + |
| 134 | +Regardless, **I'm really excited about the new features for version 2.0, specially the reverse converter and how it will tie in |
| 135 | +toegether with the new parser and event system** |
| 136 | + |
| 137 | + |
| 138 | +## I would really, really, really appreciate if you could donate, you know? |
| 139 | + |
| 140 | +For all those reasons, working on the 2.0 version consumes a lot of my free time which, unfortunately, I don't have that much lately. |
| 141 | + |
| 142 | +I would really, REALLY appreciate if you could donate. Your contribution will mean a lot to me and really help me |
| 143 | +dedicate less time to my dayjob (and those annoying extra hours) and more time developing this (awesome) library. |
| 144 | + |
| 145 | +So... if you like my work and find our library useful, |
| 146 | +please donate ~~[through Patreon (coming soon)](https://www.patreon.com/showdownjs) or~~ |
| 147 | +directly [through paypal](https://www.paypal.me/tiviesantos)!! |
| 148 | + |
| 149 | +Thank you!! |
0 commit comments