| 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 | 63 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 63 | ▶ | use PPI::Token::Number (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 63 | our $VERSION = '1.260'; # VERSION | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 63 | our @ISA = "PPI::Token::Number"; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =pod | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 base | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Returns the base for the number: 256. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub base() { 256 } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =pod | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head2 literal | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Return the numeric value of this token. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub literal { | ||||||||||||||||||||||||||||
| 59 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
literal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 8 | my $content = $self->{content}; | |||||||||||||||||||||||||||
| 61 | 8 | $content =~ s/^v//; | |||||||||||||||||||||||||||
| 62 | 8 | return join '', map { chr $_ } ( split /\./, $content ); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ##################################################################### | ||||||||||||||||||||||||||||
| 70 | # Tokenizer Methods | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub __TOKENIZER__on_char { | ||||||||||||||||||||||||||||
| 73 | 3085 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
__TOKENIZER__on_char: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 74 | 3085 | my $t = shift; | |||||||||||||||||||||||||||
| 75 | 3085 | my $char = substr( $t->{line}, $t->{line_cursor}, 1 ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Allow digits | ||||||||||||||||||||||||||||
| 78 | 3085 | ▶ | return 1 if $char =~ /\d/o; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1330 | ▶ | return $t->{token}->{content} =~ /\.$/ ? 0 : 1 if $char eq '_'; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Is this a second decimal point in a row? Then the '..' operator | ||||||||||||||||||||||||||||
| 83 | 1148 | ▶ | 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 | 1140 | $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub __TOKENIZER__commit { | ||||||||||||||||||||||||||||
| 103 | 2968 | ▶ | my $t = $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
__TOKENIZER__commit: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Capture the rest of the token | ||||||||||||||||||||||||||||
| 106 | 2968 | pos $t->{line} = $t->{line_cursor}; | |||||||||||||||||||||||||||
| 107 | # This was not a v-string after all (it's a word); | ||||||||||||||||||||||||||||
| 108 | return PPI::Token::Word->__TOKENIZER__commit($t) | ||||||||||||||||||||||||||||
| 109 | 2968 | ▶ | if $t->{line} !~ m/\G(v\d[_\d]*(?:\.\d[_\d]*)+|v\d[_\d]*\b)/gc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2538 | my $content = $1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # If there are no periods this could be a word starting with v\d | ||||||||||||||||||||||||||||
| 114 | # Forced to be a word. Done. | ||||||||||||||||||||||||||||
| 115 | 2538 | ▶ | 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)
| |||||||||||||||||||||||||||||
| 116 | if $content !~ /\./ and $t->__current_token_is_forced_word($content); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # This is a v-string | ||||||||||||||||||||||||||||
| 119 | 2524 | $t->{line_cursor} += length $content; | |||||||||||||||||||||||||||
| 120 | 2524 | $t->_new_token( 'Number::Version', $content ); | |||||||||||||||||||||||||||
| 121 | 2524 | $t->_finalize_token->__TOKENIZER__on_char($t); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 63 | 1; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =pod | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 BUGS | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | - Does not handle leading minus sign correctly. Should translate to a DashedWord. | ||||||||||||||||||||||||||||
| 131 | See L<http://perlmonks.org/?node_id=574573> | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | -95.0.1.0 --> "-_\000\cA\000" | ||||||||||||||||||||||||||||
| 134 | -96.0.1.0 --> Argument "`\0^A\0" isn't numeric in negation (-) | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Chris Dolan E<lt>cdolan@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Copyright 2006 Chris Dolan. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 149 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 152 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||