Note that there are some explanatory texts on larger screens.

plurals
  1. POParsec-Parser works alright, but could it be done better?
    primarykey
    data
    text
    <p>I try to do this:</p> <p>Parse a Text in the form:</p> <blockquote> <p>Some Text #{0,0,0} some Text #{0,0,0}#{0,0,0} more Text #{0,0,0}</p> </blockquote> <p>into a list of some data structure:</p> <blockquote> <p>[Inside "Some Text ",Outside (0,0,0),Inside " some Text ",Outside (0,0,0),Outside (0,0,0),Inside " more Text ",Outside (0,0,0)]</p> </blockquote> <p>So these #{a,b,c}-bits should turn into different things as the rest of the text.</p> <p>I have this code:</p> <pre><code>module ParsecTest where import Text.ParserCombinators.Parsec import Monad type Reference = (Int, Int, Int) data Transc = Inside String | Outside Reference deriving (Show) text :: Parser Transc text = do x &lt;- manyTill anyChar ((lookAhead reference) &lt;|&gt; (eof &gt;&gt; return (Inside ""))); return (Inside x) transc = reference &lt;|&gt; text alot :: Parser [Transc] alot = do manyTill transc eof reference :: Parser Transc reference = try (do{ char '#'; char '{'; a &lt;- number; char ','; b &lt;- number; char ','; c &lt;- number; char '}'; return (Outside (a,b,c)) }) number :: Parser Int number = do{ x &lt;- many1 digit; return (read x) } </code></pre> <p>This works as expected. You can test this in ghci by typing</p> <blockquote> <p>parseTest alot "Some Text #{0,0,0} some Text #{0,0,0}#{0,0,0} more Text #{0,0,0}"</p> </blockquote> <p>But I think it's not nice. </p> <p>1) Is the use of <code>lookAhead</code> really necessary for my problem?</p> <p>2) Is the <code>return (Inside "")</code> an ugly hack?</p> <p>3) Is there generally a more concise/smarter way to archieve the same? </p>
    singulars
    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.
    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