line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::DPath::Path; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:SCHWIGON'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Abstraction for a DPath |
4
|
|
|
|
|
|
|
$Data::DPath::Path::VERSION = '0.59'; |
5
|
12
|
|
|
12
|
|
1573
|
use strict; |
|
12
|
|
|
|
|
20
|
|
|
12
|
|
|
|
|
357
|
|
6
|
12
|
|
|
12
|
|
60
|
use warnings; |
|
12
|
|
|
|
|
21
|
|
|
12
|
|
|
|
|
276
|
|
7
|
|
|
|
|
|
|
|
8
|
12
|
|
|
12
|
|
7314
|
use Data::Dumper; |
|
12
|
|
|
|
|
79693
|
|
|
12
|
|
|
|
|
867
|
|
9
|
12
|
|
|
12
|
|
5880
|
use aliased 'Data::DPath::Step'; |
|
12
|
|
|
|
|
9047
|
|
|
12
|
|
|
|
|
75
|
|
10
|
12
|
|
|
12
|
|
1168
|
use aliased 'Data::DPath::Point'; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
59
|
|
11
|
12
|
|
|
12
|
|
1031
|
use aliased 'Data::DPath::Context'; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
55
|
|
12
|
12
|
|
|
12
|
|
10110
|
use Text::Balanced 2.02 'extract_delimited', 'extract_codeblock'; |
|
12
|
|
|
|
|
195934
|
|
|
12
|
|
|
|
|
1402
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use Class::XSAccessor |
15
|
12
|
|
|
|
|
163
|
chained => 1, |
16
|
|
|
|
|
|
|
accessors => { |
17
|
|
|
|
|
|
|
path => 'path', |
18
|
|
|
|
|
|
|
_steps => '_steps', |
19
|
|
|
|
|
|
|
give_references => 'give_references', |
20
|
12
|
|
|
12
|
|
112
|
}; |
|
12
|
|
|
|
|
37
|
|
21
|
|
|
|
|
|
|
|
22
|
12
|
|
|
|
|
7862
|
use constant { ROOT => 'ROOT', |
23
|
|
|
|
|
|
|
ANYWHERE => 'ANYWHERE', |
24
|
|
|
|
|
|
|
KEY => 'KEY', |
25
|
|
|
|
|
|
|
ANYSTEP => 'ANYSTEP', |
26
|
|
|
|
|
|
|
NOSTEP => 'NOSTEP', |
27
|
|
|
|
|
|
|
PARENT => 'PARENT', |
28
|
|
|
|
|
|
|
ANCESTOR => 'ANCESTOR', |
29
|
|
|
|
|
|
|
ANCESTOR_OR_SELF => 'ANCESTOR_OR_SELF', |
30
|
12
|
|
|
12
|
|
4148
|
}; |
|
12
|
|
|
|
|
28
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub new { |
33
|
460
|
|
|
460
|
1
|
27295
|
my $class = shift; |
34
|
460
|
|
|
|
|
1502
|
my $self = bless { @_ }, $class; |
35
|
460
|
|
|
|
|
1370
|
$self->_build__steps; |
36
|
460
|
|
|
|
|
4108
|
return $self; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub unescape { |
40
|
1850
|
|
|
1850
|
1
|
5838
|
my ($str) = @_; |
41
|
|
|
|
|
|
|
|
42
|
1850
|
50
|
|
|
|
3448
|
return unless defined $str; |
43
|
1850
|
|
|
|
|
3192
|
$str =~ s/\\{2}/\\/g; |
44
|
1850
|
|
|
|
|
2575
|
$str =~ s/\\(["'])/$1/g; # '"$ |
45
|
1850
|
|
|
|
|
3794
|
return $str; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub unquote { |
49
|
39
|
|
|
39
|
1
|
79
|
my ($str) = @_; |
50
|
39
|
|
|
|
|
207
|
$str =~ s/^"(.*)"$/$1/g; |
51
|
39
|
|
|
|
|
104
|
return $str; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
1845
|
|
|
1845
|
1
|
6413
|
sub quoted { shift =~ m,^/["'],; } # " |
55
|
|
|
|
|
|
|
|
56
|
12
|
|
|
12
|
|
86
|
eval 'use overload "~~" => \&op_match' if $] >= 5.010; |
|
12
|
|
|
|
|
29
|
|
|
12
|
|
|
|
|
65
|
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub op_match { |
59
|
158
|
|
|
158
|
1
|
359
|
my ($self, $data, $rhs) = @_; |
60
|
|
|
|
|
|
|
|
61
|
158
|
|
|
|
|
347
|
return $self->matchr( $data ); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# essentially the Path parser |
65
|
|
|
|
|
|
|
sub _build__steps { |
66
|
460
|
|
|
460
|
|
838
|
my ($self) = @_; |
67
|
|
|
|
|
|
|
|
68
|
460
|
|
|
|
|
1157
|
my $remaining_path = $self->path; |
69
|
460
|
|
|
|
|
707
|
my $extracted; |
70
|
|
|
|
|
|
|
my @steps; |
71
|
|
|
|
|
|
|
|
72
|
460
|
|
|
|
|
2499
|
push @steps, Step->new->part('')->kind(ROOT); |
73
|
|
|
|
|
|
|
|
74
|
460
|
|
|
|
|
1257
|
while ($remaining_path) { |
75
|
1845
|
|
|
|
|
4180
|
my $plain_part; |
76
|
|
|
|
|
|
|
my $filter; |
77
|
1845
|
|
|
|
|
0
|
my $kind; |
78
|
1845
|
100
|
|
|
|
3112
|
if ( quoted($remaining_path) ) { |
79
|
39
|
|
|
|
|
130
|
($plain_part, $remaining_path) = extract_delimited($remaining_path, q/'"/, "/"); # ' |
80
|
39
|
|
|
|
|
4557
|
($filter, $remaining_path) = extract_codeblock($remaining_path, "[]"); |
81
|
39
|
|
|
|
|
4451
|
$plain_part = unescape unquote $plain_part; |
82
|
39
|
|
|
|
|
76
|
$kind = KEY; # quoted is always a key |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else |
85
|
|
|
|
|
|
|
{ |
86
|
1806
|
|
|
|
|
2584
|
my $filter_already_extracted = 0; |
87
|
1806
|
|
|
|
|
3960
|
($extracted, $remaining_path) = extract_delimited($remaining_path,'/'); |
88
|
|
|
|
|
|
|
|
89
|
1806
|
100
|
|
|
|
118157
|
if (not $extracted) { |
90
|
443
|
|
|
|
|
1003
|
($extracted, $remaining_path) = ($remaining_path, undef); # END OF PATH |
91
|
|
|
|
|
|
|
} else { |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# work around to recognize slashes in filter expressions and handle them: |
94
|
|
|
|
|
|
|
# |
95
|
|
|
|
|
|
|
# - 1) see if key unexpectedly contains opening "[" but no closing "]" |
96
|
|
|
|
|
|
|
# - 2) use the part before "[" |
97
|
|
|
|
|
|
|
# - 3) unshift the rest to remaining |
98
|
|
|
|
|
|
|
# - 4) extract_codeblock() explicitely |
99
|
1363
|
100
|
100
|
|
|
4493
|
if ($extracted =~ /(.*)((?
|
100
|
32
|
|
|
|
|
211
|
$remaining_path = $2 . $remaining_path; |
101
|
32
|
|
|
|
|
140
|
( $plain_part = $1 ) =~ s|^/||; |
102
|
32
|
|
|
|
|
147
|
($filter, $remaining_path) = extract_codeblock($remaining_path, "[]"); |
103
|
32
|
|
|
|
|
12388
|
$filter_already_extracted = 1; |
104
|
|
|
|
|
|
|
} else { |
105
|
1331
|
|
|
|
|
3406
|
$remaining_path = (chop $extracted) . $remaining_path; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
1806
|
100
|
|
|
|
9783
|
($plain_part, $filter) = $extracted =~ m,^/ # leading / |
110
|
|
|
|
|
|
|
(.*?) # path part |
111
|
|
|
|
|
|
|
(\[.*\])?$ # optional filter |
112
|
|
|
|
|
|
|
,xsg unless $filter_already_extracted; |
113
|
1806
|
|
|
|
|
3947
|
$plain_part = unescape $plain_part; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
12
|
|
|
12
|
|
99
|
no warnings 'uninitialized'; |
|
12
|
|
|
|
|
48
|
|
|
12
|
|
|
|
|
4360
|
|
117
|
1845
|
100
|
100
|
|
|
6062
|
if ($plain_part eq '') { $kind ||= ANYWHERE } |
|
310
|
100
|
|
|
|
1187
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
118
|
304
|
|
100
|
|
|
1055
|
elsif ($plain_part eq '*') { $kind ||= ANYSTEP } |
119
|
125
|
|
100
|
|
|
844
|
elsif ($plain_part eq '.') { $kind ||= NOSTEP } |
120
|
114
|
|
100
|
|
|
415
|
elsif ($plain_part eq '..') { $kind ||= PARENT } |
121
|
19
|
|
50
|
|
|
74
|
elsif ($plain_part eq '::ancestor') { $kind ||= ANCESTOR } |
122
|
21
|
|
100
|
|
|
79
|
elsif ($plain_part eq '::ancestor-or-self') { $kind ||= ANCESTOR_OR_SELF } |
123
|
952
|
|
100
|
|
|
2817
|
else { $kind ||= KEY } |
124
|
|
|
|
|
|
|
|
125
|
1845
|
|
|
|
|
9412
|
push @steps, Step->new->part($plain_part)->kind($kind)->filter($filter); |
126
|
|
|
|
|
|
|
} |
127
|
460
|
100
|
|
|
|
1341
|
pop @steps if $steps[-1]->kind eq ANYWHERE; # ignore final '/' |
128
|
460
|
|
|
|
|
1553
|
$self->_steps( \@steps ); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub match { |
132
|
245
|
|
|
245
|
1
|
488
|
my ($self, $data) = @_; |
133
|
|
|
|
|
|
|
|
134
|
245
|
|
|
|
|
318
|
return @{$self->matchr($data)}; |
|
245
|
|
|
|
|
468
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub matchr { |
138
|
436
|
|
|
436
|
1
|
700
|
my ($self, $data) = @_; |
139
|
|
|
|
|
|
|
|
140
|
436
|
|
|
|
|
2997
|
my $context = Context |
141
|
|
|
|
|
|
|
->new |
142
|
|
|
|
|
|
|
->current_points([ Point->new->ref(\$data) ]) |
143
|
|
|
|
|
|
|
->give_references($self->give_references); |
144
|
436
|
|
|
|
|
1410
|
return $context->matchr($self); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
1; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
__END__ |