[Haskell-cafe] A simple attoparsec question

Robert Clausecker fuzxxl at gmail.com
Tue Mar 1 22:15:38 CET 2011


Hi Haskellers!

I'm currently trying to write an assembler for Knuths MMIXAL language.
Currently, I'm writing the parser using attoparsec, and have one
question:

In MMIXAL, a string literal starts with a ", followed by an arbitrary
amount of arbitrary characters excluding the newline character and
finished with another ". For example: "Hello, World!" is a string, "ſ"
is a string too, but "\n" (\n is a newline) isn't. Here's the parser,
which parses a s tring and other kinds of constants:

  parseConstant = Reference <$> try parseLocLabel
              <|> PlainNum <$> decimal
              <|> char '#' *> fmap PlainNum hexadecimal
              <|> char '\'' *> (CharLit <$> notChar '\n') <* char '\''
              <|> try $ (char '"' *> (StringLit . B.pack <$>
                    manyTill (notChar '\n') (char '"')))
              <?> "constant"

The problem is, that attoparsec just silently fails on this kind of
strings and tries other parsers afterwards, which leads to strange
results. Is there a way to force the whole parser to fail, even if
there's an alternative parser afterwards?

I hope, you understand my question.

Yours, Robert Clausecker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110301/93b4f850/attachment.pgp>


More information about the Haskell-Cafe mailing list