Can I use Treetop to parse an IO?

你离开我真会死。 提交于 2019-12-07 15:26:23

问题


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

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.

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/13655658/can-i-use-treetop-to-parse-an-io

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!