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

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