哈斯克尔二进制解析 [英] Haskell Binary Parsing
问题描述
我一直试图实现在Haskell的协议分析器和我pretty新的语言,尤其是当它涉及到单子。我一直在使用二进制0.5.0.2和所描述的头和我的协议的所有有效载荷。我想消息解析看起来类似如下:头+(有效载荷,有效载荷B,...),其中在头一个字段指定邮件有什么类型的有效载荷
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.
这可能是比较模糊的,但我宁愿得到一个广义的解析器输入不是让我难看code改变以这种方式工作。
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.
感谢您的帮助。
推荐答案
只需使用解析操作的顺序,因为他们走,他们将消耗输入。
Just use a sequence of parse operations and they will consume the input as they go along.
parseAll = do
hdr <- parseHeader
pa <- parsePayloadA
pb <- parsePayloadB
...
这篇关于哈斯克尔二进制解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!