| Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/PPI/Token/Number.pm |
| Statements | Executed 11431 statements in 18.2ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1170 | 1 | 1 | 17.7ms | 38.4ms | PPI::Token::Number::__TOKENIZER__on_char |
| 2626 | 3 | 1 | 1.80ms | 1.80ms | PPI::Token::Number::CORE:match (opcode) |
| 1 | 1 | 1 | 12µs | 24µs | PPI::Token::Number::BEGIN@32 |
| 1 | 1 | 1 | 8µs | 8µs | PPI::Token::Number::BEGIN@36 |
| 1 | 1 | 1 | 6µs | 35µs | PPI::Token::Number::BEGIN@35 |
| 1 | 1 | 1 | 3µs | 3µs | PPI::Token::Number::BEGIN@33 |
| 0 | 0 | 0 | 0s | 0s | PPI::Token::Number::_literal |
| 0 | 0 | 0 | 0s | 0s | PPI::Token::Number::base |
| 0 | 0 | 0 | 0s | 0s | PPI::Token::Number::literal |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package PPI::Token::Number; | ||||
| 2 | |||||
| 3 | =pod | ||||
| 4 | |||||
| 5 | =head1 NAME | ||||
| 6 | |||||
| 7 | PPI::Token::Number - Token class for a number | ||||
| 8 | |||||
| 9 | =head1 SYNOPSIS | ||||
| 10 | |||||
| 11 | $n = 1234; # decimal integer | ||||
| 12 | $n = 0b1110011; # binary integer | ||||
| 13 | $n = 01234; # octal integer | ||||
| 14 | $n = 0x1234; # hexadecimal integer | ||||
| 15 | $n = 12.34e-56; # exponential notation ( currently not working ) | ||||
| 16 | |||||
| 17 | =head1 INHERITANCE | ||||
| 18 | |||||
| 19 | PPI::Token::Number | ||||
| 20 | isa PPI::Token | ||||
| 21 | isa PPI::Element | ||||
| 22 | |||||
| 23 | =head1 DESCRIPTION | ||||
| 24 | |||||
| 25 | The C<PPI::Token::Number> class is used for tokens that represent numbers, | ||||
| 26 | in the various types that Perl supports. | ||||
| 27 | |||||
| 28 | =head1 METHODS | ||||
| 29 | |||||
| 30 | =cut | ||||
| 31 | |||||
| 32 | 2 | 19µs | 2 | 37µs | # spent 24µs (12+12) within PPI::Token::Number::BEGIN@32 which was called:
# once (12µs+12µs) by PPI::Token::BEGIN@42 at line 32 # spent 24µs making 1 call to PPI::Token::Number::BEGIN@32
# spent 12µs making 1 call to strict::import |
| 33 | 2 | 18µs | 1 | 3µs | # spent 3µs within PPI::Token::Number::BEGIN@33 which was called:
# once (3µs+0s) by PPI::Token::BEGIN@42 at line 33 # spent 3µs making 1 call to PPI::Token::Number::BEGIN@33 |
| 34 | |||||
| 35 | 2 | 28µs | 2 | 64µs | # spent 35µs (6+29) within PPI::Token::Number::BEGIN@35 which was called:
# once (6µs+29µs) by PPI::Token::BEGIN@42 at line 35 # spent 35µs making 1 call to PPI::Token::Number::BEGIN@35
# spent 29µs making 1 call to vars::import |
| 36 | # spent 8µs within PPI::Token::Number::BEGIN@36 which was called:
# once (8µs+0s) by PPI::Token::BEGIN@42 at line 39 | ||||
| 37 | 1 | 300ns | $VERSION = '1.215'; | ||
| 38 | 1 | 8µs | @ISA = 'PPI::Token'; | ||
| 39 | 1 | 282µs | 1 | 8µs | } # spent 8µs making 1 call to PPI::Token::Number::BEGIN@36 |
| 40 | |||||
| 41 | =pod | ||||
| 42 | |||||
| 43 | =head2 base | ||||
| 44 | |||||
| 45 | The C<base> method is provided by all of the ::Number subclasses. | ||||
| 46 | This is 10 for decimal, 16 for hexadecimal, 2 for binary, etc. | ||||
| 47 | |||||
| 48 | =cut | ||||
| 49 | |||||
| 50 | sub base { | ||||
| 51 | return 10; | ||||
| 52 | } | ||||
| 53 | |||||
| 54 | =pod | ||||
| 55 | |||||
| 56 | =head2 literal | ||||
| 57 | |||||
| 58 | Return the numeric value of this token. | ||||
| 59 | |||||
| 60 | =cut | ||||
| 61 | |||||
| 62 | sub literal { | ||||
| 63 | return 0 + $_[0]->_literal; | ||||
| 64 | } | ||||
| 65 | |||||
| 66 | sub _literal { | ||||
| 67 | # De-sugar the string representation | ||||
| 68 | my $self = shift; | ||||
| 69 | my $string = $self->content; | ||||
| 70 | $string =~ s/^\+//; | ||||
| 71 | $string =~ s/_//g; | ||||
| 72 | return $string; | ||||
| 73 | } | ||||
| 74 | |||||
| - - | |||||
| 79 | ##################################################################### | ||||
| 80 | # Tokenizer Methods | ||||
| 81 | |||||
| 82 | # spent 38.4ms (17.7+20.7) within PPI::Token::Number::__TOKENIZER__on_char which was called 1170 times, avg 33µs/call:
# 1170 times (17.7ms+20.7ms) by PPI::Tokenizer::_process_next_char at line 554 of PPI/Tokenizer.pm, avg 33µs/call | ||||
| 83 | 1170 | 376µs | my $class = shift; | ||
| 84 | 1170 | 223µs | my $t = shift; | ||
| 85 | 1170 | 763µs | my $char = substr( $t->{line}, $t->{line_cursor}, 1 ); | ||
| 86 | |||||
| 87 | # Allow underscores straight through | ||||
| 88 | 1170 | 291µs | return 1 if $char eq '_'; | ||
| 89 | |||||
| 90 | # Handle the conversion from an unknown to known type. | ||||
| 91 | # The regex covers "potential" hex/bin/octal number. | ||||
| 92 | 1170 | 317µs | my $token = $t->{token}; | ||
| 93 | 1170 | 7.13ms | 1170 | 1.02ms | if ( $token->{content} =~ /^-?0_*$/ ) { # spent 1.02ms making 1170 calls to PPI::Token::Number::CORE:match, avg 869ns/call |
| 94 | # This could be special | ||||
| 95 | 286 | 735µs | 286 | 139µs | if ( $char eq 'x' ) { # spent 139µs making 286 calls to PPI::Token::Number::CORE:match, avg 486ns/call |
| 96 | $t->{class} = $t->{token}->set_class( 'Number::Hex' ); | ||||
| 97 | return 1; | ||||
| 98 | } elsif ( $char eq 'b' ) { | ||||
| 99 | $t->{class} = $t->{token}->set_class( 'Number::Binary' ); | ||||
| 100 | return 1; | ||||
| 101 | } elsif ( $char =~ /\d/ ) { | ||||
| 102 | # You cannot have 8s and 9s on octals | ||||
| 103 | if ( $char eq '8' or $char eq '9' ) { | ||||
| 104 | $token->{_error} = "Illegal character in octal number '$char'"; | ||||
| 105 | } | ||||
| 106 | $t->{class} = $t->{token}->set_class( 'Number::Octal' ); | ||||
| 107 | return 1; | ||||
| 108 | } | ||||
| 109 | } | ||||
| 110 | |||||
| 111 | # Handle the easy case, integer or real. | ||||
| 112 | 1170 | 2.80ms | 1170 | 647µs | return 1 if $char =~ /\d/o; # spent 647µs making 1170 calls to PPI::Token::Number::CORE:match, avg 553ns/call |
| 113 | |||||
| 114 | 982 | 250µs | if ( $char eq '.' ) { | ||
| 115 | 150 | 487µs | 150 | 5.50ms | $t->{class} = $t->{token}->set_class( 'Number::Float' ); # spent 5.50ms making 150 calls to PPI::Token::set_class, avg 37µs/call |
| 116 | 150 | 482µs | return 1; | ||
| 117 | } | ||||
| 118 | 832 | 332µs | if ( $char eq 'e' || $char eq 'E' ) { | ||
| 119 | $t->{class} = $t->{token}->set_class( 'Number::Exp' ); | ||||
| 120 | return 1; | ||||
| 121 | } | ||||
| 122 | |||||
| 123 | # Doesn't fit a special case, or is after the end of the token | ||||
| 124 | # End of token. | ||||
| 125 | 832 | 3.66ms | 1664 | 13.4ms | $t->_finalize_token->__TOKENIZER__on_char( $t ); # spent 11.3ms making 832 calls to PPI::Token::Whitespace::__TOKENIZER__on_char, avg 14µs/call
# spent 2.14ms making 832 calls to PPI::Tokenizer::_finalize_token, avg 3µs/call |
| 126 | } | ||||
| 127 | |||||
| 128 | 1 | 2µs | 1; | ||
| 129 | |||||
| 130 | =pod | ||||
| 131 | |||||
| 132 | =head1 CAVEATS | ||||
| 133 | |||||
| 134 | Compared to Perl, the number tokenizer is too liberal about allowing | ||||
| 135 | underscores anywhere. For example, the following is a syntax error in | ||||
| 136 | Perl, but is allowed in PPI: | ||||
| 137 | |||||
| 138 | 0_b10 | ||||
| 139 | |||||
| 140 | =head1 TO DO | ||||
| 141 | |||||
| 142 | - Treat v-strings as binary strings or barewords, not as "base-256" | ||||
| 143 | numbers | ||||
| 144 | |||||
| 145 | - Break out decimal integers into their own subclass? | ||||
| 146 | |||||
| 147 | - Implement literal() | ||||
| 148 | |||||
| 149 | =head1 SUPPORT | ||||
| 150 | |||||
| 151 | See the L<support section|PPI/SUPPORT> in the main module. | ||||
| 152 | |||||
| 153 | =head1 AUTHOR | ||||
| 154 | |||||
| 155 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||
| 156 | |||||
| 157 | =head1 COPYRIGHT | ||||
| 158 | |||||
| 159 | Copyright 2001 - 2011 Adam Kennedy. | ||||
| 160 | |||||
| 161 | This program is free software; you can redistribute | ||||
| 162 | it and/or modify it under the same terms as Perl itself. | ||||
| 163 | |||||
| 164 | The full text of the license can be found in the | ||||
| 165 | LICENSE file included with this module. | ||||
| 166 | |||||
| 167 | =cut | ||||
# spent 1.80ms within PPI::Token::Number::CORE:match which was called 2626 times, avg 686ns/call:
# 1170 times (1.02ms+0s) by PPI::Token::Number::__TOKENIZER__on_char at line 93, avg 869ns/call
# 1170 times (647µs+0s) by PPI::Token::Number::__TOKENIZER__on_char at line 112, avg 553ns/call
# 286 times (139µs+0s) by PPI::Token::Number::__TOKENIZER__on_char at line 95, avg 486ns/call |