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

filter: Statementstmt 100% 90 / 90 Branchbran 87.5% 14 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 21 / 21 Podpod 100% 15 / 15 totaltotal 96.0% 146 / 152 CC 11 SCAR 7.4
CC
11
Cov
96%
consume
5.0
consume_up_to
3.0
_next_line_ref_wo_leading_space
2.0
CRAP 2.1
« blib/lib/XML/Grammar/Screenplay/App/FromProto.pm ↑ Summary blib/lib/XML/Grammar/Fiction/FromProto/Node/WithContent.pm »
1 package XML::Grammar::FictionBase::FromProto::Parser::LineIterator;
2 18 $XML::Grammar::FictionBase::FromProto::Parser::LineIterator::VERSION = '0.22.0';
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use MooX 'late';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 ▶use XML::Grammar::Fiction::Err ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 extends("XML::Grammar::Fiction::FromProto::Parser");
11  
12 18 has "_curr_line_idx" => ( isa => "Int", is => "rw", reader => "line_idx", );
13 18 has "_lines" => ( isa => "ArrayRef", is => "rw" );
14  
15  
16 sub setup_text
17 {
18 58 ▶ my ( $self, $text ) = @_;
Subroutine setup_text: called CC=1 SCAR=0.0
Pod documented
19  
20 # We include the lines trailing newlines for safety.
21 58 $self->_lines( [ split( /^/, $text ) ] );
22  
23 58 $self->_curr_line_idx(0);
24  
25 58 ${ $self->curr_line_ref() } =~ m{\A}g;
26  
27 58 return;
28 }
29  
30  
31 sub curr_line_ref
32 {
33 26494 ▶ my $self = shift;
Subroutine curr_line_ref: called CC=1 SCAR=0.0
Pod documented
34  
35 26494 return \( $self->_lines()->[ $self->_curr_line_idx() ] );
36 }
37  
38  
39 sub curr_pos
40 {
41 4192 ▶ my $self = shift;
Subroutine curr_pos: called CC=1 SCAR=0.0
Pod documented
42  
43 4192 return pos( ${ $self->curr_line_ref() } );
44 }
45  
46  
47 sub at_line_start
48 {
49 2011 ▶ my $self = shift;
Subroutine at_line_start: called CC=1 SCAR=0.0
Pod documented
50  
51 2011 return ( $self->curr_pos == 0 );
52 }
53  
54  
55 sub curr_line_and_pos
56 {
57 1 ▶ my $self = shift;
Subroutine curr_line_and_pos: called CC=1 SCAR=0.0
Pod documented
58  
59 1 return ( $self->curr_line_ref(), $self->curr_pos() );
60 }
61  
62  
63 sub curr_line_copy
64 {
65 1846 ▶ my $self = shift;
Subroutine curr_line_copy: called CC=1 SCAR=0.0
Pod documented
66  
67 1846 my $l = ${ $self->curr_line_ref() } . "";
68  
69 1846 pos($l) = $self->curr_pos();
70  
71 1846 return \$l;
72 }
73  
74  
75 sub next_line_ref
76 {
77 1479 ▶ my $self = shift;
Subroutine next_line_ref: called CC=2 SCAR=6.9
Pod documented
78  
79 1479 $self->_curr_line_idx( $self->_curr_line_idx() + 1 );
80  
81 1479 ▶ if ( !$self->eof() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->eof) 1478 1 1479
82 {
83 1478 pos( ${ $self->curr_line_ref() } ) = 0;
84 }
85  
86 1479 return $self->curr_line_ref();
87 }
88  
89  
90 # Skip the whitespace.
91 sub skip_space
92 {
93 225 ▶ my $self = shift;
Subroutine skip_space: called CC=1 SCAR=0.0
Pod documented
94  
95 225 $self->consume(qr{[ \t]});
96  
97 225 return;
98 }
99  
100  
101 sub skip_multiline_space
102 {
103 627 ▶ my $self = shift;
Subroutine skip_multiline_space: called CC=2 SCAR=6.9
Pod documented
104  
105 627 ▶ if ( ${ $self->curr_line_ref() } =~ m{\G.*?\S} )
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->curr_line_ref;} =~ /\G.*?\S/) 235 392 627
106 {
107 235 return;
108 }
109  
110 392 $self->consume(qr{\s});
111  
112 391 return;
113 }
114  
115  
116 sub curr_line_continues_with
117 {
118 2170 ▶ my ( $self, $re ) = @_;
Subroutine curr_line_continues_with: called CC=1 SCAR=0.0
Pod documented
119  
120 2170 my $l = $self->curr_line_ref();
121  
122 2170 return $$l =~ m{\G$re}cg;
123 }
124  
125  
126 sub line_num
127 {
128 1168 ▶ my $self = shift;
Subroutine line_num: called CC=1 SCAR=0.0
Pod documented
129  
130 1168 return $self->_curr_line_idx() + 1;
131 }
132  
133  
134 sub _next_line_ref_wo_leading_space
135 {
136 1177 ▶ my $self = shift;
Subroutine _next_line_ref_wo_leading_space: called CC=2 SCAR=7.0
137  
138 1177 my $l = $self->next_line_ref();
139  
140 1177 ▶ if ( defined($$l) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$l) 1177 0 1177
141 {
142 1177 $self->_check_if_line_starts_with_whitespace();
143 }
144  
145 1175 return $l;
146 }
147  
148 sub consume
149 {
150 621 ▶ my ( $self, $match_regex ) = @_;
Subroutine consume: called CC=5 SCAR=16.2
Pod documented
151  
152 621 my $return_value = "";
153 621 my $l = $self->curr_line_ref();
154  
155 621 ▶ while ( defined($$l) && ( $$l =~ m[\G(${match_regex}*)\z]cgms ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$l and $$l =~ /\G($match_regex*)\z/cgms) 670 620 1290
Condition66%2/3
defined $$l and $$l =~ /\G($match_regex*)\z/cgms
!ll&&!rl&&r
0620670
MC/DC50%1/2
defined $$l and $$l =~ /\G($match_regex*)\z/cgms
defined $$l $$l =~ /\G($match_regex*)\z/cgms
Truth table66%2/3
A and B
A= defined $$lB= $$l =~ /\G($match_regex*)\z/cgms
ABresult
0X0
100
111
156 {
157 670 $return_value .= $$l;
158 }
159 continue
160 {
161 670 $l = $self->_next_line_ref_wo_leading_space();
162 }
163  
164 620 ▶ if ( defined($$l) && ( $$l =~ m[\G(${match_regex}*)]cg ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$l and $$l =~ /\G($match_regex*)/cg) 597 23 620
Condition66%2/3
defined $$l and $$l =~ /\G($match_regex*)/cg
!ll&&!rl&&r
023597
MC/DC50%1/2
defined $$l and $$l =~ /\G($match_regex*)/cg
defined $$l $$l =~ /\G($match_regex*)/cg
Truth table66%2/3
A and B
A= defined $$lB= $$l =~ /\G($match_regex*)/cg
ABresult
0X0
100
111
165 {
166 597 $return_value .= $1;
167 }
168  
169 620 return $return_value;
170 }
171  
172  
173 # TODO : copied and pasted from _consume - abstract
174 sub consume_up_to
175 {
176 872 ▶ my ( $self, $match_regex ) = @_;
Subroutine consume_up_to: called CC=3 SCAR=11.0
Pod documented
177  
178 872 my $return_value = "";
179 872 my $l = $self->curr_line_ref();
180  
181 LINE_LOOP:
182 872 ▶ while ( defined($$l) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$l) 1378 0 1378
183 {
184 # We assign to a scalar for scalar context, but we're not making
185 # use of the variable.
186 1378 my $verdict = ( $$l =~ m[\G(.*?)((?:${match_regex})|\z)]cgms );
187 1378 $return_value .= $1;
188  
189 # Find if it matched the regex.
190 1378 ▶ if ( length($2) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $2 > 0) 871 507 1378
191 {
192 871 last LINE_LOOP;
193 }
194 }
195 continue
196 {
197 507 $l = $self->_next_line_ref_wo_leading_space();
198 }
199  
200 871 return $return_value;
201 }
202  
203  
204 sub throw_text_error
205 {
206 5 ▶ my ( $self, $error_class, $text ) = @_;
Subroutine throw_text_error: called CC=1 SCAR=0.0
Pod documented
207  
208 5 return $error_class->throw(
209 error => $text,
210 line => $self->line_num(),
211 );
212 }
213  
214 sub _check_if_line_starts_with_whitespace
215 {
216 1177 ▶ my $self = shift;
Subroutine _check_if_line_starts_with_whitespace: called CC=2 SCAR=6.9
217  
218 1177 ▶ if ( ${ $self->curr_line_ref() } =~ m{\A[ \t]} )
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->curr_line_ref;} =~ /\A[ \t]/) 2 1175 1177
219 {
220 2 $self->throw_text_error(
221 'XML::Grammar::Fiction::Err::Parse::LeadingSpace',
222 "Leading space detected in the text.",
223 );
224 }
225 }
226  
227  
228 sub eof
229 {
230 3982 ▶ my $self = shift;
Subroutine eof: called CC=1 SCAR=0.0
Pod documented
231  
232 3982 return ( !defined( ${ $self->curr_line_ref() } ) );
233 }
234  
235  
236 18 1;
237  
238 __END__
239  
240 =pod
241  
242 =encoding UTF-8
243  
244 =head1 NAME
245  
246 XML::Grammar::FictionBase::FromProto::Parser::LineIterator - line iterator base
247 class for the parser.
248  
249 B<For internal use only>.
250  
251 =head1 VERSION
252  
253 version 0.22.0
254  
255 =head1 SYNOPSIS
256  
257 B<TODO:> write one.
258  
259 =head1 DESCRIPTION
260  
261 This is a line iterator that is useful to handle text (e.g: out of a file)
262 and process it incrementally.
263  
264 =head1 METHODS
265  
266 =head2 $self->setup_text($multi_line_text)
267  
268 Use $multi_line_text as the text to process, populate the lines array
269 with it and reset the other variables.
270  
271 =head2 $line_ref = $self->curr_line_ref()
272  
273 Returns a reference to the current line (a string).
274  
275 For example:
276  
277 my $l_ref = $self->curr_line_ref();
278  
279 if ($$l_ref !~ m{\G<tag>}g)
280 {
281 die "Could not match tag.";
282 }
283  
284 =head2 my $pos = $self->curr_pos()
285  
286 Returns the current position (using pos($$l)) of the current line.
287  
288 =head2 $self->at_line_start()
289  
290 Returns if at start of line (curr_pos == 0).
291  
292 =head2 my ($line_ref, $pos) = $self->curr_line_and_pos();
293  
294 Convenience method to return the line reference and the position.
295  
296 For example:
297  
298 # Check for a tag.
299 my ($l_ref, $p) = $self->curr_line_and_pos();
300  
301 my $is_tag_cond = ($$l_ref =~ m{\G<}cg);
302 my $is_close = $is_tag_cond && ($$l_ref =~ m{\G/}cg);
303  
304 pos($$l) = $p;
305  
306 return ($is_tag_cond, $is_close);
307  
308 =head2 my $line_copy_ref = $self->curr_line_copy()
309  
310 Returns a reference to a copy of the current line that is allowed to be
311 tempered with (by assigning to pos() or in a different way.). The line is
312 returned as a reference so to avoid destroying its pos() value.
313  
314 For example:
315  
316 sub _look_ahead_for_tag
317 {
318 my $self = shift;
319  
320 my $l = $self->curr_line_copy();
321  
322 my $is_tag_cond = ($$l =~ m{\G<}cg);
323 my $is_close = $is_tag_cond && ($$l =~ m{\G/}cg);
324  
325 return ($is_tag_cond, $is_close);
326 }
327  
328 =head2 my $line_ref = $self->next_line_ref()
329  
330 Advance the line pointer and return the next line.
331  
332 =head2 $self->skip_space()
333  
334 Skip whitespace (spaces and tabs) from the current position onwards.
335  
336 =head2 $self->skip_multiline_space()
337  
338 Skip multiline space.
339  
340 =head2 $self->curr_line_continues_with($regex)
341  
342 Matches the current line with $regex starting from the current position and
343 returns the result. The position remains at the original position if the
344 regular expression does not match (using C< qr//cg >).
345  
346 =head2 my $line_number = $self->line_idx()
347  
348 Returns the line index as an integer. It starts from 0 for the
349 first line (like in Perl lines.)
350  
351 =head2 my $line_number = $self->line_num()
352  
353 Returns the line number as an integer. It starts from 1 for the
354 first line (like in file lines.)
355  
356 =head2 $self->consume($regex)
357  
358 Consume as much text as $regex matches.
359  
360 =head2 $self->consume_up_to($regex)
361  
362 Consume up to the point where $regex matches.
363  
364 =head2 $self->throw_text_error($exception_class, $text)
365  
366 Throws the Error class $exception_class with the text $text (and the current
367 line number.
368  
369 =head2 eof()
370  
371 Returns if the parser reached the end of the file.
372  
373 =head2 $self->meta()
374  
375 Leftover from Moo.
376  
377 =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
378  
379 =head1 SUPPORT
380  
381 =head2 Websites
382  
383 The following websites have more information about this module, and may be of help to you. As always,
384 in addition to those websites please use your favorite search engine to discover more resources.
385  
386 =over 4
387  
388 =item *
389  
390 MetaCPAN
391  
392 A modern, open-source CPAN search engine, useful to view POD in HTML format.
393  
394 L<https://metacpan.org/release/XML-Grammar-Fiction>
395  
396 =item *
397  
398 RT: CPAN's Bug Tracker
399  
400 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
401  
402 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-Grammar-Fiction>
403  
404 =item *
405  
406 CPANTS
407  
408 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
409  
410 L<http://cpants.cpanauthors.org/dist/XML-Grammar-Fiction>
411  
412 =item *
413  
414 CPAN Testers
415  
416 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
417  
418 L<http://www.cpantesters.org/distro/X/XML-Grammar-Fiction>
419  
420 =item *
421  
422 CPAN Testers Matrix
423  
424 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
425  
426 L<http://matrix.cpantesters.org/?dist=XML-Grammar-Fiction>
427  
428 =item *
429  
430 CPAN Testers Dependencies
431  
432 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
433  
434 L<http://deps.cpantesters.org/?module=XML::Grammar::Fiction>
435  
436 =back
437  
438 =head2 Bugs / Feature Requests
439  
440 Please report any bugs or feature requests by email to C<bug-xml-grammar-fiction at rt.cpan.org>, or through
441 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-Grammar-Fiction>. You will be automatically notified of any
442 progress on the request by the system.
443  
444 =head2 Source Code
445  
446 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
447 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
448 from your repository :)
449  
450 L<https://github.com/shlomif/fiction-xml>
451  
452 git clone git://github.com/shlomif/fiction-xml.git
453  
454 =head1 AUTHOR
455  
456 Shlomi Fish <shlomif@cpan.org>
457  
458 =head1 BUGS
459  
460 Please report any bugs or feature requests on the bugtracker website
461 L<https://github.com/shlomif/fiction-xml/issues>
462  
463 When submitting a bug or request, please include a test-file or a
464 patch to an existing test-file that illustrates the bug or desired
465 feature.
466  
467 =head1 COPYRIGHT AND LICENSE
468  
469 This software is Copyright (c) 2007 by Shlomi Fish.
470  
471 This is free software, licensed under:
472  
473 The MIT (X11) License
474  
475 =cut
« blib/lib/XML/Grammar/Screenplay/App/FromProto.pm ↑ Summary blib/lib/XML/Grammar/Fiction/FromProto/Node/WithContent.pm »