File Coverage

blib/lib/PPI/Document.pm
Criterion Covered Total %
statement 250 273 91.5
branch 88 128 68.7
condition 18 29 62.0
subroutine 47 51 92.1
pod 20 24 83.3
total 423 505 83.7


line stmt bran cond sub pod time code
1             package PPI::Document;
2              
3             =pod
4              
5             =head1 NAME
6              
7             PPI::Document - Object representation of a Perl document
8              
9             =head1 INHERITANCE
10              
11             PPI::Document
12             isa PPI::Node
13             isa PPI::Element
14              
15             =head1 SYNOPSIS
16              
17             use PPI;
18            
19             # Load a document from a file
20             my $Document = PPI::Document->new('My/Module.pm');
21            
22             # Strip out comments
23             $Document->prune('PPI::Token::Comment');
24            
25             # Find all the named subroutines
26             my $sub_nodes = $Document->find(
27             sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name }
28             );
29             my @sub_names = map { $_->name } @$sub_nodes;
30            
31             # Save the file
32             $Document->save('My/Module.pm.stripped');
33              
34             =head1 DESCRIPTION
35              
36             The C class represents a single Perl "document". A
37             C object acts as a root L, with some
38             additional methods for loading and saving, and working with
39             the line/column locations of Elements within a file.
40              
41             The exemption to its L-like behavior this is that a
42             C object can NEVER have a parent node, and is always
43             the root node in a tree.
44              
45             =head2 Storable Support
46              
47             C implements the necessary C and
48             C hooks to provide native support for L,
49             if you have it installed.
50              
51             However if you want to clone a Document, you are highly recommended
52             to use the C<$Document-Eclone> method rather than Storable's
53             C function (although C should still work).
54              
55             =head1 METHODS
56              
57             Most of the things you are likely to want to do with a Document are
58             probably going to involve the methods from L class, of which
59             this is a subclass.
60              
61             The methods listed here are the remaining few methods that are truly
62             Document-specific.
63              
64             =cut
65              
66 67     67   1112352 use strict;
  67         112  
  67         1990  
67 67     67   299 use Carp ();
  67         150  
  67         1283  
68 67     67   249 use List::Util 1.33 ();
  67         1154  
  67         1695  
69 67     67   4714 use Params::Util 1.00 qw{_SCALAR0 _ARRAY0 _INSTANCE};
  67         62194  
  67         3684  
70 67     67   336 use Digest::MD5 ();
  67         98  
  67         963  
71 67     67   6025 use PPI::Util ();
  67         120  
  67         1021  
72 67     67   7114 use PPI ();
  67         135  
  67         3605  
73 67     67   1018 use PPI::Node ();
  67         133  
  67         942  
74 67     67   31093 use YAML::PP ();
  67         3992180  
  67         2345  
75              
76 67     67   468 use overload 'bool' => \&PPI::Util::TRUE;
  67         114  
  67         314  
77 67     67   3134 use overload '""' => 'content';
  67         129  
  67         205  
78              
79             our $VERSION = '1.287';
80              
81             our ( $errstr, @ISA ) = ( "", "PPI::Node" );
82              
83 67     67   25010 use PPI::Document::Fragment ();
  67         140  
  67         1834  
84              
85             # Document cache
86             my $CACHE;
87              
88             # Convenience constants related to constants
89 67     67   274 use constant LOCATION_LINE => 0;
  67         104  
  67         3898  
90 67     67   277 use constant LOCATION_CHARACTER => 1;
  67         113  
  67         2602  
91 67     67   261 use constant LOCATION_COLUMN => 2;
  67         127  
  67         2128  
92 67     67   244 use constant LOCATION_LOGICAL_LINE => 3;
  67         95  
  67         2156  
93 67     67   241 use constant LOCATION_LOGICAL_FILE => 4;
  67         97  
  67         193002  
94              
95              
96              
97              
98              
99             #####################################################################
100             # Constructor and Static Methods
101              
102             =pod
103              
104             =head2 new
105              
106             # Simple construction
107             $doc = PPI::Document->new( $filename );
108             $doc = PPI::Document->new( \$source );
109            
110             # With the readonly attribute set
111             $doc = PPI::Document->new( $filename,
112             readonly => 1,
113             );
114              
115             The C constructor takes as argument a variety of different sources of
116             Perl code, and creates a single cohesive Perl C
117             for it.
118              
119             If passed a file name as a normal string, it will attempt to load the
120             document from the file.
121              
122             If passed a reference to a C, this is taken to be source code and
123             parsed directly to create the document.
124              
125             If passed zero arguments, a "blank" document will be created that contains
126             no content at all.
127              
128             In all cases, the document is considered to be "anonymous" and not tied back
129             to where it was created from. Specifically, if you create a PPI::Document from
130             a filename, the document will B remember where it was created from.
131              
132             Returns a C object, or C if parsing fails.
133             L objects can also be thrown if there are parsing problems.
134              
135             The constructor also takes attribute flags.
136              
137             =head3 readonly
138              
139             Setting C to true will allow various systems to provide additional
140             optimisations and caching. Note that because C is an optimisation
141             flag, it is off by default and you will need to explicitly enable it.
142              
143             =head3 feature_mods
144              
145             Setting feature_mods with a hashref allows defining perl parsing features to be
146             enabled for the whole document. (e.g. when the code is assumed to be run as a
147             oneliner)
148              
149             =head3 custom_feature_includes
150              
151             custom_feature_includes =>
152             { MyStrict => { signatures => "Syntax::Keyword::Try" } }
153              
154             Setting custom_feature_includes with a hashref allows defining include names
155             which act like pragmas that enable parsing features within their scope.
156              
157             This is mostly useful when your work project has its own boilerplate module.
158              
159             It can also be provided as JSON or YAML in the environment variable
160             PPI_CUSTOM_FEATURE_INCLUDES, like so:
161              
162             PPI_CUSTOM_FEATURE_INCLUDES='MyStrict: {signatures: perl}' \
163             perlcritic lib/OurModule.pm
164              
165             PPI_CUSTOM_FEATURE_INCLUDES='{"MyStrict":{"signatures":"perl"}}' \
166             perlcritic lib/OurModule.pm
167              
168             =head3 custom_feature_include_cb
169              
170             custom_feature_include_cb => sub {
171             my ($statement) = @_;
172             return $statement->module eq "MyStrict" ? { signatures => "perl" } : ();
173             },
174              
175             Setting custom_feature_include_cb with a code reference causes all inspections
176             on includes to call that sub before doing any other inspections. The sub can
177             decide to either return a hashref of features to be enabled or disabled, which
178             will be used for the scope the include was called in, or undef to continue with
179             the default inspections. The argument to the sub will be the
180             L object.
181              
182             This can be useful when your work project has a complex boilerplate module.
183              
184             =cut
185              
186             sub new {
187 33679     33679 1 32594807 local $_; # An extra one, just in case
188 33679 50       68996 my $class = ref $_[0] ? ref shift : shift;
189              
190 33679 100       67716 unless ( @_ ) {
191 16840         52645 my $self = $class->SUPER::new;
192 16840         41007 $self->{readonly} = ! 1;
193 16840         26037 $self->{tab_width} = 1;
194 16840         37340 return $self;
195             }
196              
197             # Check constructor attributes
198 16839         24759 my $source = shift;
199 16839         31388 my %attr = @_;
200              
201             # Check the data source
202 16839 50       79290 if ( ! defined $source ) {
    100          
    100          
    50          
203 0         0 $class->_error("An undefined value was passed to PPI::Document::new");
204              
205             } elsif ( ! ref $source ) {
206             # Catch people using the old API
207 517 50       3400 if ( $source =~ /(?:\012|\015)/ ) {
208 0         0 Carp::croak("API CHANGE: Source code should only be passed to PPI::Document->new as a SCALAR reference");
209             }
210              
211             # Save the filename
212 517   66     3804 $attr{filename} ||= $source;
213              
214             # When loading from a filename, use the caching layer if it exists.
215 517 100       1464 if ( $CACHE ) {
216 3         10 my $file_contents = PPI::Util::_slurp( $source );
217              
218             # Errors returned as plain string
219 3 50       7 return $class->_error($file_contents) if !ref $file_contents;
220              
221             # Retrieve the document from the cache
222 3         12 my $document = $CACHE->get_document($file_contents);
223 3 100       14 return $class->_setattr( $document, %attr ) if $document;
224              
225 1         8 $document = PPI::Lexer->lex_source( $$file_contents, %attr );
226 1 50       5 if ( $document ) {
227             # Save in the cache
228 1         4 $CACHE->store_document( $document );
229 1         50 return $document;
230             }
231             } else {
232 514         5208 my $document = PPI::Lexer->lex_file( $source, %attr );
233 514 100       4162 return $document if $document;
234             }
235              
236             } elsif ( _SCALAR0($source) ) {
237 16319         64468 my $document = PPI::Lexer->lex_source( $$source, %attr );
238 16319 100       77387 return $document if $document;
239              
240             } elsif ( _ARRAY0($source) ) {
241 3         11 my $document = PPI::Lexer->lex_file( $source, %attr );
242 3 50       16 return $document if $document;
243              
244             } else {
245 0         0 $class->_error("Unknown object or reference was passed to PPI::Document::new");
246             }
247              
248             # Pull and store the error from the lexer
249 2         5 my $errstr;
250 2 50       10 if ( PPI::Lexer->errstr ) {
    0          
    0          
251 2         6 $errstr = PPI::Lexer->errstr;
252             } elsif ( _INSTANCE($@, 'PPI::Exception') ) {
253 0         0 $errstr = $@->message;
254             } elsif ( $@ ) {
255 0         0 $errstr = $@;
256 0         0 $errstr =~ s/\sat line\s.+$//;
257             } else {
258 0         0 $errstr = "Unknown error parsing Perl document";
259             }
260 2         8 PPI::Lexer->_clear;
261 2         10 $class->_error( $errstr );
262             }
263              
264             sub load {
265 0     0 0 0 Carp::croak("API CHANGE: File names should now be passed to PPI::Document->new to load a file");
266             }
267              
268             sub _setattr {
269 16841     16841   28585 my ( $class, $document, %attr ) = @_;
270 16841         32007 $document->{readonly} = !!$attr{readonly};
271 16841         27184 $document->{filename} = $attr{filename};
272 16841         24818 $document->{feature_mods} = $attr{feature_mods};
273 16841         32603 $document->{custom_feature_includes} = $attr{custom_feature_includes};
274 16841         28422 $document->{custom_feature_include_cb} = $attr{custom_feature_include_cb};
275 16841 100       40027 if ( $ENV{PPI_CUSTOM_FEATURE_INCLUDES} ) {
276 1         6 my $includes = YAML::PP::Load $ENV{PPI_CUSTOM_FEATURE_INCLUDES};
277 1 50       5698 die "\$ENV{PPI_CUSTOM_FEATURE_INCLUDES} "
278             . "does not contain valid perl:\n"
279             . "val: '$ENV{PPI_CUSTOM_FEATURE_INCLUDES}'\nerr: $@"
280             if $@;
281             $document->{custom_feature_includes} =
282 1 50       3 { %{ $document->{custom_feature_includes} || {} }, %{$includes} };
  1         6  
  1         3  
283             }
284 16841         29271 return $document;
285             }
286              
287             =pod
288              
289             =head2 set_cache $cache
290              
291             As of L 1.100, C supports parser caching.
292              
293             The default cache class L provides a L-based
294             caching or the parsed document based on the MD5 hash of the document as
295             a string.
296              
297             The static C method is used to set the cache object for
298             C to use when loading documents. It takes as argument
299             a L object (or something that C the same).
300              
301             If passed C, this method will stop using the current cache, if any.
302              
303             For more information on caching, see L.
304              
305             Returns true on success, or C if not passed a valid param.
306              
307             =cut
308              
309             sub set_cache {
310 3 50   3 1 11 my $class = ref $_[0] ? ref shift : shift;
311              
312 3 100       5 if ( defined $_[0] ) {
313             # Enable the cache
314 2 50       14 my $object = _INSTANCE(shift, 'PPI::Cache') or return undef;
315 2         5 $CACHE = $object;
316             } else {
317             # Disable the cache
318 1         2 $CACHE = undef;
319             }
320              
321 3         8 1;
322             }
323              
324             =pod
325              
326             =head2 get_cache
327              
328             If a document cache is currently set, the C method will
329             return it.
330              
331             Returns a L object, or C if there is no cache
332             currently set for C.
333              
334             =cut
335              
336             sub get_cache {
337 7     7 1 1134 $CACHE;
338             }
339              
340              
341              
342              
343              
344             #####################################################################
345             # PPI::Document Instance Methods
346              
347             =pod
348              
349             =head2 filename
350              
351             The C accessor returns the name of the file in which the document
352             is stored.
353              
354             =cut
355              
356             sub filename {
357 267     267 1 1452 $_[0]->{filename};
358             }
359              
360             =pod
361              
362             =head2 readonly
363              
364             The C attribute indicates if the document is intended to be
365             read-only, and will never be modified. This is an advisory flag, that
366             writers of L-related systems may or may not use to enable
367             optimisations and caches for your document.
368              
369             Returns true if the document is read-only or false if not.
370              
371             =cut
372              
373             sub readonly {
374 4     4 1 7208 $_[0]->{readonly};
375             }
376              
377             =pod
378              
379             =head2 tab_width [ $width ]
380              
381             In order to handle support for C correctly, C
382             need to understand the concept of tabs and tab width. The C
383             method is used to get and set the size of the tab width.
384              
385             At the present time, PPI only supports "naive" (width 1) tabs, but we do
386             plan on supporting arbitrary, default and auto-sensing tab widths later.
387              
388             Returns the tab width as an integer, or Cs if you attempt to set the
389             tab width.
390              
391             =cut
392              
393             sub tab_width {
394 57915     57915 1 52312 my $self = shift;
395 57915 100       82989 return $self->{tab_width} unless @_;
396 2         7 $self->{tab_width} = shift;
397             }
398              
399             =head2 feature_mods { feature_name => $provider }
400              
401             =cut
402              
403             sub feature_mods {
404 16853     16853 1 18411 my $self = shift;
405 16853 50       53108 return $self->{feature_mods} unless @_;
406 0         0 $self->{feature_mods} = shift;
407             }
408              
409             =head2 custom_feature_includes { module_name => { feature_name => $provider } }
410              
411             =cut
412              
413             sub custom_feature_includes {
414 2249     2249 1 2894 my $self = shift;
415 2249 50       11885 return $self->{custom_feature_includes} unless @_;
416 0         0 $self->{custom_feature_includes} = shift;
417             }
418              
419             =head2 custom_feature_include_cb sub { ... }
420              
421             =cut
422              
423             sub custom_feature_include_cb {
424 3300     3300 1 3631 my $self = shift;
425 3300 50       23858 return $self->{custom_feature_include_cb} unless @_;
426 0         0 $self->{custom_feature_include_cb} = shift;
427             }
428              
429             =pod
430              
431             =head2 save
432              
433             $document->save( $file )
434            
435             The C method serializes the C object and saves the
436             resulting Perl document to a file. Returns C on failure to open
437             or write to the file.
438              
439             =cut
440              
441             sub save {
442 2     2 1 5 my $self = shift;
443 2         9 local *FILE;
444 2 50       354 open( FILE, '>', $_[0] ) or return undef;
445 2         10 binmode FILE;
446 2 50       13 print FILE $self->serialize or return undef;
447 2 50       252 close FILE or return undef;
448 2         30 return 1;
449             }
450              
451             =pod
452              
453             =head2 serialize
454              
455             Unlike the C method, which shows only the immediate content
456             within an element, Document objects also have to be able to be written
457             out to a file again.
458              
459             When doing this we need to take into account some additional factors.
460              
461             Primarily, we need to handle here-docs correctly, so that are written
462             to the file in the expected place.
463              
464             The C method generates the actual file content for a given
465             Document object. The resulting string can be written straight to a file.
466              
467             Returns the serialized document as a string.
468              
469             =cut
470              
471             sub serialize {
472 7229     7229 1 9614230 my $self = shift;
473 7229         29112 my @tokens = $self->tokens;
474              
475             # The here-doc content buffer
476 7229         13360 my $heredoc = '';
477              
478             # Start the main loop
479 7229         11366 my $output = '';
480 7229         21370 foreach my $i ( 0 .. $#tokens ) {
481 278889         298837 my $Token = $tokens[$i];
482              
483             # Handle normal tokens
484 278889 100       578375 unless ( $Token->isa('PPI::Token::HereDoc') ) {
485 278258         373206 my $content = $Token->content;
486              
487             # Handle the trivial cases
488 278258 100 100     409905 unless ( $heredoc ne '' and $content =~ /\n/ ) {
489 277815         271921 $output .= $content;
490 277815         312151 next;
491             }
492              
493             # We have pending here-doc content that needs to be
494             # inserted just after the first newline in the content.
495 443 100       780 if ( $content eq "\n" ) {
496             # Shortcut the most common case for speed
497 322         547 $output .= $content . $heredoc;
498             } else {
499             # Slower and more general version
500 121         635 $content =~ s/\n/\n$heredoc/;
501 121         212 $output .= $content;
502             }
503              
504 443         661 $heredoc = '';
505 443         712 next;
506             }
507              
508             # This token is a HereDoc.
509             # First, add the token content as normal, which in this
510             # case will definitely not contain a newline.
511 631         1522 $output .= $Token->content;
512              
513             # Pick up the indentation, which may be undef.
514 631   100     1844 my $indentation = $Token->indentation || '';
515              
516             # Now add all of the here-doc content to the heredoc buffer.
517 631         1624 foreach my $line ( $Token->heredoc ) {
518 990 100       2251 $heredoc .= "\n" eq $line ? $line : $indentation . $line;
519             }
520              
521 631 100       1542 if ( $Token->{_damaged} ) {
522             # Special Case:
523             # There are a couple of warning/bug situations
524             # that can occur when a HereDoc content was read in
525             # from the end of a file that we silently allow.
526             #
527             # When writing back out to the file we have to
528             # auto-repair these problems if we aren't going back
529             # on to the end of the file.
530              
531             # When calculating $last_line, ignore the final token if
532             # and only if it has a single newline at the end.
533 459         577 my $last_index = $#tokens;
534 459 100       2320 if ( $tokens[$last_index]->{content} =~ /^[^\n]*\n$/ ) {
535 283         410 $last_index--;
536             }
537              
538             # This is a two part test.
539             # First, are we on the last line of the
540             # content part of the file
541             my $last_line = List::Util::none {
542 1225 50   1225   4593 $tokens[$_] and $tokens[$_]->{content} =~ /\n/
543 459         2827 } (($i + 1) .. $last_index);
544 459 50       1798 if ( ! defined $last_line ) {
545             # Handles the null list case
546 0         0 $last_line = 1;
547             }
548              
549             # Secondly, are their any more here-docs after us,
550             # (with content or a terminator)
551             my $any_after = List::Util::any {
552             $tokens[$_]->isa('PPI::Token::HereDoc')
553             and (
554 2         8 scalar(@{$tokens[$_]->{_heredoc}})
555             or
556             defined $tokens[$_]->{_terminator_line}
557             )
558 459 100 33 1508   1742 } (($i + 1) .. $#tokens);
  1508         4242  
559 459 50       1381 if ( ! defined $any_after ) {
560             # Handles the null list case
561 0         0 $any_after = '';
562             }
563              
564             # We don't need to repair the last here-doc on the
565             # last line. But we do need to repair anything else.
566 459 50 33     1494 unless ( $last_line and ! $any_after ) {
567             # Add a terminating string if it didn't have one
568 0 0       0 unless ( defined $Token->{_terminator_line} ) {
569 0         0 $Token->{_terminator_line} = $Token->{_terminator};
570             }
571              
572             # Add a trailing newline to the terminating
573             # string if it didn't have one.
574 0 0       0 unless ( $Token->{_terminator_line} =~ /\n$/ ) {
575 0         0 $Token->{_terminator_line} .= "\n";
576             }
577             }
578             }
579              
580             # Now add the termination line to the heredoc buffer
581 631 100       1402 if ( defined $Token->{_terminator_line} ) {
582 179         339 $heredoc .= $indentation . $Token->{_terminator_line};
583             }
584             }
585              
586             # End of tokens
587              
588 7229 50       14353 if ( $heredoc ne '' ) {
589             # If the file doesn't end in a newline, we need to add one
590             # so that the here-doc content starts on the next line.
591 0 0       0 unless ( $output =~ /\n$/ ) {
592 0         0 $output .= "\n";
593             }
594              
595             # Now we add the remaining here-doc content
596             # to the end of the file.
597 0         0 $output .= $heredoc;
598             }
599              
600 7229         41430 $output;
601             }
602              
603             =pod
604              
605             =head2 hex_id
606              
607             The C method generates an unique identifier for the Perl document.
608              
609             This identifier is basically just the serialized document, with
610             Unix-specific newlines, passed through MD5 to produce a hexadecimal string.
611              
612             This identifier is used by a variety of systems (such as L
613             and L) as a unique key against which to store or cache
614             information about a document (or indeed, to cache the document itself).
615              
616             Returns a 32 character hexadecimal string.
617              
618             =cut
619              
620             sub hex_id {
621 168     168 1 276637 PPI::Util::md5hex($_[0]->serialize);
622             }
623              
624             =pod
625              
626             =head2 index_locations
627              
628             Within a document, all L objects can be considered to have a
629             "location", a line/column position within the document when considered as a
630             file. This position is primarily useful for debugging type activities.
631              
632             The method for finding the position of a single Element is a bit laborious,
633             and very slow if you need to do it a lot. So the C method
634             will index and save the locations of every Element within the Document in
635             advance, making future calls to virtually free.
636              
637             Please note that this index should always be cleared using C
638             once you are finished with the locations. If content is added to or removed
639             from the file, these indexed locations will be B.
640              
641             =cut
642              
643             sub index_locations {
644 263     263 1 119406 my $self = shift;
645 263         997 my @tokens = $self->tokens;
646              
647             # Whenever we hit a heredoc we will need to increment by
648             # the number of lines in its content section when we
649             # encounter the next token with a newline in it.
650 263         737 my $heredoc = 0;
651              
652             # Find the first Token without a location
653 263         631 my ($first, $location) = ();
654 263         955 foreach ( 0 .. $#tokens ) {
655 263         547 my $Token = $tokens[$_];
656 263 50       844 next if $Token->{_location};
657              
658             # Found the first Token without a location
659             # Calculate the new location if needed.
660 263 50       1104 $location =
661             $_
662             ? $self->_add_location( $location, $tokens[ $_ - 1 ], \$heredoc )
663             : $self->_default_location;
664 263         461 $first = $_;
665 263         519 last;
666             }
667              
668             # Calculate locations for the rest
669 263 50       641 if ( defined $first ) {
670 263         620 foreach ( $first .. $#tokens ) {
671 68755         72459 my $Token = $tokens[$_];
672 68755         75995 $Token->{_location} = $location;
673 68755         82132 $location = $self->_add_location( $location, $Token, \$heredoc );
674              
675             # Add any here-doc lines to the counter
676 68755 100       148677 if ( $Token->isa('PPI::Token::HereDoc') ) {
677 34         124 $heredoc += $Token->heredoc + 1;
678             }
679             }
680             }
681              
682 263         6525 1;
683             }
684              
685             sub _default_location {
686 266     266   568 my ($self) = @_;
687 266 50       1521 my $logical_file = $self->can('filename') ? $self->filename : undef;
688 266         957 return [ 1, 1, 1, 1, $logical_file ];
689             }
690              
691             sub location {
692 3     3 1 512 my ($self) = @_;
693 3   33     12 return $self->SUPER::location || $self->_default_location;
694             }
695              
696             sub _add_location {
697 68755     68755   75632 my ($self, $start, $Token, $heredoc) = @_;
698 68755         89054 my $content = $Token->{content};
699              
700             # Does the content contain any newlines
701 68755         79637 my $newlines =()= $content =~ /\n/g;
702 68755         83035 my ($logical_line, $logical_file) =
703             $self->_logical_line_and_file($start, $Token, $newlines);
704              
705 68755 100       82050 unless ( $newlines ) {
706             # Handle the simple case
707             return [
708 57607         74693 $start->[LOCATION_LINE],
709             $start->[LOCATION_CHARACTER] + length($content),
710             $start->[LOCATION_COLUMN]
711             + $self->_visual_length(
712             $content,
713             $start->[LOCATION_COLUMN]
714             ),
715             $logical_line,
716             $logical_file,
717             ];
718             }
719              
720             # This is the more complex case where we hit or
721             # span a newline boundary.
722 11148         10488 my $physical_line = $start->[LOCATION_LINE] + $newlines;
723 11148         17608 my $location = [ $physical_line, 1, 1, $logical_line, $logical_file ];
724 11148 100 66     23001 if ( $heredoc and $$heredoc ) {
725 31         44 $location->[LOCATION_LINE] += $$heredoc;
726 31         41 $location->[LOCATION_LOGICAL_LINE] += $$heredoc;
727 31         47 $$heredoc = 0;
728             }
729              
730             # Does the token have additional characters
731             # after their last newline.
732 11148 100       23809 if ( $content =~ /\n([^\n]+?)\z/ ) {
733 305         841 $location->[LOCATION_CHARACTER] += length($1);
734 305         463 $location->[LOCATION_COLUMN] +=
735             $self->_visual_length(
736             $1, $location->[LOCATION_COLUMN],
737             );
738             }
739              
740 11148         14173 $location;
741             }
742              
743             sub _logical_line_and_file {
744 68755     68755   73191 my ($self, $start, $Token, $newlines) = @_;
745              
746             # Regex taken from perlsyn, with the correction that there's no space
747             # required between the line number and the file name.
748 68755 100       82787 if ($start->[LOCATION_CHARACTER] == 1) {
749 10869 100       31838 if ( $Token->isa('PPI::Token::Comment') ) {
    100          
750 1764 100       3395 if (
751             $Token->content =~ m<
752             \A
753             \# \s*
754             line \s+
755             (\d+) \s*
756             (?: (\"?) ([^\"]* [^\s\"]) \2 )?
757             \s*
758             \z
759             >xms
760             ) {
761 13   66     73 return $1, ($3 || $start->[LOCATION_LOGICAL_FILE]);
762             }
763             }
764             elsif ( $Token->isa('PPI::Token::Pod') ) {
765 351         1059 my $content = $Token->content;
766 351         431 my $line;
767 351         489 my $file = $start->[LOCATION_LOGICAL_FILE];
768 351         512 my $end_of_directive;
769 351         1332 while (
770             $content =~ m<
771             ^
772             \# \s*?
773             line \s+?
774             (\d+) (?: (?! \n) \s)*
775             (?: (\"?) ([^\"]*? [^\s\"]) \2 )??
776             \s*?
777             $
778             >xmsg
779             ) {
780 6   66     23 ($line, $file) = ($1, ( $3 || $file ) );
781 6         13 $end_of_directive = pos $content;
782             }
783              
784 351 100       614 if (defined $line) {
785 6         7 pos $content = $end_of_directive;
786 6         20 my $post_directive_newlines =()= $content =~ m< \G [^\n]* \n >xmsg;
787 6         15 return $line + $post_directive_newlines - 1, $file;
788             }
789             }
790             }
791              
792             return
793 68736         95728 $start->[LOCATION_LOGICAL_LINE] + $newlines,
794             $start->[LOCATION_LOGICAL_FILE];
795             }
796              
797             sub _visual_length {
798 57912     57912   63210 my ($self, $content, $pos) = @_;
799              
800 57912         66256 my $tab_width = $self->tab_width;
801 57912         54023 my ($length, $vis_inc);
802              
803 57912 100       135119 return length $content if $content !~ /\t/;
804              
805             # Split the content in tab and non-tab parts and calculate the
806             # "visual increase" of each part.
807 4393         12936 for my $part ( split(/(\t)/, $content) ) {
808 16070 100       17281 if ($part eq "\t") {
809 7996         7728 $vis_inc = $tab_width - ($pos-1) % $tab_width;
810             }
811             else {
812 8074         6920 $vis_inc = length $part;
813             }
814 16070         14448 $length += $vis_inc;
815 16070         15513 $pos += $vis_inc;
816             }
817              
818 4393         10287 $length;
819             }
820              
821             =pod
822              
823             =head2 flush_locations
824              
825             When no longer needed, the C method clears all location data
826             from the tokens.
827              
828             =cut
829              
830             sub flush_locations {
831 1     1 1 655 shift->_flush_locations(@_);
832             }
833              
834             =pod
835              
836             =head2 normalized
837              
838             The C method is used to generate a "Layer 1"
839             L object for the current Document.
840              
841             A "normalized" Perl Document is an arbitrary structure that removes any
842             irrelevant parts of the document and refactors out variations in style,
843             to attempt to approach something that is closer to the "true meaning"
844             of the Document.
845              
846             See L for more information on document normalization and
847             the tasks for which it is useful.
848              
849             Returns a L object, or C on error.
850              
851             =cut
852              
853             sub normalized {
854             # The normalization process will utterly destroy and mangle
855             # anything passed to it, so we are going to only give it a
856             # clone of ourselves.
857 4     4 1 2209 PPI::Normal->process( $_[0]->clone );
858             }
859              
860             =pod
861              
862             =head1 complete
863              
864             The C method is used to determine if a document is cleanly
865             structured, all braces are closed, the final statement is
866             fully terminated and all heredocs are fully entered.
867              
868             Returns true if the document is complete or false if not.
869              
870             =cut
871              
872             sub complete {
873 2     2 0 2062 my $self = shift;
874              
875             # Every structure has to be complete
876             $self->find_any( sub {
877 15 50   15   66 $_[1]->isa('PPI::Structure')
878             and
879             ! $_[1]->complete
880             } )
881 2 50       14 and return '';
882              
883             # Strip anything that isn't a statement off the end
884 2         12 my @child = $self->children;
885 2   66     23 while ( @child and not $child[-1]->isa('PPI::Statement') ) {
886 2         6 pop @child;
887             }
888              
889             # We must have at least one statement
890 2 50       3 return '' unless @child;
891              
892             # Check the completeness of the last statement
893 2         9 return $child[-1]->_complete;
894             }
895              
896              
897              
898              
899              
900             #####################################################################
901             # PPI::Node Methods
902              
903             # We are a scope boundary
904             ### XS -> PPI/XS.xs:_PPI_Document__scope 0.903+
905             sub scope() { 1 }
906              
907              
908              
909              
910              
911             #####################################################################
912             # PPI::Element Methods
913              
914             sub insert_before {
915 0     0 1 0 return undef;
916             # die "Cannot insert_before a PPI::Document";
917             }
918              
919             sub insert_after {
920 0     0 1 0 return undef;
921             # die "Cannot insert_after a PPI::Document";
922             }
923              
924             sub replace {
925 0     0 1 0 return undef;
926             # die "Cannot replace a PPI::Document";
927             }
928              
929              
930              
931              
932              
933             #####################################################################
934             # Error Handling
935              
936             # Set the error message
937             sub _error {
938 2     2   5 $errstr = $_[1];
939 2         8 undef;
940             }
941              
942             # Clear the error message.
943             # Returns the object as a convenience.
944             sub _clear {
945 16782     16782   50678 $errstr = '';
946 16782         23682 $_[0];
947             }
948              
949             =pod
950              
951             =head2 errstr
952              
953             For error that occur when loading and saving documents, you can use
954             C, as either a static or object method, to access the error message.
955              
956             If a Document loads or saves without error, C will return false.
957              
958             =cut
959              
960             sub errstr {
961 16783     16783 1 78698 $errstr;
962             }
963              
964              
965              
966              
967              
968             #####################################################################
969             # Native Storable Support
970              
971             sub STORABLE_freeze {
972 4     4 0 804 my $self = shift;
973 4         9 my $class = ref $self;
974 4         25 my %hash = %$self;
975 4         484 return ($class, \%hash);
976             }
977              
978             sub STORABLE_thaw {
979 6     6 0 508 my ($self, undef, $class, $hash) = @_;
980 6         11 bless $self, $class;
981 6         20 foreach ( keys %$hash ) {
982 42         58 $self->{$_} = delete $hash->{$_};
983             }
984 6         29 $self->__link_children;
985             }
986              
987             1;
988              
989             =pod
990              
991             =head1 TO DO
992              
993             - May need to overload some methods to forcefully prevent Document
994             objects becoming children of another Node.
995              
996             =head1 SUPPORT
997              
998             See the L in the main module.
999              
1000             =head1 AUTHOR
1001              
1002             Adam Kennedy Eadamk@cpan.orgE
1003              
1004             =head1 SEE ALSO
1005              
1006             L, L
1007              
1008             =head1 COPYRIGHT
1009              
1010             Copyright 2001 - 2011 Adam Kennedy.
1011              
1012             This program is free software; you can redistribute
1013             it and/or modify it under the same terms as Perl itself.
1014              
1015             The full text of the license can be found in the
1016             LICENSE file included with this module.
1017              
1018             =cut