Tokenized.pm

filter: Statementstmt 78.9% 139 / 176 Branchbran 53.4% 31 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 79.1% 19 / 24 Podpod 83.3% 10 / 12 totaltotal 73.7% 199 / 270 CC 33 SCAR 17.3
CC
33
Cov
73%
setparser
12.4
__ANON__
12.0
__ANON__
7.6
CRAP 5.6
↑ Summary Tokenized/File.pm »
1 package IO::Tokenized;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use vars qw/$VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use constant BUFFER_SIZE => 30 * 1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use constant TOKEN_SEPARATOR => qr/\s/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 $VERSION = '0.04';
15  
16 3 @ISA = qw(Exporter);
17 3 @EXPORT = ();
18 3 @EXPORT_OK = qw(initialize_parsing buffer_space token_separator flushbuffer
19 skip resynch getline getlines gettoken gettokens setparser);
20  
21 3 %EXPORT_TAGS = (all => [@EXPORT_OK],
22 parse => [qw(initialize_parsing gettoken gettokens)],
23 buffer => [qw(bufferspace flushbuffer resynch)]
24 );
25  
26 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
27 1 my $class = shift;
28 1 my ($self,@tokens) = @_;
29 1 ▶ $self = gensym unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 1 1
30 1 bless $self,$class;
31 1 initialize_parsing($self,@tokens);
32 }
33  
34 2 ▶sub initialize_parsing {
Subroutine initialize_parsing: called CC=3 SCAR=11.1
Pod undocumented
35 2 my $self = shift;
36 2 my @tokens = @_;
37 2 @{${*$self}{__PACKAGE__}{definition}} = ();
38 2 ▶ flushbuffer($self) &&
Branch: 1 branch
BranchTrueFalseTotal
flushbuffer($self) and carp("IO::Tokenized(re)initializing parser with not empty buffer") 0 2 2
39 carp __PACKAGE__ ."(re)initializing parser with not empty buffer";
40 2 token_separator($self,TOKEN_SEPARATOR);
41 2 buffer_space($self,BUFFER_SIZE);
42 2 ▶ setparser($self,@tokens) if @tokens;
Branch: 1 branch
BranchTrueFalseTotal
if @tokens 2 0 2
43 2 return $self;
44 }
45  
46 2 ▶sub buffer_space {
Subroutine buffer_space: called CC=3 SCAR=11.1
Pod documented
47 2 my $self = shift;
48  
49 2 ${*$self}{__PACKAGE__}{bufsize} = BUFFER_SIZE
50 2 ▶ unless defined${*$self}{__PACKAGE__}{bufsize};
Branch: 1 branch
BranchTrueFalseTotal
unless defined do { *$self }->{"__PACKAGE__"}{"bufsize"} 2 0 2
51  
52 2 my $oldvalue = ${*$self}{__PACKAGE__}{bufsize};
53 2 ▶ ${*$self}{__PACKAGE__}{bufsize} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
54 2 return $oldvalue;
55 }
56  
57 12 ▶sub token_separator {
Subroutine token_separator: called CC=2 SCAR=6.9
Pod documented
58 12 my $self = shift;
59 12 my $oldvalue = ${*$self}{__PACKAGE__}{token_separator};
60 12 ▶ ${*$self}{__PACKAGE__}{token_separator} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 10 12
61 12 return $oldvalue;
62 }
63  
64 2 ▶sub flushbuffer {
Subroutine flushbuffer: called CC=1 SCAR=0.0
Pod documented
65 2 my $self = shift;
66 2 my $oldvalue = ${*$self}{__PACKAGE__}{buffer};
67 2 ${*$self}{__PACKAGE__}{buffer} = "";
68 2 return $oldvalue;
69 }
70  
71 # tries to read from self until repetedly removing skip prefix until
72 # one of the following is verified:
73 # 1. the buffer is not empty and doesn't start with a skip prefix
74 # 2. the end of file is reached without 1. beeing fulfilled.
75 #
76 # The function returns true in the first case, false in the second
77  
78 10 ▶sub skip {
Subroutine skip: called CC=4 SCAR=13.9
Pod documented
79 10 my $self = shift;
80 10 my $re = token_separator($self);
81 10 my $buffer = \${*$self}{__PACKAGE__}{buffer};
82 10 while (1) {
83 14 ▶ $$buffer = scalar <$self> unless length($$buffer);
Branch: 1 branch
BranchTrueFalseTotal
unless length $$buffer 6 8 14
84 14 ▶ return unless defined $$buffer; #end of file
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$buffer 2 12 14
85 12 $$buffer =~ s/^$re+//;
86 12 ▶ return 1 if length($$buffer);
Branch: 1 branch
BranchTrueFalseTotal
if length $$buffer 8 4 12
87 }
88 }
89  
90 # flushes buffer till the first token, if possible
91 0 ▶sub resynch {
Subroutine resynch: not called CC=1 SCAR=6.9
Pod documented
92 0 my $self = shift;
93 0 my $resyncher = ${*$self}{__PACKAGE__}{resyncher};
94 0 &$resyncher($self);
95 }
96  
97 0 ▶sub getline {
Subroutine getline: not called CC=2 SCAR=17.9
Pod documented
98 0 my $self = shift;
99 0 my $buffer = \${*$self}{__PACKAGE__}{buffer};
100 0 ▶ if ($$buffer =~ s!^(.*?$/)!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffer =~ s[^(.*?$/)][]) { } 0 0 0
101 0 return $1;
102 }
103 else {
104 0 $$buffer .= scalar <$self>;
105 0 flusbuffer($self);
106 }
107 }
108  
109 0 ▶sub getlines {
Subroutine getlines: not called CC=2 SCAR=17.9
Pod documented
110 0 my $self = shift;
111 0 my @lines = (getline($self));
112 0 ▶ push @lines,<$self> unless eof $self;
Branch: 1 branch
BranchTrueFalseTotal
unless eof $self 0 0 0
113 0 return @lines;
114 }
115
116  
117 10 ▶sub gettoken {
Subroutine gettoken: called CC=4 SCAR=14.6
Pod documented
118 10 my $self = shift;
119 10 my $parser = ${*$self}{__PACKAGE__}{parser};
120
121 10 ▶ skip($self) || return;
Branch: 1 branch
BranchTrueFalseTotal
skip($self) or return 8 2 10
122 8 my ($token,$value) = &$parser($self);
123 8 ▶ if ($token eq '<eof>') {
Branch: 2 branches
BranchTrueFalseTotal
if ($token eq '<eof>') { } 0 8 8
elsif ($token eq '<overflow>') { } 0 8 8
124 0 return;
125 }
126 elsif ($token eq '<overflow>') {
127 0 croak "Overflowed buffer with no token found";
128 }
129 else {
130 8 return ($token => $value);
131 }
132 }
133  
134 0 ▶sub gettokens {
Subroutine gettokens: not called CC=2 SCAR=17.9
Pod documented
135 0 my $self = shift;
136 0 my @result;
137 0 ▶ while (my $t = gettoken($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = gettoken($self)) 0 0 0
138 0 push @result,$t;
139 }
140 0 return @result;
141 }
142  
143 2 ▶sub setparser {
Subroutine setparser: called CC=10 SCAR=25.2
Pod documented
144 2 my $self = shift;
145 2 my @oldvalue = @{${*$self}{__PACKAGE__}{definition}};
146 2 @{${*$self}{__PACKAGE__}{definition}} = @_;
147 2 my %regexp = ();
148 2 my %functions = ();
149 2 my @order = ();
150  
151 2 foreach my $definition (@{${*$self}{__PACKAGE__}{definition}}) {
152 6 my ($tok,$re,$func) = @$definition;
153 6 ▶ if ($tok eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok eq "") 0 6 6
154 0 token_separator($self,$re);
155 0 next;
156 }
157 6 ▶ if (exists $regexp{$tok}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $regexp{$tok}) 0 6 6
158 0 carp "token '$tok' redefined!";
159 0 next;
160 }
161 6 push @order,$tok;
162 6 $regexp{$tok} = $re;
163 6 ▶ $functions{$tok} = $func if defined $func;
Branch: 1 branch
BranchTrueFalseTotal
if defined $func 2 4 6
164 }
165 2 ${*$self}{__PACKAGE__}{parser} = eval {
166 2 my @checkers = ();
167 2 foreach my $tok (@order) {
168 6 my $sub = eval {
169 6 ▶ my $func = exists $functions{$tok} ? $functions{$tok} : sub {shift @_};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
exists $functions{$tok} ? : 2 4 6
170 6 my $re = qr/^($regexp{$tok})(.*)/s;
171 6 my $token = $tok;
172  
173 16 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
174 16 my $self = shift;
175 16 my $buffer = \${*$self}{__PACKAGE__}{buffer};
176 16 my @items = ($$buffer =~ $re);
177 16 ▶ return (undef,undef) unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 8 8 16
178 8 my $tmp = pop @items;
179 8 my $value = &$func(@items);
180 8 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 8 8
181 8 $$buffer = $tmp;
182 8 return ($token => $value);
183 }
184 6 };
185 6 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
186 6 push @checkers,$sub;
187 }
188 # now define the parser
189 8 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=20.3
190 8 my $self = shift;
191 8 my $buffer = \${*$self}{__PACKAGE__}{buffer};
192 8 while (1) {
193 8 foreach my $checker (@checkers) {
194 16 my ($token,$value) = &$checker($self);
195 16 ▶ return ($token,$value) if defined $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 8 8 16
196 }
197 # no token matched... we try to extend the buffer by reading
198 # another line but first we check that there is no overflow
199 0 ▶ return ('<overflow>' => undef) unless
Branch: 1 branch
BranchTrueFalseTotal
unless length $$buffer < buffer_space($self) 0 0 0
200 length($$buffer) < buffer_space($self);
201 0 my $line = scalar <$self>;
202 0 ▶ return ('<unparsable>' => undef) unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
203 0 $$buffer .= $line;
204 }
205 2 };
206 };
207 2 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
208 2 my $resynch = "(?=" . join("|",
209 map("(?:$regexp{$_})",@order)) . ")";
210 2 ${*$self}{__PACKAGE__}{resyncher} = eval {
211 2 my $resynch_re = qr/$resynch/os;
212 #the resyncher
213 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
214 0 my $self = shift;
215 0 my $buffer = \${*$self}{__PACKAGE__}{buffer};
216 0 while (1) {
217 0 ▶ return 1 if $$buffer =~ s/^.*?(?=$resynch_re)//;
Branch: 1 branch
BranchTrueFalseTotal
if $$buffer =~ s/^.*?(?=$resynch_re)// 0 0 0
218 0 $$buffer = scalar <$self>;
219 0 ▶ return unless defined $$buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$buffer 0 0 0
220 }
221 }
222 2 };
223 2 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
224 2 return @oldvalue;
225 }
226  
227 3 1;
228 __END__
↑ Summary Tokenized/File.pm »