Note that there are some explanatory texts on larger screens.

plurals
  1. POParsec, read text ended by a string
    primarykey
    data
    text
    <p>I am struggling with <code>Parsec</code> to parse a small subset of the <a href="https://code.google.com/p/support/wiki/WikiSyntax" rel="nofollow">Google project wiki syntax</a>, and convert it into HTML. My syntax is limited to text sequences and item lists. Here is an example of what I want to recognize:</p> <pre><code>Text that can contain any kind of characters, except the string "\n *" * list item 1 * list item 2 End of list </code></pre> <p>My code so far is:</p> <pre><code>import Text.Blaze.Html5 (Html, toHtml) import qualified Text.Blaze.Html5 as H import Text.ParserCombinators.Parsec hiding (spaces) parseList :: Parser Html parseList = do items &lt;- many1 parseItem return $ H.ul $ sequence_ items parseItem :: Parser Html parseItem = do string "\n *" item &lt;- manyTill anyChar $ (try $ lookAhead $ string "\n *") &lt;|&gt; (try $ string "\n\n") return $ H.li $ toHtml item parseText :: Parser Html parseText = do text &lt;- manyTill anyChar $ (try $ lookAhead $ string "\n *") &lt;|&gt; (eof &gt;&gt; (string "")) return $ toHtml text parseAll :: Parser Html parseAll = do l &lt;- many (parseUl &lt;|&gt; parseText) return $ H.html $ sequence_ l </code></pre> <p>When applying <code>parseAll</code> to any sequence of characters, I get the following error message: <code>"*** Exception: Text.ParserCombinators.Parsec.Prim.many: combinator 'many' is applied to a parser that accepts an empty string.</code> I understand that it is because my parser <code>parseText</code> can read empty strings, but I can't see any other way. How can I recognize text delimited by a string? (<code>"\n *"</code> here).</p> <p>I am also open to any remarks or suggestions concerning the way I am using Parsec. I can't help but see that my code is a bit ugly. Can I do all this in a simpler way? For example, there is code replication (which is kind of painful) because of the string <code>"\n *"</code>, that is used to recognize the end of a text sequence, the beginning of a list item, AND the end of a list item...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload