raku

How can I use Raku Grammars with the comb function?


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+\s\d\d\s\d\d\:\d\d\:\d\d};
  token hostname { \S* };
  token ctl_unit { <-[\[]>+ };
  token pid { \d+ };
  regex preamble { <date> <.ws> <hostname> <.ws> <ctl_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.

Solution

  • I suggest you try this:

    my &h = rx/<Journalctl::full_log>/;