PPI/Token/Number/Exp.pm

filter: Statementstmt 84.7% 39 / 46 Branchbran 81.8% 18 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 82.5% 66 / 80 CC 13 SCAR 19.2
CC
13
Cov
83%
__TOKENIZER__on_char
10.0
literal
7.0
BEGIN
1.0
CRAP 6.8
« PPI/Node.pm ↑ Summary PPI/Statement/Variable.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 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 62 ▶use PPI::Token::Number::Float ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 62 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
35 BEGIN {
36 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 62 @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 9 ▶ my $self = shift;
Subroutine literal: called CC=7 SCAR=19.5
Pod documented
50 9 ▶ return if $self->{_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error"} 0 9 9
51 9 my ($mantissa, $exponent) = split m/e/i, $self->_literal;
52 9 my $neg = $mantissa =~ s/^\-//;
53 9 $mantissa =~ s/^\./0./;
54 9 $exponent =~ s/^\+//;
55  
56 # This algorithm is reasonably close to the S_mulexp10()
57 # algorithm from the Perl source code, so it should arrive
58 # at the same answer as Perl most of the time.
59 9 my $negpow = 0;
60 9 ▶ if ($exponent < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exponent < 0) 2 7 9
61 2 $negpow = 1;
62 2 $exponent *= -1;
63 }
64  
65 9 my $result = 1;
66 9 my $power = 10;
67 9 ▶ for (my $bit = 1; $exponent; $bit = $bit << 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exponent) 22 9 31
68 22 ▶ if ($exponent & $bit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exponent & $bit) 12 10 22
69 12 $exponent = $exponent ^ $bit;
70 12 $result *= $power;
71 }
72 22 $power *= $power;
73 }
74  
75 9 ▶ my $val = $neg ? 0 - $mantissa : $mantissa;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 1 8 9
76 9 ▶ return $negpow ? $val / $result : $val * $result;
Branch: 1 branch
BranchTrueFalseTotal
$negpow ? : 2 7 9
77 }
78  
79  
80  
81  
82  
83 #####################################################################
84 # Tokenizer Methods
85  
86 sub __TOKENIZER__on_char {
87 78 ▶ my $class = shift;
Subroutine __TOKENIZER__on_char: called CC=7 SCAR=23.0
88 78 my $t = shift;
89 78 my $char = substr( $t->{line}, $t->{line_cursor}, 1 );
90  
91 # To get here, the token must have already encountered an 'E'
92  
93 # Allow underscores straight through
94 78 ▶ return 1 if $char eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "_" 9 69 78
95  
96 # Allow digits
97 69 ▶ return 1 if $char =~ /\d/o;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /\d/o 34 35 69
98  
99 # Start of exponent is special
100 35 ▶ if ( $t->{token}->{content} =~ /e$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"token"}{"content"} =~ /e$/i) 14 21 35
101 # Allow leading +/- in exponent
102 14 ▶ return 1 if $char eq '-' || $char eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "-" or $char eq "+" 14 0 14
Condition66%2/3
$char eq "-" or $char eq "+"
l!l&&r!l&&!r
680
MC/DC0%0/2
$char eq "-" or $char eq "+"
$char eq "-" $char eq "+"
Truth table66%2/3
A or B
A= $char eq "-"B= $char eq "+"
ABresult
1X1
011
000
103  
104 # Invalid character in exponent. Recover
105 0 ▶ if ( $t->{token}->{content} =~ s/\.(e)$//i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'token'}{'content'} =~ s/\.(e)$//i) { } 0 0 0
106 0 my $word = $1;
107 0 $t->{class} = $t->{token}->set_class('Number');
108 0 $t->_new_token('Operator', '.');
109 0 $t->_new_token('Word', $word);
110 0 return $t->{class}->__TOKENIZER__on_char( $t );
111 }
112 else {
113 0 $t->{token}->{_error} = "Illegal character in exponent '$char'";
114 }
115 }
116  
117 # Doesn't fit a special case, or is after the end of the token
118 # End of token.
119 21 $t->_finalize_token->__TOKENIZER__on_char( $t );
120 }
121  
122 62 1;
123  
124 =pod
125  
126 =head1 SUPPORT
127  
128 See the L<support section|PPI/SUPPORT> in the main module.
129  
130 =head1 AUTHOR
131  
132 Chris Dolan E<lt>cdolan@cpan.orgE<gt>
133  
134 =head1 COPYRIGHT
135  
136 Copyright 2006 Chris Dolan.
137  
138 This program is free software; you can redistribute
139 it and/or modify it under the same terms as Perl itself.
140  
141 The full text of the license can be found in the
142 LICENSE file included with this module.
143  
144 =cut
« PPI/Node.pm ↑ Summary PPI/Statement/Variable.pm »