| 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 | 62 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 62 | 62 | ▶ | use PPI::Token (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 63 | |||||||||||
| 64 | 62 | ▶ | use vars qw{$VERSION @ISA}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 65 | BEGIN { | ||||||||||
| 66 | 62 | ▶ | $VERSION = '1.236'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 62 | @ISA = 'PPI::Token'; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | ### XS -> PPI/XS.xs:_PPI_Token_Comment__significant 0.900+ | ||||||||||
| 71 | sub significant() { '' } | ||||||||||
| 72 | |||||||||||
| 73 | # Most stuff goes through __TOKENIZER__commit. | ||||||||||
| 74 | # This is such a rare case, do char at a time to keep the code small | ||||||||||
| 75 | sub __TOKENIZER__on_char { | ||||||||||
| 76 | 0 | ▶ | my $t = $_[1]; | ||||||||
|
Subroutine
__TOKENIZER__on_char: not called CC=2 SCAR=17.9
| |||||||||||
| 77 | |||||||||||
| 78 | # Make sure not to include the trailing newline | ||||||||||
| 79 | 0 | ▶ | if ( substr( $t->{line}, $t->{line_cursor}, 1 ) eq "\n" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | return $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 0 | 1; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | sub __TOKENIZER__commit { | ||||||||||
| 87 | 900 | ▶ | my $t = $_[1]; | ||||||||
|
Subroutine
__TOKENIZER__commit: called CC=2 SCAR=6.9
| |||||||||||
| 88 | |||||||||||
| 89 | # Get the rest of the line | ||||||||||
| 90 | 900 | my $rest = substr( $t->{line}, $t->{line_cursor} ); | |||||||||
| 91 | 900 | ▶ | if ( chomp $rest ) { # Include the newline separately | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | # Add the current token, and the newline | ||||||||||
| 93 | 737 | $t->_new_token('Comment', $rest); | |||||||||
| 94 | 737 | $t->_new_token('Whitespace', "\n"); | |||||||||
| 95 | } else { | ||||||||||
| 96 | # Add this token only | ||||||||||
| 97 | 163 | $t->_new_token('Comment', $rest); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | # Advance the line cursor to the end | ||||||||||
| 101 | 900 | $t->{line_cursor} = $t->{line_length} - 1; | |||||||||
| 102 | |||||||||||
| 103 | 900 | 0; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # Comments end at the end of the line | ||||||||||
| 107 | sub __TOKENIZER__on_line_end { | ||||||||||
| 108 | 163 | ▶ | $_[1]->_finalize_token if $_[1]->{token}; | ||||||||
|
Subroutine
__TOKENIZER__on_line_end: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||
| 109 | 163 | 1; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | =pod | ||||||||||
| 113 | |||||||||||
| 114 | =head2 line | ||||||||||
| 115 | |||||||||||
| 116 | The C<line> accessor returns true if the C<PPI::Token::Comment> is a | ||||||||||
| 117 | line comment, or false if it is an inline comment. | ||||||||||
| 118 | |||||||||||
| 119 | =cut | ||||||||||
| 120 | |||||||||||
| 121 | sub line { | ||||||||||
| 122 | # Entire line comments have a newline at the end | ||||||||||
| 123 | 0 | ▶ | $_[0]->{content} =~ /\n$/ ? 1 : 0; | ||||||||
|
Subroutine
line: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 62 | 1; | |||||||||
| 127 | |||||||||||
| 128 | =pod | ||||||||||
| 129 | |||||||||||
| 130 | =head1 SUPPORT | ||||||||||
| 131 | |||||||||||
| 132 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||
| 133 | |||||||||||
| 134 | =head1 AUTHOR | ||||||||||
| 135 | |||||||||||
| 136 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||
| 137 | |||||||||||
| 138 | =head1 COPYRIGHT | ||||||||||
| 139 | |||||||||||
| 140 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||
| 141 | |||||||||||
| 142 | This program is free software; you can redistribute | ||||||||||
| 143 | it and/or modify it under the same terms as Perl itself. | ||||||||||
| 144 | |||||||||||
| 145 | The full text of the license can be found in the | ||||||||||
| 146 | LICENSE file included with this module. | ||||||||||
| 147 | |||||||||||
| 148 | =cut | ||||||||||