| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
substr($sym, 0, 1) eq "'" or substr($sym, 0, 1) eq "\""
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 "\""
| |||||||||||||||||||||||||||||||||
| 53 | 64 | my $quote = substr($sym, 0, 1); | |||||||||||||||||||||||||||||||
| 54 | 64 | ▶ | return ($sym eq $quote.$val.$quote) ? (1, {}) : (0, {}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||||||
| 56 | 12 | ▶ | return ($sym == $val) ? (1, {}) : (0, {}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$#{$l;} >= 0 and $#{$val;} >= 0
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
| |||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $val->{"type"} and defined $val->{"val"}
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"}
| |||||||||||||||||||||||||||||||||
| 81 | 12 | my ($sym, $typelist) = ($adt->[0], $adt->[1]); | |||||||||||||||||||||||||||||||
| 82 | 12 | ▶ | return (0, {}) if ($adt->[0] ne $val->{"type"}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 12 | ▶ | return (0, {}) if ($#{$adt->[1]} != $#{$val->{"val"}}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 99 | 242 | my ($status, $result) = $switches{$ast->[$idx]->{"kind"}}->($ast->[$idx]->{"val"}, $args->[$idx]); | |||||||||||||||||||||||||||||||
| 100 | 242 | ▶ | if ($status) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||