Posts in this category

Sat, 22 Aug 2009

Visualizing match trees

Permanent link

I think I mentioned once or twice that regexes and grammars in Perl 6 really rock. Most Perl 6 compilers use them to parse Perl 6, which demonstrates how powerful they are. Writing a full grammar for JSON took only about 70 generously spaced lines.

A successful match stores all kind of captures in a Match objects, which is actually a full match tree. If you're new to Perl 6 regexes, you might find the structure a bit surprising at first.

So I wrote a module which visualizes a match tree, annotating parts of the original string with the access path on the match object.

use SVG::MatchDumper;

token fruit     { banana | apple }
token currency  { 'USD' | 'dollar' | 'EUR' | '$' | '' }

my $x = 'just 20,000 dollar per apple';

if $x ~~ m/:s ((\d+) ** ',') <currency> 'per' <fruit> $ / {
    svg-dump($/, $x);
} else {
    die "no match";


match tree visualization

I appreciate any feedback and testing.

[/perl-6] Permanent link