Highlife/Tokenizer.pm

filter: Statementstmt 18.3% 16 / 87 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 50.0% 5 / 10 Podpod 0.0% 0 / 4 totaltotal 12.0% 21 / 175 CC 27 SCAR 53.5
CC
27
Cov
11%
token
380.0
tokenize
42.0
get_token
20.0
CRAP 210.0
↑ Summary Highlife/Parser.pm »
1 package Parse::Highlife::Tokenizer;
2  
3 1 ▶use Parse::Highlife::Utils qw(params offset_to_coordinate get_source_info extend_match);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Parse::Highlife::Token::Regex;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Parse::Highlife::Token::Delimited;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Parse::Highlife::Token::Characters;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new
11 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
12 0 my( $class, @args ) = @_;
13 0 my $self = bless {}, $class;
14 0 return $self -> _init( @args );
15 }
16  
17 sub _init
18 0 ▶{
Subroutine _init: not called CC=1 SCAR=6.9
19 0 my( $self, @args ) = @_;
20 0 $self->{'tokens'} = [];
21 0 $self->{'tokennames'} = []; # to preserve order
22 0 $self->{'debug'} = 1;
23 0 return $self;
24 }
25  
26 sub get_token
27 0 ▶{
Subroutine get_token: not called CC=4 SCAR=30.0
Pod undocumented
28 0 my( $self, $tokenname ) = @_;
29 0 my $pos = -1;
30 0 my $p = 0;
31 0 ▶ for( my $p = 0; $p < scalar @{$self->{'tokennames'}}; $p++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < scalar @{$self->{"tokennames"};}) 0 0 0
32 0 ▶ if( $self->{'tokennames'}->[$p] eq $tokenname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tokennames"}[$p] eq $tokenname) 0 0 0
33 0 $pos = $p;
34 0 last;
35 }
36 }
37 0 ▶ die "ERR: I do not know about a token named '$tokenname'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $pos == -1 0 0 0
38 if $pos == -1;
39 0 return $self->{'tokens'}->[$pos];
40 }
41  
42 sub token
43 0 ▶{
Subroutine token: not called CC=19 SCAR=59.4
Pod undocumented
44 0 my( $self, $name, $regex, $start, $end, $escape, $characters )
45 = params( \@_,
46 -name => '',
47 -regex => '',
48 -start => '',
49 -end => '',
50 -escape => "\\",
51 -characters => '',
52 );
53 0 my @args = splice( @_, 1 );
54  
55 0 ▶ die "ERR: token has no name.\n" unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 0 0
56  
57 0 my $token;
58  
59 # try to find a same token definition that can be reused
60 0 my $already_defined = 0;
61 0 foreach my $t ( @{$self->{'tokens'}} ) {
62 0 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (length $regex and ref $t eq "Parse::Highlife::Token::Regex" and $t->{"regex"} eq $regex or length $start and length $end and ref $t eq "Parse::Highlife::Token::Delimited" and $t->{"start"} eq $start and $t->{"end"} eq $end or length $characters and ref $t eq "Parse::Highlife::Token::Characters" and $t->{"characters"} eq $characters) 0 0 0
Condition0%0/30
length $regex and ref $t eq "Parse::Highlife::Token::Regex" and $t->{"regex"} eq $regex or length $start and length $end and ref $t eq "Parse::Highlife::Token::Delimited" and $t->{"start"} eq $start and $t->{"end"} eq $end or length $characters and ref $t eq "Parse::Highlife::Token::Characters" and $t->{"characters"} eq $characters
l!l&&r!l&&!r
000
length $regex and ref $t eq "Parse::Highlife::Token::Regex" and $t->{"regex"} eq $regex or length $start and length $end and ref $t eq "Parse::Highlife::Token::Delimited" and $t->{"start"} eq $start and $t->{"end"} eq $end
l!l&&r!l&&!r
000
length $regex and ref $t eq "Parse::Highlife::Token::Regex" and $t->{"regex"} eq $regex
!ll&&!rl&&r
000
length $regex and ref $t eq "Parse::Highlife::Token::Regex"
!ll&&!rl&&r
000
length $start and length $end and ref $t eq "Parse::Highlife::Token::Delimited" and $t->{"start"} eq $start and $t->{"end"} eq $end
!ll&&!rl&&r
000
length $start and length $end and ref $t eq "Parse::Highlife::Token::Delimited" and $t->{"start"} eq $start
!ll&&!rl&&r
000
length $start and length $end and ref $t eq "Parse::Highlife::Token::Delimited"
!ll&&!rl&&r
000
length $start and length $end
!ll&&!rl&&r
000
length $characters and ref $t eq "Parse::Highlife::Token::Characters" and $t->{"characters"} eq $characters
!ll&&!rl&&r
000
length $characters and ref $t eq "Parse::Highlife::Token::Characters"
!ll&&!rl&&r
000
MC/DC0%0/11
length $regex and ref $t eq "Parse::Highlife::Token::Regex" and $t->{"regex"} eq $regex or length $start and length $end and ref $t eq "Parse::Highlife::Token::Delimited" and $t->{"start"} eq $start and $t->{"end"} eq $end or length $characters and ref $t eq "Parse::Highlife::Token::Characters" and $t->{"characters"} eq $characters
length $regex ref $t eq "Parse::Highlife::Token::Regex" $t->{"regex"} eq $regex length $start length $end ref $t eq "Parse::Highlife::Token::Delimited" $t->{"start"} eq $start $t->{"end"} eq $end length $characters ref $t eq "Parse::Highlife::Token::Characters" $t->{"characters"} eq $characters
Truth table0%0/64
A and B and C or D and E and F and G and H or I and J and K
A= length $regexB= ref $t eq "Parse::Highlife::Token::Regex"C= $t->{"regex"} eq $regexD= length $startE= length $endF= ref $t eq "Parse::Highlife::Token::Delimited"G= $t->{"start"} eq $startH= $t->{"end"} eq $endI= length $charactersJ= ref $t eq "Parse::Highlife::Token::Characters"K= $t->{"characters"} eq $characters
ABCDEFGHIJKresult
111XXXXXXXX1
0XX11111XXX1
10X11111XXX1
11011111XXX1
0XX0XXXX1111
0XX10XXX1111
0XX110XX1111
0XX1110X1111
0XX111101111
10X0XXXX1111
10X10XXX1111
10X110XX1111
10X1110X1111
10X111101111
1100XXXX1111
11010XXX1111
110110XX1111
1101110X1111
110111101111
0XX0XXXX0XX0
0XX0XXXX10X0
0XX0XXXX1100
0XX10XXX0XX0
0XX10XXX10X0
0XX10XXX1100
0XX110XX0XX0
0XX110XX10X0
0XX110XX1100
0XX1110X0XX0
0XX1110X10X0
0XX1110X1100
0XX111100XX0
0XX1111010X0
0XX111101100
10X0XXXX0XX0
10X0XXXX10X0
10X0XXXX1100
10X10XXX0XX0
10X10XXX10X0
10X10XXX1100
10X110XX0XX0
10X110XX10X0
10X110XX1100
10X1110X0XX0
10X1110X10X0
10X1110X1100
10X111100XX0
10X1111010X0
10X111101100
1100XXXX0XX0
1100XXXX10X0
1100XXXX1100
11010XXX0XX0
11010XXX10X0
11010XXX1100
110110XX0XX0
110110XX10X0
110110XX1100
1101110X0XX0
1101110X10X0
1101110X1100
110111100XX0
1101111010X0
110111101100
63 ( length $regex &&
64 ref $t eq 'Parse::Highlife::Token::Regex' &&
65 $t->{'regex'} eq $regex )
66 ||
67 ( length $start && length $end &&
68 ref $t eq 'Parse::Highlife::Token::Delimited' &&
69 $t->{'start'} eq $start &&
70 $t->{'end'} eq $end )
71 ||
72 ( length $characters &&
73 ref $t eq 'Parse::Highlife::Token::Characters' &&
74 $t->{'characters'} eq $characters )
75 )
76 {
77 0 $token = $t;
78 0 $already_defined = 1;
79 0 last;
80 }
81 }
82  
83 0 ▶ if( ! $already_defined ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($already_defined) 0 0 0
84
85 0 ▶ if( length $regex ) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $regex) { } 0 0 0
elsif (length $start and length $end) { } 0 0 0
elsif (length $characters) { } 0 0 0
86 0 $token = Parse::Highlife::Token::Regex -> new( @args );
87 }
88 ▶ elsif( length $start && length $end ) {
Condition0%0/3
length $start and length $end
!ll&&!rl&&r
000
MC/DC0%0/2
length $start and length $end
length $start length $end
Truth table0%0/3
A and B
A= length $startB= length $end
ABresult
0X0
100
111
89 0 $token = Parse::Highlife::Token::Delimited -> new( @args );
90 }
91 elsif( length $characters ) {
92 0 $token = Parse::Highlife::Token::Characters -> new( @args );
93 }
94 else {
95 0 die "ERR: incomplete token definition.\n";
96 }
97
98 0 $token->{'name'} = $name;
99
100 0 push @{$self->{'tokens'}}, $token;
101 0 push @{$self->{'tokennames'}}, $name;
102 }
103 0 return $token;
104 }
105  
106 sub tokenize
107 0 ▶{
Subroutine tokenize: not called CC=6 SCAR=37.4
Pod undocumented
108 0 my( $self, $string ) = @_;
109 0 my $tokens = [];
110
111 0 my $i = 0;
112 0 my $unknown_characters = '';
113 0 ▶ while( $i < length $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $string) 0 0 0
114 # find the first matching token
115 0 my $found = 0;
116 0 my $match;
117 0 ▶ for( my $t = 0; $t < @{$self->{'tokens'}}; $t++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t < @{$self->{"tokens"};}) 0 0 0
118 0 my $tokenname = $self->{'tokennames'}->[$t];
119 0 my $token = $self->{'tokens'}->[$t];
120 0 $match = $token -> match( $string, $i ); # returns 0 oder hash with info
121 0 ▶ if( $match ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 0 0 0
122 0 $match->{'token-name'} = $tokenname; # only the Tokenizer knows this
123 0 $match->{'is-ignored'} = $token -> is_ignored();
124 0 $i = $match->{'offset-after-match'};
125 0 $found = 1;
126 0 last;
127 }
128 }
129 0 ▶ if( $found ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) { } 0 0 0
130 # save unknown token
131 0 ▶ if( length $unknown_characters ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $unknown_characters) 0 0 0
132 0 my $unknown =
133 extend_match(
134 $string,
135 {
136 'token-classname' => 'Parse::Highlife::Token::Unknown',
137 'matched-substring' => $unknown_characters,
138 'first-offset' => $i - length( $unknown_characters ),
139 'token-name' => '',
140 }
141 );
142 0 $unknown->{'is-ignored'} = 1; # unknown tokens are ignored (good?)
143 0 push @{$tokens}, $unknown;
144 0 $unknown_characters = '';
145 }
146 0 push @{$tokens}, $match;
147 }
148 else {
149 0 $unknown_characters .= substr $string, $i, 1;
150 0 $i ++;
151
152 #my( $line, $column ) = offset_to_coordinate( $string, $i );
153 #print "ERR: could not find a matching token at line $line, column $column:\n\n";
154 #print get_source_info( $string, $i );
155 #exit;
156 }
157 }
158 0 return $tokens;
159 }
160  
161 1 1;
↑ Summary Highlife/Parser.pm »