6
votes

I've managed to use Parsec to parse a String, but cannot manage to do the same with a ByteString.

How can I make Parsec work with ByteStrings without manually converting them to Strings?

I get the feeling this isn't hard to accomplish. Am I wrong? (I'm new to Haskell. ^^)

Thanks!

3
Based on [the other question][1] you asked, you should learn to read the documentation of the library you're using. It's available on Hackage. [1]: stackoverflow.com/questions/2086842/…Wei Hu

3 Answers

8
votes

Just import the Parser type from Text.Parsec.ByteString or Text.Parsec.ByteString.Lazy, instead of from Text.Parsec.String.

2
votes

the answer actually depends on the version of Parsec you are using. If you are using version 3.0 or later then yes. For version 2.x.x I don't think you can.

http://hackage.haskell.org/packages/archive/parsec/3.0.1/doc/html/Text-Parsec-ByteString.html

Cheers

edit: The Parsec that STH suggest (Text.Parsec.ByteString) is actually version 3.0 the previous version 2.x.x is located in Text.ParserCombinators.Parsec.

0
votes
import Text.Parsec.ByteString ()

will give you the instance

forall m. Stream ByteString m Char

hence if you use, instead of Parser a:

p ::  Stream s m Char => ParsecT s u m a