为无限流封装(纯 Ruby)Ragel 解析器?

发布于 2024-08-18 10:52:48 字数 770 浏览 2 评论 0原文

我想使用 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 技术交流群。

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

发布评论

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

评论(2

把梦留给海 2024-08-25 10:52:48

乍一看,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.

高跟鞋的旋律 2024-08-25 10:52:48

它可能不完全是您正在寻找的,但是 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.

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