File Coverage

blib/lib/PPI/Token/Attribute.pm
Criterion Covered Total %
statement 36 37 97.3
branch 16 16 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 60 61 98.3


line stmt bran cond sub pod time code
1             package PPI::Token::Attribute;
2              
3             =pod
4              
5             =head1 NAME
6              
7             PPI::Token::Attribute - A token for a subroutine attribute
8              
9             =head1 INHERITANCE
10              
11             PPI::Token::Attribute
12             isa PPI::Token
13             isa PPI::Element
14              
15             =head1 DESCRIPTION
16              
17             In Perl, attributes are a relatively recent addition to the language.
18              
19             Given the code C< sub foo : bar(something) {} >, the C<bar(something)>
20             part is the attribute.
21              
22             A C<PPI::Token::Attribute> token represents the entire of the attribute,
23             as the braces and its contents are not parsed into the tree, and are
24             treated by Perl (and thus by us) as a single string.
25              
26             =head1 METHODS
27              
28             This class provides some additional methods beyond those provided by its
29             L<PPI::Token> and L<PPI::Element> parent classes.
30              
31             =cut
32              
33 67     67   344 use strict;
  67         111  
  67         1749  
34 67     67   245 use PPI::Token ();
  67         105  
  67         30599  
35              
36             our $VERSION = '1.284';
37              
38             our @ISA = "PPI::Token";
39              
40              
41              
42              
43             #####################################################################
44             # PPI::Token::Attribute Methods
45              
46             =pod
47              
48             =head2 identifier
49              
50             The C<identifier> attribute returns the identifier part of the attribute.
51              
52             That is, for the attribute C<foo(bar)>, the C<identifier> method would
53             return C<"foo">.
54              
55             =cut
56              
57             sub identifier {
58 684     684 1 284408 my $self = shift;
59 684 100       3837 $self->{content} =~ /^(.+?)\(/ ? $1 : $self->{content};
60             }
61              
62             =pod
63              
64             =head2 parameters
65              
66             The C<parameters> method returns the parameter string for the attribute.
67              
68             That is, for the attribute C<foo(bar)>, the C<parameters> method would
69             return C<"bar">.
70              
71             Returns the parameters as a string (including the null string C<''> for
72             the case of an attribute such as C<foo()>.)
73              
74             Returns C<undef> if the attribute does not have parameters.
75              
76             =cut
77              
78             sub parameters {
79 684     684 1 966 my $self = shift;
80 684 100       4231 $self->{content} =~ /\((.*)\)$/ ? $1 : undef;
81             }
82              
83              
84              
85              
86              
87             #####################################################################
88             # Tokenizer Methods
89              
90             sub __TOKENIZER__on_char {
91 1065     1065   1529 my $class = shift;
92 1065         1137 my $t = shift;
93 1065         1614 my $char = substr( $t->{line}, $t->{line_cursor}, 1 );
94              
95             # Unless this is a '(', we are finished.
96 1065 100       1926 unless ( $char eq '(' ) {
97             # Finalise and recheck
98 592         937 return $t->_finalize_token->__TOKENIZER__on_char( $t );
99             }
100              
101             # This is a bar(...) style attribute.
102             # We are currently on the ( so scan in until the end.
103             # We finish on the character AFTER our end
104 473         1038 my $string = $class->__TOKENIZER__scan_for_end( $t );
105 473 100       872 if ( ref $string ) {
106             # EOF
107 24         82 $t->{token}->{content} .= $$string;
108 24         44 $t->_finalize_token;
109 24         75 return 0;
110             }
111              
112             # Found the end of the attribute
113 449         1030 $t->{token}->{content} .= $string;
114 449         822 $t->_finalize_token->__TOKENIZER__on_char( $t );
115             }
116              
117             # Scan for a close braced, and take into account both escaping,
118             # and open close bracket pairs in the string. When complete, the
119             # method leaves the line cursor on the LAST character found.
120             sub __TOKENIZER__scan_for_end {
121 473     473   582 my $t = $_[1];
122              
123             # Loop as long as we can get new lines
124 473         592 my $string = '';
125 473         523 my $depth = 0;
126 473         884 while ( exists $t->{line} ) {
127             # Get the search area
128 1285         1896 pos $t->{line} = $t->{line_cursor};
129              
130             # Look for a match
131 1285 100       3475 unless ( $t->{line} =~ /\G((?:\\.|[^()])*?[()])/gc ) {
132             # Load in the next line and push to first character
133 65         103 $string .= substr( $t->{line}, $t->{line_cursor} );
134 65 100       146 $t->_fill_line(1) or return \$string;
135 41         84 $t->{line_cursor} = 0;
136 41         65 next;
137             }
138              
139             # Add to the string
140 1220         2026 $string .= $1;
141 1220         1420 $t->{line_cursor} += length $1;
142              
143             # Alter the depth and continue if we aren't at the end
144 1220 100       3603 $depth += ($1 =~ /\($/) ? 1 : -1 and next;
    100          
145              
146             # Found the end
147 449         942 return $string;
148             }
149              
150             # Returning the string as a reference indicates EOF
151 0           \$string;
152             }
153              
154             1;
155              
156             =pod
157              
158             =head1 SUPPORT
159              
160             See the L<support section|PPI/SUPPORT> in the main module.
161              
162             =head1 AUTHOR
163              
164             Adam Kennedy E<lt>adamk@cpan.orgE<gt>
165              
166             =head1 COPYRIGHT
167              
168             Copyright 2001 - 2011 Adam Kennedy.
169              
170             This program is free software; you can redistribute
171             it and/or modify it under the same terms as Perl itself.
172              
173             The full text of the license can be found in the
174             LICENSE file included with this module.
175              
176             =cut