XPath/Tree.pm

filter: Statementstmt 86.9% 346 / 398 Branchbran 77.0% 114 / 148 Conditioncond 83.3% 10 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 90.9% 20 / 22 Podpod 0.0% 0 / 20 totaltotal 81.3% 494 / 607 CC 89 SCAR 22.3
CC
89
Cov
84%
build_tree
18.0
claim_groups
15.1
build_steps
13.8
CRAP 9.3
« XPath/Tokener.pm ↑ Summary XPath/Result.pm »
1 package XML::Parser::Lite::Tree::XPath::Tree;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use XML::Parser::Lite::Tree::XPath::Tokener;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 179 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
7 179 my ($class) = @_;
8 179 my $self = bless {}, $class;
9 179 $self->{error} = 0;
10 179 return $self;
11 }
12  
13 179 ▶sub build_tree {
Subroutine build_tree: called CC=14 SCAR=28.9
Pod undocumented
14 179 my ($self, $tokens) = @_;
15  
16 179 $self->{error} = 0;
17 179 $self->{tokens} = $tokens;
18  
19 #
20 # build a basic tree using the brackets
21 #
22  
23 179 ▶ return 0 unless $self->make_groups();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->make_groups 0 179 179
24 179 $self->recurse_before($self, 'del_links');
25  
26  
27 #
28 # simple groupings
29 #
30  
31 179 ▶ return 0 unless $self->recurse_before($self, 'clean_axis_and_abbreviations');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recurse_before($self, "clean_axis_and_abbreviations") 0 179 179
32 179 ▶ return 0 unless $self->recurse_before($self, 'claim_groups');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recurse_before($self, "claim_groups") 3 176 179
33 176 ▶ return 0 unless $self->recurse_after($self, 'build_steps');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recurse_after($self, "build_steps") 0 176 176
34 176 ▶ return 0 unless $self->recurse_after($self, 'build_paths');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recurse_after($self, "build_paths") 0 176 176
35  
36  
37 #
38 # get operator oprands
39 #
40  
41 176 ▶ return 0 unless $self->binops(['|'], 'UnionExpr');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->binops(["|"], "UnionExpr") 0 176 176
42 176 ▶ return 0 unless $self->recurse_before($self, 'unary_minus');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recurse_before($self, "unary_minus") 0 176 176
43 176 ▶ return 0 unless $self->binops(['*','div','mod'], 'MultiplicativeExpr');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->binops(["*", "div", "mod"], "MultiplicativeExpr") 0 176 176
44 176 ▶ return 0 unless $self->binops(['+','-'], 'AdditiveExpr');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->binops(["+", "-"], "AdditiveExpr") 0 176 176
45 176 ▶ return 0 unless $self->binops(['<','<=','>','>='], 'RelationalExpr');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->binops(["<", "<=", ">", ">="], "RelationalExpr") 0 176 176
46 176 ▶ return 0 unless $self->binops(['=','!='], 'EqualityExpr');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->binops(["=", "!="], "EqualityExpr") 0 176 176
47 176 ▶ return 0 unless $self->binops(['and'], 'AndExpr');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->binops(["and"], "AndExpr") 0 176 176
48 176 ▶ return 0 unless $self->binops(['or'], 'OrExpr');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->binops(["or"], "OrExpr") 0 176 176
49  
50 #return 0 unless $self->find_expressions(['UnionExpr', 'MultiplicativeExpr', 'AdditiveExpr', 'RelationalExpr', 'EqualityExpr', 'AndExpr', 'OrExpr']);
51  
52  
53 176 return 1;
54 }
55  
56 41 ▶sub dump_flat {
Subroutine dump_flat: called CC=2 SCAR=6.9
Pod undocumented
57 41 my ($self) = @_;
58 41 $self->{dump} = '';
59  
60 41 for my $token(@{$self->{tokens}}){
61 41 $self->dump_flat_go($token);
62 }
63  
64 41 my $dump = $self->{dump};
65 41 delete $self->{dump};
66 41 return $dump;
67 }
68  
69 279 ▶sub dump_flat_go {
Subroutine dump_flat_go: called CC=2 SCAR=6.9
Pod undocumented
70 279 my ($self, $node) = @_;
71  
72 279 $self->{dump} .= '['.$node->dump();
73  
74 279 for my $token(@{$node->{tokens}}){
75  
76 238 $self->dump_flat_go($token);
77 }
78  
79 279 $self->{dump} .= ']';
80 }
81  
82 0 ▶sub dump_tree {
Subroutine dump_tree: not called CC=2 SCAR=17.9
Pod undocumented
83 0 my ($self) = @_;
84 0 $self->{dump} = '';
85 0 $self->{indent} = [''];
86  
87 0 for my $token(@{$self->{tokens}}){
88 0 $self->dump_tree_go($token);
89 }
90  
91 0 my $dump = $self->{dump};
92 0 delete $self->{dump};
93 0 delete $self->{indent};
94 0 return $dump;
95 }
96  
97 0 ▶sub dump_tree_go {
Subroutine dump_tree_go: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my ($self, $node) = @_;
99  
100 0 $self->{dump} .= @{$self->{indent}}[-1].$node->dump()."\n";
101  
102 0 push @{$self->{indent}}, @{$self->{indent}}[-1].' - ';
103  
104 0 for my $token(@{$node->{tokens}}){
105  
106 0 $self->dump_tree_go($token);
107 }
108  
109 0 pop @{$self->{indent}};
110 }
111  
112 179 ▶sub make_groups {
Subroutine make_groups: called CC=8 SCAR=21.0
Pod undocumented
113 179 my ($self) = @_;
114  
115 179 my $tokens = $self->{tokens};
116 179 $self->{tokens} = [];
117  
118 179 my $parent = $self;
119  
120 179 for my $token(@{$tokens}){
121  
122 1223 ▶ if ($token->match('Symbol', '(')){
Branch: 4 branches
BranchTrueFalseTotal
if ($token->match('Symbol', '(')) { } 112 1111 1223
elsif ($token->match('Symbol', '[')) { } 50 1061 1111
elsif ($token->match('Symbol', ')')) { } 112 949 1061
elsif ($token->match('Symbol', ']')) { } 50 899 949
123  
124 112 my $group = XML::Parser::Lite::Tree::XPath::Token->new();
125 112 $group->{type} = 'Group()';
126 112 $group->{tokens} = [];
127 112 $group->{parent} = $parent;
128  
129 112 push @{$parent->{tokens}}, $group;
130 112 $parent = $group;
131  
132 }elsif ($token->match('Symbol', '[')){
133  
134 50 my $group = XML::Parser::Lite::Tree::XPath::Token->new();
135 50 $group->{type} = 'Predicate';
136 50 $group->{tokens} = [];
137 50 $group->{parent} = $parent;
138  
139 50 push @{$parent->{tokens}}, $group;
140 50 $parent = $group;
141  
142 }elsif ($token->match('Symbol', ')')){
143  
144 112 ▶ if ($parent->{type} ne 'Group()'){
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->{"type"} ne "Group()") 0 112 112
145 0 $self->{error} = "Found unexpected closing bracket ')'.";
146 0 return 0;
147 }
148  
149 112 $parent = $parent->{parent};
150  
151 }elsif ($token->match('Symbol', ']')){
152  
153 50 ▶ if ($parent->{type} ne 'Predicate'){
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->{"type"} ne "Predicate") 0 50 50
154 0 $self->{error} = "Found unexpected closing bracket ']'.";
155 0 return 0;
156 }
157  
158 50 $parent = $parent->{parent};
159  
160 }else{
161 899 $token->{parent} = $parent;
162 899 push @{$parent->{tokens}}, $token;
163 }
164 }
165  
166 179 return 1;
167 }
168  
169 4300 ▶sub recurse_before {
Subroutine recurse_before: called CC=4 SCAR=13.9
Pod undocumented
170 4300 my ($self, $root, $method) = @_;
171  
172 4300 ▶ return 0 unless $self->$method($root);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$method($root) 182 4118 4300
173  
174 4118 for my $token(@{$root->{tokens}}){
175  
176 3587 ▶ return 0 unless $self->recurse_before($token, $method);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recurse_before($token, $method) 0 3587 3587
177 }
178  
179 4118 return 1;
180 }
181  
182 12633 ▶sub recurse_after {
Subroutine recurse_after: called CC=4 SCAR=14.2
Pod undocumented
183 12633 my ($self, $root, $method) = @_;
184  
185 12633 for my $token(@{$root->{tokens}}){
186  
187 11049 ▶ return 0 unless $self->recurse_after($token, $method);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recurse_after($token, $method) 0 11049 11049
188 }
189  
190 12633 ▶ return 0 unless $self->$method($root);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$method($root) 0 12633 12633
191  
192 12633 return 1;
193 }
194  
195 1232 ▶sub binops {
Subroutine binops: called CC=1 SCAR=0.0
Pod undocumented
196 1232 my ($self, $ops, $production) = @_;
197 1232 $self->{binops} = $ops;
198 1232 $self->{binop_production} = $production;
199  
200 1232 my $ret = $self->recurse_after($self, 'do_binops');
201  
202 1232 delete $self->{binops};
203 1232 delete $self->{binop_production};
204  
205 1232 return $ret;
206 }
207  
208 1443 ▶sub claim_groups {
Subroutine claim_groups: called CC=14 SCAR=27.1
Pod undocumented
209 1443 my ($self, $root) = @_;
210  
211 1443 my $tokens = $root->{tokens};
212 1443 $root->{tokens} = [];
213  
214 1443 ▶ while(my $token = shift @{$tokens}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @{$tokens;}) 1362 1440 2802
215  
216  
217 #
218 # makes claims
219 #
220  
221 1362 ▶ if ($token->match('NodeType')){
Branch: 3 branches
BranchTrueFalseTotal
if ($token->match('NodeType')) { } 6 1356 1362
elsif ($token->match('FunctionName')) { } 106 1250 1356
elsif ($token->match('Group()')) { } 0 1250 1250
222  
223 # node type's claim the follow group node
224  
225 6 my $next = shift @{$tokens};
226  
227 6 ▶ if (!$next->match('Group()')){
Branch: 1 branch
BranchTrueFalseTotal
unless ($next->match("Group()")) 0 6 6
228 0 $self->{error} = "Found NodeType '$token->{content}' without a following '(' (found a following '$next->{type}').";
229 0 return 0;
230 }
231  
232 6 my $childs = scalar(@{$next->{tokens}});
233  
234 6 ▶ if ($token->{content} eq 'processing-instruction'){
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{'content'} eq 'processing-instruction') { } 4 2 6
235  
236 4 ▶ if ($childs == 0){
Branch: 2 branches
BranchTrueFalseTotal
if ($childs == 0) { } 1 3 4
elsif ($childs == 1) { } 2 1 3
237  
238 #ok
239  
240 }elsif ($childs == 1){
241  
242 2 ▶ if ($next->{tokens}->[0]->{type} eq 'Literal'){
Branch: 1 branch
BranchTrueFalseTotal
if ($next->{'tokens'}[0]{'type'} eq 'Literal') { } 1 1 2
243  
244 1 $token->{argument} = $next->{tokens}->[0]->{content};
245  
246 }else{
247 1 $self->{error} = "processing-instruction node has a non-Literal child node (of type '$next->{tokens}->[0]->{type}').";
248 1 return 0;
249 }
250 }else{
251 1 $self->{error} = "processing-instruction node has more than one child node.";
252 1 return 0;
253 }
254  
255 }else{
256 2 ▶ if ($childs > 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($childs > 0) 1 1 2
257 1 $self->{error} = "NodeType $token->{content} node has unexpected children.";
258 1 return 0;
259 }
260 }
261  
262 3 $token->{type} = 'NodeTypeTest';
263 3 push @{$root->{tokens}}, $token;
264  
265 }elsif ($token->match('FunctionName')){
266  
267 # FunctionNames's claim the follow group node - it should be an arglist
268  
269 106 my $next = shift @{$tokens};
270  
271 106 ▶ if (!$next->match('Group()')){
Branch: 1 branch
BranchTrueFalseTotal
unless ($next->match("Group()")) 0 106 106
272 0 $self->{error} = "Found FunctionName '$token->{content}' without a following '(' (found a following '$next->{type}').";
273 0 return 0;
274 }
275  
276 #
277 # recurse manually - this node will never be scanned by this loop
278 #
279  
280 106 ▶ return 0 unless $self->claim_groups($next);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->claim_groups($next) 0 106 106
281  
282  
283 #
284 # organise it into an arg list
285 #
286  
287 106 ▶ return 0 unless $self->make_arg_list($token, $next);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->make_arg_list($token, $next) 0 106 106
288
289  
290  
291 106 push @{$root->{tokens}}, $token;
292  
293  
294 }elsif ($token->match('Group()')){
295  
296 0 $token->{type} = 'PrimaryExpr';
297  
298 0 push @{$root->{tokens}}, $token;
299  
300 }else{
301  
302 1250 push @{$root->{tokens}}, $token;
303 }
304  
305 }
306  
307 1440 return 1;
308 }
309  
310 106 ▶sub make_arg_list {
Subroutine make_arg_list: called CC=4 SCAR=13.9
Pod undocumented
311 106 my ($self, $root, $arg_group) = @_;
312  
313 106 $root->{type} = 'FunctionCall';
314 106 $root->{tokens} = [];
315  
316 # no need to construct an arg list if there aren't any args
317 106 ▶ return 1 unless scalar @{$arg_group->{tokens}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$arg_group->{"tokens"};} 35 71 106
318  
319 71 my $arg = XML::Parser::Lite::Tree::XPath::Token->new();
320 71 $arg->{type} = 'FunctionArg';
321 71 $arg->{tokens} = [];
322  
323 71 ▶ while(my $token = shift @{$arg_group->{tokens}}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @{$$arg_group{"tokens"};}) 201 71 272
324  
325 201 ▶ if ($token->match('Symbol', ',')){
Branch: 1 branch
BranchTrueFalseTotal
if ($token->match('Symbol', ',')) { } 25 176 201
326  
327 25 push @{$root->{tokens}}, $arg;
328  
329 25 $arg = XML::Parser::Lite::Tree::XPath::Token->new();
330 25 $arg->{type} = 'FunctionArg';
331 25 $arg->{tokens} = [];
332  
333 }else{
334  
335 176 $token->{parent} = $arg;
336 176 push @{$arg->{tokens}}, $token;
337 }
338 }
339  
340 71 $arg->{parent} = $root;
341 71 push @{$root->{tokens}}, $arg;
342
343  
344 71 return 1;
345 }
346  
347 1386 ▶sub clean_axis_and_abbreviations {
Subroutine clean_axis_and_abbreviations: called CC=8 SCAR=20.8
Pod undocumented
348  
349 1386 my ($self, $root) = @_;
350  
351 1386 my $tokens = $root->{tokens};
352 1386 $root->{tokens} = [];
353  
354 1386 ▶ while(my $token = shift @{$tokens}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @{$tokens;}) 1022 1386 2408
355  
356 1022 ▶ if ($token->match('AxisName')){
Branch: 5 branches
BranchTrueFalseTotal
if ($token->match('AxisName')) { } 39 983 1022
elsif ($token->match('Symbol', '@')) { } 15 968 983
elsif ($token->match('Operator', '//')) { } 61 907 968
elsif ($token->match('Symbol', '.')) { } 1 906 907
elsif ($token->match('Symbol', '..')) { } 1 905 906
357  
358 39 my $next = shift @{$tokens};
359  
360 39 ▶ unless ($next->match('Symbol', '::')){
Branch: 1 branch
BranchTrueFalseTotal
unless ($next->match("Symbol", "::")) 0 39 39
361  
362 0 $self->{error} = "Found an AxisName '$token->{content}' without a following ::";
363 0 return 0;
364 }
365  
366 39 $token->{type} = 'AxisSpecifier';
367  
368 39 push @{$root->{tokens}}, $token;
369  
370  
371 }elsif ($token->match('Symbol', '@')){
372  
373 15 $token->{type} = 'AxisSpecifier';
374 15 $token->{content} = 'attribute';
375  
376 15 push @{$root->{tokens}}, $token;
377  
378  
379 }elsif ($token->match('Operator', '//')){
380  
381 # // == /descendant-or-self::node()/
382  
383 61 $token = XML::Parser::Lite::Tree::XPath::Token->new();
384 61 $token->{type} = 'Operator';
385 61 $token->{content} = '/';
386 61 push @{$root->{tokens}}, $token;
387  
388 61 $token = XML::Parser::Lite::Tree::XPath::Token->new();
389 61 $token->{type} = 'AxisSpecifier';
390 61 $token->{content} = 'descendant-or-self';
391 61 push @{$root->{tokens}}, $token;
392  
393 61 $token = XML::Parser::Lite::Tree::XPath::Token->new();
394 61 $token->{type} = 'NodeTypeTest';
395 61 $token->{content} = 'node';
396 61 push @{$root->{tokens}}, $token;
397  
398 61 $token = XML::Parser::Lite::Tree::XPath::Token->new();
399 61 $token->{type} = 'Operator';
400 61 $token->{content} = '/';
401 61 push @{$root->{tokens}}, $token;
402  
403  
404 }elsif ($token->match('Symbol', '.')){
405  
406 1 $token = XML::Parser::Lite::Tree::XPath::Token->new();
407 1 $token->{type} = 'AxisSpecifier';
408 1 $token->{content} = 'self';
409 1 push @{$root->{tokens}}, $token;
410  
411 1 $token = XML::Parser::Lite::Tree::XPath::Token->new();
412 1 $token->{type} = 'NodeTypeTest';
413 1 $token->{content} = 'node';
414 1 push @{$root->{tokens}}, $token;
415  
416  
417 }elsif ($token->match('Symbol', '..')){
418  
419 1 $token = XML::Parser::Lite::Tree::XPath::Token->new();
420 1 $token->{type} = 'AxisSpecifier';
421 1 $token->{content} = 'parent';
422 1 push @{$root->{tokens}}, $token;
423  
424 1 $token = XML::Parser::Lite::Tree::XPath::Token->new();
425 1 $token->{type} = 'NodeTypeTest';
426 1 $token->{content} = 'node';
427 1 push @{$root->{tokens}}, $token;
428  
429  
430 }else{
431  
432 905 push @{$root->{tokens}}, $token;
433 }
434 }
435  
436 1386 return 1;
437 }
438  
439 1334 ▶sub build_steps {
Subroutine build_steps: called CC=13 SCAR=26.3
Pod undocumented
440 1334 my ($self, $root) = @_;
441  
442 1334 my $tokens = $root->{tokens};
443 1334 $root->{tokens} = [];
444  
445 1334 ▶ while(my $token = shift @{$tokens}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @{$tokens;}) 991 1334 2325
446  
447 991 ▶ if ($token->match('AxisSpecifier')){
Branch: 3 branches
BranchTrueFalseTotal
if ($token->match('AxisSpecifier')) { } 117 874 991
elsif ($token->match('NodeTypeTest') or $token->match('NameTest')) { } 179 695 874
elsif ($token->match('Predicate')) { } 0 695 695
448  
449 117 my $next = shift @{$tokens};
450  
451 117 ▶ unless (defined $next){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $next) 0 117 117
452  
453 0 $self->{error} = "AxisSpecifier found without following NodeTest.";
454 0 return 0;
455 }
456  
457 117 ▶ unless ($next->match('NodeTypeTest') || $next->match('NameTest')){
Branch: 1 branch
BranchTrueFalseTotal
unless ($next->match("NodeTypeTest") or $next->match("NameTest")) 0 117 117
Condition66%2/3
$next->match("NodeTypeTest") or $next->match("NameTest")
l!l&&r!l&&!r
63540
MC/DC0%0/2
$next->match("NodeTypeTest") or $next->match("NameTest")
$next->match("NodeTypeTest") $next->match("NameTest")
Truth table66%2/3
A or B
A= $next->match("NodeTypeTest")B= $next->match("NameTest")
ABresult
1X1
011
000
458  
459 0 $self->{error} = "AxisSpecifier found without following NodeTest (NodeTypeTest | NameTest) (found $next->{type} instead).";
460 0 return 0;
461 }
462  
463 117 my $step = XML::Parser::Lite::Tree::XPath::Token->new();
464 117 $step->{type} = 'Step';
465 117 $step->{axis} = $token->{content};
466 117 $step->{tokens} = [];
467  
468 117 push @{$step->{tokens}}, $next;
469  
470  
471 117 ▶ while(my $token = shift @{$tokens}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @{$tokens;}) 73 44 117
472  
473 73 ▶ if ($token->match('Predicate')){
Branch: 1 branch
BranchTrueFalseTotal
if ($token->match('Predicate')) { } 0 73 73
474  
475 0 push @{$step->{tokens}}, $token;
476 }else{
477 73 unshift @{$tokens}, $token;
478 73 last;
479 }
480 }
481  
482 117 push @{$root->{tokens}}, $step;
483  
484  
485 ▶ }elsif ($token->match('NodeTypeTest') || $token->match('NameTest')){
Condition100%3/3
$token->match("NodeTypeTest") or $token->match("NameTest")
l!l&&r!l&&!r
3176695
MC/DC100%2/2
$token->match("NodeTypeTest") or $token->match("NameTest")
$token->match("NodeTypeTest") $token->match("NameTest")
Truth table100%3/3
A or B
A= $token->match("NodeTypeTest")B= $token->match("NameTest")
ABresult
1X1
011
000
486  
487 179 my $step = XML::Parser::Lite::Tree::XPath::Token->new();
488 179 $step->{type} = 'Step';
489 179 $step->{tokens} = [];
490  
491 179 push @{$step->{tokens}}, $token;
492  
493  
494 179 ▶ while(my $token = shift @{$tokens}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @{$tokens;}) 134 95 229
495  
496 134 ▶ if ($token->match('Predicate')){
Branch: 1 branch
BranchTrueFalseTotal
if ($token->match('Predicate')) { } 50 84 134
497  
498 50 push @{$step->{tokens}}, $token;
499 }else{
500 84 unshift @{$tokens}, $token;
501 84 last;
502 }
503 }
504  
505 179 push @{$root->{tokens}}, $step;
506  
507  
508 }elsif ($token->match('Predicate')){
509  
510 0 $self->{error} = "Predicate found without preceeding NodeTest.";
511 0 return 0;
512  
513 }else{
514  
515 695 push @{$root->{tokens}}, $token;
516 }
517 }
518  
519 1334 return 1;
520 }
521  
522 1513 ▶sub build_paths {
Subroutine build_paths: called CC=7 SCAR=19.6
Pod undocumented
523 1513 my ($self, $root) = @_;
524  
525 1513 my $tokens = $root->{tokens};
526 1513 $root->{tokens} = [];
527  
528 1513 ▶ while(my $token = shift @{$tokens}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @{$tokens;}) 926 1513 2439
529  
530 926 ▶ if ($token->match('Step')){
Branch: 2 branches
BranchTrueFalseTotal
if ($token->match('Step')) { } 25 901 926
elsif ($token->match('Operator', '/')) { } 131 770 901
531  
532 25 my $path = XML::Parser::Lite::Tree::XPath::Token->new();
533 25 $path->{type} = 'LocationPath';
534 25 $path->{absolute} = 0;
535 25 $path->{tokens} = [$token];
536  
537 25 ▶ return 0 unless $self->slurp_path($path, $tokens);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->slurp_path($path, $tokens) 0 25 25
538  
539 25 push @{$root->{tokens}}, $path;
540  
541 }elsif ($token->match('Operator', '/')){
542  
543 131 unshift @{$tokens}, $token;
544  
545 131 my $path = XML::Parser::Lite::Tree::XPath::Token->new();
546 131 $path->{type} = 'LocationPath';
547 131 $path->{absolute} = 1;
548 131 $path->{tokens} = [];
549  
550 131 ▶ return 0 unless $self->slurp_path($path, $tokens);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->slurp_path($path, $tokens) 0 131 131
551  
552 131 ▶ unless (scalar @{$path->{tokens}}){
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$path->{"tokens"};}) 0 131 131
553 0 $self->{error} = "Slash found at end of path.";
554 0 return 0;
555 }
556  
557 131 push @{$root->{tokens}}, $path;
558  
559 }else{
560  
561 770 push @{$root->{tokens}}, $token;
562 }
563 }
564  
565 1513 return 1;
566 }
567  
568 156 ▶sub slurp_path {
Subroutine slurp_path: called CC=5 SCAR=16.6
Pod undocumented
569 156 my ($self, $path, $tokens) = @_;
570  
571 156 while(1){
572  
573 427 my $t1 = shift @{$tokens};
574  
575 427 ▶ if (defined $t1){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t1) { } 288 139 427
576 288 ▶ if ($t1->match('Operator', '/')){
Branch: 1 branch
BranchTrueFalseTotal
if ($t1->match('Operator', '/')) { } 271 17 288
577  
578 271 my $t2 = shift @{$tokens};
579  
580 271 ▶ if (defined $t2){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t2) { } 271 0 271
581 271 ▶ if ($t2->match('Step')){
Branch: 1 branch
BranchTrueFalseTotal
if ($t2->match('Step')) { } 271 0 271
582  
583 271 push @{$path->{tokens}}, $t2;
584 }else{
585 0 $self->{error} = "Non Step token ($t2->{type}) found after slash.";
586 0 return 0;
587 }
588 }else{
589 0 $self->{error} = "Slash found at end of path.";
590 0 return 0;
591 }
592 }else{
593 17 unshift @{$tokens}, $t1;
594 17 return 1;
595 }
596 }else{
597 139 return 1;
598 }
599 }
600 }
601  
602 9786 ▶sub do_binops {
Subroutine do_binops: called CC=6 SCAR=18.2
Pod undocumented
603 9786 my ($self, $root) = @_;
604  
605 9786 my $tokens = $root->{tokens};
606 9786 $root->{tokens} = [];
607  
608 9786 ▶ while(my $token = shift @{$tokens}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @{$tokens;}) 8478 9786 18264
609  
610  
611 8478 for my $op(@{$self->{binops}}){
612
613 16895 ▶ if ($token->match('Operator', $op)){
Branch: 1 branch
BranchTrueFalseTotal
if ($token->match("Operator", $op)) 76 16819 16895
614  
615 76 ▶ if (!scalar(@{$root->{tokens}})){
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$root->{"tokens"};}) 0 76 76
616 0 $self->{error} = "Found a binop $token->{content} with no preceeding token";
617 0 return 0;
618 }
619  
620 76 ▶ if (!scalar(@{$tokens})){
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$tokens;}) 0 76 76
621 0 $self->{error} = "Found a binop $token->{content} with no following token";
622 0 return 0;
623 }
624  
625 76 my $prev = pop @{$root->{tokens}};
626 76 my $next = shift @{$tokens};
627  
628 76 push @{$token->{tokens}}, $prev;
629 76 push @{$token->{tokens}}, $next;
630 76 $token->{type} = $self->{binop_production};
631  
632 76 last;
633 }
634 }
635  
636 8478 push @{$root->{tokens}}, $token;
637 }
638  
639 9786 return 1;
640 }
641  
642 1398 ▶sub add_links {
Subroutine add_links: called CC=3 SCAR=11.0
Pod undocumented
643 1398 my ($self, $root) = @_;
644  
645 1398 my $prev = undef;
646  
647 1398 for my $token(@{$root->{tokens}}){
648  
649 1230 $token->{prev} = $prev;
650 1230 ▶ $prev->{next} = $token if defined $prev;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev 365 865 1230
651  
652 1230 $prev = $token;
653 }
654 }
655  
656 179 ▶sub del_links {
Subroutine del_links: called CC=2 SCAR=6.9
Pod undocumented
657 179 my ($self, $root) = @_;
658  
659 179 for my $token(@{$root->{tokens}}){
660  
661 683 delete $token->{parent};
662 683 delete $token->{prev};
663 683 delete $token->{next};
664 }
665 }
666  
667 1398 ▶sub unary_minus {
Subroutine unary_minus: called CC=6 SCAR=17.9
Pod undocumented
668 1398 my ($self, $root) = @_;
669  
670 1398 $self->add_links($root);
671  
672 1398 my $tokens = $root->{tokens};
673 1398 $root->{tokens} = [];
674  
675 1398 ▶ while(my $token = shift @{$tokens}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @{$tokens;}) 1222 1398 2620
676  
677 1222 ▶ if ($token->match('Operator', '-')){
Branch: 1 branch
BranchTrueFalseTotal
if ($token->match("Operator", "-")) 13 1209 1222
678  
679 13 ▶ if (defined($token->{next}) && defined($token->{prev}) && $token->{prev}->is_expression){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token->{'next'} and defined $token->{'prev'} and $token->{'prev'}->is_expression) { } 5 8 13
Condition83%5/6
defined $token->{"next"} and defined $token->{"prev"} and $token->{"prev"}->is_expression
!ll&&!rl&&r
535
defined $token->{"next"} and defined $token->{"prev"}
!ll&&!rl&&r
058
MC/DC66%2/3
defined $token->{"next"} and defined $token->{"prev"} and $token->{"prev"}->is_expression
defined $token->{"next"} defined $token->{"prev"} $token->{"prev"}->is_expression
Truth table75%3/4
A and B and C
A= defined $token->{"next"}B= defined $token->{"prev"}C= $token->{"prev"}->is_expression
ABCresult
0XX0
10X0
1100
1111
680  
681 # not unary
682 }else{
683 # unary minus
684  
685 8 $token->{type} = 'UnaryExpr';
686 8 push @{$token->{tokens}}, shift @{$tokens};
687 }
688 }
689  
690 1222 push @{$root->{tokens}}, $token;
691 }
692  
693 1398 return 1;
694 }
695  
696 30 1;
« XPath/Tokener.pm ↑ Summary XPath/Result.pm »