| 1 | package PPI::Token::Comment; | ||||||||||
| 2 | |||||||||||
| 3 | =pod | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | PPI::Token::Comment - A comment in Perl source code | ||||||||||
| 8 | |||||||||||
| 9 | =head1 INHERITANCE | ||||||||||
| 10 | |||||||||||
| 11 | PPI::Token::Comment | ||||||||||
| 12 | isa PPI::Token | ||||||||||
| 13 | isa PPI::Element | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | # This is a PPI::Token::Comment | ||||||||||
| 18 | |||||||||||
| 19 | print "Hello World!"; # So it this | ||||||||||
| 20 | |||||||||||
| 21 | $string =~ s/ foo # This, unfortunately, is not :( | ||||||||||
| 22 | bar | ||||||||||
| 23 | /w; | ||||||||||
| 24 | |||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||
| 26 | |||||||||||
| 27 | In PPI, comments are represented by C<PPI::Token::Comment> objects. | ||||||||||
| 28 | |||||||||||
| 29 | These come in two flavours, line comment and inline comments. | ||||||||||
| 30 | |||||||||||
| 31 | A C<line comment> is a comment that stands on its own line. These comments | ||||||||||
| 32 | hold their own newline and whitespace (both leading and trailing) as part | ||||||||||
| 33 | of the one C<PPI::Token::Comment> object. | ||||||||||
| 34 | |||||||||||
| 35 | An inline comment is a comment that appears after some code, and | ||||||||||
| 36 | continues to the end of the line. This does B<not> include whitespace, | ||||||||||
| 37 | and the terminating newlines is considered a separate | ||||||||||
| 38 | L<PPI::Token::Whitespace> token. | ||||||||||
| 39 | |||||||||||
| 40 | This is largely a convenience, simplifying a lot of normal code relating | ||||||||||
| 41 | to the common things people do with comments. | ||||||||||
| 42 | |||||||||||
| 43 | Most commonly, it means when you C<prune> or C<delete> a comment, a line | ||||||||||
| 44 | comment disappears taking the entire line with it, and an inline comment | ||||||||||
| 45 | is removed from the inside of the line, allowing the newline to drop | ||||||||||
| 46 | back onto the end of the code, as you would expect. | ||||||||||
| 47 | |||||||||||
| 48 | It also means you can move comments around in blocks much more easily. | ||||||||||
| 49 | |||||||||||
| 50 | For now, this is a suitably handy way to do things. However, I do reserve | ||||||||||
| 51 | the right to change my mind on this one if it gets dangerously | ||||||||||
| 52 | anachronistic somewhere down the line. | ||||||||||
| 53 | |||||||||||
| 54 | =head1 METHODS | ||||||||||
| 55 | |||||||||||
| 56 | Only very limited methods are available, beyond those provided by our | ||||||||||
| 57 | parent L<PPI::Token> and L<PPI::Element> classes. | ||||||||||
| 58 | |||||||||||
| 59 | =cut | ||||||||||
| 60 | |||||||||||
| 61 | 63 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 62 | 63 | ▶ | use PPI::Token (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 63 | |||||||||||
| 64 | 63 | our $VERSION = '1.260'; # VERSION | |||||||||
| 65 | |||||||||||
| 66 | 63 | our @ISA = "PPI::Token"; | |||||||||
| 67 | |||||||||||
| 68 | ### XS -> PPI/XS.xs:_PPI_Token_Comment__significant 0.900+ | ||||||||||
| 69 | sub significant() { '' } | ||||||||||
| 70 | |||||||||||
| 71 | # Most stuff goes through __TOKENIZER__commit. | ||||||||||
| 72 | # This is such a rare case, do char at a time to keep the code small | ||||||||||
| 73 | sub __TOKENIZER__on_char { | ||||||||||
| 74 | 0 | ▶ | my $t = $_[1]; | ||||||||
|
Subroutine
__TOKENIZER__on_char: not called CC=2 SCAR=17.9
| |||||||||||
| 75 | |||||||||||
| 76 | # Make sure not to include the trailing newline | ||||||||||
| 77 | 0 | ▶ | if ( substr( $t->{line}, $t->{line_cursor}, 1 ) eq "\n" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | return $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | 1; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | sub __TOKENIZER__commit { | ||||||||||
| 85 | 1373 | ▶ | my $t = $_[1]; | ||||||||
|
Subroutine
__TOKENIZER__commit: called CC=2 SCAR=6.9
| |||||||||||
| 86 | |||||||||||
| 87 | # Get the rest of the line | ||||||||||
| 88 | 1373 | my $rest = substr( $t->{line}, $t->{line_cursor} ); | |||||||||
| 89 | 1373 | ▶ | if ( chomp $rest ) { # Include the newline separately | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | # Add the current token, and the newline | ||||||||||
| 91 | 971 | $t->_new_token('Comment', $rest); | |||||||||
| 92 | 971 | $t->_new_token('Whitespace', "\n"); | |||||||||
| 93 | } else { | ||||||||||
| 94 | # Add this token only | ||||||||||
| 95 | 402 | $t->_new_token('Comment', $rest); | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | # Advance the line cursor to the end | ||||||||||
| 99 | 1373 | $t->{line_cursor} = $t->{line_length} - 1; | |||||||||
| 100 | |||||||||||
| 101 | 1373 | 0; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | # Comments end at the end of the line | ||||||||||
| 105 | sub __TOKENIZER__on_line_end { | ||||||||||
| 106 | 402 | ▶ | $_[1]->_finalize_token if $_[1]->{token}; | ||||||||
|
Subroutine
__TOKENIZER__on_line_end: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||
| 107 | 402 | 1; | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =pod | ||||||||||
| 111 | |||||||||||
| 112 | =head2 line | ||||||||||
| 113 | |||||||||||
| 114 | The C<line> accessor returns true if the C<PPI::Token::Comment> is a | ||||||||||
| 115 | line comment, or false if it is an inline comment. | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||
| 118 | |||||||||||
| 119 | sub line { | ||||||||||
| 120 | # Entire line comments have a newline at the end | ||||||||||
| 121 | 0 | ▶ | $_[0]->{content} =~ /\n$/ ? 1 : 0; | ||||||||
|
Subroutine
line: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 63 | 1; | |||||||||
| 125 | |||||||||||
| 126 | =pod | ||||||||||
| 127 | |||||||||||
| 128 | =head1 SUPPORT | ||||||||||
| 129 | |||||||||||
| 130 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||
| 131 | |||||||||||
| 132 | =head1 AUTHOR | ||||||||||
| 133 | |||||||||||
| 134 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||
| 135 | |||||||||||
| 136 | =head1 COPYRIGHT | ||||||||||
| 137 | |||||||||||
| 138 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||
| 139 | |||||||||||
| 140 | This program is free software; you can redistribute | ||||||||||
| 141 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 142 | |||||||||||
| 143 | The full text of the license can be found in the | ||||||||||
| 144 | LICENSE file included with this module. | ||||||||||
| 145 | |||||||||||
| 146 | =cut | ||||||||||