为无限流封装(纯 Ruby)Ragel 解析器?
我想使用 Ragel 使用状态机解析连续的字节流(来自套接字)
但是,我找到的所有示例要么一次性解析完整的文件(例如 Gherkin 词法分析器 或正在使用 Ragels C 目标(例如 mongrel HTTP1.1 Parser)
我正在寻找一些建议或示例关于如何实例化 Ragel 状态机,然后向其添加字节,保持现有状态完整
我正在寻找的最终界面类似于:
parser = MyStreamParser.new(Grammar)
parser.on_token { |t| puts t.inspect }
# I can't parse lines seperately because tokens can span multiple lines.
$stdin.each_line do |line|
parser.add(line)
end
对于如何在 Ragel 中执行此操作的任何建议,我非常感激。 ?
也许 Ragel 不是正确的工具?如果不是:我应该使用什么
I want to parse a continuous stream of bytes (from a socket) with a state machine using Ragel
However, all the Examples I have found are either parsing a complete file in one pass (like the Gherkin lexer or are using Ragels C Target (like the mongrel HTTP1.1 Parser)
I'm looking for some advice or examples on how to instantiate a Ragel State machine and then add bytes to it, keeping the existing state intact.
The final interface I am looking for is something like:
parser = MyStreamParser.new(Grammar)
parser.on_token { |t| puts t.inspect }
# I can't parse lines seperately because tokens can span multiple lines.
$stdin.each_line do |line|
parser.add(line)
end
Any advice on how to do that in Ragel is greatly appreciated. I'd rather use that than code another state machine by hand.
Maybe Ragel is not the right tool? If not: What should I use instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
乍一看,Ragel 看起来不太像 Ruby。您看过 Statemachine 吗?看起来您可以一次向状态机事件(问题中的字符)提供一个。
At first glance, Ragel doesn't look very Ruby-like. Have you taken a look at Statemachine? It looks like you can feed the state machine events (characters, in your problem) one at a time.
它可能不完全是您正在寻找的,但是 Dhaka 是另一个不错的解析器生成器,值得一看。我不确定这会有帮助,但它过去对我很有帮助。
It may not be exactly what you are looking for, but Dhaka is another decent parser generator to take a look at. I'm not sure that will help, but it has served me well in the past.