Rand Stats

Grammar::Message

github:JJ

Grammar message

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

zef install Grammar::Message

Running

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.

$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 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).