I am interested in writing micro-grammars. This is where the parser does not understand the full grammar of a language, only part of it; kind of "m4 on acid".
The idea is that the parser reads in characters and writing them to output until it "wakes up" when it identifies keywords, does some processing according to a grammar, and then falls asleep again.
I'm having trouble constructing one, and there doesn't really seem to be any examples out there.
The .subparse
method might be what you are looking for. It anchors at the beginning of the data and matches as much as it can. You can use the :c
adverb to tell it where to start then look at the Match
object to see where it left off
my $position = 0;
my $match = Some::Grammar.subparse( $data, :c($position) );
# update the position to the last thing your grammar consumed
$position = $match.end if $match.so;
If you keep track of where you are, you can switch grammars, try again when a buffer get more data, and so on.
Beyond that, you haven't presented a specific problem you're trying to solve.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With