From: Sam Vilain Date: Wed, 16 Dec 2009 22:23:34 +0000 (+1300) Subject: TypeConstraints: update the type notation parser to avoid back-tracking X-Git-Tag: 0.93_01~27 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=68d5a469d9dad0c5507cebbe6f539840bb45828a;p=gitmo%2FMoose.git TypeConstraints: update the type notation parser to avoid back-tracking The regex might be slow due to character-by-character back-tracking. However, there are few if any abiguous forms here; it is almost LL(1), save for the '$any' rule. We can therefore limit backtracking by surrounding all "tokens" in the 5.8+ (?>) independent sub-expression construct. --- diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index b6fa18e..7d716b1 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -572,18 +572,19 @@ sub _install_type_coercions ($$) { use re "eval"; my $valid_chars = qr{[\w:\.]}; - my $type_atom = qr{ $valid_chars+ }; + my $type_atom = qr{ (?>$valid_chars+) }x; + my $ws = qr{ (?>\s*) }x; my $any; - my $type = qr{ $valid_chars+ (?: \[ \s* (??{$any}) \s* \] )? }x; + my $type = qr{ $type_atom (?: \[ $ws (??{$any}) $ws \] )? }x; my $type_capture_parts - = qr{ ($valid_chars+) (?: \[ \s* ((??{$any})) \s* \] )? }x; + = qr{ ($type_atom) (?: \[ $ws ((??{$any})) $ws \] )? }x; my $type_with_parameter - = qr{ $valid_chars+ \[ \s* (??{$any}) \s* \] }x; + = qr{ $type_atom \[ $ws (??{$any}) $ws \] }x; - my $op_union = qr{ \s* \| \s* }x; - my $union = qr{ $type (?: $op_union $type )+ }x; + my $op_union = qr{ $ws \| $ws }x; + my $union = qr{ $type (?> (?: $op_union $type )+ ) }x; $any = qr{ $type | $union }x;