PPI/Token/Number.pm

filter: Statementstmt 100% 41 / 41 Branchbran 100% 18 / 18 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 100% 87 / 87 CC 14 SCAR 23.1
CC
14
Cov
100%
__TOKENIZER__on_char
14.0
BEGIN
1.0
BEGIN
1.0
CRAP 10.1
« PPI/Document.pm ↑ Summary PPI/Token/Pod.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 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 62 ▶use PPI::Token ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 62 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
36 BEGIN {
37 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 62 @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() { 10 }
51  
52 =pod
53  
54 =head2 literal
55  
56 Return the numeric value of this token.
57  
58 =cut
59  
60 sub literal {
61 4 ▶ return 0 + $_[0]->_literal;
Subroutine literal: called CC=1 SCAR=0.0
Pod documented
62 }
63  
64 sub _literal {
65 # De-sugar the string representation
66 70 ▶ my $self = shift;
Subroutine _literal: called CC=1 SCAR=0.0
67 70 my $string = $self->content;
68 70 $string =~ s/^\+//;
69 70 $string =~ s/_//g;
70 70 return $string;
71 }
72  
73  
74  
75  
76  
77 #####################################################################
78 # Tokenizer Methods
79  
80 sub __TOKENIZER__on_char {
81 28968 ▶ my $class = shift;
Subroutine __TOKENIZER__on_char: called CC=14 SCAR=26.4
82 28968 my $t = shift;
83 28968 my $char = substr( $t->{line}, $t->{line_cursor}, 1 );
84  
85 # Allow underscores straight through
86 28968 ▶ return 1 if $char eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "_" 42 28926 28968
87  
88 # Handle the conversion from an unknown to known type.
89 # The regex covers "potential" hex/bin/octal number.
90 28926 my $token = $t->{token};
91 28926 ▶ if ( $token->{content} =~ /^-?0_*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"content"} =~ /^-?0_*$/) 2481 26445 28926
92 # This could be special
93 2481 ▶ if ( $char eq 'x' || $char eq 'X' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq 'x' or $char eq 'X') { } 51 2430 2481
elsif ($char eq 'b' or $char eq 'B') { } 26 2404 2430
elsif ($char =~ /\d/) { } 33 2371 2404
Condition100%3/3
$char eq "x" or $char eq "X"
l!l&&r!l&&!r
32192430
MC/DC100%2/2
$char eq "x" or $char eq "X"
$char eq "x" $char eq "X"
Truth table100%3/3
A or B
A= $char eq "x"B= $char eq "X"
ABresult
1X1
011
000
94 51 $t->{class} = $t->{token}->set_class( 'Number::Hex' );
95 51 return 1;
96 ▶ } elsif ( $char eq 'b' || $char eq 'B' ) {
Condition100%3/3
$char eq "b" or $char eq "B"
l!l&&r!l&&!r
2242404
MC/DC100%2/2
$char eq "b" or $char eq "B"
$char eq "b" $char eq "B"
Truth table100%3/3
A or B
A= $char eq "b"B= $char eq "B"
ABresult
1X1
011
000
97 26 $t->{class} = $t->{token}->set_class( 'Number::Binary' );
98 26 return 1;
99 } elsif ( $char =~ /\d/ ) {
100 # You cannot have 8s and 9s on octals
101 33 ▶ if ( $char eq '8' or $char eq '9' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq 8 or $char eq 9) 15 18 33
Condition100%3/3
$char eq 8 or $char eq 9
l!l&&r!l&&!r
6918
MC/DC100%2/2
$char eq 8 or $char eq 9
$char eq 8 $char eq 9
Truth table100%3/3
A or B
A= $char eq 8B= $char eq 9
ABresult
1X1
011
000
102 15 $token->{_error} = "Illegal character in octal number '$char'";
103 }
104 33 $t->{class} = $t->{token}->set_class( 'Number::Octal' );
105 33 return 1;
106 }
107 }
108  
109 # Handle the easy case, integer or real.
110 28816 ▶ return 1 if $char =~ /\d/o;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /\d/o 12073 16743 28816
111  
112 16743 ▶ if ( $char eq '.' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq ".") 1872 14871 16743
113 1872 $t->{class} = $t->{token}->set_class( 'Number::Float' );
114 1872 return 1;
115 }
116 14871 ▶ if ( $char eq 'e' || $char eq 'E' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "e" or $char eq "E") 9 14862 14871
Condition100%3/3
$char eq "e" or $char eq "E"
l!l&&r!l&&!r
3614862
MC/DC100%2/2
$char eq "e" or $char eq "E"
$char eq "e" $char eq "E"
Truth table100%3/3
A or B
A= $char eq "e"B= $char eq "E"
ABresult
1X1
011
000
117 9 $t->{class} = $t->{token}->set_class( 'Number::Exp' );
118 9 return 1;
119 }
120  
121 # Doesn't fit a special case, or is after the end of the token
122 # End of token.
123 14862 $t->_finalize_token->__TOKENIZER__on_char( $t );
124 }
125  
126 62 1;
127  
128 =pod
129  
130 =head1 CAVEATS
131  
132 Compared to Perl, the number tokenizer is too liberal about allowing
133 underscores anywhere. For example, the following is a syntax error in
134 Perl, but is allowed in PPI:
135  
136 0_b10
137  
138 =head1 TO DO
139  
140 - Treat v-strings as binary strings or barewords, not as "base-256"
141 numbers
142  
143 - Break out decimal integers into their own subclass?
144  
145 - Implement literal()
146  
147 =head1 SUPPORT
148  
149 See the L<support section|PPI/SUPPORT> in the main module.
150  
151 =head1 AUTHOR
152  
153 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
154  
155 =head1 COPYRIGHT
156  
157 Copyright 2001 - 2011 Adam Kennedy.
158  
159 This program is free software; you can redistribute
160 it and/or modify it under the same terms as Perl itself.
161  
162 The full text of the license can be found in the
163 LICENSE file included with this module.
164  
165 =cut
« PPI/Document.pm ↑ Summary PPI/Token/Pod.pm »