Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / PPI / Token / Number / Exp.pm
1 package PPI::Token::Number::Exp;
2
3 =pod
4
5 =head1 NAME
6
7 PPI::Token::Number::Exp - Token class for an exponential notation number
8
9 =head1 SYNOPSIS
10
11   $n = 1.0e-2;
12   $n = 1e+2;
13
14 =head1 INHERITANCE
15
16   PPI::Token::Number::Exp
17   isa PPI::Token::Number::Float
18       isa PPI::Token::Number
19           isa PPI::Token
20               isa PPI::Element
21
22 =head1 DESCRIPTION
23
24 The C<PPI::Token::Number::Exp> class is used for tokens that
25 represent floating point numbers with exponential notation.
26
27 =head1 METHODS
28
29 =cut
30
31 use strict;
32 use PPI::Token::Number::Float ();
33
34 use vars qw{$VERSION @ISA};
35 BEGIN {
36         $VERSION = '1.206';
37         @ISA     = 'PPI::Token::Number::Float';
38 }
39
40 =pod
41
42 =head2 literal
43
44 Return the numeric value of this token.
45
46 =cut
47
48 sub literal {
49         my $self = shift;
50         return if $self->{_error};
51         my ($mantissa, $exponent) = split m/e/i, $self->_literal;
52         my $neg = $mantissa =~ s/^\-//;
53         $mantissa =~ s/^\./0./;
54         $exponent =~ s/^\+//;
55         my $val = $mantissa * 10 ** $exponent;
56         return $neg ? -$val : $val;
57 }
58
59
60
61
62
63 #####################################################################
64 # Tokenizer Methods
65
66 sub __TOKENIZER__on_char {
67         my $class = shift;
68         my $t     = shift;
69         my $char  = substr( $t->{line}, $t->{line_cursor}, 1 );
70
71         # To get here, the token must have already encountered an 'E'
72
73         # Allow underscores straight through
74         return 1 if $char eq '_';
75
76         # Allow digits
77         return 1 if $char =~ /\d/o;
78
79         # Start of exponent is special
80         if ( $t->{token}->{content} =~ /e$/i ) {
81                 # Allow leading +/- in exponent
82                 return 1 if $char eq '-' || $char eq '+';
83
84                 # Invalid character in exponent.  Recover
85                 if ( $t->{token}->{content} =~ s/\.(e)$//i ) {
86                         my $word = $1;
87                         $t->{class} = $t->{token}->set_class('Number');
88                         $t->_new_token('Operator', '.');
89                         $t->_new_token('Word', $word);
90                         return $t->{class}->__TOKENIZER__on_char( $t );
91                 }
92                 else {
93                         $t->{token}->{_error} = "Illegal character in exponent '$char'";
94                 }
95         }
96
97         # Doesn't fit a special case, or is after the end of the token
98         # End of token.
99         $t->_finalize_token->__TOKENIZER__on_char( $t );
100 }
101
102 1;
103
104 =pod
105
106 =head1 SUPPORT
107
108 See the L<support section|PPI/SUPPORT> in the main module.
109
110 =head1 AUTHOR
111
112 Chris Dolan E<lt>cdolan@cpan.orgE<gt>
113
114 =head1 COPYRIGHT
115
116 Copyright 2006 Chris Dolan.
117
118 This program is free software; you can redistribute
119 it and/or modify it under the same terms as Perl itself.
120
121 The full text of the license can be found in the
122 LICENSE file included with this module.
123
124 =cut