6

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!

  • 2
    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]: http://stackoverflow.com/questions/2086842/using-haskell-to-output-a-utf-8-encoded-bytestring – Wei Hu Jan 19 '10 at 02:07

3 Answers3

8

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

sth
  • 222,467
  • 53
  • 283
  • 367
  • What is numbers in Text.Parsec.ByteString.Lazy(http://hackage.haskell.org/packages/archive/parsec/3.0.0/doc/html/Text-Parsec-ByteString-Lazy.html)? – HHC Apr 02 '13 at 10:19
  • see (http://stackoverflow.com/questions/15455084/matching-bytestrings-in-parsec?rq=1)[http://stackoverflow.com/questions/15455084/matching-bytestrings-in-parsec?rq=1] for where to go from here – Karl Sep 27 '16 at 00:16
2

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.

JFT
  • 827
  • 8
  • 6
  • Oh, then I seem to currently lack the newest version, and have been using the older one. I will upgrade. Thank you both! –  Jan 19 '10 at 01:26
0
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
alternative
  • 12,703
  • 5
  • 41
  • 41
  • were you trying to explain how to get Word8 instead of Char, by any chance. I can't figure that out. – dbanas Oct 03 '19 at 00:44