4 use Test::More tests=>3;
7 ## This is a first pass at what the regex enhancements to
8 ## Moose::Util::TypeConstraints is going to look like. Basically I copyied
9 ## bits and added a little more parsing ability.
12 ## Copied from Moose::Util::TypeConstraints
16 my $valid_chars = qr{[\w:]};
17 my $type_atom = qr{ $valid_chars+ };
19 my $type = qr{ $valid_chars+ (?: \[ (??{$any}) \] )? }x;
20 my $type_capture_parts = qr{ ($valid_chars+) (?: \[ ((??{$any})) \] )? }x;
21 my $type_with_parameter = qr{ $valid_chars+ \[ (??{$any}) \] }x;
23 my $op_union = qr{ \s* \| \s* }x;
24 my $union = qr{ $type (?: $op_union $type )+ }x;
26 ## New Stuff for structured types.
28 my $indirection = qr{=>};
29 my $divider_ops = qr{ $comma | $indirection }x;
30 my $structure_divider = qr{\s* $divider_ops \s*}x;
31 my $structure_elements = qr{ ($type $structure_divider*)+ }x;
33 ## Addd the | $structure_elements to this.
34 $any = qr{ $type | $union | $structure_elements }x;
36 ## New Proposed methods to parse and create
37 sub _parse_structured_type_constraint {
38 { no warnings 'void'; $any; } # force capture of interpolated lexical
40 my($base, $elements) = ($_[0] =~ m{ $type_capture_parts }x);
41 return ($base, [split($structure_divider, $elements)]);
45 [_parse_structured_type_constraint('ArrayRef[Int,Str]')],
46 ["ArrayRef", ["Int", "Str"]]
47 => 'Correctly parsed ArrayRef[Int,Str]';
50 [_parse_structured_type_constraint('ArrayRef[ArrayRef[Int],Str]')],
51 ["ArrayRef", ["ArrayRef[Int]", "Str"]]
52 => 'Correctly parsed ArrayRef[ArrayRef[Int],Str]';
55 [_parse_structured_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]')],
56 ["HashRef", ["key1", "Int", "key2", "Int", "key3", "ArrayRef[Int]"]]
57 => 'Correctly parsed HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]';