Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do grammar subparse on a file

Tags:

grammar

raku

Let's say you only want to parse the start of a large file using Perl 6 grammar. In order to avoid reading the whole file into a string, and then call subparse on the string. Is it possible to do a subparse when reading the file?

I could not find any subparsefile() method in the Grammar class, so I guess this is difficult to implement. But it should be possible in theory, see for example How do I search a file for a multiline pattern without reading the whole file into memory?

like image 868
Håkon Hægland Avatar asked Dec 14 '17 16:12

Håkon Hægland


1 Answers

Currently you can't. 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 pattern may expand over, you could 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
}

It wouldn't be the fastest way of doing it, but it would not have to read the whole file in memory.

like image 128
Elizabeth Mattijsen Avatar answered Nov 15 '22 08:11

Elizabeth Mattijsen