TestML1/Compiler/Lite.pm

filter: Statementstmt 94.1% 144 / 153 Branchbran 76.2% 61 / 80 Conditioncond 85.1% 23 / 27 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 11 totaltotal 83.3% 251 / 301 CC 53 SCAR 21.2
CC
53
Cov
88%
compile_data
13.7
parse_expression
13.0
parse_assertion
8.2
CRAP 8.3
« TestML1/Compiler.pm ↑ Summary TestML1/Runtime.pm »
1 package TestML1::Compiler::Lite;
2  
3 5 ▶use TestML1::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 extends 'TestML1::Compiler';
5  
6 5 ▶use TestML1::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 has input => ();
9 5 has points => ();
10 5 has tokens => ();
11 5 has function => ();
12  
13 5 my $WS = qr!\s+!;
14 5 my $ANY = qr!.!;
15 5 my $STAR = qr!\*!;
16 5 my $NUM = qr!-?[0-9]+!;
17 5 my $WORD = qr![-\w]+!;
18 5 my $HASH = qr!#!;
19 5 my $EQ = qr!=!;
20 5 my $TILDE = qr!~!;
21 5 my $LP = qr!\(!;
22 5 my $RP = qr!\)!;
23 5 my $DOT = qr!\.!;
24 5 my $COMMA = qr!,!;
25 5 my $SEMI = qr!;!;
26 5 my $SSTR = qr!'(?:[^']*)'!;
27 5 my $DSTR = qr!"(?:[^"]*)"!;
28 5 my $ENDING = qr!(?:$RP|$COMMA|$SEMI)!;
29  
30 5 my $POINT = qr!$STAR$WORD!;
31 5 my $QSTR = qr!(?:$SSTR|$DSTR)!;
32 5 my $COMP = qr!(?:$EQ$EQ|$TILDE$TILDE)!;
33 5 my $OPER = qr!(?:$COMP|$EQ)!;
34 5 my $PUNCT = qr!(?:$LP|$RP|$DOT|$COMMA|$SEMI)!;
35  
36 5 my $TOKENS = qr!(?:$POINT|$NUM|$WORD|$QSTR|$PUNCT|$OPER)!;
37  
38 5 our $block_marker = '===';
39 5 our $point_marker = '---';
40  
41 4 ▶sub compile_code {
Subroutine compile_code: called CC=5 SCAR=16.2
Pod undocumented
42 4 my ($self) = @_;
43 4 $self->{function} = TestML1::Function->new;
44 4 ▶ while (length $self->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"code"}) 48 4 52
45 48 $self->{code} =~ s{^(.*)(\r\n|\n|)}{};
46 48 $self->{line} = $1;
47 48 $self->tokenize;
48 48 ▶ next if $self->done;
Branch: 1 branch
BranchTrueFalseTotal
if $self->done 31 17 48
49 17 ▶ $self->parse_assignment ||
Branch: 1 branch
BranchTrueFalseTotal
$self->parse_assignment or $self->parse_assertion or $self->fail 17 0 17
Condition66%2/3
$self->parse_assignment or $self->parse_assertion
l!l&&r!l&&!r
6110
MC/DC0%0/2
$self->parse_assignment or $self->parse_assertion
$self->parse_assignment $self->parse_assertion
Truth table66%2/3
A or B
A= $self->parse_assignmentB= $self->parse_assertion
ABresult
1X1
011
000
50 $self->parse_assertion ||
51 $self->fail;
52 }
53 }
54  
55 48 ▶sub tokenize {
Subroutine tokenize: called CC=5 SCAR=16.4
Pod undocumented
56 48 my ($self) = @_;
57 48 $self->{tokens} = [];
58 48 ▶ while (length $self->{line}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"line"}) 132 48 180
59 132 ▶ next if $self->{line} =~ s/^$WS//;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"line"} =~ s/^$WS// 36 96 132
60 96 ▶ next if $self->{line} =~ s/^$HASH$ANY*//;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"line"} =~ s/^$HASH$ANY*// 0 96 96
61 96 ▶ if ($self->{line} =~ s/^($TOKENS)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'line'} =~ s/^($TOKENS)//) { } 96 0 96
62 96 push @{$self->{tokens}}, $1;
63 }
64 else {
65 0 $self->fail("Failed to get token here: '$self->{line}'");
66 }
67 }
68 }
69  
70 17 ▶sub parse_assignment {
Subroutine parse_assignment: called CC=5 SCAR=16.2
Pod undocumented
71 17 my ($self) = @_;
72 17 ▶ return unless $self->peek(2) eq '=';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->peek(2) eq "=" 11 6 17
73 6 my ($var, $op) = $self->pop(2);
74 6 my $expr = $self->parse_expression;
75 6 ▶ $self->pop if not $self->done and $self->peek eq ';';
Branch: 1 branch
BranchTrueFalseTotal
if not $self->done and $self->peek eq ";" 1 5 6
Condition66%2/3
not $self->done and $self->peek eq ";"
!ll&&!rl&&r
501
MC/DC50%1/2
not $self->done and $self->peek eq ";"
not $self->done $self->peek eq ";"
Truth table66%2/3
not(A) and B
A= not $self->doneB= $self->peek eq ";"
ABresult
0X0
100
111
76 6 ▶ $self->fail unless $self->done;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->done 0 6 6
77 6 push @{$self->function->statements},
78 TestML1::Assignment->new(name => $var, expr => $expr);
79 6 return 1;
80 }
81  
82 11 ▶sub parse_assertion {
Subroutine parse_assertion: called CC=8 SCAR=21.0
Pod undocumented
83 11 my ($self) = @_;
84 11 ▶ return unless grep /^$COMP$/, @{$self->tokens};
Branch: 1 branch
BranchTrueFalseTotal
unless grep /^$COMP$/, @{$self->tokens;} 0 11 11
85 11 $self->{points} = [];
86 11 my $left = $self->parse_expression;
87 11 my $token = $self->pop;
88 11 ▶ my $op =
Branch: 2 branches
BranchTrueFalseTotal
$token eq '==' ? : 10 1 11
$token eq '~~' ? : 1 0 1
89 $token eq '==' ? 'EQ' :
90 $token eq '~~' ? 'HAS' :
91 $self->fail;
92 11 my $right = $self->parse_expression;
93 11 ▶ $self->pop if not $self->done and $self->peek eq ';';
Branch: 1 branch
BranchTrueFalseTotal
if not $self->done and $self->peek eq ";" 1 10 11
Condition66%2/3
not $self->done and $self->peek eq ";"
!ll&&!rl&&r
1001
MC/DC50%1/2
not $self->done and $self->peek eq ";"
not $self->done $self->peek eq ";"
Truth table66%2/3
not(A) and B
A= not $self->doneB= $self->peek eq ";"
ABresult
0X0
100
111
94 11 ▶ $self->fail unless $self->done;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->done 0 11 11
95  
96 11 ▶ push @{$self->function->statements}, TestML1::Statement->new(
Branch: 1 branch
BranchTrueFalseTotal
@{$self->points;} ? : 7 4 11
97 expr => $left,
98 assert => TestML1::Assertion->new(
99 name => $op,
100 expr => $right,
101 ),
102 11 @{$self->points} ? (points => $self->points) : (),
103 );
104 11 return 1;
105 }
106  
107 35 ▶sub parse_expression {
Subroutine parse_expression: called CC=13 SCAR=25.7
Pod undocumented
108 35 my ($self) = @_;
109  
110 35 my $calls = [];
111 35 ▶ while (not $self->done and $self->peek !~ /^($ENDING|$COMP)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->done or $self->peek =~ /^($ENDING|$COMP)$/) 47 35 82
Condition100%3/3
$self->done or $self->peek =~ /^($ENDING|$COMP)$/
l!l&&r!l&&!r
152047
MC/DC100%2/2
$self->done or $self->peek =~ /^($ENDING|$COMP)$/
$self->done $self->peek =~ /^($ENDING|$COMP)$/
Truth table100%3/3
A or B
A= $self->doneB= $self->peek =~ /^($ENDING|$COMP)$/
ABresult
1X1
011
000
112 47 my $token = $self->pop;
113 47 ▶ if ($token =~ /^$NUM$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($token =~ /^$NUM$/) { } 4 43 47
elsif ($token =~ /^$QSTR$/) { } 10 33 43
elsif ($token =~ /^$WORD$/) { } 16 17 33
elsif ($token =~ /^$POINT$/) { } 17 0 17
114 4 push @$calls, TestML1::Num->new(value => $token + 0);
115 }
116 elsif ($token =~/^$QSTR$/) {
117 10 my $str = substr($token, 1, length($token) - 2);
118 10 push @$calls, TestML1::Str->new(value => $str);
119 }
120 elsif ($token =~ /^$WORD$/) {
121 16 my $call = TestML1::Call->new(name => $token);
122 16 ▶ if (not $self->done and $self->peek eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->done and $self->peek eq "(") 5 11 16
Condition100%3/3
not $self->done and $self->peek eq "("
!ll&&!rl&&r
295
MC/DC100%2/2
not $self->done and $self->peek eq "("
not $self->done $self->peek eq "("
Truth table100%3/3
not(A) and B
A= not $self->doneB= $self->peek eq "("
ABresult
0X0
100
111
123 5 $call->{args} = $self->parse_args;
124 }
125 16 push @$calls, $call;
126 }
127 elsif ($token =~ /^$POINT$/) {
128 17 ▶ $token =~ /($WORD)/ or die;
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /($WORD)/ or die 17 0 17
129 17 $token = $1;
130 17 $token =~ s/-/_/g;
131 17 push @{$self->{points}}, $token;
132 17 push @$calls, TestML1::Point->new(name => $token);
133 }
134 else {
135 0 $self->fail("Unknown token '$token'");
136 }
137 47 ▶ if (not $self->done and $self->peek eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->done and $self->peek eq ".") 12 35 47
Condition100%3/3
not $self->done and $self->peek eq "."
!ll&&!rl&&r
152012
MC/DC100%2/2
not $self->done and $self->peek eq "."
not $self->done $self->peek eq "."
Truth table100%3/3
not(A) and B
A= not $self->doneB= $self->peek eq "."
ABresult
0X0
100
111
138 12 $self->pop;
139 }
140 }
141 35 ▶ return @$calls == 1
Branch: 1 branch
BranchTrueFalseTotal
@$calls == 1 ? : 25 10 35
142 ? $calls->[0]
143 : TestML1::Expression->new(calls => $calls);
144 }
145  
146 5 ▶sub parse_args {
Subroutine parse_args: called CC=4 SCAR=13.9
Pod undocumented
147 5 my ($self) = @_;
148 5 ▶ $self->pop eq '(' or die;
Branch: 1 branch
BranchTrueFalseTotal
$self->pop eq "(" or die 5 0 5
149 5 my $args = [];
150 5 ▶ while ($self->peek ne ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->peek ne ")") 7 5 12
151 7 push @$args, $self->parse_expression;
152 7 ▶ $self->pop if $self->peek eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if $self->peek eq "," 2 5 7
153 }
154 5 $self->pop;
155 5 return $args;
156 }
157  
158 4 ▶sub compile_data {
Subroutine compile_data: called CC=13 SCAR=26.2
Pod undocumented
159 4 my ($self) = @_;
160 4 my $input = $self->data;
161 4 $input =~ s/^#.*\n/\n/mg;
162 4 my @blocks = grep $_, split /(^$block_marker.*?(?=^$block_marker|\z))/ms, $input;
163 4 for my $block (@blocks) {
164 5 $block =~ s/\n+\z/\n/;
165 }
166  
167 4 my $data = [];
168 4 for my $string_block (@blocks) {
169 5 my $block = TestML1::Block->new;
170 5 ▶ $string_block =~ s/^$block_marker\ +(.*?)\ *\n//g
Branch: 1 branch
BranchTrueFalseTotal
$string_block =~ s/^$TestML1::Compiler::Lite::block_marker\ +(.*?)\ *\n//g or die "No block label! $string_block" 5 0 5
171 or die "No block label! $string_block";
172 5 $block->{label} = $1;
173 5 $string_block =~ s/\A(.*?)(^$point_marker\ )/$2/sm;
174 5 ▶ while (length $string_block) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string_block) 14 5 19
175 14 ▶ next if $string_block =~ s/^\n+//;
Branch: 1 branch
BranchTrueFalseTotal
if $string_block =~ s/^\n+// 0 14 14
176 14 my ($key, $value);
177 14 ▶ if ($string_block =~ s/\A$point_marker\ +($WORD):\ +(.*)\n//g or
Branch: 1 branch
BranchTrueFalseTotal
if ($string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD):\ +(.*)\n//g or $string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD)\n(.*?)(?=^$TestML1::Compiler::Lite::point_marker|\z)//gms) { } 14 0 14
Condition66%2/3
$string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD):\ +(.*)\n//g or $string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD)\n(.*?)(?=^$TestML1::Compiler::Lite::point_marker|\z)//gms
l!l&&r!l&&!r
1130
MC/DC0%0/2
$string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD):\ +(.*)\n//g or $string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD)\n(.*?)(?=^$TestML1::Compiler::Lite::point_marker|\z)//gms
$string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD):\ +(.*)\n//g $string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD)\n(.*?)(?=^$TestML1::Compiler::Lite::point_marker|\z)//gms
Truth table66%2/3
A or B
A= $string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD):\ +(.*)\n//gB= $string_block =~ s/\A$TestML1::Compiler::Lite::point_marker\ +($WORD)\n(.*?)(?=^$TestML1::Compiler::Lite::point_marker|\z)//gms
ABresult
1X1
011
000
178 $string_block =~
179 s/\A$point_marker\ +($WORD)\n(.*?)(?=^$point_marker|\z)//msg
180 ) {
181 14 ($key, $value) = ($1, $2);
182 14 $key =~ s/-/_/g;
183 }
184 else {
185 0 die "Failed to parse TestML1 string:\n$string_block";
186 }
187 14 ▶ $block->{points} ||= {};
Condition100%2/2
$block->{"points"} ||= {}
l!l
95
MC/DC100%1/1
$block->{"points"} ||= {}
$block->{"points"}
Truth table100%2/2
A
A= $block->{"points"}
Aresult
11
00
188 14 ▶ my $eol = ($value =~ s/(\r?\n)\s*\z//) ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$value =~ s/(\r?\n)\s*\z// ? : 3 11 14
189 14 ▶ if (length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value) 14 0 14
190 14 $value .= $eol;
191 }
192 14 $block->{points}{$key} = $value;
193  
194 14 ▶ if ($key =~ /^(ONLY|SKIP|LAST)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^(ONLY|SKIP|LAST)$/) 0 14 14
195 0 $block->{$key} = 1;
196 }
197 }
198 5 push @$data, $block;
199 }
200 4 ▶ $self->function->{data} = $data if @$data;
Branch: 1 branch
BranchTrueFalseTotal
if @$data 3 1 4
201 }
202  
203 227 ▶sub done {
Subroutine done: called CC=2 SCAR=6.9
Pod undocumented
204 227 my ($self) = @_;
205 227 ▶ @{$self->{tokens}} ? 0 : 1
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'tokens'};} ? : 132 95 227
206 }
207  
208 151 ▶sub peek {
Subroutine peek: called CC=3 SCAR=11.0
Pod undocumented
209 151 my ($self, $index) = @_;
210 151 ▶ $index ||= 1;
Condition100%2/2
$index ||= 1
l!l
17134
MC/DC100%1/1
$index ||= 1
$index
Truth table100%2/2
A
A= $index
Aresult
11
00
211 151 ▶ die if $index > @{$self->{tokens}};
Branch: 1 branch
BranchTrueFalseTotal
if $index > @{$self->{"tokens"};} 0 151 151
212 151 $self->{tokens}->[$index - 1];
213 }
214  
215 90 ▶sub pop {
Subroutine pop: called CC=3 SCAR=11.0
Pod undocumented
216 90 my ($self, $count) = @_;
217 90 ▶ $count ||= 1;
Condition100%2/2
$count ||= 1
l!l
684
MC/DC100%1/1
$count ||= 1
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
218 90 ▶ die if $count > @{$self->{tokens}};
Branch: 1 branch
BranchTrueFalseTotal
if $count > @{$self->{"tokens"};} 0 90 90
219 90 splice @{$self->{tokens}}, 0, $count;
220 }
221  
222 0 ▶sub fail {
Subroutine fail: not called CC=2 SCAR=17.9
Pod undocumented
223 0 my ($self, $message) = @_;
224 0 my $text = "Failed to compile TestML1 document.\n";
225 0 ▶ $text .= "Reason: $message\n" if $message;
Branch: 1 branch
BranchTrueFalseTotal
if $message 0 0 0
226 0 $text .= "\nCode section of failure:\n$self->{line}\n$self->{code}\n";
227 0 die $text;
228 }
229  
230 5 1;
« TestML1/Compiler.pm ↑ Summary TestML1/Runtime.pm »