Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Grammar::Tracer with a unit scoped grammar?

Tags:

grammar

raku

I am using Grammar::Tracer with the following setup:

p.p6

use v6;
use lib '.';
use MyGrammar;
my $res = MyGrammar.parse('hello 6 9 bye');
say $res;

MyGrammar.pm6:

unit grammar MyGrammar;
use Grammar::Tracer;

rule TOP { [<number> || <word> ]* }
rule number { \d+ }
rule word { \w+}

But the tracing is not enabled. I guess it is because the grammer MyGrammar is not in the lexical scope of the use Grammar::Tracer statement?

like image 322
Håkon Hægland Avatar asked Apr 08 '19 09:04

Håkon Hægland


1 Answers

The Grammar::Tracer module works by exporting a custom metaclass to be used in place of the default one for the grammar keyword. This must already be in place before the keyword grammar is encountered, since that is when we resolve and commit to the metaclass to be used for the type being declared.

The solution is to put the use statement ahead of the grammar declaration:

use Grammar::Tracer;
unit grammar MyGrammar;
like image 175
Jonathan Worthington Avatar answered Oct 22 '22 02:10

Jonathan Worthington