| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | 2 | my $oldvalue = ${*$self}{__PACKAGE__}{bufsize}; | |||||||||||||
| 53 | 2 | ▶ | ${*$self}{__PACKAGE__}{bufsize} = shift if @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 84 | 14 | ▶ | return unless defined $$buffer; #end of file | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 12 | $$buffer =~ s/^$re+//; | |||||||||||||
| 86 | 12 | ▶ | return 1 if length($$buffer); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 122 | 8 | my ($token,$value) = &$parser($self); | |||||||||||||
| 123 | 8 | ▶ | if ($token eq '<eof>') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 154 | 0 | token_separator($self,$re); | |||||||||||||
| 155 | 0 | next; | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | 6 | ▶ | if (exists $regexp{$tok}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 178 | 8 | my $tmp = pop @items; | |||||||||||||
| 179 | 8 | my $value = &$func(@items); | |||||||||||||
| 180 | 8 | ▶ | return unless defined $value; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 181 | 8 | $$buffer = $tmp; | |||||||||||||
| 182 | 8 | return ($token => $value); | |||||||||||||
| 183 | } | ||||||||||||||
| 184 | 6 | }; | |||||||||||||
| 185 | 6 | ▶ | croak $@ if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 200 | length($$buffer) < buffer_space($self); | ||||||||||||||
| 201 | 0 | my $line = scalar <$self>; | |||||||||||||
| 202 | 0 | ▶ | return ('<unparsable>' => undef) unless defined $line; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 203 | 0 | $$buffer .= $line; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | 2 | }; | |||||||||||||
| 206 | }; | ||||||||||||||
| 207 | 2 | ▶ | croak $@ if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 218 | 0 | $$buffer = scalar <$self>; | |||||||||||||
| 219 | 0 | ▶ | return unless defined $$buffer; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 220 | } | ||||||||||||||
| 221 | } | ||||||||||||||
| 222 | 2 | }; | |||||||||||||
| 223 | 2 | ▶ | croak $@ if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 224 | 2 | return @oldvalue; | |||||||||||||
| 225 | } | ||||||||||||||
| 226 | |||||||||||||||
| 227 | 3 | 1; | |||||||||||||
| 228 | __END__ | ||||||||||||||