Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write micro-grammars in perl6?

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.

like image 396
blippy Avatar asked Mar 17 '17 14:03

blippy


1 Answers

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.

like image 179
brian d foy Avatar answered Oct 11 '22 14:10

brian d foy