I would like to use Raku’s comb
function with a regex from a Grammar instead of a standalone regex. For example, in the following code, which parses journalctl
logs from stdin, I would like to replace the &h
in MAIN
with something like &Journalctl::full_log
:
role Log {
token preamble { ... };
token message { [^(<preamble>)]* };
regex full_log { <preamble> <message> };
}
grammar Journalctl does Log {
token date { S+sdd:dd:dd };
token hostname { [^s]+ };
token ctl_unit { [^[]+ };
token pid { d+ };
regex preamble { <date> 's' <hostname> <ctrl_unit> '[' <pid> ']' ':' }
}
sub MAIN( ) {
my regex h { h. };
for $*IN.comb(&h) -> $wof { # FIXME
say $wof;
}
}
Here are some example journalctl
logs for reference:
Jun 25 14:45:54 cpu-hostname systemd-timesyncd[725]: Initial synchronization to time server 185.125.190.56:123 (ntp.ubuntu.com).
Jun 25 14:45:54 cpu-hostname systemd-resolved[722]: Clock change detected. Flushing caches.
My sense is that trying to wrap a Grammar as a Regex to feed to .comb
is an unusual use of raku.
I think you want to generate a Seq of messages from the logfile using this Grammar.
The textbook way to do that would be something like:
for "logfile".IO.lines -> $line {
say Journalctl.parse($line)<message>
}
Notes:
- I don’t know for sure how $*IN works with lines, would need to test that…
.parse
is going to return a Match object (I usually dd the Match to see how to access its pieces, eg here using the key )
Otherwise, some other ideas are:
- make your Grammar work with line breaks
- use Actions to
make
the result you want (ie for each line), then just decant as a Seq from the Match result
my @wofs = Journalctl.parse($line, actions => JournalctlActions.new)<message>;
Or, if you really want to make the whole Grammar appear as a Regex (ie via .comb
) then you will need something like:
my h = rx/ {Journalctl.regex} /;
Ie using the curlies to have the regex call a .regex
method you write, which in turn calls self.parse
– there is some incantation in how that needs to fit to the expectations of routines in regexes (such as returns True on Match)