| 1 | package PPI::Token::Magic; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | PPI::Token::Magic - Tokens representing magic variables | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 INHERITANCE | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | PPI::Token::Magic | ||||||||||||||||||||||||||||||||||||||||||
| 12 | isa PPI::Token::Symbol | ||||||||||||||||||||||||||||||||||||||||||
| 13 | isa PPI::Token | ||||||||||||||||||||||||||||||||||||||||||
| 14 | isa PPI::Element | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # When we say magic variables, we mean these... | ||||||||||||||||||||||||||||||||||||||||||
| 19 | $1 $2 $3 $4 $5 $6 $7 $8 $9 | ||||||||||||||||||||||||||||||||||||||||||
| 20 | $_ $& $` $' $+ @+ %+ $* $. $/ $| | ||||||||||||||||||||||||||||||||||||||||||
| 21 | $\ $" $; $% $= $- @- %- $) $# | ||||||||||||||||||||||||||||||||||||||||||
| 22 | $~ $^ $: $? $! %! $@ $$ $< $> | ||||||||||||||||||||||||||||||||||||||||||
| 23 | $( $0 $[ $] @_ @* $} $, $#+ $#- | ||||||||||||||||||||||||||||||||||||||||||
| 24 | $^L $^A $^E $^C $^D $^F $^H | ||||||||||||||||||||||||||||||||||||||||||
| 25 | $^I $^M $^N $^O $^P $^R $^S | ||||||||||||||||||||||||||||||||||||||||||
| 26 | $^T $^V $^W $^X %^H | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | C<PPI::Token::Magic> is a sub-class of L<PPI::Token::Symbol> which | ||||||||||||||||||||||||||||||||||||||||||
| 31 | identifies the token as "magic variable", one of the strange and | ||||||||||||||||||||||||||||||||||||||||||
| 32 | unusual variables that are connected to "things" behind the scenes. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | Some are extremely common, like C<$_>, and others you will quite | ||||||||||||||||||||||||||||||||||||||||||
| 35 | probably never encounter in your Perl career. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | The class provides no additional methods, beyond those provided by | ||||||||||||||||||||||||||||||||||||||||||
| 40 | L<PPI::Token::Symbol>, L<PPI::Token> and L<PPI::Element>. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 63 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 63 | ▶ | use PPI::Token::Symbol (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 63 | ▶ | use PPI::Token::Unknown (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 63 | ▶ | use PPI::Singletons qw' %MAGIC $CURLY_SYMBOL '; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 63 | our $VERSION = '1.260'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 63 | our @ISA = "PPI::Token::Symbol"; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | sub __TOKENIZER__on_char { | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 1339 | ▶ | my $t = $_[1]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__TOKENIZER__on_char: called CC=23 SCAR=31.4
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # $c is the candidate new content | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 1339 | my $c = $t->{token}->{content} . substr( $t->{line}, $t->{line_cursor}, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # Do a quick first test so we don't have to do more than this one. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # All of the tests below match this one, so it should provide a | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # small speed up. This regex should be updated to match the inside | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # tests if they are changed. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 1339 | ▶ | if ( $c =~ /^ \$ .* [ \w : \$ \{ ] $/x ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 910 | ▶ | if ( $c =~ /^(\$(?:\_[\w:]|::))/ or $c =~ /^\$\'[\w]/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c =~ /^(\$(?:\_[\w:]|::))/ or $c =~ /^\$\'[\w]/
MC/DC100%2/2
$c =~ /^(\$(?:\_[\w:]|::))/ or $c =~ /^\$\'[\w]/
$c =~ /^(\$(?:\_[\w:]|::))/
$c =~ /^\$\'[\w]/
Truth table100%3/3
A or B
A= $c =~ /^(\$(?:\_[\w:]|::))/B= $c =~ /^\$\'[\w]/
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | # If and only if we have $'\d, it is not a | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # symbol. (this was apparently a conscious choice) | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # Note that $::0 on the other hand is legal | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 90 | ▶ | if ( $c =~ /^\$\'\d$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | # In this case, we have a magic plus a digit. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # Save the CURRENT token, and rerun the on_char | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 3 | return $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | # A symbol in the style $_foo or $::foo or $'foo. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # Overwrite the current token | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 87 | $t->{class} = $t->{token}->set_class('Symbol'); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 87 | return PPI::Token::Symbol->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 820 | ▶ | if ( $c =~ /^\$\$\w/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | # This is really a scalar dereference. ( $$foo ) | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # Add the current token as the cast... | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 78 | $t->{token} = PPI::Token::Cast->new( '$' ); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 78 | $t->_finalize_token; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | # ... and create a new token for the symbol | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 78 | return $t->_new_token( 'Symbol', '$' ); | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 742 | ▶ | if ( $c eq '$${' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | # This _might_ be a dereference of one of the | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # control-character symbols. | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | pos $t->{line} = $t->{line_cursor} + 1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if ( $t->{line} =~ m/$CURLY_SYMBOL/gc ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | # This is really a dereference. ( $${^_foo} ) | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # Add the current token as the cast... | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | $t->{token} = PPI::Token::Cast->new( '$' ); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | $t->_finalize_token; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | # ... and create a new token for the symbol | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | return $t->_new_token( 'Magic', '$' ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 741 | ▶ | if ( $c eq '$#$' or $c eq '$#{' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c eq "\$#\$" or $c eq "\$#{"
MC/DC100%2/2
$c eq "\$#\$" or $c eq "\$#{"
$c eq "\$#\$"
$c eq "\$#{"
Truth table100%3/3
A or B
A= $c eq "\$#\$"B= $c eq "\$#{"
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | # This is really an index dereferencing cast, although | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # it has the same two chars as the magic variable $#. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 33 | $t->{class} = $t->{token}->set_class('Cast'); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 33 | return $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 708 | ▶ | if ( $c =~ /^(\$\#)\w/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | # This is really an array index thingy ( $#array ) | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 95 | $t->{token} = PPI::Token::ArrayIndex->new( "$1" ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 95 | return PPI::Token::ArrayIndex->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 613 | ▶ | if ( $c =~ /^\$\^\w+$/o ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | # It's an escaped char magic... maybe ( like $^M ) | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 356 | my $next = substr( $t->{line}, $t->{line_cursor}+1, 1 ); # Peek ahead | |||||||||||||||||||||||||||||||||||||||||
| 122 | 356 | ▶ | if ($MAGIC{$c} && (!$next || $next !~ /\w/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$PPI::Token::Magic::MAGIC{$c} and not $next && $next =~ /\w/
$next && $next =~ /\w/
MC/DC66%2/3
$PPI::Token::Magic::MAGIC{$c} and not $next && $next =~ /\w/
$PPI::Token::Magic::MAGIC{$c}
$next
$next =~ /\w/
Truth table75%3/4
A and not(B && C)
A= $PPI::Token::Magic::MAGIC{$c}B= $nextC= $next =~ /\w/
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 33 | $t->{token}->{content} = $c; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 33 | $t->{line_cursor}++; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # Maybe it's a long magic variable like $^WIDE_SYSTEM_CALLS | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 323 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 290 | ▶ | if ( $c =~ /^\$\#\{/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | # The $# is actually a cast, and { is its block | ||||||||||||||||||||||||||||||||||||||||||
| 133 | # Add the current token as the cast... | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $t->{token} = PPI::Token::Cast->new( '$#' ); | |||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $t->_finalize_token; | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # ... and create a new token for the block | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | return $t->_new_token( 'Structure', '{' ); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | } elsif ($c =~ /^%\^/) { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 22 | ▶ | return 1 if $c eq '%^'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | # It's an escaped char magic... maybe ( like %^H ) | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 22 | ▶ | if ($MAGIC{$c}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 8 | $t->{token}->{content} = $c; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 8 | $t->{line_cursor}++; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # Back off, treat '%' as an operator | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 14 | chop $t->{token}->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 14 | bless $t->{token}, $t->{class} = 'PPI::Token::Operator'; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 14 | $t->{line_cursor}--; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 719 | ▶ | if ( $MAGIC{$c} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | # $#+ and $#- | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 42 | $t->{line_cursor} += length( $c ) - length( $t->{token}->{content} ); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 42 | $t->{token}->{content} = $c; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 677 | pos $t->{line} = $t->{line_cursor}; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 677 | ▶ | if ( $t->{line} =~ m/($CURLY_SYMBOL)/gc ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | # control character symbol (e.g. ${^MATCH}) | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 7 | $t->{token}->{content} .= $1; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 7 | $t->{line_cursor} += length $1; | |||||||||||||||||||||||||||||||||||||||||
| 164 | ▶ | } elsif ( $c =~ /^\$\d+$/ and $t->{line} =~ /\G(\d+)/gc ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$c =~ /^\$\d+$/ and $t->{"line"} =~ /\G(\d+)/cg
MC/DC100%2/2
$c =~ /^\$\d+$/ and $t->{"line"} =~ /\G(\d+)/cg
$c =~ /^\$\d+$/
$t->{"line"} =~ /\G(\d+)/cg
Truth table100%3/3
A and B
A= $c =~ /^\$\d+$/B= $t->{"line"} =~ /\G(\d+)/cg
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | # Grab trailing digits of regex capture variables. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | $t->{token}{content} .= $1; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 4 | $t->{line_cursor} += length $1; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | # End the current magic token, and recheck | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 719 | $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | # Our version of canonical is plain simple | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 17 | ▶ | sub canonical { $_[0]->content } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
canonical: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 63 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||||||||||||||||
| 195 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | The full text of the license can be found in the | ||||||||||||||||||||||||||||||||||||||||||
| 198 | LICENSE file included with this module. | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||||||||||||||||