| 1 | package Pugs::Runtime::Match; | ||||||||||||||||||||||||||||
| 2 | # Documentation in the __END__ | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 36 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 36 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 36 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 36 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | #use Class::InsideOut qw( public register id ); | ||||||||||||||||||||||||||||
| 9 | 36 | ▶ | use Scalar::Util qw( refaddr blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 36 | ▶ | use Pugs::Runtime::StrPos; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 36 | ▶ | use overload ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 36 | '@{}' => \&array, | |||||||||||||||||||||||||||
| 14 | '%{}' => \&hash, | ||||||||||||||||||||||||||||
| 15 | 'bool' => \&bool, | ||||||||||||||||||||||||||||
| 16 | '&{}' => \&code, | ||||||||||||||||||||||||||||
| 17 | '${}' => \&scalar, | ||||||||||||||||||||||||||||
| 18 | '""' => \&flat, | ||||||||||||||||||||||||||||
| 19 | '0+' => \&flat, | ||||||||||||||||||||||||||||
| 20 | fallback => 1, | ||||||||||||||||||||||||||||
| 21 | 36 | ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # class method | ||||||||||||||||||||||||||||
| 24 | # ::fail can be called from inside closures | ||||||||||||||||||||||||||||
| 25 | # sub ::fail { $::_V6_SUCCEED = 0 } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 36 | my %_data; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 227 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 227 | my $obj = bless \$_[1], $_[0]; | |||||||||||||||||||||||||||
| 31 | 227 | $_data{ refaddr $obj } = $_[1]; | |||||||||||||||||||||||||||
| 32 | 227 | return $obj; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 227 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 227 | delete $_data{ refaddr $_[0] }; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 646 | ▶ | sub data { $_data{refaddr $_[0]} } | ||||||||||||||||||||||||||
|
Subroutine
data: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 516 | ▶ | sub bool { ${$_data{refaddr $_[0]}->{bool}} } | ||||||||||||||||||||||||||
|
Subroutine
bool: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 84 | ▶ | sub array { $_data{refaddr $_[0]}->{match} } | ||||||||||||||||||||||||||
|
Subroutine
array: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 213 | ▶ | sub from { | ||||||||||||||||||||||||||
|
Subroutine
from: called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 213 | return ${$_data{refaddr $_[0]}->{from}}; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | my $obj = $_data{refaddr $_[0]}; | |||||||||||||||||||||||||||
| 48 | #return ${$obj->{from}} if blessed ${$obj->{from}}; | ||||||||||||||||||||||||||||
| 49 | 0 | Pugs::Runtime::StrPos->from_str_codes( ${$obj->{str}}, ${$obj->{from}} ); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub pos { $_[0]->to } # pugs pos.t - lvalue ??? | ||||||||||||||||||||||||||
|
Subroutine
pos: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 395 | ▶ | sub to { | ||||||||||||||||||||||||||
|
Subroutine
to: called CC=1 SCAR=2.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 395 | return ${$_data{refaddr $_[0]}->{to}}; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my $obj = $_data{refaddr $_[0]}; | |||||||||||||||||||||||||||
| 57 | #return ${$obj->{to}} if blessed ${$obj->{to}}; | ||||||||||||||||||||||||||||
| 58 | #print "TO: ",${$obj->{to}},"\n"; | ||||||||||||||||||||||||||||
| 59 | 0 | Pugs::Runtime::StrPos->from_str_codes( ${$obj->{str}}, ${$obj->{to}} ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | # "low-level" position defaults to perl5-utf8 | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub from_as_codes { ${$_data{refaddr $_[0]}->{from}} } | ||||||||||||||||||||||||||
|
Subroutine
from_as_codes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub to_as_codes { ${$_data{refaddr $_[0]}->{to}} } | ||||||||||||||||||||||||||
|
Subroutine
to_as_codes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 77 | ▶ | sub hash { | ||||||||||||||||||||||||||
|
Subroutine
hash: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 77 | my $array = $_data{refaddr $_[0]}->{match}; | |||||||||||||||||||||||||||
| 67 | 77 | my $hash = $_data{refaddr $_[0]}->{named}; | |||||||||||||||||||||||||||
| 68 | 77 | $hash->{$_} = $array->[$_] for 0 .. $#$array; | |||||||||||||||||||||||||||
| 69 | 77 | return $hash; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | #my $array = $_data{refaddr $_[0]}->{match}; | ||||||||||||||||||||||||||||
| 72 | #return { | ||||||||||||||||||||||||||||
| 73 | # %{ $_data{refaddr $_[0]}->{named} }, | ||||||||||||||||||||||||||||
| 74 | # ( | ||||||||||||||||||||||||||||
| 75 | # map { ( $_, $array->[$_] ) } | ||||||||||||||||||||||||||||
| 76 | # 0 .. $#$array | ||||||||||||||||||||||||||||
| 77 | # ), | ||||||||||||||||||||||||||||
| 78 | #} | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub keys { | ||||||||||||||||||||||||||
|
Subroutine
keys: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | CORE::keys %{$_data{refaddr $_[0]}->{named}}, | |||||||||||||||||||||||||||
| 82 | 0 | 0 .. $#{ $_[0]->array } | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub values { | ||||||||||||||||||||||||||
|
Subroutine
values: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 0 | CORE::values %{$_data{refaddr $_[0]}->{named}}, | |||||||||||||||||||||||||||
| 86 | 0 | @{ $_[0]->array } | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub kv { | ||||||||||||||||||||||||||
|
Subroutine
kv: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 0 | map { ( $_, $_[0]->{$_} ) } | |||||||||||||||||||||||||||
| 90 | $_[0]->keys | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub elems { | ||||||||||||||||||||||||||
|
Subroutine
elems: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 0 | scalar $_[0]->keys | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub chars { CORE::length $_[0]->str } | ||||||||||||||||||||||||||
|
Subroutine
chars: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 41 | ▶ | sub flat { | ||||||||||||||||||||||||||
|
Subroutine
flat: called CC=5 SCAR=17.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 41 | my $obj = $_data{refaddr $_[0]}; | |||||||||||||||||||||||||||
| 100 | 41 | my $cap = $obj->{capture}; | |||||||||||||||||||||||||||
| 101 | #print ref $cap; | ||||||||||||||||||||||||||||
| 102 | 41 | ▶ | return $$cap | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $cap eq "REF" or ref $cap eq "SCALAR"
MC/DC0%0/2
ref $cap eq "REF" or ref $cap eq "SCALAR"
ref $cap eq "REF"
ref $cap eq "SCALAR"
Truth table33%1/3
A or B
A= ref $cap eq "REF"B= ref $cap eq "SCALAR"
| |||||||||||||||||||||||||||||
| 103 | if ref $cap eq 'REF' || | ||||||||||||||||||||||||||||
| 104 | ref $cap eq 'SCALAR'; | ||||||||||||||||||||||||||||
| 105 | 41 | ▶ | return '' unless ${$obj->{bool}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 41 | ▶ | return '' if $_[0]->from > length( ${$obj->{str}} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 41 | return substr( ${$obj->{str}}, $_[0]->from, $_[0]->to - $_[0]->from ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 19 | ▶ | sub str { | ||||||||||||||||||||||||||
|
Subroutine
str: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 19 | "" . $_[0]->flat; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub perl { | ||||||||||||||||||||||||||
|
Subroutine
perl: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | 0 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 118 | 0 | local $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||
| 119 | 0 | local $Data::Dumper::Pad = ' '; | |||||||||||||||||||||||||||
| 120 | 0 | return __PACKAGE__ . "->new( " . Dumper( $_[0]->data ) . ")\n"; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub yaml { | ||||||||||||||||||||||||||
|
Subroutine
yaml: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 0 | require YAML::Syck; | |||||||||||||||||||||||||||
| 125 | # interoperability with other YAML/Syck bindings: | ||||||||||||||||||||||||||||
| 126 | 0 | $YAML::Syck::ImplicitTyping = 1; | |||||||||||||||||||||||||||
| 127 | 0 | YAML::Syck::Dump( $_[0] ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # for Pugs interoperability | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub dump_hs { | ||||||||||||||||||||||||||
|
Subroutine
dump_hs: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 132 | 0 | my $obj; | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if (ref($_[0]) eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $obj = ${$_[0]}; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 0 | $obj = $_data{refaddr $_[0]}; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($obj) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 141 | # Ok, this is a genuine Match object. | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | return "PGE_Fail" unless ${$obj->{bool}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Now we matched; dump the rest of data | ||||||||||||||||||||||||||||
| 145 | 0 | join(' ', 'PGE_Match', ${$obj->{from}}, ${$obj->{to}}, | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | ('['.join(', ', map { dump_hs($_) } @{$obj->{match}||[]} ).']'), | ||||||||||||||||||||||||||
|
Condition0%0/2
$obj->{'match'} || []
MC/DC0%0/1
$obj->{'match'} || []
$obj->{'match'}
Truth table0%0/2
A
A= $obj->{'match'}
| |||||||||||||||||||||||||||||
| 147 | ('['.join(', ', map { | ||||||||||||||||||||||||||||
| 148 | 0 | my $str = $_; | |||||||||||||||||||||||||||
| 149 | 0 | ▶ | if ( my $dump = dump_hs($obj->{named}{$_}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | $str =~ s/([^ \!\#\$\%\&\x28-\x5B\x5D-\x7E])/'\\'.ord($1)/eg; | |||||||||||||||||||||||||||
| 151 | 0 | qq[("$str", $dump)]; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | else { | ||||||||||||||||||||||||||||
| 154 | 0 | (); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | } sort(CORE::keys(%{$obj->{named}||{}})) ).']'), | ||||||||||||||||||||||||||
|
Condition0%0/2
$obj->{'named'} || {}
MC/DC0%0/1
$obj->{'named'} || {}
$obj->{'named'}
Truth table0%0/2
A
A= $obj->{'named'}
| |||||||||||||||||||||||||||||
| 157 | ) | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | elsif (ref($_[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 160 | 0 | return "PGE_Array [" . join(', ', map { dump_hs($_) } @$obj) . "]" | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | elsif (!ref($_[0])) { | ||||||||||||||||||||||||||||
| 163 | 0 | my $str = shift; | |||||||||||||||||||||||||||
| 164 | 0 | $str =~ s/([^ \!\#\$\%\&\x28-\x5B\x5D-\x7E])/'\\'.ord($1)/eg; | |||||||||||||||||||||||||||
| 165 | 0 | return "PGE_String \"$str\""; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | else { | ||||||||||||||||||||||||||||
| 168 | 0 | warn "Unrecognized blessed match object: $_[0]"; | |||||||||||||||||||||||||||
| 169 | 0 | return ''; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # tail() for backwards compatibility | ||||||||||||||||||||||||||||
| 174 | # - doesn't work on failed matches | ||||||||||||||||||||||||||||
| 175 | 6 | ▶ | sub tail { | ||||||||||||||||||||||||||
|
Subroutine
tail: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | 6 | return substr( ${$_data{refaddr $_[0]}->{str}}, $_[0]->to ); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # state() is used for multiple matches and backtracking control | ||||||||||||||||||||||||||||
| 180 | 321 | ▶ | sub state { | ||||||||||||||||||||||||||
|
Subroutine
state: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 181 | 321 | return $_data{refaddr $_[0]}->{state}; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # return the capture | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub code { | ||||||||||||||||||||||||||
|
Subroutine
code: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 186 | 0 | my $c = $_[0]; | |||||||||||||||||||||||||||
| 187 | 0 | ▶ | return sub { $c->flat }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # return the capture | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | sub scalar { | ||||||||||||||||||||||||||
|
Subroutine
scalar: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 192 | 0 | return \( $_[0]->flat ); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 36 | 1; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | __END__ | ||||||||||||||||||||||||||||