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

filter: Statementstmt 94.7% 324 / 342 Branchbran 83.7% 77 / 92 Conditioncond 64.2% 9 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 95.0% 77 / 81 Podpod 100% 1 / 1 totaltotal 91.2% 492 / 539 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/Screenplay/App/FromProto.pm »
1 package XML::Grammar::FictionBase::FromProto::Parser::XmlIterator;
2 21 $XML::Grammar::FictionBase::FromProto::Parser::XmlIterator::VERSION = '0.32.0';
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 ▶use MooX 'late';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 21 ▶use XML::Grammar::Fiction::Err ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use XML::Grammar::Fiction::Struct::Tag ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use XML::Grammar::FictionBase::Event ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 21 ▶use XML::Grammar::Fiction::FromProto::Node::WithContent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 21 ▶use XML::Grammar::Fiction::FromProto::Node::Element ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 21 ▶use XML::Grammar::Fiction::FromProto::Node::List ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 21 ▶use XML::Grammar::Fiction::FromProto::Node::Text ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 21 ▶use XML::Grammar::Fiction::FromProto::Node::Saying ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 21 ▶use XML::Grammar::Fiction::FromProto::Node::Description ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 21 ▶use XML::Grammar::Fiction::FromProto::Node::Paragraph ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 21 ▶use XML::Grammar::Fiction::FromProto::Node::InnerDesc ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 21 ▶use XML::Grammar::Fiction::FromProto::Node::Comment ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 21 extends("XML::Grammar::FictionBase::FromProto::Parser::LineIterator");
27  
28 has "_tags_stack" => (
29 isa => "ArrayRef",
30 is => "rw",
31 21 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 21 );
33  
34  
35 sub _get_tag
36 2943 ▶{
Subroutine _get_tag: called CC=1 SCAR=0.0
37 2943 my ( $self, $idx ) = @_;
38  
39 2943 return $self->_tags_stack->[$idx];
40 }
41  
42 sub _tags_stack_is_empty
43 1746 ▶{
Subroutine _tags_stack_is_empty: called CC=1 SCAR=0.0
44 1746 my $self = shift;
45  
46 1746 return ( !@{ $self->_tags_stack } );
47 }
48  
49 sub _grep_tags_stack
50 1842 ▶{
Subroutine _grep_tags_stack: called CC=1 SCAR=0.0
51 1842 my $self = shift;
52 1842 my $cb = shift;
53  
54 1842 return grep { $cb->($_) } @{ $self->_tags_stack };
55 }
56  
57 sub _push_tag
58 943 ▶{
Subroutine _push_tag: called CC=5 SCAR=17.0
59 943 my $self = shift;
60  
61 943 my $tag = shift;
62  
63 943 ▶ die "extra args" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 943 943
64  
65 943 ▶ if ( @{ $self->_tags_stack } )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_tags_stack;}) 889 54 943
66 {
67 889 ▶ 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 889 889
Condition33%1/3
$tag->name eq "para" and $self->_top_is_para
!ll&&!rl&&r
88900
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 943 push( @{ $self->_tags_stack }, $tag );
73  
74 943 return;
75 }
76  
77 sub _pop_tag
78 926 ▶{
Subroutine _pop_tag: called CC=1 SCAR=0.0
79 926 my $self = shift;
80  
81 926 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 21 ▶ default => sub { []; },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 21 );
91  
92  
93 sub _clear_events
94 917 ▶{
Subroutine _clear_events: called CC=1 SCAR=0.0
95 917 my $self = shift;
96  
97 917 $self->_events_queue( [] );
98  
99 917 return;
100 }
101  
102 sub _no_events
103 1 ▶{
Subroutine _no_events: called CC=1 SCAR=0.0
104 1 my $self = shift;
105  
106 1 return ( !@{ $self->_events_queue } );
107 }
108  
109 sub _enqueue_event
110 1868 ▶{
Subroutine _enqueue_event: called CC=2 SCAR=7.1
111 1868 my $self = shift;
112 1868 my $event = shift;
113  
114 1868 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1868 1868
115 {
116 0 Carp::confess("More than one argument.");
117 }
118  
119 1868 push( @{ $self->_events_queue }, $event );
120  
121 1868 return;
122 }
123  
124 sub _extract_event
125 3529 ▶{
Subroutine _extract_event: called CC=1 SCAR=0.0
126 3529 my $self = shift;
127  
128 3529 return shift( @{ $self->_events_queue } );
129 }
130  
131 21 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 21 has "_in_para" => ( isa => "Bool", is => "rw", default => 0, );
141  
142 21 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 10 ▶{
Subroutine _build_tag_names_to_be_handled: called CC=1 SCAR=0.0
151 10 my $self = shift;
152  
153 10 return { map { $_ => 1 } @{ $self->_list_valid_tag_events } };
154 }
155  
156 sub _get_id_regex
157 1094 ▶{
Subroutine _get_id_regex: called CC=1 SCAR=0.0
158 1094 return qr{[a-zA-Z_\-]+};
159 }
160  
161 sub _top_tag
162 2943 ▶{
Subroutine _top_tag: called CC=1 SCAR=0.0
163 2943 my $self = shift;
164 2943 return $self->_get_tag(-1);
165 }
166  
167 sub _add_to_top_tag
168 1576 ▶{
Subroutine _add_to_top_tag: called CC=1 SCAR=0.0
169 1576 my ( $self, $child ) = @_;
170  
171 1576 $self->_top_tag->append_child($child);
172  
173 1576 return;
174 }
175  
176 # TODO : Maybe move to a different sub-class or role.
177 sub _new_empty_list
178 0 ▶{
Subroutine _new_empty_list: not called CC=1 SCAR=6.9
179 0 my $self = shift;
180 0 return $self->_new_list( [] );
181 }
182  
183 sub _new_node
184 4366 ▶{
Subroutine _new_node: called CC=1 SCAR=0.0
185 4366 my $self = shift;
186 4366 my $args = shift;
187  
188 # t == type
189 my $class =
190 4366 "XML::Grammar::Fiction::FromProto::Node::" . delete( $args->{'t'} );
191  
192 4366 return $class->new(%$args);
193 }
194  
195 sub _create_elem
196 428 ▶{
Subroutine _create_elem: called CC=4 SCAR=13.9
197 428 my $self = shift;
198 428 my $open = shift;
199  
200 428 ▶ my $children = @_ ? shift(@_) : $self->_new_empty_list();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 428 0 428
201  
202 428 ▶ return $self->_new_node(
Branch: 2 branches
BranchTrueFalseTotal
$open->name eq 'desc' ? : 67 361 428
$open->name eq 'innerdesc' ? : 4 357 361
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 2172 ▶{
Subroutine _new_list: called CC=1 SCAR=0.0
219 2172 my $self = shift;
220 2172 my $contents = shift;
221  
222 2172 return $self->_new_node(
223 {
224 t => "List",
225 contents => $contents,
226 }
227 );
228 }
229  
230 sub _generic_para_contents_assert
231 854 ▶{
Subroutine _generic_para_contents_assert: called CC=3 SCAR=11.8
232 854 my ( $self, $predicate, $message, $contents ) = @_;
233  
234 854 ▶ if ( List::MoreUtils::any { $predicate->($_) } @{ $contents || [] } )
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&List::MoreUtils::any(sub { &$predicate($_); } , @{$contents or [];})) 0 854 854
Condition50%1/2
$contents || []
l!l
8540
MC/DC0%0/1
$contents || []
$contents
Truth table50%1/2
A
A= $contents
Aresult
11
00
235 {
236 0 Carp::confess($message);
237 }
238  
239 854 return;
240 }
241  
242 sub _assert_not_contains_saying
243 427 ▶{
Subroutine _assert_not_contains_saying: called CC=1 SCAR=0.1
244 427 my ( $self, $contents ) = @_;
245  
246 return $self->_generic_para_contents_assert(
247 586 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
248 586 ▶ ref($_) ne ""
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ne "" and $_->isa("XML::Grammar::Fiction::FromProto::Node::Saying") 586 0 586
249 && $_->isa("XML::Grammar::Fiction::FromProto::Node::Saying");
250 },
251 427 qq{Para contains a saying.},
252 $contents
253 );
254 }
255  
256 sub _assert_not_contains_undef
257 427 ▶{
Subroutine _assert_not_contains_undef: called CC=1 SCAR=0.0
258 427 my ( $self, $contents ) = @_;
259  
260 586 ▶ return $self->_generic_para_contents_assert( sub { !defined($_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
261 427 qq{Para contains an undef member.}, $contents );
262 }
263  
264 sub _new_para
265 427 ▶{
Subroutine _new_para: called CC=1 SCAR=0.0
266 427 my ( $self, $contents ) = @_;
267  
268 427 $self->_assert_not_contains_saying($contents);
269 427 $self->_assert_not_contains_undef($contents);
270  
271 427 return $self->_new_node(
272 {
273 t => "Paragraph",
274 children => $self->_new_list($contents),
275 }
276 );
277 }
278  
279 sub _new_text
280 1056 ▶{
Subroutine _new_text: called CC=1 SCAR=0.0
281 1056 my $self = shift;
282 1056 my $contents = shift;
283  
284 1056 return $self->_new_node(
285 {
286 t => "Text",
287 children => $self->_new_list($contents),
288 }
289 );
290 }
291  
292 sub _new_comment
293 22 ▶{
Subroutine _new_comment: called CC=1 SCAR=0.0
294 22 my $self = shift;
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 376 ▶{
Subroutine _parse_opening_tag_attrs: called CC=2 SCAR=6.9
307 376 my $self = shift;
308  
309 376 my $l = $self->curr_line_ref();
310  
311 376 my @attrs;
312  
313 376 my $id_regex = $self->_get_id_regex();
314  
315 376 ▶ while ( $$l =~ m{\G\s*($id_regex)="([^"]+)"\s*}cg )
Branch: 1 branch
BranchTrueFalseTotal
if ($$l =~ /\G\s*($id_regex)="([^"]+)"\s*/cg) 261 376 637
316 {
317 261 my ( $k, $v ) = ( $1, $2 );
318 261 push @attrs,
319 +{ 'key' => $k, 'value' => HTML::Entities::decode_entities($v), };
320 }
321  
322 376 return \@attrs;
323 }
324  
325 sub _opening_tag_asserts
326 376 ▶{
Subroutine _opening_tag_asserts: called CC=3 SCAR=12.7
327 376 my $self = shift;
328  
329 376 ▶ if ( $self->eof )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->eof) 0 376 376
330 {
331 0 Carp::confess(qq{Reached EOF in _parse_opening_tag.});
332 }
333  
334 376 ▶ if ( !defined( $self->curr_pos() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->curr_pos) 0 376 376
335 {
336 0 Carp::confess(qq{curr_pos is not defined in _parse_opening_tag.});
337 }
338  
339 376 return;
340 }
341  
342 sub _parse_opening_tag
343 376 ▶{
Subroutine _parse_opening_tag: called CC=4 SCAR=13.9
344 376 my $self = shift;
345  
346 376 $self->_opening_tag_asserts;
347  
348 376 my $l = $self->curr_line_ref();
349  
350 376 my $id_regex = $self->_get_id_regex();
351  
352 376 ▶ if ( $$l !~ m{\G<($id_regex)}cg )
Branch: 1 branch
BranchTrueFalseTotal
unless ($$l =~ /\G<($id_regex)/cg) 0 376 376
353 {
354 0 $self->throw_text_error(
355 'XML::Grammar::Fiction::Err::Parse::CannotMatchOpeningTag',
356 "Cannot match opening tag.",
357 );
358 }
359  
360 376 my $id = $1;
361  
362 376 my $attrs = $self->_parse_opening_tag_attrs();
363  
364 376 my $is_standalone = 0;
365 376 ▶ if ( $$l =~ m{\G\s*/\s*>}cg )
Branch: 2 branches
BranchTrueFalseTotal
if ($$l =~ m[\G\s*/\s*>]cg) { } 18 358 376
elsif (not $$l =~ /\G>/g) { } 1 357 358
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 375 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 342 ▶{
Subroutine _parse_closing_tag: called CC=2 SCAR=6.9
387 342 my $self = shift;
388  
389 342 my $l = $self->curr_line_ref();
390  
391 342 my $id_regex = $self->_get_id_regex();
392  
393 342 ▶ if ( $$l !~ m{\G</($id_regex)>}g )
Branch: 1 branch
BranchTrueFalseTotal
unless ($$l =~ m[\G</($id_regex)>]g) 1 341 342
394 {
395 1 $self->throw_text_error(
396 'XML::Grammar::Fiction::Err::Parse::WrongClosingTagSyntax',
397 "Cannot match closing tag",
398 );
399 }
400  
401 341 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 1317 ▶{
Subroutine _check_for_open_tag: called CC=2 SCAR=6.9
409 1317 my $self = shift;
410  
411 1317 ▶ if ( $self->_tags_stack_is_empty() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_tags_stack_is_empty) 1 1316 1317
412 {
413 1 $self->throw_text_error(
414 'XML::Grammar::Fiction::Err::Parse::CannotMatchOpeningTag',
415 "Cannot match opening tag.",
416 );
417 }
418  
419 1316 return;
420 }
421  
422 sub _is_event_a_saying
423 0 ▶{
Subroutine _is_event_a_saying: not called CC=1 SCAR=6.9
424 0 my ( $self, $event ) = @_;
425  
426 0 return $event->is_tag_of_name("saying");
427 }
428  
429 sub _is_event_a_para
430 0 ▶{
Subroutine _is_event_a_para: not called CC=1 SCAR=6.9
431 0 my ( $self, $event ) = @_;
432  
433 0 return $event->is_tag_of_name("para");
434 }
435  
436 sub _is_event_elem
437 1174 ▶{
Subroutine _is_event_elem: called CC=1 SCAR=0.0
438 1174 my ( $self, $event ) = @_;
439  
440 1174 return $event->type() eq "elem";
441 }
442  
443 sub _handle_event
444 1868 ▶{
Subroutine _handle_event: called CC=3 SCAR=11.0
445 1868 my ( $self, $event ) = @_;
446  
447 1868 ▶ 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)) 659 1209 1868
Condition100%3/3
not $self->_check_and_handle_tag_event($event) and $self->_is_event_elem($event)
!ll&&!rl&&r
694515659
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 659 $self->_handle_elem_event($event);
451 }
452  
453 1868 return;
454 }
455  
456 sub _handle_specific_tag_event
457 694 ▶{
Subroutine _handle_specific_tag_event: called CC=2 SCAR=6.9
458 694 my ( $self, $event ) = @_;
459  
460 694 my $tag_name = $event->tag();
461 694 ▶ my $type = $event->is_open() ? "open" : "close";
Branch: 1 branch
BranchTrueFalseTotal
$event->is_open ? : 497 197 694
462  
463 694 my $method = "_handle_${type}_${tag_name}";
464  
465 694 $self->$method($event);
466  
467 694 return 1;
468 }
469  
470 sub _check_and_handle_tag_event
471 1868 ▶{
Subroutine _check_and_handle_tag_event: called CC=3 SCAR=11.0
472 1868 my ( $self, $event ) = @_;
473  
474 1868 ▶ if ( $event->tag
Branch: 1 branch
BranchTrueFalseTotal
if ($event->tag and exists $self->_tag_names_to_be_handled->{$event->tag}) { } 694 1174 1868
Condition66%2/3
$event->tag and exists $self->_tag_names_to_be_handled->{$event->tag}
!ll&&!rl&&r
11740694
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 694 return $self->_handle_specific_tag_event($event);
478 }
479 else
480 {
481 1174 return;
482 }
483 }
484  
485 sub _handle_para_event
486 0 ▶{
Subroutine _handle_para_event: not called CC=2 SCAR=17.9
487 0 my ( $self, $event ) = @_;
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 659 ▶{
Subroutine _handle_elem_event: called CC=1 SCAR=0.0
496 659 my ( $self, $event ) = @_;
497  
498 659 $self->_add_to_top_tag( $event->elem() );
499  
500 659 return;
501 }
502  
503 sub _handle_non_tag_text
504 1317 ▶{
Subroutine _handle_non_tag_text: called CC=2 SCAR=6.9
505 1317 my $self = shift;
506  
507 1317 $self->_check_for_open_tag();
508  
509 1316 my $contents = $self->_parse_text();
510  
511 1313 foreach my $event (@$contents)
512 {
513 1868 $self->_handle_event($event);
514 }
515  
516 1313 return;
517 }
518  
519 sub _look_for_and_handle_tag
520 2175 ▶{
Subroutine _look_for_and_handle_tag: called CC=3 SCAR=11.0
521 2175 my $self = shift;
522  
523 2175 my ( $is_tag_cond, $is_close ) = $self->_look_ahead_for_tag();
524  
525 # Check if it's a closing tag.
526 2175 ▶ if ($is_close)
Branch: 2 branches
BranchTrueFalseTotal
if ($is_close) { } 413 1762 2175
elsif ($is_tag_cond) { } 445 1317 1762
527 {
528 413 return $self->_handle_close_tag();
529 }
530 elsif ($is_tag_cond)
531 {
532 445 $self->_handle_open_tag();
533 }
534 else
535 {
536 1317 $self->_handle_non_tag_text();
537 }
538 1757 return;
539 }
540  
541 sub _merge_tag
542 428 ▶{
Subroutine _merge_tag: called CC=2 SCAR=6.9
543 428 my $self = shift;
544 428 my $open_tag = shift;
545  
546 428 my $new_elem =
547 $self->_create_elem( $open_tag,
548 $self->_new_list( $open_tag->detach_children() ),
549 );
550  
551 428 ▶ if ( !$self->_tags_stack_is_empty() )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_tags_stack_is_empty) { } 382 46 428
552 {
553 382 $self->_add_to_top_tag($new_elem);
554 382 return;
555 }
556 else
557 {
558 46 return $new_elem;
559 }
560 }
561  
562 sub _handle_close_tag
563 413 ▶{
Subroutine _handle_close_tag: called CC=2 SCAR=6.9
564 413 my $self = shift;
565  
566 413 my $close = $self->_parse_closing_tag();
567  
568 412 my $open = $self->_pop_tag();
569  
570 412 ▶ if ( $open->name() ne $close->name() )
Branch: 1 branch
BranchTrueFalseTotal
if ($open->name ne $close->name) 2 410 412
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 410 return $self->_merge_tag($open);
580 }
581  
582 sub _look_ahead_for_comment
583 2611 ▶{
Subroutine _look_ahead_for_comment: called CC=2 SCAR=6.9
584 2611 my $self = shift;
585  
586 2611 ▶ if ( $self->curr_line_continues_with(qr{<!--}) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->curr_line_continues_with(qr/<!--/)) { } 22 2589 2611
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 2589 return;
597 }
598 }
599  
600 sub _decode_entities_in_text
601 1056 ▶{
Subroutine _decode_entities_in_text: called CC=4 SCAR=13.9
602 1056 my ( $self, $orig_text ) = @_;
603  
604 1056 my $ret = '';
605  
606 # Incrementally parse $text for entities.
607 1056 pos($orig_text) = 0;
608  
609 1056 ▶ while ( $orig_text =~ m{\G(.*?)(\&|\z)}msg )
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_text =~ /\G(.*?)(\&|\z)/gms) 1982 1056 3038
610 {
611 1982 my ( $before, $indicator ) = ( $1, $2 );
612  
613 1982 $ret .= $before;
614  
615 1982 ▶ if ( $indicator eq '&' )
Branch: 1 branch
BranchTrueFalseTotal
if ($indicator eq "&") 57 1925 1982
616 {
617 57 ▶ if ( $orig_text =~ m{\G(\#?\w+;)}cg )
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_text =~ /\G(\#?\w+;)/cg) { } 57 0 57
618 {
619 57 $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 1056 return $ret;
635 }
636  
637 sub _parse_non_tag_text_unit
638 1057 ▶{
Subroutine _parse_non_tag_text_unit: called CC=4 SCAR=13.9
639 1057 my $self = shift;
640  
641 1057 my $orig_text =
642 $self->consume_up_to( $self->_non_tag_text_unit_consume_regex );
643  
644 1056 my $text = $self->_decode_entities_in_text($orig_text);
645  
646 1056 my $l = $self->curr_line_ref();
647  
648 1056 my $ret_elem = $self->_new_text( [$text] );
649 1056 my $is_para_end = 0;
650  
651 # Demote the cursor to before the < of the tag.
652 #
653 1056 ▶ if ( $self->at_line_start )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->at_line_start) { } 0 1056 1056
654 {
655 0 $is_para_end = 1;
656 }
657 else
658 {
659 1056 pos($$l)--;
660 1056 ▶ if ( substr( $$l, pos($$l), 1 ) eq "\n" )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$l, pos $$l, 1) eq "\n") 708 348 1056
661 {
662 708 $is_para_end = 1;
663 }
664 }
665  
666 1056 ▶ if ( $text !~ /\S/ )
Branch: 1 branch
BranchTrueFalseTotal
if (not $text =~ /\S/) { } 396 660 1056
667 {
668 396 return;
669 }
670 else
671 {
672 return {
673 660 elem => $ret_elem,
674 para_end => $is_para_end,
675 };
676 }
677 }
678  
679 sub _parse_text_unit
680 1784 ▶{
Subroutine _parse_text_unit: called CC=2 SCAR=6.9
681 1784 my $self = shift;
682  
683 1784 ▶ if ( defined( my $event = $self->_extract_event() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $event = $self->_extract_event)) { } 36 1748 1784
684 {
685 36 return $event;
686 }
687 else
688 {
689 1748 $self->_generate_text_unit_events();
690 1745 return $self->_extract_event();
691 }
692 }
693  
694 sub _flush_events
695 917 ▶{
Subroutine _flush_events: called CC=1 SCAR=0.0
696 917 my $self = shift;
697  
698 917 my @ret = @{ $self->_events_queue() };
699  
700 917 $self->_clear_events;
701  
702 917 return \@ret;
703 }
704  
705 sub _parse_text
706 1316 ▶{
Subroutine _parse_text: called CC=3 SCAR=11.0
707 1316 my $self = shift;
708  
709 1316 my @ret;
710  
711 1316 ▶ while ( my $unit = $self->_parse_text_unit() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $unit = $self->_parse_text_unit) 1385 396 1781
712 {
713 1385 push @ret, $unit;
714  
715 1385 ▶ if ( $unit->is_open_or_close )
Branch: 1 branch
BranchTrueFalseTotal
if ($unit->is_open_or_close) 917 468 1385
716 {
717 917 return [ @ret, @{ $self->_flush_events() } ];
718 }
719 }
720  
721 396 return \@ret;
722 }
723  
724 sub _look_for_tag_opener
725 626 ▶{
Subroutine _look_for_tag_opener: called CC=2 SCAR=6.9
726 626 my $self = shift;
727  
728 626 my $l = $self->curr_line_ref();
729  
730 626 ▶ if ( my ($str) = $$l =~ m{\G(<(?:/)?)}cg )
Branch: 1 branch
BranchTrueFalseTotal
if (my($str) = $$l =~ m[\G(<(?:/)?)]cg) 369 257 626
731 {
732 369 return $str;
733 }
734 257 return;
735 }
736  
737 sub _is_closing_tag
738 369 ▶{
Subroutine _is_closing_tag: called CC=1 SCAR=0.0
739 369 my $self = shift;
740 369 my $tag_start = shift;
741  
742 369 return $tag_start =~ m{/};
743 }
744  
745 sub _generate_tag_event
746 1747 ▶{
Subroutine _generate_tag_event: called CC=3 SCAR=11.0
747 1747 my $self = shift;
748  
749 1747 my $l = $self->curr_line_ref();
750 1747 my $orig_pos = pos($$l);
751  
752 1747 ▶ if ( defined( my $tag_start = $self->_look_for_tag_opener() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tag_start = $self->_look_for_tag_opener)) { } 515 1232 1747
753 {
754 # If it's a tag.
755  
756 # TODO : implement the comment handling.
757 # We have a tag.
758  
759 515 pos($$l) = $orig_pos;
760  
761 515 ▶ $self->_enqueue_event(
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_closing_tag($tag_start) ? : 290 225 515
762 XML::Grammar::FictionBase::Event->new(
763 {
764 line_idx => scalar( $self->line_idx() ),
765 'type' => (
766 $self->_is_closing_tag($tag_start) ? "close" : "open"
767 )
768 }
769 ),
770 );
771  
772 515 return 1;
773 }
774 else
775 {
776 1232 return;
777 }
778 }
779  
780 sub _handle_open_tag
781 445 ▶{
Subroutine _handle_open_tag: called CC=3 SCAR=11.1
782 445 my $self = shift;
783  
784 445 my $open = $self->_parse_opening_tag();
785  
786 444 $open->children( [] );
787  
788 # TODO : add the check for is_standalone in XML-Grammar-Fiction
789 # too.
790 444 ▶ if ( $open->is_standalone() )
Branch: 1 branch
BranchTrueFalseTotal
if ($open->is_standalone) { } 18 426 444
791 {
792 18 ▶ if ( defined( $self->_merge_tag($open) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_merge_tag($open)) { } 0 18 18
793 {
794 0 Carp::confess("Top element/tag cannot be standalone.");
795 }
796 else
797 {
798 18 return;
799 }
800 }
801 else
802 {
803 426 return $self->_push_tag($open);
804 }
805 }
806  
807 sub _generate_text_unit_events
808 1747 ▶{
Subroutine _generate_text_unit_events: called CC=2 SCAR=6.9
809 1747 my $self = shift;
810  
811 # $self->skip_multiline_space();
812  
813 1747 ▶ if ( !$self->_generate_tag_event() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_generate_tag_event) 1232 515 1747
814 {
815 1232 $self->_generate_non_tag_text_event();
816 }
817  
818 1745 return;
819 }
820  
821 sub _flush_ret_tag
822 46 ▶{
Subroutine _flush_ret_tag: called CC=1 SCAR=0.0
823 46 my $self = shift;
824  
825 46 my $ret = $self->_ret_tag();
826  
827 46 $self->_clear_ret_tag();
828  
829 46 return $ret;
830 }
831  
832 sub _main_loop
833 56 ▶{
Subroutine _main_loop: called CC=2 SCAR=6.9
834 56 my $self = shift;
835  
836 56 ▶ while ( !defined( $self->_ret_tag() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_ret_tag) 2612 46 2658
837 {
838 2612 $self->_main_loop_iter();
839 }
840  
841 46 return;
842 }
843  
844 sub _parse_all
845 56 ▶{
Subroutine _parse_all: called CC=1 SCAR=0.0
846 56 my $self = shift;
847  
848 56 $self->_main_loop();
849  
850 46 return $self->_flush_ret_tag();
851 }
852  
853 sub _assert_not_eof
854 2612 ▶{
Subroutine _assert_not_eof: called CC=4 SCAR=14.3
855 2612 my $self = shift;
856  
857 2612 ▶ if ( $self->eof() && $self->_no_events() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->eof and $self->_no_events) 1 2611 2612
Condition66%2/3
$self->eof and $self->_no_events
!ll&&!rl&&r
261101
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
858 {
859 1 ▶ if ( !$self->_tags_stack_is_empty() )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_tags_stack_is_empty) { } 1 0 1
860 {
861 1 XML::Grammar::Fiction::Err::Parse::TagNotClosedAtEOF->throw(
862 error => "Tag not closed at EOF.",
863 opening_tag => $self->_top_tag(),
864 );
865 }
866 else
867 {
868 0 Carp::confess(qq{Reached EOF.});
869 }
870 }
871  
872 2611 return;
873 }
874  
875 sub _main_loop_iter
876 2612 ▶{
Subroutine _main_loop_iter: called CC=2 SCAR=6.9
877 2612 my $self = shift;
878  
879 2612 $self->_assert_not_eof;
880  
881 2611 ▶ if ( $self->_look_ahead_for_comment )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_look_ahead_for_comment) { } 22 2589 2611
882 {
883 22 return;
884 }
885 else
886 {
887 2589 return $self->_main_loop_iter_body;
888 }
889 }
890  
891 sub _attempt_to_calc_new_ret_tag
892 2175 ▶{
Subroutine _attempt_to_calc_new_ret_tag: called CC=1 SCAR=0.0
893 2175 my $self = shift;
894  
895 2175 $self->_ret_tag( scalar( $self->_look_for_and_handle_tag() ) );
896  
897 2167 return;
898 }
899  
900 sub _main_loop_iter_body
901 2589 ▶{
Subroutine _main_loop_iter_body: called CC=2 SCAR=6.9
902 2589 my $self = shift;
903  
904 2589 ▶ if ( $self->_main_loop_iter_body_prelude() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_main_loop_iter_body_prelude) 2175 413 2588
905 {
906 2175 $self->_attempt_to_calc_new_ret_tag();
907 }
908  
909 2580 return;
910 }
911  
912  
913 sub process_text
914 56 ▶{
Subroutine process_text: called CC=1 SCAR=0.0
Pod documented
915 56 my ( $self, $text ) = @_;
916  
917 56 $self->setup_text($text);
918  
919 56 return $self->_parse_all();
920 }
921  
922  
923 21 1;
924  
925 __END__
« blib/lib/XML/Grammar/FictionBase/Event.pm ↑ Summary blib/lib/XML/Grammar/Screenplay/App/FromProto.pm »