Sub grammar in file

Suppose you just want to parse the beginning of a large file using Perl 6 grammar. To avoid reading the entire file into a string, then call subparse in the string. Is it possible to make a subparameter when reading a file?

I could not find any subparsefile() method in the Grammar class, so I assume this is hard to implement. But this should be possible in theory, see, for example, How to search for a file for a multi-line template without reading the entire file in memory?

+5
source share
1 answer

You cannot currently. Parsing anything at the moment requires the entire string to exist in memory.

Having said that, if you know the maximum number of lines your template can expand, you can do something like:

 my $max = 3; # maximum number of lines for "textfile".IO.lines(:!chomp).rotor( $max => -$max + 1 ) -> @lines { @lines.join.subparse( $grammar) # and whatever you would like to do } 

This is not the fastest way to do this, but he would not need to read the entire file in memory.

+7
source

Source: https://habr.com/ru/post/1274069/


All Articles