Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / PPI / Token / Number.pm
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 use strict;
33 use PPI::Token ();
34
35 use vars qw{$VERSION @ISA};
36 BEGIN {
37         $VERSION = '1.206';
38         @ISA     = 'PPI::Token';
39 }
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
75
76
77
78
79 #####################################################################
80 # Tokenizer Methods
81
82 sub __TOKENIZER__on_char {
83         my $class = shift;
84         my $t     = shift;
85         my $char  = substr( $t->{line}, $t->{line_cursor}, 1 );
86
87         # Allow underscores straight through
88         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         my $token = $t->{token};
93         if ( $token->{content} =~ /^-?0_*$/ ) {
94                 # This could be special
95                 if ( $char eq 'x' ) {
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         return 1 if $char =~ /\d/o;
113
114         if ( $char eq '.' ) {
115                 $t->{class} = $t->{token}->set_class( 'Number::Float' );
116                 return 1;
117         }
118         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         $t->_finalize_token->__TOKENIZER__on_char( $t );
126 }
127
128 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 - 2009 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