Haskell 二进制解析
我一直在尝试在 haskell 中实现一个协议解析器,而且我对这门语言还很陌生,特别是当涉及到 monad 时。我一直在使用binary-0.5.0.2,并描述了协议的标头和所有有效负载。我想要解析的消息如下所示: header + (payload A, Payload B, ..) 其中 header 中的字段指定消息具有的有效负载类型。
我已经成功解析了字节串中的第一条消息,但不知道如何读取下一条消息,丢弃在处理第一条消息时读取的字节。
这可能相当模糊,但我宁愿在通用解析器上获取输入,也不愿将丑陋的代码更改为以这种方式工作。
感谢您的帮助
I've been trying to implement a protocol parser in haskell and I'm pretty new to the language, especially when it comes to monads. I've been using binary-0.5.0.2 and have described the header and all the payloads of my protocol. the messages I'd like to parse look something like the following: header + (payload A, payload B, ..) where a field in the header specifies what type of payload the message has.
I have had success in parsing the first message in the bytestring, but am at a loss with how to go about reading the next messages, discarding the bytes that were read in processing the first message.
This might be rather vague, but I'd rather get input on a generalized parser than having my ugly code altered to work in this manner.
Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用一系列解析操作,它们就会在执行过程中消耗输入。
Just use a sequence of parse operations and they will consume the input as they go along.
runGetState
从Data.Binary.Get
返回的元组的第二个元素是剩余的ByteString
。您可以继续应用解析器,直到出现错误或字节用完为止。The second element of the tuple returned by
runGetState
fromData.Binary.Get
is the remainingByteString
. You can simply keep applying your parser until you get an error or run out of bytes.