Match.pm

filter: Statementstmt 97.6% 83 / 85 Branchbran 85.2% 29 / 34 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 12 / 12 Podpod 25.0% 1 / 4 totaltotal 88.3% 137 / 155 CC 23 SCAR 13.7
CC
23
Cov
92%
__ANON__
5.3
__ANON__
5.0
__ANON__
5.0
CRAP 3.9
↑ Summary typeparser.pm »
1 package HOI::Match;
2  
3 2 require Exporter;
4  
5 2 ▶use Parse::Lex;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HOI::typeparser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our @ISA = qw( Exporter );
9 2 our @EXPORT_OK = qw( pmatch );
10 2 our $VERSION = '0.072';
11  
12 2 my @tokens = (
13 qw (
14 LPAREN [\(]
15 RPAREN [\)]
16 CONCAT ::
17 STRCONCAT [:]
18 NIL nil
19 IDENT [A-Za-z_][A-Za-z0-9_]*
20 CONST (?:0(?:\.[0-9]+)?)|(?:[1-9][0-9]*(?:\.[0-9]+)?)|(?:\".*\")|(?:\'.*\')
21 ),
22 COMMA => q/,/
23 );
24  
25 2 my $lexer = Parse::Lex->new(@tokens);
26 2 $lexer->skip('\s+');
27 2 my $parser = HOI::typeparser->new();
28  
29 102 ▶sub lexana {
Subroutine lexana: called CC=2 SCAR=6.9
Pod undocumented
30 102 my $token = $lexer->next;
31 102 ▶ if (not $lexer->eoi) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lexer->eoi) { } 74 28 102
32 74 return ($token->name, $token->text);
33 } else {
34 28 return ('', undef);
35 }
36 }
37  
38 2 my %compiled_patterns;
39  
40 28 ▶sub pcompile {
Subroutine pcompile: called CC=1 SCAR=0.0
Pod undocumented
41 28 $lexer->from(shift);
42 28 $parser->YYParse(yylex => \&lexana)
43 }
44  
45 230 ▶sub astmatch {
Subroutine astmatch: called CC=4 SCAR=13.9
Pod undocumented
46 230 my ($ast, $args) = @_;
47 230 ▶ return (0, {}) if ($#{$ast} ne $#{$args});
Branch: 1 branch
BranchTrueFalseTotal
if $#{$ast;} ne $#{$args;} 0 230 230
48 my %switches = (
49 "const" =>
50 76 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
51 76 my ($sym, $val) = @_;
52 76 ▶ if( (substr($sym, 0, 1) eq '\'') or (substr($sym, 0, 1) eq '"') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($sym, 0, 1) eq q['] or substr($sym, 0, 1) eq '"') { } 64 12 76
Condition66%2/3
substr($sym, 0, 1) eq "'" or substr($sym, 0, 1) eq "\""
l!l&&r!l&&!r
06412
MC/DC50%1/2
substr($sym, 0, 1) eq "'" or substr($sym, 0, 1) eq "\""
substr($sym, 0, 1) eq "'" substr($sym, 0, 1) eq "\""
Truth table66%2/3
A or B
A= substr($sym, 0, 1) eq "'"B= substr($sym, 0, 1) eq "\""
ABresult
1X1
011
000
53 64 my $quote = substr($sym, 0, 1);
54 64 ▶ return ($sym eq $quote.$val.$quote) ? (1, {}) : (0, {});
Branch: 1 branch
BranchTrueFalseTotal
$sym eq $quote . $val . $quote ? : 4 60 64
55 } else {
56 12 ▶ return ($sym == $val) ? (1, {}) : (0, {});
Branch: 1 branch
BranchTrueFalseTotal
$sym == $val ? : 2 10 12
57 }
58 },
59 "any" =>
60 116 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
61 116 my ($sym, $val) = @_;
62 116 ▶ (1, ((substr($sym, 0, 1) ne '_') ? { $sym => $val } : {}))
Branch: 1 branch
BranchTrueFalseTotal
substr($sym, 0, 1) ne '_' ? : 104 12 116
63 },
64 "list" =>
65 28 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
66 28 my ($l, $val) = @_;
67 28 ▶ if (($#{$l} >= 0) and ($#{$val} >= 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#{$l;} >= 0 and $#{$val;} >= 0) { } 20 8 28
elsif ($#{$l;} < 0 and $#{$val;} < 0) { } 4 4 8
Condition100%3/3
$#{$l;} >= 0 and $#{$val;} >= 0
!ll&&!rl&&r
4420
MC/DC100%2/2
$#{$l;} >= 0 and $#{$val;} >= 0
$#{$l;} >= 0 $#{$val;} >= 0
Truth table100%3/3
A and B
A= $#{$l;} >= 0B= $#{$val;} >= 0
ABresult
0X0
100
111
68 20 my ($s1, $r1) = astmatch([ $l->[0] ], [ $val->[0] ]);
69 20 my ($s2, $r2) = astmatch([ $l->[1] ], [ [ @$val[1..$#{$val}] ] ]);
70 20 return ($s1 * $s2, { %$r1, %$r2 });
71 8 ▶ } elsif (($#{$l} < 0) and ($#{$val} < 0)) {
Condition66%2/3
$#{$l;} < 0 and $#{$val;} < 0
!ll&&!rl&&r
404
MC/DC50%1/2
$#{$l;} < 0 and $#{$val;} < 0
$#{$l;} < 0 $#{$val;} < 0
Truth table66%2/3
A and B
A= $#{$l;} < 0B= $#{$val;} < 0
ABresult
0X0
100
111
72 4 return (1, {});
73 } else {
74 4 return (0, {});
75 }
76 },
77 "adt" =>
78 12 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
79 12 my ($adt, $val) = @_;
80 12 ▶ return (0, {}) if ((not defined $val->{"type"}) or (not defined $val->{"val"}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val->{"type"} and defined $val->{"val"} 0 12 12
Condition33%1/3
defined $val->{"type"} and defined $val->{"val"}
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $val->{"type"} and defined $val->{"val"}
defined $val->{"type"} defined $val->{"val"}
Truth table33%1/3
A and B
A= defined $val->{"type"}B= defined $val->{"val"}
ABresult
0X0
100
111
81 12 my ($sym, $typelist) = ($adt->[0], $adt->[1]);
82 12 ▶ return (0, {}) if ($adt->[0] ne $val->{"type"});
Branch: 1 branch
BranchTrueFalseTotal
if $adt->[0] ne $val->{"type"} 0 12 12
83 12 ▶ return (0, {}) if ($#{$adt->[1]} != $#{$val->{"val"}});
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$adt[1];} != $#{$$val{"val"};} 2 10 12
84 10 astmatch($adt->[1], $val->{"val"})
85 },
86 "strspl" =>
87 10 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
88 10 my ($idents, $val) = @_;
89 10 my ($x, $xs);
90 10 ▶ if ( ($x, $xs) = ($val =~ /(.)(.*)/s) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $val =~ /(.)(.*)/s) { } 10 0 10
91 10 return (1, { $idents->[0] => $x, $idents->[1] => $xs });
92 } else {
93 0 return (0, {});
94 }
95 }
96 230 );
97 230 my $ret = {};
98 230 ▶ for (my $idx = 0; $idx <= $#{$ast}; $idx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx <= $#{$ast;}) 242 154 396
99 242 my ($status, $result) = $switches{$ast->[$idx]->{"kind"}}->($ast->[$idx]->{"val"}, $args->[$idx]);
100 242 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 166 76 242
101 166 $ret = { %$ret, %$result };
102 } else {
103 76 return (0, {})
104 }
105 }
106 154 (1, $ret)
107 }
108  
109 104 ▶sub pmatch {
Subroutine pmatch: called CC=1 SCAR=0.0
Pod documented
110 104 my $patterns = \@_;
111 104 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
112 104 my $args = \@_;
113 104 ▶ while (@$patterns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$patterns) 180 0 180
114 180 my $pattern = shift @$patterns;
115 180 my $handler = shift @$patterns;
116 180 my $pattern_sig = (caller(1))[3].$pattern;
117 180 ▶ $compiled_patterns{$pattern_sig} = pcompile($pattern) if (not defined $compiled_patterns{$pattern_sig});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $compiled_patterns{$pattern_sig} 28 152 180
118 180 my $pattern_ast = $compiled_patterns{$pattern_sig};
119 180 my ($status, $results) = astmatch($pattern_ast, $args);
120 180 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 104 76 180
121 104 my ($package) = caller(1);
122 104 local $AttrPrefix = $package.'::';
123 #attr $results;
124 104 my $evalstr = '';
125 104 for my $key (keys %$results) {
126 124 $evalstr .= 'local $'."$AttrPrefix"."$key".' = $results->{'."$key".'}; ';
127 }
128 104 return eval "{ $evalstr ".'$handler->(%$results); }';
129 }
130 }
131 0
132 0 }
133 104 }
134  
135 2 1;
136 __END__
↑ Summary typeparser.pm »