1 package PPI::Token::Number;
7 PPI::Token::Number - Token class for a number
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 )
25 The C<PPI::Token::Number> class is used for tokens that represent numbers,
26 in the various types that Perl supports.
35 use vars qw{$VERSION @ISA};
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.
58 Return the numeric value of this token.
63 return 0 + $_[0]->_literal;
67 # De-sugar the string representation
69 my $string = $self->content;
79 #####################################################################
82 sub __TOKENIZER__on_char {
85 my $char = substr( $t->{line}, $t->{line_cursor}, 1 );
87 # Allow underscores straight through
88 return 1 if $char eq '_';
90 # Handle the conversion from an unknown to known type.
91 # The regex covers "potential" hex/bin/octal number.
92 my $token = $t->{token};
93 if ( $token->{content} =~ /^-?0_*$/ ) {
94 # This could be special
96 $t->{class} = $t->{token}->set_class( 'Number::Hex' );
98 } elsif ( $char eq 'b' ) {
99 $t->{class} = $t->{token}->set_class( 'Number::Binary' );
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'";
106 $t->{class} = $t->{token}->set_class( 'Number::Octal' );
111 # Handle the easy case, integer or real.
112 return 1 if $char =~ /\d/o;
114 if ( $char eq '.' ) {
115 $t->{class} = $t->{token}->set_class( 'Number::Float' );
118 if ( $char eq 'e' || $char eq 'E' ) {
119 $t->{class} = $t->{token}->set_class( 'Number::Exp' );
123 # Doesn't fit a special case, or is after the end of the token
125 $t->_finalize_token->__TOKENIZER__on_char( $t );
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:
142 - Treat v-strings as binary strings or barewords, not as "base-256"
145 - Break out decimal integers into their own subclass?
147 - Implement literal()
151 See the L<support section|PPI/SUPPORT> in the main module.
155 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
159 Copyright 2001 - 2009 Adam Kennedy.
161 This program is free software; you can redistribute
162 it and/or modify it under the same terms as Perl itself.
164 The full text of the license can be found in the
165 LICENSE file included with this module.