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 String
s? I've been poking around the treetop docs, but haven't found anything definitive.
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