| 1 | package PPI::Token::Number::Version; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | PPI::Token::Number::Version - Token class for a byte-packed number | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | $n = 1.1.0; | ||||||||||||||||||||||||||||
| 12 | $n = 127.0.0.1; | ||||||||||||||||||||||||||||
| 13 | $n = 10_000.10_000.10_000; | ||||||||||||||||||||||||||||
| 14 | $n = v1.2.3.4 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | PPI::Token::Number::Version | ||||||||||||||||||||||||||||
| 19 | isa PPI::Token::Number | ||||||||||||||||||||||||||||
| 20 | isa PPI::Token | ||||||||||||||||||||||||||||
| 21 | isa PPI::Element | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | The C<PPI::Token::Number::Version> class is used for tokens that have | ||||||||||||||||||||||||||||
| 26 | multiple decimal points. In truth, these aren't treated like numbers | ||||||||||||||||||||||||||||
| 27 | at all by Perl, but they look like numbers to a parser. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 METHODS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 62 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 62 | ▶ | use PPI::Token::Number (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 62 | ▶ | use vars qw{$VERSION @ISA}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | BEGIN { | ||||||||||||||||||||||||||||
| 38 | 62 | ▶ | $VERSION = '1.236'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 62 | @ISA = 'PPI::Token::Number'; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =pod | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head2 base | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Returns the base for the number: 256. | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub base() { 256 } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =pod | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head2 literal | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Return the numeric value of this token. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub literal { | ||||||||||||||||||||||||||||
| 61 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
literal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 8 | my $content = $self->{content}; | |||||||||||||||||||||||||||
| 63 | 8 | $content =~ s/^v//; | |||||||||||||||||||||||||||
| 64 | 8 | return join '', map { chr $_ } ( split /\./, $content ); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | ##################################################################### | ||||||||||||||||||||||||||||
| 72 | # Tokenizer Methods | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub __TOKENIZER__on_char { | ||||||||||||||||||||||||||||
| 75 | 1775 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
__TOKENIZER__on_char: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 76 | 1775 | my $t = shift; | |||||||||||||||||||||||||||
| 77 | 1775 | my $char = substr( $t->{line}, $t->{line_cursor}, 1 ); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Allow digits | ||||||||||||||||||||||||||||
| 80 | 1775 | ▶ | return 1 if $char =~ /\d/o; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Is this a second decimal point in a row? Then the '..' operator | ||||||||||||||||||||||||||||
| 83 | 769 | ▶ | if ( $char eq '.' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 8 | ▶ | if ( $t->{token}->{content} =~ /\.$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | # We have a .., which is an operator. | ||||||||||||||||||||||||||||
| 86 | # Take the . off the end of the token.. | ||||||||||||||||||||||||||||
| 87 | # and finish it, then make the .. operator. | ||||||||||||||||||||||||||||
| 88 | 2 | chop $t->{token}->{content}; | |||||||||||||||||||||||||||
| 89 | 2 | $t->{class} = $t->{token}->set_class( 'Number::Float' ); | |||||||||||||||||||||||||||
| 90 | 2 | $t->_new_token('Operator', '..'); | |||||||||||||||||||||||||||
| 91 | 2 | return 0; | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 6 | return 1; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Doesn't fit a special case, or is after the end of the token | ||||||||||||||||||||||||||||
| 98 | # End of token. | ||||||||||||||||||||||||||||
| 99 | 761 | $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub __TOKENIZER__commit { | ||||||||||||||||||||||||||||
| 103 | 2894 | ▶ | my $t = $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
__TOKENIZER__commit: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Capture the rest of the token | ||||||||||||||||||||||||||||
| 106 | 2894 | pos $t->{line} = $t->{line_cursor}; | |||||||||||||||||||||||||||
| 107 | 2894 | ▶ | if ( $t->{line} !~ m/\G(v\d+(?:\.\d+)+|v\d+\b)/gc ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | # This was not a v-string after all (it's a word) | ||||||||||||||||||||||||||||
| 109 | 623 | return PPI::Token::Word->__TOKENIZER__commit($t); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2271 | my $content = $1; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # If there are no periods this could be a word starting with v\d | ||||||||||||||||||||||||||||
| 115 | # Forced to be a word. Done. | ||||||||||||||||||||||||||||
| 116 | 2271 | ▶ | return PPI::Token::Word->__TOKENIZER__commit($t) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $content =~ /\./ and $t->__current_token_is_forced_word($content)
MC/DC100%2/2
not $content =~ /\./ and $t->__current_token_is_forced_word($content)
not $content =~ /\./
$t->__current_token_is_forced_word($content)
Truth table100%3/3
not(A) and B
A= not $content =~ /\./B= $t->__current_token_is_forced_word($content)
| |||||||||||||||||||||||||||||
| 117 | if $content !~ /\./ and $t->__current_token_is_forced_word($content); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # This is a v-string | ||||||||||||||||||||||||||||
| 120 | 2257 | $t->{line_cursor} += length $content; | |||||||||||||||||||||||||||
| 121 | 2257 | $t->_new_token( 'Number::Version', $content ); | |||||||||||||||||||||||||||
| 122 | 2257 | $t->_finalize_token->__TOKENIZER__on_char($t); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 62 | 1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =pod | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 BUGS | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | - Does not handle leading minus sign correctly. Should translate to a DashedWord. | ||||||||||||||||||||||||||||
| 132 | See L<http://perlmonks.org/?node_id=574573> | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | -95.0.1.0 --> "-_\000\cA\000" | ||||||||||||||||||||||||||||
| 135 | -96.0.1.0 --> Argument "`\0^A\0" isn't numeric in negation (-) | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Chris Dolan E<lt>cdolan@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Copyright 2006 Chris Dolan. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 150 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 153 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||