Skip to content

Instantly share code, notes, and snippets.

@lucs
Last active December 11, 2015 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucs/cd3e912f20d6953f480b to your computer and use it in GitHub Desktop.
Save lucs/cd3e912f20d6953f480b to your computer and use it in GitHub Desktop.
use v6;
# Match : X aX baX
# No match : cX aaX
grammar Goo {
my %seen;
# Allow at most only one each of 'a' or 'b' per match.
token TOP {
<!{ %seen = () }> <ab>? <ab>? 'X'
}
token ab {
('a' | 'b') <?{
sub ($a_or_b) {
my $seen = $a_or_b ~~ %seen;
%seen{$a_or_b} = True;
! $seen;
}.($0)
}>
}
}
for ( :X, :aX, :baX, :!bbX, :!cX, ) {
my ($str, $should-match) = $_.kv;
my $match = Goo.parse($str);
say ($match.Bool == $should-match)
?? "Okay: $str"
!! ($should-match ?? "? " !! "! ") ~ "{$str}"
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment