File Coverage

blib/lib/PPI/Token/Number/Binary.pm
Criterion Covered Total %
statement 24 24 100.0
branch 9 10 90.0
condition 3 3 100.0
subroutine 4 4 100.0
pod 1 1 100.0
total 41 42 97.6


line stmt bran cond sub pod time code
1             package PPI::Token::Number::Binary;
2              
3             =pod
4              
5             =head1 NAME
6              
7             PPI::Token::Number::Binary - Token class for a binary number
8              
9             =head1 SYNOPSIS
10              
11             $n = 0b1110011; # binary integer
12              
13             =head1 INHERITANCE
14              
15             PPI::Token::Number::Binary
16             isa PPI::Token::Number
17             isa PPI::Token
18             isa PPI::Element
19              
20             =head1 DESCRIPTION
21              
22             The C class is used for tokens that
23             represent base-2 numbers.
24              
25             =head1 METHODS
26              
27             =cut
28              
29 65     65   372 use strict;
  65         141  
  65         1492  
30 65     65   289 use PPI::Token::Number ();
  65         116  
  65         21590  
31              
32             our $VERSION = '1.276';
33              
34             our @ISA = "PPI::Token::Number";
35              
36             =pod
37              
38             =head2 base
39              
40             Returns the base for the number: 2.
41              
42             =cut
43              
44             sub base() { 2 }
45              
46             =pod
47              
48             =head2 literal
49              
50             Return the numeric value of this token.
51              
52             =cut
53              
54             sub literal {
55 16     16 1 6480 my $self = shift;
56 16 100       51 return if $self->{_error};
57 11         26 my $str = $self->_literal;
58 11         18 my $neg = $str =~ s/^\-//;
59 11         36 $str =~ s/^0[bB]//;
60 11         13 my $val = 0;
61 11         38 for my $bit ( $str =~ m/(.)/g ) {
62 17         43 $val = $val * 2 + $bit;
63             }
64 11 50       49 return $neg ? -$val : $val;
65             }
66              
67              
68              
69              
70              
71             #####################################################################
72             # Tokenizer Methods
73              
74             sub __TOKENIZER__on_char {
75 67     67   91 my $class = shift;
76 67         86 my $t = shift;
77 67         143 my $char = substr( $t->{line}, $t->{line_cursor}, 1 );
78              
79             # Allow underscores straight through
80 67 100       117 return 1 if $char eq '_';
81              
82 59 100       140 if ( $char =~ /[\w\d]/ ) {
83 33 100 100     113 unless ( $char eq '1' or $char eq '0' ) {
84             # Add a warning if it contains non-binary chars
85 9         27 $t->{token}->{_error} = "Illegal character in binary number '$char'";
86             }
87 33         78 return 1;
88             }
89              
90             # Doesn't fit a special case, or is after the end of the token
91             # End of token.
92 26         55 $t->_finalize_token->__TOKENIZER__on_char( $t );
93             }
94              
95             1;
96              
97             =pod
98              
99             =head1 SUPPORT
100              
101             See the L in the main module.
102              
103             =head1 AUTHOR
104              
105             Chris Dolan Ecdolan@cpan.orgE
106              
107             =head1 COPYRIGHT
108              
109             Copyright 2006 Chris Dolan.
110              
111             This program is free software; you can redistribute
112             it and/or modify it under the same terms as Perl itself.
113              
114             The full text of the license can be found in the
115             LICENSE file included with this module.
116              
117             =cut