blib/lib/XML/Grammar/FictionBase/FromProto/Parser/XmlIterator.pm

filter: Statementstmt 94.7% 326 / 344 Branchbran 82.9% 78 / 94 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 95.1% 78 / 82 Podpod 100% 1 / 1 totaltotal 91.5% 495 / 541 CC 53 SCAR 8.5
CC
53
Cov
92%
_handle_para_event
6.0
_push_tag
5.5
_assert_not_eof
4.2
CRAP 2.3
« blib/lib/XML/Grammar/FictionBase/Event.pm ↑ Summary blib/lib/XML/Grammar/Fiction/App/FromProto.pm »
1 package XML::Grammar::FictionBase::FromProto::Parser::XmlIterator;
2 16 $XML::Grammar::FictionBase::FromProto::Parser::XmlIterator::VERSION = '0.22.4';
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 16 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶use MooX 'late';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 16 ▶use XML::Grammar::Fiction::Err ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use XML::Grammar::Fiction::Struct::Tag ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use XML::Grammar::FictionBase::Event ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 16 ▶use XML::Grammar::Fiction::FromProto::Node::WithContent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use XML::Grammar::Fiction::FromProto::Node::Element ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use XML::Grammar::Fiction::FromProto::Node::List ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use XML::Grammar::Fiction::FromProto::Node::Text ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 ▶use XML::Grammar::Fiction::FromProto::Node::Saying ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶use XML::Grammar::Fiction::FromProto::Node::Description ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶use XML::Grammar::Fiction::FromProto::Node::Paragraph ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 ▶use XML::Grammar::Fiction::FromProto::Node::InnerDesc ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 16 ▶use XML::Grammar::Fiction::FromProto::Node::Comment ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 16 extends("XML::Grammar::FictionBase::FromProto::Parser::LineIterator");
27  
28 has "_tags_stack" => (
29 isa => "ArrayRef",
30 is => "rw",
31 20 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 16 );
33  
34  
35 sub _get_tag
36 {
37 2388 ▶ my ( $self, $idx ) = @_;
Subroutine _get_tag: called CC=1 SCAR=0.0
38  
39 2388 return $self->_tags_stack->[$idx];
40 }
41  
42 sub _tags_stack_is_empty
43 {
44 1628 ▶ my $self = shift;
Subroutine _tags_stack_is_empty: called CC=1 SCAR=0.0
45  
46 1628 return ( !@{ $self->_tags_stack } );
47 }
48  
49 sub _grep_tags_stack
50 {
51 1626 ▶ my $self = shift;
Subroutine _grep_tags_stack: called CC=1 SCAR=0.0
52 1626 my $cb = shift;
53  
54 1626 return grep { $cb->($_) } @{ $self->_tags_stack };
55 }
56  
57 sub _push_tag
58 {
59 871 ▶ my $self = shift;
Subroutine _push_tag: called CC=5 SCAR=17.0
60  
61 871 my $tag = shift;
62  
63 871 ▶ die "extra args" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 871 871
64  
65 871 ▶ if ( @{ $self->_tags_stack } )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_tags_stack;}) 820 51 871
66 {
67 820 ▶ if ( $tag->name eq 'para' and $self->_top_is_para() )
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->name eq "para" and $self->_top_is_para) 0 820 820
Condition33%1/3
$tag->name eq "para" and $self->_top_is_para
!ll&&!rl&&r
82000
MC/DC0%0/2
$tag->name eq "para" and $self->_top_is_para
$tag->name eq "para" $self->_top_is_para
Truth table33%1/3
A and B
A= $tag->name eq "para"B= $self->_top_is_para
ABresult
0X0
100
111
68 {
69 0 die "extra paragraph";
70 }
71 }
72 871 push( @{ $self->_tags_stack }, $tag );
73  
74 871 return;
75 }
76  
77 sub _pop_tag
78 {
79 856 ▶ my $self = shift;
Subroutine _pop_tag: called CC=1 SCAR=0.0
80  
81 856 return pop( @{ $self->_tags_stack } );
82 }
83  
84 has "_events_queue" => (
85 isa => "ArrayRef[XML::Grammar::FictionBase::Event]",
86  
87 # isa => "ArrayRef",
88 is => "rw",
89 20 ▶ default => sub { []; },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 16 );
91  
92  
93 sub _clear_events
94 {
95 872 ▶ my $self = shift;
Subroutine _clear_events: called CC=1 SCAR=0.0
96  
97 872 $self->_events_queue( [] );
98  
99 872 return;
100 }
101  
102 sub _no_events
103 {
104 1 ▶ my $self = shift;
Subroutine _no_events: called CC=1 SCAR=0.0
105  
106 1 return ( !@{ $self->_events_queue } );
107 }
108  
109 sub _enqueue_event
110 {
111 1754 ▶ my $self = shift;
Subroutine _enqueue_event: called CC=2 SCAR=7.1
112 1754 my $event = shift;
113  
114 1754 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1754 1754
115 {
116 0 Carp::confess("More than one argument.");
117 }
118  
119 1754 push( @{ $self->_events_queue }, $event );
120  
121 1754 return;
122 }
123  
124 sub _extract_event
125 {
126 3261 ▶ my $self = shift;
Subroutine _extract_event: called CC=1 SCAR=0.0
127  
128 3261 return shift( @{ $self->_events_queue } );
129 }
130  
131 16 has '_ret_tag' => (
132 is => "rw",
133  
134 # TODO : add isa.
135 predicate => "_has_ret_tag",
136 clearer => "_clear_ret_tag",
137 );
138  
139 # Whether we are inside a paragraph or not.
140 16 has "_in_para" => ( isa => "Bool", is => "rw", default => 0, );
141  
142 16 has '_tag_names_to_be_handled' => (
143 is => 'ro',
144 isa => 'HashRef[Bool]',
145 lazy => 1,
146 builder => '_build_tag_names_to_be_handled',
147 );
148  
149 sub _build_tag_names_to_be_handled
150 {
151 9 ▶ my $self = shift;
Subroutine _build_tag_names_to_be_handled: called CC=1 SCAR=0.0
152  
153 9 return { map { $_ => 1 } @{ $self->_list_valid_tag_events } };
154 }
155  
156 sub _get_id_regex
157 {
158 1036 ▶ return qr{[a-zA-Z_\-]+};
Subroutine _get_id_regex: called CC=1 SCAR=0.0
159 }
160  
161 sub _top_tag
162 {
163 2388 ▶ my $self = shift;
Subroutine _top_tag: called CC=1 SCAR=0.0
164 2388 return $self->_get_tag(-1);
165 }
166  
167 sub _add_to_top_tag
168 {
169 1458 ▶ my ( $self, $child ) = @_;
Subroutine _add_to_top_tag: called CC=1 SCAR=0.0
170  
171 1458 $self->_top_tag->append_child($child);
172  
173 1458 return;
174 }
175  
176 # TODO : Maybe move to a different sub-class or role.
177 sub _new_empty_list
178 {
179 0 ▶ my $self = shift;
Subroutine _new_empty_list: not called CC=1 SCAR=6.9
180 0 return $self->_new_list( [] );
181 }
182  
183 sub _new_node
184 {
185 3998 ▶ my $self = shift;
Subroutine _new_node: called CC=1 SCAR=0.0
186 3998 my $args = shift;
187  
188 # t == type
189 my $class =
190 3998 "XML::Grammar::Fiction::FromProto::Node::" . delete( $args->{'t'} );
191  
192 3998 return $class->new(%$args);
193 }
194  
195 sub _create_elem
196 {
197 401 ▶ my $self = shift;
Subroutine _create_elem: called CC=4 SCAR=13.9
198 401 my $open = shift;
199  
200 401 ▶ my $children = @_ ? shift(@_) : $self->_new_empty_list();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 401 0 401
201  
202 401 ▶ return $self->_new_node(
Branch: 2 branches
BranchTrueFalseTotal
$open->name eq 'desc' ? : 58 343 401
$open->name eq 'innerdesc' ? : 4 339 343
203 {
204 t => (
205 $open->name() eq "desc" ? "Description"
206 : $open->name() eq "innerdesc" ? "InnerDesc"
207 : "Element"
208 ),
209 name => $open->name(),
210 children => $children,
211 attrs => $open->attrs(),
212 open_line => $open->line(),
213 }
214 );
215 }
216  
217 sub _new_list
218 {
219 1988 ▶ my $self = shift;
Subroutine _new_list: called CC=1 SCAR=0.0
220 1988 my $contents = shift;
221  
222 1988 return $self->_new_node(
223 {
224 t => "List",
225 contents => $contents,
226 }
227 );
228 }
229  
230 sub _generic_para_contents_assert
231 {
232 782 ▶ my ( $self, $predicate, $message, $contents ) = @_;
Subroutine _generic_para_contents_assert: called CC=3 SCAR=11.8
233  
234 782 ▶ if ( List::MoreUtils::any { $predicate->($_) } @{ $contents || [] } )
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 2 branches
BranchTrueFalseTotal
if (&List::MoreUtils::any(sub { &$predicate($_); } , @{$contents or [];})) 0 782 782
$contents or [] 0 782 782
235 {
236 0 Carp::confess($message);
237 }
238  
239 782 return;
240 }
241  
242 sub _assert_not_contains_saying
243 {
244 391 ▶ my ( $self, $contents ) = @_;
Subroutine _assert_not_contains_saying: called CC=1 SCAR=0.1
245  
246 return $self->_generic_para_contents_assert(
247 sub {
248 539 ▶ ref($_) ne ""
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ne "" and $_->isa("XML::Grammar::Fiction::FromProto::Node::Saying") 539 0 539
249 && $_->isa("XML::Grammar::Fiction::FromProto::Node::Saying");
250 },
251 391 qq{Para contains a saying.},
252 $contents
253 );
254 }
255  
256 sub _assert_not_contains_undef
257 {
258 391 ▶ my ( $self, $contents ) = @_;
Subroutine _assert_not_contains_undef: called CC=1 SCAR=0.0
259  
260 539 ▶ return $self->_generic_para_contents_assert( sub { !defined($_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
261 391 qq{Para contains an undef member.}, $contents );
262 }
263  
264 sub _new_para
265 {
266 391 ▶ my ( $self, $contents ) = @_;
Subroutine _new_para: called CC=1 SCAR=0.0
267  
268 391 $self->_assert_not_contains_saying($contents);
269 391 $self->_assert_not_contains_undef($contents);
270  
271 391 return $self->_new_node(
272 {
273 t => "Paragraph",
274 children => $self->_new_list($contents),
275 }
276 );
277 }
278  
279 sub _new_text
280 {
281 959 ▶ my $self = shift;
Subroutine _new_text: called CC=1 SCAR=0.0
282 959 my $contents = shift;
283  
284 959 return $self->_new_node(
285 {
286 t => "Text",
287 children => $self->_new_list($contents),
288 }
289 );
290 }
291  
292 sub _new_comment
293 {
294 22 ▶ my $self = shift;
Subroutine _new_comment: called CC=1 SCAR=0.0
295 22 my $text = shift;
296  
297 22 return $self->_new_node(
298 {
299 t => "Comment",
300 text => $text,
301 }
302 );
303 }
304  
305 sub _parse_opening_tag_attrs
306 {
307 356 ▶ my $self = shift;
Subroutine _parse_opening_tag_attrs: called CC=2 SCAR=6.9
308  
309 356 my $l = $self->curr_line_ref();
310  
311 356 my @attrs;
312  
313 356 my $id_regex = $self->_get_id_regex();
314  
315 356 ▶ while ( $$l =~ m{\G\s*($id_regex)="([^"]+)"\s*}cg )
Branch: 1 branch
BranchTrueFalseTotal
if ($$l =~ /\G\s*($id_regex)="([^"]+)"\s*/cg) 241 356 597
316 {
317 241 my ( $k, $v ) = ( $1, $2 );
318 241 push @attrs,
319 +{ 'key' => $k, 'value' => HTML::Entities::decode_entities($v), };
320 }
321  
322 356 return \@attrs;
323 }
324  
325 sub _opening_tag_asserts
326 {
327 356 ▶ my $self = shift;
Subroutine _opening_tag_asserts: called CC=3 SCAR=12.7
328  
329 356 ▶ if ( $self->eof )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->eof) 0 356 356
330 {
331 0 Carp::confess(qq{Reached EOF in _parse_opening_tag.});
332 }
333  
334 356 ▶ if ( !defined( $self->curr_pos() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->curr_pos) 0 356 356
335 {
336 0 Carp::confess(qq{curr_pos is not defined in _parse_opening_tag.});
337 }
338  
339 356 return;
340 }
341  
342 sub _parse_opening_tag
343 {
344 356 ▶ my $self = shift;
Subroutine _parse_opening_tag: called CC=4 SCAR=13.9
345  
346 356 $self->_opening_tag_asserts;
347  
348 356 my $l = $self->curr_line_ref();
349  
350 356 my $id_regex = $self->_get_id_regex();
351  
352 356 ▶ if ( $$l !~ m{\G<($id_regex)}cg )
Branch: 1 branch
BranchTrueFalseTotal
unless ($$l =~ /\G<($id_regex)/cg) 0 356 356
353 {
354 0 $self->throw_text_error(
355 'XML::Grammar::Fiction::Err::Parse::CannotMatchOpeningTag',
356 "Cannot match opening tag.",
357 );
358 }
359  
360 356 my $id = $1;
361  
362 356 my $attrs = $self->_parse_opening_tag_attrs();
363  
364 356 my $is_standalone = 0;
365 356 ▶ if ( $$l =~ m{\G\s*/\s*>}cg )
Branch: 2 branches
BranchTrueFalseTotal
if ($$l =~ m[\G\s*/\s*>]cg) { } 18 338 356
elsif (not $$l =~ /\G>/g) { } 1 337 338
366 {
367 18 $is_standalone = 1;
368 }
369 elsif ( $$l !~ m{\G>}g )
370 {
371 1 $self->throw_text_error(
372 'XML::Grammar::Fiction::Err::Parse::NoRightAngleBracket',
373 "Cannot match the \">\" of the opening tag",
374 );
375 }
376  
377 355 return XML::Grammar::Fiction::Struct::Tag->new(
378 name => $id,
379 is_standalone => $is_standalone,
380 line => $self->line_num(),
381 attrs => $attrs,
382 );
383 }
384  
385 sub _parse_closing_tag
386 {
387 324 ▶ my $self = shift;
Subroutine _parse_closing_tag: called CC=2 SCAR=6.9
388  
389 324 my $l = $self->curr_line_ref();
390  
391 324 my $id_regex = $self->_get_id_regex();
392  
393 324 ▶ if ( $$l !~ m{\G</($id_regex)>}g )
Branch: 1 branch
BranchTrueFalseTotal
unless ($$l =~ m[\G</($id_regex)>]g) 1 323 324
394 {
395 1 $self->throw_text_error(
396 'XML::Grammar::Fiction::Err::Parse::WrongClosingTagSyntax',
397 "Cannot match closing tag",
398 );
399 }
400  
401 323 return XML::Grammar::Fiction::Struct::Tag->new(
402 name => $1,
403 line => $self->line_num(),
404 );
405 }
406  
407 sub _check_for_open_tag
408 {
409 1226 ▶ my $self = shift;
Subroutine _check_for_open_tag: called CC=2 SCAR=6.9
410  
411 1226 ▶ if ( $self->_tags_stack_is_empty() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_tags_stack_is_empty) 1 1225 1226
412 {
413 1 $self->throw_text_error(
414 'XML::Grammar::Fiction::Err::Parse::CannotMatchOpeningTag',
415 "Cannot match opening tag.",
416 );
417 }
418  
419 1225 return;
420 }
421  
422 sub _is_event_a_saying
423 {
424 0 ▶ my ( $self, $event ) = @_;
Subroutine _is_event_a_saying: not called CC=1 SCAR=6.9
425  
426 0 return $event->is_tag_of_name("saying");
427 }
428  
429 sub _is_event_a_para
430 {
431 0 ▶ my ( $self, $event ) = @_;
Subroutine _is_event_a_para: not called CC=1 SCAR=6.9
432  
433 0 return $event->is_tag_of_name("para");
434 }
435  
436 sub _is_event_elem
437 {
438 1103 ▶ my ( $self, $event ) = @_;
Subroutine _is_event_elem: called CC=1 SCAR=0.0
439  
440 1103 return $event->type() eq "elem";
441 }
442  
443 sub _handle_event
444 {
445 1754 ▶ my ( $self, $event ) = @_;
Subroutine _handle_event: called CC=3 SCAR=11.0
446  
447 1754 ▶ if ( ( !$self->_check_and_handle_tag_event($event) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_check_and_handle_tag_event($event) and $self->_is_event_elem($event)) 608 1146 1754
Condition100%3/3
not $self->_check_and_handle_tag_event($event) and $self->_is_event_elem($event)
!ll&&!rl&&r
651495608
MC/DC100%2/2
not $self->_check_and_handle_tag_event($event) and $self->_is_event_elem($event)
not $self->_check_and_handle_tag_event($event) $self->_is_event_elem($event)
Truth table100%3/3
not(A) and B
A= not $self->_check_and_handle_tag_event($event)B= $self->_is_event_elem($event)
ABresult
0X0
100
111
448 && $self->_is_event_elem($event) )
449 {
450 608 $self->_handle_elem_event($event);
451 }
452  
453 1754 return;
454 }
455  
456 sub _handle_specific_tag_event
457 {
458 651 ▶ my ( $self, $event ) = @_;
Subroutine _handle_specific_tag_event: called CC=2 SCAR=6.9
459  
460 651 my $tag_name = $event->tag();
461 651 ▶ my $type = $event->is_open() ? "open" : "close";
Branch: 1 branch
BranchTrueFalseTotal
$event->is_open ? : 460 191 651
462  
463 651 my $method = "_handle_${type}_${tag_name}";
464  
465 651 $self->$method($event);
466  
467 651 return 1;
468 }
469  
470 sub _check_and_handle_tag_event
471 {
472 1754 ▶ my ( $self, $event ) = @_;
Subroutine _check_and_handle_tag_event: called CC=3 SCAR=11.0
473  
474 1754 ▶ if ( $event->tag
Branch: 1 branch
BranchTrueFalseTotal
if ($event->tag and exists $self->_tag_names_to_be_handled->{$event->tag}) { } 651 1103 1754
Condition66%2/3
$event->tag and exists $self->_tag_names_to_be_handled->{$event->tag}
!ll&&!rl&&r
11030651
MC/DC50%1/2
$event->tag and exists $self->_tag_names_to_be_handled->{$event->tag}
$event->tag exists $self->_tag_names_to_be_handled->{$event->tag}
Truth table66%2/3
A and B
A= $event->tagB= exists $self->_tag_names_to_be_handled->{$event->tag}
ABresult
0X0
100
111
475 && exists( $self->_tag_names_to_be_handled->{ $event->tag } ) )
476 {
477 651 return $self->_handle_specific_tag_event($event);
478 }
479 else
480 {
481 1103 return;
482 }
483 }
484  
485 sub _handle_para_event
486 {
487 0 ▶ my ( $self, $event ) = @_;
Subroutine _handle_para_event: not called CC=2 SCAR=17.9
488  
489 0 ▶ return $event->is_open()
Branch: 1 branch
BranchTrueFalseTotal
$event->is_open ? : 0 0 0
490 ? $self->_handle_open_para($event)
491 : $self->_handle_close_para($event);
492 }
493  
494 sub _handle_elem_event
495 {
496 608 ▶ my ( $self, $event ) = @_;
Subroutine _handle_elem_event: called CC=1 SCAR=0.0
497  
498 608 $self->_add_to_top_tag( $event->elem() );
499  
500 608 return;
501 }
502  
503 sub _handle_non_tag_text
504 {
505 1226 ▶ my $self = shift;
Subroutine _handle_non_tag_text: called CC=2 SCAR=6.9
506  
507 1226 $self->_check_for_open_tag();
508  
509 1225 my $contents = $self->_parse_text();
510  
511 1222 foreach my $event (@$contents)
512 {
513 1754 $self->_handle_event($event);
514 }
515  
516 1222 return;
517 }
518  
519 sub _look_for_and_handle_tag
520 {
521 2028 ▶ my $self = shift;
Subroutine _look_for_and_handle_tag: called CC=3 SCAR=11.0
522  
523 2028 my ( $is_tag_cond, $is_close ) = $self->_look_ahead_for_tag();
524  
525 # Check if it's a closing tag.
526 2028 ▶ if ($is_close)
Branch: 2 branches
BranchTrueFalseTotal
if ($is_close) { } 386 1642 2028
elsif ($is_tag_cond) { } 416 1226 1642
527 {
528 386 return $self->_handle_close_tag();
529 }
530 elsif ($is_tag_cond)
531 {
532 416 $self->_handle_open_tag();
533 }
534 else
535 {
536 1226 $self->_handle_non_tag_text();
537 }
538 1637 return;
539 }
540  
541 sub _merge_tag
542 {
543 401 ▶ my $self = shift;
Subroutine _merge_tag: called CC=2 SCAR=6.9
544 401 my $open_tag = shift;
545  
546 401 my $new_elem =
547 $self->_create_elem( $open_tag,
548 $self->_new_list( $open_tag->detach_children() ),
549 );
550  
551 401 ▶ if ( !$self->_tags_stack_is_empty() )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_tags_stack_is_empty) { } 357 44 401
552 {
553 357 $self->_add_to_top_tag($new_elem);
554 357 return;
555 }
556 else
557 {
558 44 return $new_elem;
559 }
560 }
561  
562 sub _handle_close_tag
563 {
564 386 ▶ my $self = shift;
Subroutine _handle_close_tag: called CC=2 SCAR=6.9
565  
566 386 my $close = $self->_parse_closing_tag();
567  
568 385 my $open = $self->_pop_tag();
569  
570 385 ▶ if ( $open->name() ne $close->name() )
Branch: 1 branch
BranchTrueFalseTotal
if ($open->name ne $close->name) 2 383 385
571 {
572 2 XML::Grammar::Fiction::Err::Parse::TagsMismatch->throw(
573 error => "Tags do not match",
574 opening_tag => $open,
575 closing_tag => $close,
576 );
577 }
578  
579 383 return $self->_merge_tag($open);
580 }
581  
582 sub _look_ahead_for_comment
583 {
584 2413 ▶ my $self = shift;
Subroutine _look_ahead_for_comment: called CC=2 SCAR=6.9
585  
586 2413 ▶ if ( $self->curr_line_continues_with(qr{<!--}) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->curr_line_continues_with(qr/<!--/)) { } 22 2391 2413
587 {
588 22 my $text = $self->consume_up_to(qr{-->});
589  
590 22 $self->_add_to_top_tag( $self->_new_comment($text), );
591  
592 22 return 1;
593 }
594 else
595 {
596 2391 return;
597 }
598 }
599  
600 sub _decode_entities_in_text
601 {
602 959 ▶ my ( $self, $orig_text ) = @_;
Subroutine _decode_entities_in_text: called CC=4 SCAR=13.9
603  
604 959 my $ret = '';
605  
606 # Incrementally parse $text for entities.
607 959 pos($orig_text) = 0;
608  
609 959 ▶ while ( $orig_text =~ m{\G(.*?)(\&|\z)}msg )
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_text =~ /\G(.*?)(\&|\z)/gms) 1799 959 2758
610 {
611 1799 my ( $before, $indicator ) = ( $1, $2 );
612  
613 1799 $ret .= $before;
614  
615 1799 ▶ if ( $indicator eq '&' )
Branch: 1 branch
BranchTrueFalseTotal
if ($indicator eq "&") 45 1754 1799
616 {
617 45 ▶ if ( $orig_text =~ m{\G(\#?\w+;)}cg )
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_text =~ /\G(\#?\w+;)/cg) { } 45 0 45
618 {
619 45 $ret .= HTML::Entities::decode_entities("&$1");
620 }
621 else
622 {
623 0 Carp::confess(
624 sprintf(
625 "Cannot match entity '%s' at line %d",
626 substr( $orig_text, pos($orig_text) - 1, 10 ),
627 $self->line_num(),
628 )
629 );
630 }
631 }
632 }
633  
634 959 return $ret;
635 }
636  
637 sub _parse_non_tag_text_unit
638 {
639 960 ▶ my $self = shift;
Subroutine _parse_non_tag_text_unit: called CC=4 SCAR=13.9
640  
641 960 my $orig_text =
642 $self->consume_up_to( $self->_non_tag_text_unit_consume_regex );
643  
644 959 my $text = $self->_decode_entities_in_text($orig_text);
645  
646 959 my $l = $self->curr_line_ref();
647  
648 959 my $ret_elem = $self->_new_text( [$text] );
649 959 my $is_para_end = 0;
650  
651 # Demote the cursor to before the < of the tag.
652 #
653 959 ▶ if ( $self->at_line_start )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->at_line_start) { } 0 959 959
654 {
655 0 $is_para_end = 1;
656 }
657 else
658 {
659 959 pos($$l)--;
660 959 ▶ if ( substr( $$l, pos($$l), 1 ) eq "\n" )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$l, pos $$l, 1) eq "\n") 638 321 959
661 {
662 638 $is_para_end = 1;
663 }
664 }
665  
666 959 ▶ if ( $text !~ /\S/ )
Branch: 1 branch
BranchTrueFalseTotal
if (not $text =~ /\S/) { } 350 609 959
667 {
668 350 return;
669 }
670 else
671 {
672 return {
673 609 elem => $ret_elem,
674 para_end => $is_para_end,
675 };
676 }
677 }
678  
679 sub _parse_text_unit
680 {
681 1650 ▶ my $self = shift;
Subroutine _parse_text_unit: called CC=2 SCAR=6.9
682  
683 1650 ▶ if ( defined( my $event = $self->_extract_event() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $event = $self->_extract_event)) { } 36 1614 1650
684 {
685 36 return $event;
686 }
687 else
688 {
689 1614 $self->_generate_text_unit_events();
690 1611 return $self->_extract_event();
691 }
692 }
693  
694 sub _flush_events
695 {
696 872 ▶ my $self = shift;
Subroutine _flush_events: called CC=1 SCAR=0.0
697  
698 872 my @ret = @{ $self->_events_queue() };
699  
700 872 $self->_clear_events;
701  
702 872 return \@ret;
703 }
704  
705 sub _parse_text
706 {
707 1225 ▶ my $self = shift;
Subroutine _parse_text: called CC=3 SCAR=11.0
708  
709 1225 my @ret;
710  
711 1225 ▶ while ( my $unit = $self->_parse_text_unit() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $unit = $self->_parse_text_unit) 1297 350 1647
712 {
713 1297 push @ret, $unit;
714  
715 1297 ▶ if ( $unit->is_open_or_close )
Branch: 1 branch
BranchTrueFalseTotal
if ($unit->is_open_or_close) 872 425 1297
716 {
717 872 return [ @ret, @{ $self->_flush_events() } ];
718 }
719 }
720  
721 350 return \@ret;
722 }
723  
724 sub _look_for_tag_opener
725 {
726 626 ▶ my $self = shift;
Subroutine _look_for_tag_opener: called CC=2 SCAR=6.9
727  
728 626 my $l = $self->curr_line_ref();
729  
730 626 ▶ if ( $$l =~ m{\G(<(?:/)?)}cg )
Branch: 1 branch
BranchTrueFalseTotal
if ($$l =~ m[\G(<(?:/)?)]cg) { } 369 257 626
731 {
732 369 return $1;
733 }
734 else
735 {
736 257 return;
737 }
738 }
739  
740 sub _is_closing_tag
741 {
742 369 ▶ my $self = shift;
Subroutine _is_closing_tag: called CC=1 SCAR=0.0
743 369 my $tag_start = shift;
744  
745 369 return $tag_start =~ m{/};
746 }
747  
748 sub _generate_tag_event
749 {
750 1613 ▶ my $self = shift;
Subroutine _generate_tag_event: called CC=3 SCAR=11.0
751  
752 1613 my $l = $self->curr_line_ref();
753 1613 my $orig_pos = pos($$l);
754  
755 1613 ▶ if ( defined( my $tag_start = $self->_look_for_tag_opener() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tag_start = $self->_look_for_tag_opener)) { } 495 1118 1613
756 {
757 # If it's a tag.
758  
759 # TODO : implement the comment handling.
760 # We have a tag.
761  
762 495 pos($$l) = $orig_pos;
763  
764 495 ▶ $self->_enqueue_event(
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_closing_tag($tag_start) ? : 276 219 495
765 XML::Grammar::FictionBase::Event->new(
766 {
767 'type' => (
768 $self->_is_closing_tag($tag_start) ? "close" : "open"
769 )
770 }
771 ),
772 );
773  
774 495 return 1;
775 }
776 else
777 {
778 1118 return;
779 }
780 }
781  
782 sub _handle_open_tag
783 {
784 416 ▶ my $self = shift;
Subroutine _handle_open_tag: called CC=3 SCAR=11.1
785  
786 416 my $open = $self->_parse_opening_tag();
787  
788 415 $open->children( [] );
789  
790 # TODO : add the check for is_standalone in XML-Grammar-Fiction
791 # too.
792 415 ▶ if ( $open->is_standalone() )
Branch: 1 branch
BranchTrueFalseTotal
if ($open->is_standalone) { } 18 397 415
793 {
794 18 ▶ if ( defined( $self->_merge_tag($open) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_merge_tag($open)) { } 0 18 18
795 {
796 0 Carp::confess("Top element/tag cannot be standalone.");
797 }
798 else
799 {
800 18 return;
801 }
802 }
803 else
804 {
805 my $cb = sub {
806 397 ▶ $self->_push_tag($open);
Subroutine __ANON__: called CC=1 SCAR=0.0
807 397 };
808  
809 397 return $cb->();
810 }
811 }
812  
813 sub _generate_text_unit_events
814 {
815 1613 ▶ my $self = shift;
Subroutine _generate_text_unit_events: called CC=2 SCAR=6.9
816  
817 # $self->skip_multiline_space();
818  
819 1613 ▶ if ( !$self->_generate_tag_event() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_generate_tag_event) 1118 495 1613
820 {
821 1118 $self->_generate_non_tag_text_event();
822 }
823  
824 1611 return;
825 }
826  
827 sub _flush_ret_tag
828 {
829 44 ▶ my $self = shift;
Subroutine _flush_ret_tag: called CC=1 SCAR=0.0
830  
831 44 my $ret = $self->_ret_tag();
832  
833 44 $self->_clear_ret_tag();
834  
835 44 return $ret;
836 }
837  
838 sub _main_loop
839 {
840 53 ▶ my $self = shift;
Subroutine _main_loop: called CC=2 SCAR=6.9
841  
842 53 ▶ while ( !defined( $self->_ret_tag() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_ret_tag) 2414 44 2458
843 {
844 2414 $self->_main_loop_iter();
845 }
846  
847 44 return;
848 }
849  
850 sub _parse_all
851 {
852 53 ▶ my $self = shift;
Subroutine _parse_all: called CC=1 SCAR=0.0
853  
854 53 $self->_main_loop();
855  
856 44 return $self->_flush_ret_tag();
857 }
858  
859 sub _assert_not_eof
860 {
861 2414 ▶ my $self = shift;
Subroutine _assert_not_eof: called CC=4 SCAR=14.3
862  
863 2414 ▶ if ( $self->eof() && $self->_no_events() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->eof and $self->_no_events) 1 2413 2414
Condition66%2/3
$self->eof and $self->_no_events
!ll&&!rl&&r
241301
MC/DC50%1/2
$self->eof and $self->_no_events
$self->eof $self->_no_events
Truth table66%2/3
A and B
A= $self->eofB= $self->_no_events
ABresult
0X0
100
111
864 {
865 1 ▶ if ( !$self->_tags_stack_is_empty() )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_tags_stack_is_empty) { } 1 0 1
866 {
867 1 XML::Grammar::Fiction::Err::Parse::TagNotClosedAtEOF->throw(
868 error => "Tag not closed at EOF.",
869 opening_tag => $self->_top_tag(),
870 );
871 }
872 else
873 {
874 0 Carp::confess(qq{Reached EOF.});
875 }
876 }
877  
878 2413 return;
879 }
880  
881 sub _main_loop_iter
882 {
883 2414 ▶ my $self = shift;
Subroutine _main_loop_iter: called CC=2 SCAR=6.9
884  
885 2414 $self->_assert_not_eof;
886  
887 2413 ▶ if ( $self->_look_ahead_for_comment )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_look_ahead_for_comment) { } 22 2391 2413
888 {
889 22 return;
890 }
891 else
892 {
893 2391 return $self->_main_loop_iter_body;
894 }
895 }
896  
897 sub _attempt_to_calc_new_ret_tag
898 {
899 2028 ▶ my $self = shift;
Subroutine _attempt_to_calc_new_ret_tag: called CC=1 SCAR=0.0
900  
901 2028 $self->_ret_tag( scalar( $self->_look_for_and_handle_tag() ) );
902  
903 2020 return;
904 }
905  
906 sub _main_loop_iter_body
907 {
908 2391 ▶ my $self = shift;
Subroutine _main_loop_iter_body: called CC=2 SCAR=6.9
909  
910 2391 ▶ if ( $self->_main_loop_iter_body_prelude() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_main_loop_iter_body_prelude) 2028 363 2391
911 {
912 2028 $self->_attempt_to_calc_new_ret_tag();
913 }
914  
915 2383 return;
916 }
917  
918  
919 sub process_text
920 {
921 53 ▶ my ( $self, $text ) = @_;
Subroutine process_text: called CC=1 SCAR=0.0
Pod documented
922  
923 53 $self->setup_text($text);
924  
925 53 return $self->_parse_all();
926 }
927  
928  
929 16 1;
930  
931 __END__
932  
933 =pod
934  
935 =encoding UTF-8
936  
937 =head1 NAME
938  
939 XML::Grammar::FictionBase::FromProto::Parser::XmlIterator - line iterator base
940 class with some nested XMLisms.
941  
942 B<For internal use only>.
943  
944 =head1 VERSION
945  
946 version 0.22.4
947  
948 =head1 SYNOPSIS
949  
950 B<TODO:> write one.
951  
952 =head1 DESCRIPTION
953  
954 This is a line iterator with some features for parsing, nested,
955 XML-like grammars.
956  
957 =begin Removed # Not supported by Moo / MooX yet.
958  
959 traits => ['Array'],
960 handles =>
961 {
962 '_push_tag' => 'push',
963 '_grep_tags_stack' => 'grep',
964 '_tags_stack_is_empty' => 'is_empty',
965 '_pop_tag' => 'pop',
966 '_get_tag' => 'get',
967 },
968  
969  
970 =end Removed
971  
972 =begin Removed # Not supported by Moo / MooX yet.
973  
974 traits => ['Array'],
975 handles =>
976 {
977 _enqueue_event => 'push',
978 _extract_event => 'shift',
979 _no_events => 'is_empty',
980 _clear_events => 'clear',
981 },
982  
983  
984 =end Removed
985  
986 =head1 METHODS
987  
988 =head2 $self->process_text($string)
989  
990 Processes the text and returns the parse tree.
991  
992 =head2 $self->meta()
993  
994 Leftover from Moo.
995  
996 =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
997  
998 =head1 SUPPORT
999  
1000 =head2 Websites
1001  
1002 The following websites have more information about this module, and may be of help to you. As always,
1003 in addition to those websites please use your favorite search engine to discover more resources.
1004  
1005 =over 4
1006  
1007 =item *
1008  
1009 MetaCPAN
1010  
1011 A modern, open-source CPAN search engine, useful to view POD in HTML format.
1012  
1013 L<https://metacpan.org/release/XML-Grammar-Fiction>
1014  
1015 =item *
1016  
1017 RT: CPAN's Bug Tracker
1018  
1019 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
1020  
1021 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-Grammar-Fiction>
1022  
1023 =item *
1024  
1025 CPANTS
1026  
1027 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
1028  
1029 L<http://cpants.cpanauthors.org/dist/XML-Grammar-Fiction>
1030  
1031 =item *
1032  
1033 CPAN Testers
1034  
1035 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
1036  
1037 L<http://www.cpantesters.org/distro/X/XML-Grammar-Fiction>
1038  
1039 =item *
1040  
1041 CPAN Testers Matrix
1042  
1043 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
1044  
1045 L<http://matrix.cpantesters.org/?dist=XML-Grammar-Fiction>
1046  
1047 =item *
1048  
1049 CPAN Testers Dependencies
1050  
1051 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
1052  
1053 L<http://deps.cpantesters.org/?module=XML::Grammar::Fiction>
1054  
1055 =back
1056  
1057 =head2 Bugs / Feature Requests
1058  
1059 Please report any bugs or feature requests by email to C<bug-xml-grammar-fiction at rt.cpan.org>, or through
1060 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-Grammar-Fiction>. You will be automatically notified of any
1061 progress on the request by the system.
1062  
1063 =head2 Source Code
1064  
1065 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
1066 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
1067 from your repository :)
1068  
1069 L<https://github.com/shlomif/fiction-xml>
1070  
1071 git clone git://github.com/shlomif/fiction-xml.git
1072  
1073 =head1 AUTHOR
1074  
1075 Shlomi Fish <shlomif@cpan.org>
1076  
1077 =head1 BUGS
1078  
1079 Please report any bugs or feature requests on the bugtracker website
1080 L<https://github.com/shlomif/fiction-xml/issues>
1081  
1082 When submitting a bug or request, please include a test-file or a
1083 patch to an existing test-file that illustrates the bug or desired
1084 feature.
1085  
1086 =head1 COPYRIGHT AND LICENSE
1087  
1088 This software is Copyright (c) 2007 by Shlomi Fish.
1089  
1090 This is free software, licensed under:
1091  
1092 The MIT (X11) License
1093  
1094 =cut
« blib/lib/XML/Grammar/FictionBase/Event.pm ↑ Summary blib/lib/XML/Grammar/Fiction/App/FromProto.pm »