Haskell 标识符识别

发布于 2025-01-01 17:05:10 字数 645 浏览 4 评论 0原文

我使用 Haskell 进行文件解析,并且同时使用 Data.Attoparsec.Char8Data.ByteString.Char8。我想解析一个可以包含以下符号的表达式:- / [ ] _ .(减号、斜杠、括号和下划线)。

我已经编写了以下解析器

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_//- /[/]"

...但它没有按预期工作。

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]"

有人可以帮助我吗?

感谢您抽出时间。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2025-01-08 17:05:10

查看文档: http://hackage.haskell.org/packages/archive/attoparsec/0.10.1.0/doc/html/Data-Attoparsec-ByteString-Char8.html#g:9

添加“-”到一个集合,将其放置在字符串的开头或结尾。

后者不会解析,因为您的类列表中没有点。

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.

等数载,海棠开 2025-01-08 17:05:10

您希望在标识符中允许使用 '-' 字符,但 A.inClass 使用 '-' 作为范围。您必须将其放在范围字符串的开头或结尾:

要将文字 '-' 添加到集合中,请将其放在字符串的开头或结尾。

attoparsec文档

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文