Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing data with Parsec and omitting comments
    primarykey
    data
    text
    <p>I am trying to write a Haksell Parsec Parser that parses input data from a file into the LogLine datatype as follows:</p> <pre><code>--Final parser that holds the indvidual parsers. final :: Parser [LogLine] final = do{ logLines &lt;- sepBy1 logLine eol ; return logLines } --The logline token declaration logLine :: Parser LogLine logLine = do name &lt;- plainValue -- parse the name (identifier) many1 space -- parse and throw away a space args1 &lt;- bracketedValue -- parse the first arguments many1 space -- throw away the second sapce args2 &lt;- bracketedValue -- parse the second list of arguments many1 space -- constant &lt;- plainValue -- parse the constant identifier space weighting &lt;- plainValue --parse the weighting double space return $ LogLine name args1 args2 constant weighting </code></pre> <p>It parses everything just fine, but now I need to add comments to the file, and I have to modify the parser so that it ignores them. It should support single-line comments only beginning with "--" and ending with a '\n' I've tried defining the comment token as follows:</p> <pre><code>comments :: Parser String comments = do string "--" comment &lt;- (manyTill anyChar newline) return "" </code></pre> <p>And then plugging it into the <code>final</code> parser like so:</p> <pre><code>final :: Parser [LogLine] final = do optional comments logLines &lt;- sepBy1 logLine (comments&lt;|&gt;newline) optional comments return logLines </code></pre> <p>It compiles fine, but it does not parse. I've tried several minor modifications but the best result was parsing everything up to the first comment, so I'm beginning to think that this is not the way to do it. PS: I've seen this <a href="https://stackoverflow.com/questions/12940490/how-would-i-strip-out-comments-from-a-file-with-parsec">Similar Question</a>, but it is slightly different from what I'm trying to achieve.</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.
 

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