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

1

10

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?

Nephew answered 8/4, 2019 at 9:12 Comment(0)
J
9

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;
Jollify answered 8/4, 2019 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.