我可以使用Treetop解析IO吗? [英] Can I use Treetop to parse an IO?

查看:103
本文介绍了我可以使用Treetop解析IO吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要与Treetop一起解析的文件.如果我想解析整个内容,请使用

I've got a file that I want to parse with Treetop. If I wanted to parse the entire thing, I'd use

rule document
  category_listing*
end

我真的不想一次将整个文件读入内存.我知道我可以将解析器设置为一次解析一个category_listing(使用#consume_all_input = false#root = :category_listing),这是一半的问题.但是,看起来#parse希望传递给String(并且当我尝试传递给它File时肯定会失败),这使得通过category_listing读取和解析category_listing的想法听起来像PITA.

I don't really want to read the entire file into memory at once. I know I can set up the parser to parse one category_listing at a time (using #consume_all_input = false and #root = :category_listing), which is half the problem. However, it looks like #parse expects to be passed a String (and it certainly fails when I try to pass it a File), which makes the idea of reading and parsing category_listing by category_listing sound like a PITA.

Treetop只能用于解析String吗?我一直在寻找树梢文档,但是还没有发现任何确定的东西.

Can Treetop only be used to parse Strings? I've been poking around the treetop docs, but haven't found anything definitive.

推荐答案

据我从源代码看,您实际上只能传递一个String.因此,您的选择基本上是遵循Josh Voigts的想法在他的评论中或实现反向IOString:具有String接口,但懒惰"地从File中获取请求的内容.

As far as I can glance from the source code, you can indeed only pass a String in. So your options are basically to either follow the idea of Josh Voigts in his comment or to implement something of a reverse IOString: something that has a String interface, but 'lazily' fetches the requested contents from a File.

我不确定是否不借助C就可以做到这一点,即使那样,有些方法的语义也是如此,以至于根本无法一致地实现它们,但是Treetop使用的String方法的子集可能是这样的:它是可管理的.但是,我想说乔什·沃格茨(Josh Voigts)的回答最为实用.

I'm not entirely sure whether that is even possible without resorting to C and even then there may be methods whose semantics are such that they simply cannot be implemented consistently, but perhaps the subset of String methods used by Treetop is such that it is manageable. However, I'd say Josh Voigts answer is most pragmatic.

这篇关于我可以使用Treetop解析IO吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆