Search code examples
parsinghaskellregular-language

haskell identifier recognition


my working in file parsing using Haskell, and I'm using both Data.Attoparsec.Char8 and Data.ByteString.Char8. I want to parse an expression which can contains symbols like : - / [ ] _ . (minus, slashes, braquets and underscore).

I've write the following parser

import qualified Data.ByteString.Char8 as B
import qualified Data.Attoparsec.Char8 as A

identifier' :: Parser B.ByteString
identifier' = A.takeWhile $ A.inClass "A-Za-z0-9_//- /[/]"

... but it's not works like expected.

ghc>  A.parse identifier' (B.pack "EMBXSHM-PortClo")
Done "-PortClo" "EMBXSHM"

ghc> A.parse identifier' (B.pack "AU_D[1].PCMPTask")
Done ".PCMPTask" "AU_D[1]"

can someone help me.

Thanks for your time.


Solution

  • Take a look at the documentation: http://hackage.haskell.org/packages/archive/attoparsec/0.10.1.0/doc/html/Data-Attoparsec-ByteString-Char8.html#g:9

    To add a "-" to a set, place it a the beginning or end of a string.

    The latter doesn't parse because you don't have dots in your class listing.