Grammar::Message
Grammar::Message, a Raku module to make grammar and regex messages prettier
inspired by Grammar::PrettyErrors, and ripping off the code used there for printing messages, prints error messages highlighting the line and, if possible, the position where it last worked.
Installing
Do the usual
zef install Grammar::Message
Running
You will need to insert a variable assignment behind the match you are going to check
my $str = (('a'..'z').rotor(3).map: *.join("")).join("\n");
my $saved-match; # Saves in situ
$str ~~ / m { $saved-match = $/} /; # Code right behind match you need printed
say pretty-message( "Found", $saved-match);
Will print
Found
3 ā ghi
4 ā jkl
5 āā¶ mno
^
6 ā pqr
7 ā stu
8 ā vwx
with highlights for the matching line.
grammar G {
token TOP { <letters>+ % \v}
token letters { { $saved-match = $/} \w ** 3 }
}
$str .= subst( "m", "mm" );
G.parse( $str );
say pretty-message( "Some failure around here", $saved-match);
will print
Some failure around here
4 ā jkl
5 ā mmno
6 āā¶ pqr
^
7 ā stu
8 ā vwx
Take into account that the marked point will always be the best bet, and in any
case it will reflect the state of the pos
attribute of the Match at the point
you saved it.
See also
Code by Brian Duggan in Grammar::PrettyErrors is the origin of this code.
License
(c) Brian Duggan, JJ Merelo
Respecting the original code license, this is also licensed under the Artistic license (included).