Fopa Léon Constantin
Fopa Léon Constantin

Reputation: 12363

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.

Upvotes: 0

Views: 411

Answers (2)

sclv
sclv

Reputation: 38891

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.

Upvotes: 1

ehird
ehird

Reputation: 40797

You want to allow '-' characters in identifiers, but A.inClass uses '-' for ranges. You have to put it at the start or end of the range string:

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

attoparsec documentation

Upvotes: 1

Related Questions