FormulaEngine/Parser.pm

filter: Statementstmt 97.3% 254 / 261 Branchbran 89.2% 91 / 102 Conditioncond 77.1% 27 / 35 MC/DCmcdc 66.6% 14 / 21 Subroutinesub 94.2% 66 / 70 Podpod 100% 33 / 33 totaltotal 92.9% 485 / 522 CC 66 SCAR 15.4
CC
66
Cov
93%
parse_unit_expr
12.1
next_token
11.0
scan_token
10.0
CRAP 4.7
« FormulaEngine/Namespace/Default.pm ↑ Summary FormulaEngine/Compiler.pm »
1 package Language::FormulaEngine::Parser;
2 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use Language::FormulaEngine::Parser::ContextUtil
7 9 ▶ qw( calc_text_coordinates format_context_string format_context_multiline );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Create parse tree from an input string
11 9 our $VERSION = '0.08'; # VERSION
12  
13  
14 9 has parse_tree => ( is => 'rw' );
15 9 has error => ( is => 'rw' );
16 9 has functions => ( is => 'rw' );
17 9 has symbols => ( is => 'rw' );
18  
19 283 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
20 283 my ($self, $input)= @_;
21 283 $self->reset;
22 283 $self->{input}= $input;
23 283 pos( $self->{input} )= 0;
24 283 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
25 283 $self->next_token;
26 282 my $tree= $self->parse_expr;
27 # It is an error if there was un-processed input.
28 281 ▶ $self->token_type eq '0'
Branch: 1 branch
BranchTrueFalseTotal
$self->token_type eq 0 or die sprintf("Unexpected %s \"%s\" near %s", $self->token_type, $self->token_value, $self->token_context) 280 1 281
29 or die sprintf('Unexpected %s "%s" near %s',
30 $self->token_type, $self->token_value, $self->token_context);
31 280 $self->parse_tree($tree);
32 3 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 3 chomp;
34 3 $self->error($_);
35 283 };
36 283 return $self->parse_tree;
37 }
38  
39 283 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
40 283 my $self= shift;
41 283 $self->parse_tree(undef);
42 283 $self->error(undef);
43 283 $self->functions({});
44 283 $self->symbols({});
45 283 delete @{$self}{'input','token_type','token_value','token_pos'};
46 283 $self;
47 }
48  
49  
50 63 ▶sub deparse {
Subroutine deparse: called CC=2 SCAR=6.9
Pod documented
51 63 my ($self, $node)= @_;
52 63 ▶ $node= $self->parse_tree unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 9 54 63
53 63 $node->deparse($self);
54 }
55  
56  
57 0 ▶sub input { shift->{input} }
Subroutine input: not called CC=1 SCAR=6.9
Pod documented
58 0 ▶sub input_pos { pos( shift->{input} ) }
Subroutine input_pos: not called CC=1 SCAR=6.9
Pod documented
59 338 ▶sub token_type { shift->{token_type} }
Subroutine token_type: called CC=1 SCAR=0.0
Pod documented
60 47 ▶sub token_value { shift->{token_value} }
Subroutine token_value: called CC=1 SCAR=0.0
Pod documented
61 46 ▶sub token_pos { shift->{token_pos} }
Subroutine token_pos: called CC=1 SCAR=0.0
Pod documented
62  
63  
64 2078 ▶sub next_token {
Subroutine next_token: called CC=11 SCAR=24.0
Pod documented
65 2078 my $self= shift;
66
67 # If already reached end of input, throw an exception.
68 die "Can't call next_token after end of input"
69 2078 ▶ if '0' eq ($self->{token_type}||'');
Branch: 1 branch
BranchTrueFalseTotal
if 0 eq ($self->{"token_type"} || "") 0 2078 2078
Condition100%2/2
$self->{'token_type'} || ''
l!l
1785293
MC/DC100%1/1
$self->{'token_type'} || ''
$self->{'token_type'}
Truth table100%2/2
A
A= $self->{'token_type'}
Aresult
11
00
70
71 # Detect the next token
72 2078 my ($type, $val, $pos0, $pos1)= ('','');
73 2078 ▶ while ($type eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "") 2312 1786 4098
74 2312 ▶ $pos0= pos($self->{input}) || 0;
Condition100%2/2
pos $self->{'input'} || 0
l!l
2019293
MC/DC100%1/1
pos $self->{'input'} || 0
pos $self->{'input'}
Truth table100%2/2
A
A= pos $self->{'input'}
Aresult
11
00
75 2312 ($type, $val)= $self->scan_token;
76 2312 ▶ $pos1= pos($self->{input}) || 0;
Condition100%2/2
pos $self->{'input'} || 0
l!l
23093
MC/DC100%1/1
pos $self->{'input'} || 0
pos $self->{'input'}
Truth table100%2/2
A
A= pos $self->{'input'}
Aresult
11
00
77 # Check for end of buffer, even if it matched.
78 2312 ▶ if ($pos1 >= length $self->{input}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos1 >= length $self->{"input"}) 577 1735 2312
79 #pos($self->{input})= $pos0; # rewind to start of token before growing buffer
80 #if ($self->_grow_buffer) {
81 # $log->trace("grow buffer succeeded");
82 # $type= '';
83 # next;
84 #}
85 #pos($self->{input})= $pos1; # restore actual position\
86 # If we didn't get a token or are ignoring this final token, then return the EOF token
87 577 ▶ if (!defined $type || $type eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type or $type eq "") 291 286 577
Condition100%3/3
not defined $type or $type eq ""
l!l&&r!l&&!r
2883286
MC/DC100%2/2
not defined $type or $type eq ""
not defined $type $type eq ""
Truth table100%3/3
not(A) or B
A= not defined $typeB= $type eq ""
ABresult
1X1
011
000
88 291 $type= 0;
89 291 $val= '';
90 291 $pos0= $pos1;
91 291 last;
92 }
93 }
94 2021 ▶ defined $type
Branch: 1 branch
BranchTrueFalseTotal
defined $type or die 'Unknown syntax at ' . $self->token_context . "\n" 2020 1 2021
95 or die "Unknown syntax at ".$self->token_context."\n";
96 2020 ▶ $pos1 > $pos0
Branch: 1 branch
BranchTrueFalseTotal
$pos1 > $pos0 or croak("Tokenizer consumed zero characters") 2020 0 2020
97 or croak "Tokenizer consumed zero characters";
98 }
99 2077 @{$self}{'token_type','token_value','token_pos'}= ($type,$val,$pos0);
100 2077 return $type, $val;
101 }
102  
103  
104 971 ▶sub consume_token {
Subroutine consume_token: called CC=2 SCAR=6.9
Pod documented
105 971 my $self= shift;
106 croak "Can't consume EOF"
107 971 ▶ if $self->{token_type} eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"token_type"} eq 0 10 961 971
108 961 my $val= $self->{token_value};
109 961 $self->next_token;
110 961 return $val;
111 }
112  
113 2 ▶sub token_context {
Subroutine token_context: called CC=6 SCAR=20.9
Pod documented
114 2 my ($self, %args)= @_;
115 return format_context_multiline($self->{input}, $self->{token_pos}||0, pos($self->{input})||0, \%args)
116 2 ▶ if delete $args{multiline};
Branch: 1 branch
BranchTrueFalseTotal
if delete $args{"multiline"} 0 2 2
Condition0%0/4
$self->{'token_pos'} || 0
l!l
00
pos $self->{'input'} || 0
l!l
00
MC/DC0%0/1
$self->{'token_pos'} || 0
$self->{'token_pos'}
MC/DC0%0/1
pos $self->{'input'} || 0
pos $self->{'input'}
Truth table0%0/2
A
A= $self->{'token_pos'}
Aresult
11
00
Truth table0%0/2
A
A= pos $self->{'input'}
Aresult
11
00
117 2 ▶ return format_context_string($self->{input}, $self->{token_pos}||0, pos($self->{input})||0);
Condition100%4/4
$self->{'token_pos'} || 0
l!l
11
pos $self->{'input'} || 0
l!l
11
MC/DC100%1/1
$self->{'token_pos'} || 0
$self->{'token_pos'}
MC/DC100%1/1
pos $self->{'input'} || 0
pos $self->{'input'}
Truth table100%2/2
A
A= $self->{'token_pos'}
Aresult
11
00
Truth table100%2/2
A
A= pos $self->{'input'}
Aresult
11
00
118 }
119  
120  
121 739 ▶sub parse_expr { shift->parse_or_expr; }
Subroutine parse_expr: called CC=1 SCAR=0.0
Pod documented
122  
123 739 ▶sub parse_or_expr {
Subroutine parse_or_expr: called CC=3 SCAR=11.0
Pod documented
124 739 my $self= shift;
125 739 my $first= $self->parse_and_expr;
126 738 ▶ return $first unless $self->{token_type} eq 'or';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"token_type"} eq "or" 736 2 738
127 2 my @or_expr= $first;
128 2 ▶ while ($self->{token_type} eq 'or') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "or") 2 2 4
129 2 $self->next_token;
130 2 push @or_expr, $self->parse_and_expr;
131 }
132 2 return $self->new_call('or', \@or_expr);
133 }
134  
135 741 ▶sub parse_and_expr {
Subroutine parse_and_expr: called CC=3 SCAR=11.0
Pod documented
136 741 my $self= shift;
137 741 my $first= $self->parse_not_expr;
138 740 ▶ return $first unless $self->{token_type} eq 'and';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"token_type"} eq "and" 732 8 740
139 8 my @and_expr= $first;
140 8 ▶ while ($self->{token_type} eq 'and') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "and") 8 8 16
141 8 $self->next_token;
142 8 push @and_expr, $self->parse_not_expr;
143 }
144 8 return $self->new_call('and', \@and_expr);
145 }
146  
147 749 ▶sub parse_not_expr {
Subroutine parse_not_expr: called CC=3 SCAR=11.0
Pod documented
148 749 my $self= shift;
149 749 ▶ if ($self->{token_type} eq 'not' or $self->{token_type} eq '!') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "not" or $self->{"token_type"} eq "!") 5 744 749
Condition66%2/3
$self->{"token_type"} eq "not" or $self->{"token_type"} eq "!"
l!l&&r!l&&!r
50744
MC/DC50%1/2
$self->{"token_type"} eq "not" or $self->{"token_type"} eq "!"
$self->{"token_type"} eq "not" $self->{"token_type"} eq "!"
Truth table66%2/3
A or B
A= $self->{"token_type"} eq "not"B= $self->{"token_type"} eq "!"
ABresult
1X1
011
000
150 5 $self->next_token;
151 5 return $self->new_call('not', [ $self->parse_cmp_expr ]);
152 }
153 744 return $self->parse_cmp_expr;
154 }
155  
156 9 my %_cmp_ops= map { $_ => 1 } qw( > < >= <= != == );
157 749 ▶sub parse_cmp_expr {
Subroutine parse_cmp_expr: called CC=3 SCAR=11.0
Pod documented
158 749 my $self= shift;
159 749 my $first= $self->parse_sum_expr;
160 748 ▶ return $first unless $_cmp_ops{$self->{token_type}};
Branch: 1 branch
BranchTrueFalseTotal
unless $_cmp_ops{$self->{"token_type"}} 724 24 748
161 24 my @expr= $first;
162 24 ▶ while ($_cmp_ops{$self->{token_type}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_cmp_ops{$self->{"token_type"}}) 32 24 56
163 32 push @expr, $self->new_string($self->{token_type});
164 32 $self->next_token;
165 32 push @expr, $self->parse_sum_expr;
166 }
167 24 return $self->new_call('compare', \@expr);
168 }
169  
170 781 ▶sub parse_sum_expr {
Subroutine parse_sum_expr: called CC=6 SCAR=17.9
Pod documented
171 781 my $self= shift;
172 781 my $first= $self->parse_prod_expr;
173 780 ▶ return $first unless $self->{token_type} eq '+' or $self->{token_type} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"token_type"} eq "+" or $self->{"token_type"} eq "-" 754 26 780
Condition100%3/3
$self->{"token_type"} eq "+" or $self->{"token_type"} eq "-"
l!l&&r!l&&!r
206754
MC/DC100%2/2
$self->{"token_type"} eq "+" or $self->{"token_type"} eq "-"
$self->{"token_type"} eq "+" $self->{"token_type"} eq "-"
Truth table100%3/3
A or B
A= $self->{"token_type"} eq "+"B= $self->{"token_type"} eq "-"
ABresult
1X1
011
000
174 26 my @sum_expr= $first;
175 26 ▶ while ($self->{token_type} eq '+' or $self->{token_type} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "+" or $self->{"token_type"} eq "-") 35 26 61
Condition100%3/3
$self->{"token_type"} eq "+" or $self->{"token_type"} eq "-"
l!l&&r!l&&!r
241126
MC/DC100%2/2
$self->{"token_type"} eq "+" or $self->{"token_type"} eq "-"
$self->{"token_type"} eq "+" $self->{"token_type"} eq "-"
Truth table100%3/3
A or B
A= $self->{"token_type"} eq "+"B= $self->{"token_type"} eq "-"
ABresult
1X1
011
000
176 35 my $negate= $self->consume_token eq '-';
177 35 my $operand= $self->parse_prod_expr;
178 35 ▶ push @sum_expr, $negate? $self->get_negative($operand) : $operand;
Branch: 1 branch
BranchTrueFalseTotal
$negate ? : 11 24 35
179 }
180 26 return $self->new_call('sum', \@sum_expr);
181 }
182  
183 816 ▶sub parse_prod_expr {
Subroutine parse_prod_expr: called CC=4 SCAR=13.9
Pod documented
184 816 my $self= shift;
185 816 my $value= $self->parse_unit_expr;
186 815 ▶ while ($self->{token_type} eq '*' or $self->{token_type} eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "*" or $self->{"token_type"} eq "/") 39 815 854
Condition100%3/3
$self->{"token_type"} eq "*" or $self->{"token_type"} eq "/"
l!l&&r!l&&!r
336815
MC/DC100%2/2
$self->{"token_type"} eq "*" or $self->{"token_type"} eq "/"
$self->{"token_type"} eq "*" $self->{"token_type"} eq "/"
Truth table100%3/3
A or B
A= $self->{"token_type"} eq "*"B= $self->{"token_type"} eq "/"
ABresult
1X1
011
000
187 39 my $op= $self->consume_token;
188 39 my $right= $self->parse_unit_expr;
189 39 ▶ $value= $self->new_call( $op eq '*'? 'mul' : 'div', [ $value, $right ] );
Branch: 1 branch
BranchTrueFalseTotal
$op eq '*' ? : 33 6 39
190 }
191 815 return $value;
192 }
193  
194 885 ▶sub parse_unit_expr {
Subroutine parse_unit_expr: called CC=12 SCAR=24.9
Pod documented
195 885 my $self= shift;
196 885 my $negate= 0;
197 885 my $expr;
198  
199 885 ▶ if ($self->{token_type} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "-") 30 855 885
200 30 $self->next_token;
201 30 return $self->get_negative($self->parse_unit_expr);
202 }
203  
204 855 ▶ if ($self->{token_type} eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "(") 13 842 855
205 13 $self->next_token;
206 13 my $args= $self->parse_list;
207 die "Expected ')' near ".$self->token_context."\n"
208 13 ▶ if $self->{token_type} ne ')';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"token_type"} ne ")" 0 13 13
209 13 $self->next_token;
210 13 ▶ return @$args > 1? $self->new_call('list', $args) : $args->[0];
Branch: 1 branch
BranchTrueFalseTotal
@$args > 1 ? : 2 11 13
211 }
212
213 842 ▶ if ($self->{token_type} eq 'Number') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "Number") 323 519 842
214 323 return $self->new_number($self->consume_token);
215 }
216
217 519 ▶ if ($self->{token_type} eq 'String') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "String") 84 435 519
218 84 return $self->new_string($self->consume_token);
219 }
220
221 435 ▶ if ($self->{token_type} eq 'Identifier') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq "Identifier") 434 1 435
222 434 my $id= $self->consume_token;
223 434 ▶ if ($self->{token_type} eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'token_type'} eq '(') { } 266 168 434
224 266 $self->next_token;
225 266 ▶ my $args= $self->{token_type} eq ')'? [] : $self->parse_list;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'token_type'} eq ')' ? : 11 255 266
226 die "Expected ')' near ".$self->token_context."\n"
227 266 ▶ if $self->{token_type} ne ')';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"token_type"} ne ")" 0 266 266
228 266 $self->next_token;
229 266 return $self->new_call($id, $args);
230 }
231 else {
232 168 return $self->new_symbol($id);
233 }
234 }
235
236 1 ▶ if ($self->{token_type} eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq 0) 1 0 1
237 1 die "Expected expression, but reached end of input\n";
238 }
239
240 0 die "Unexpected token $self->{token_type} '$self->{token_value}' near ".$self->token_context."\n";
241 }
242  
243 268 ▶sub parse_list {
Subroutine parse_list: called CC=2 SCAR=6.9
Pod documented
244 268 my $self= shift;
245 268 my @args= $self->parse_expr;
246 268 ▶ while ($self->{token_type} eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"token_type"} eq ",") 189 268 457
247 189 $self->next_token;
248 189 push @args, $self->parse_expr;
249 }
250 268 return \@args;
251 }
252  
253  
254 8 ▶sub cmp_operators { qw( = == != <> > >= < <= ), "\x{2260}", "\x{2264}", "\x{2265}" }
Subroutine cmp_operators: called CC=1 SCAR=0.0
Pod documented
255 8 ▶sub math_operators { qw( + - * / ) }
Subroutine math_operators: called CC=1 SCAR=0.0
Pod documented
256 8 ▶sub logic_operators { qw( and or not ! ) }
Subroutine logic_operators: called CC=1 SCAR=0.0
Pod documented
257 8 ▶sub list_operators { ',', '(', ')' }
Subroutine list_operators: called CC=1 SCAR=0.0
Pod documented
258 8 ▶sub keyword_map {
Subroutine keyword_map: called CC=1 SCAR=0.0
Pod documented
259 return {
260 8 (map { $_ => $_ } cmp_operators, math_operators, logic_operators, list_operators),
261 '=' => '==', '<>' => '!=', "\x{2260}" => '!=',
262 "\x{2264}" => '<=', "\x{2265}" => '>='
263 }
264 }
265 8 ▶sub scanner_rules {
Subroutine scanner_rules: called CC=3 SCAR=11.6
Pod documented
266 8 my $self= shift;
267 8 my $keywords= $self->keyword_map;
268 my $kw_regex= join '|', map "\Q$_\E",
269 8 sort { length($b) <=> length($a) } # longest keywords get priority
270 keys %$keywords;
271
272 # Perl 5.20.1 and 5.20.2 have a bug where regex comparisons on unicode strings can crash.
273 # It seems to damage the scalar $1, but copying it first fixes the problem.
274 8 ▶ my $kw_canonical= $] >= 5.020000 && $] < 5.020003? '$keywords->{lc(my $clone1= $1)}' : '$keywords->{lc $1}';
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.02 && $] < 5.020003 ? : 0 8 8
Condition33%1/3
$] >= "5.02" and $] < "5.020003"
!ll&&!rl&&r
080
MC/DC0%0/2
$] >= "5.02" and $] < "5.020003"
$] >= "5.02" $] < "5.020003"
Truth table33%1/3
A and B
A= $] >= "5.02"B= $] < "5.020003"
ABresult
0X0
100
111
275 return (
276 # Pattern Name, Pattern, Token Type and Token Value
277 8 [ 'Whitespace', qr/(\s+)/, '"" => ""' ], # empty string causes next_token to loop
278 [ 'Decimal', qr/([0-9]*\.?[0-9]+(?:[eE][+-]?[0-9]+)?)\b/, 'Number => $1+0' ],
279 [ 'Hexadecimal', qr/0x([0-9A-Fa-f]+)/, 'Number => hex($1)' ],
280 [ 'Keywords', qr/($kw_regex)/, $kw_canonical.' => $1', { keywords => $keywords } ],
281 [ 'Identifiers', qr/([A-Za-z_][A-Za-z0-9_.]*)\b/, 'Identifier => $1' ],
282 # Single or double quoted string, using Pascal-style repeated quotes for escaping
283 [ 'StringLiteral', qr/(?:"((?:[^"]|"")*)"|'((?:[^']|'')*)')/, q%
284 do{
285 my $str= defined $1? $1 : $2;
286 $str =~ s/""/"/g if defined $1;
287 $str =~ s/''/'/g if defined $2;
288 (String => $str)
289 }
290 %],
291 );
292 }
293  
294 8 ▶sub _build_scan_token_method_body {
Subroutine _build_scan_token_method_body: called CC=1 SCAR=0.0
295 8 my ($self, $rules)= @_;
296 8 return join('', map
297 ' return ' . $_->[2] . ' if $self->{input} =~ /\G' . $_->[1] . "/gc;\n",
298 @$rules
299 ).' return;' # return empty list of no rule matched
300 }
301  
302 8 ▶sub _build_scan_token_method {
Subroutine _build_scan_token_method: called CC=5 SCAR=16.1
303 8 my ($pkg, $method_name)= @_;
304 8 ▶ $pkg= ref $pkg if ref $pkg;
Branch: 1 branch
BranchTrueFalseTotal
if ref $pkg 8 0 8
305 8 ▶ $method_name= 'scan_token' unless defined $method_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method_name 8 0 8
306 8 my @rules= $pkg->scanner_rules;
307 # collect variables which should be available to the code
308 8 ▶ my %vars= map { $_->[3]? %{ $_->[3] } : () } @rules;
Branch: 1 branch
BranchTrueFalseTotal
$_->[3] ? : 8 40 48
309 8 my $code= join "\n",
310 (map 'my $'.$_.' = $vars{'.$_.'};', keys %vars),
311 "sub ${pkg}::$method_name {",
312 ' my $self= shift;',
313 $pkg->_build_scan_token_method_body(\@rules),
314 "}\n";
315 # closure needed for 5.8 and 5.10 which complain about using a lexical
316 # in a sub declared at package scope.
317 9 ▶ no warnings 'redefine','closure';
Subroutine BEGIN: called CC=1 SCAR=0.0
318 8 ▶ eval "$code; 1" or die $@ . " for generated scanner code:\n".$code;
Subroutine scan_token: called CC=10 SCAR=23.0
Pod documented
Branch: 10 branches
BranchTrueFalseTotal
eval "$code; 1" or die $@ . " for generated scanner code:\n" . $code 8 0 8
if $self->{"input"} =~ /\G(?^:(\s+))/cg 237 2075 2312
if $self->{"input"} =~ /\G(?^:([0-9]*\.?[0-9]+(?:[eE][+-]?[0-9]+)?)\b)/cg 328 1747 2075
if $self->{"input"} =~ /\G(?^:0x([0-9A-Fa-f]+))/cg 2 1745 1747
if $self->{"input"} =~ m[\G(?^u:(not|and|\=\=|\>\=|\<\>|\<\=|\!\=|or|\+|\-|\*|\>|\=|/|\,|\\x{2265}|\)|\!|\\x{2264}|\(|\<|\\x{2260}))]cg 924 821 1745
if $self->{"input"} =~ /\G(?^:([A-Za-z_][A-Za-z0-9_.]*)\b)/cg 445 376 821
if $self->{"input"} =~ /\G(?^:(?:"((?:[^"]|"")*)"|'((?:[^']|'')*)'))/cg 87 289 376
defined $1 ? : 85 2 87
if defined $1 85 2 87
if defined $2 2 85 87
319 8 return $pkg->can('scan_token');
320 }
321  
322 8 ▶sub scan_token { my $m= $_[0]->_build_scan_token_method; goto $m; };
Subroutine scan_token: called CC=1 SCAR=0.0
Pod documented
323  
324  
325 13 ▶sub Language::FormulaEngine::Parser::Node::Call::new {
Subroutine new: called CC=1 SCAR=0.0
326 13 my ($class, $name, $params)= @_;
327 13 bless [ $name, $params ], $class;
328 }
329 7 ▶sub Language::FormulaEngine::Parser::Node::Call::is_constant { 0 }
Subroutine is_constant: called CC=1 SCAR=0.0
330 414 ▶sub Language::FormulaEngine::Parser::Node::Call::function_name { $_[0][0] }
Subroutine function_name: called CC=1 SCAR=0.0
331 444 ▶sub Language::FormulaEngine::Parser::Node::Call::parameters { $_[0][1] }
Subroutine parameters: called CC=1 SCAR=0.0
332 169 ▶sub Language::FormulaEngine::Parser::Node::Call::evaluate {
Subroutine evaluate: called CC=1 SCAR=0.0
333 169 my ($self, $namespace)= @_;
334 169 $namespace->evaluate_call($self);
335 }
336 15 ▶sub Language::FormulaEngine::Parser::Node::Call::simplify {
Subroutine simplify: called CC=1 SCAR=0.0
337 15 my ($node, $namespace)= @_;
338 15 $namespace->simplify_call($node)
339 }
340 23 ▶sub Language::FormulaEngine::Parser::Node::Call::deparse {
Subroutine deparse: called CC=2 SCAR=6.9
341 23 my ($node, $parser)= @_;
342 return $node->function_name . (
343 23 !@{$node->parameters}? '()'
344 23 ▶ : '( ' .join(', ', map $parser->deparse($_), @{$node->parameters}). ' )'
Branch: 1 branch
BranchTrueFalseTotal
!@{$node->parameters;} ? : 2 21 23
345 )
346 }
347  
348 377 ▶sub new_call {
Subroutine new_call: called CC=1 SCAR=0.0
Pod documented
349 377 my ($self, $fn, $params)= @_;
350 377 $self->functions->{$fn}++; # record dependency on this function
351 377 bless [ $fn, $params ], 'Language::FormulaEngine::Parser::Node::Call';
352 }
353  
354  
355 0 ▶sub Language::FormulaEngine::Parser::Node::symbol::new {
Subroutine new: not called CC=1 SCAR=6.9
356 0 my ($class, $name)= @_;
357 0 bless \$name, $class;
358 }
359  
360 7 ▶sub Language::FormulaEngine::Parser::Node::Symbol::is_constant { 0 }
Subroutine is_constant: called CC=1 SCAR=0.0
361 121 ▶sub Language::FormulaEngine::Parser::Node::Symbol::symbol_name { ${$_[0]} }
Subroutine symbol_name: called CC=1 SCAR=0.0
362 66 ▶sub Language::FormulaEngine::Parser::Node::Symbol::evaluate {
Subroutine evaluate: called CC=1 SCAR=0.0
363 66 my ($self, $namespace)= @_;
364 66 $namespace->get_value($$self);
365 }
366 15 ▶sub Language::FormulaEngine::Parser::Node::Symbol::simplify {
Subroutine simplify: called CC=1 SCAR=0.0
367 15 my ($self, $namespace)= @_;
368 15 return $namespace->simplify_symref($self);
369 }
370 24 ▶sub Language::FormulaEngine::Parser::Node::Symbol::deparse {
Subroutine deparse: called CC=1 SCAR=0.0
371 24 shift->symbol_name;
372 }
373  
374 168 ▶sub new_symbol {
Subroutine new_symbol: called CC=1 SCAR=0.0
Pod documented
375 168 my ($self, $name)= @_;
376 168 $self->symbols->{$name}++; # record dependency on this variable
377 168 bless \$name, 'Language::FormulaEngine::Parser::Node::Symbol';
378 }
379  
380  
381 0 ▶sub Language::FormulaEngine::Parser::Node::String::new {
Subroutine new: not called CC=1 SCAR=6.9
382 0 my ($class, $value)= @_;
383 0 bless \$value, $class;
384 }
385  
386 1 ▶sub Language::FormulaEngine::Parser::Node::String::is_constant { 1 }
Subroutine is_constant: called CC=1 SCAR=0.0
387 62 ▶sub Language::FormulaEngine::Parser::Node::String::string_value { ${$_[0]} }
Subroutine string_value: called CC=1 SCAR=0.0
388 52 ▶sub Language::FormulaEngine::Parser::Node::String::evaluate { ${$_[0]} }
Subroutine evaluate: called CC=1 SCAR=0.0
389 1 ▶sub Language::FormulaEngine::Parser::Node::String::simplify { $_[0] }
Subroutine simplify: called CC=1 SCAR=0.0
390 6 ▶sub _str_escape {
Subroutine _str_escape: called CC=1 SCAR=0.0
391 6 my $str= shift;
392 6 $str =~ s/'/''/g;
393 6 "'$str'";
394 }
395 6 ▶sub Language::FormulaEngine::Parser::Node::String::deparse {
Subroutine deparse: called CC=1 SCAR=0.0
396 6 _str_escape(shift->string_value);
397 }
398  
399 116 ▶sub new_string {
Subroutine new_string: called CC=1 SCAR=0.0
Pod documented
400 116 my ($self, $text)= @_;
401 116 bless \$text, 'Language::FormulaEngine::Parser::Node::String'
402 }
403  
404  
405 18 ▶sub Language::FormulaEngine::Parser::Node::Number::new {
Subroutine new: called CC=1 SCAR=0.0
406 18 my ($class, $value)= @_;
407 18 $value= 0+$value;
408 18 bless \$value, $class;
409 }
410
411 25 ▶sub Language::FormulaEngine::Parser::Node::Number::is_constant { 1 }
Subroutine is_constant: called CC=1 SCAR=0.0
412 219 ▶sub Language::FormulaEngine::Parser::Node::Number::number_value { ${$_[0]} }
Subroutine number_value: called CC=1 SCAR=0.0
413 159 ▶sub Language::FormulaEngine::Parser::Node::Number::evaluate { ${$_[0]} }
Subroutine evaluate: called CC=1 SCAR=0.0
414 6 ▶sub Language::FormulaEngine::Parser::Node::Number::simplify { $_[0] }
Subroutine simplify: called CC=1 SCAR=0.0
415 18 ▶sub Language::FormulaEngine::Parser::Node::Number::deparse { shift->number_value }
Subroutine deparse: called CC=1 SCAR=0.0
416  
417 359 ▶sub new_number {
Subroutine new_number: called CC=1 SCAR=0.0
Pod documented
418 359 my $value= $_[1]+0;
419 359 bless \$value, 'Language::FormulaEngine::Parser::Node::Number'
420 }
421  
422  
423 41 ▶sub get_negative {
Subroutine get_negative: called CC=4 SCAR=14.1
Pod documented
424 41 my ($self, $node)= @_;
425 41 ▶ return $self->new_number(-$node->number_value) if $node->can('number_value');
Branch: 1 branch
BranchTrueFalseTotal
if $node->can("number_value") 36 5 41
426 5 ▶ return $node->parameters->[0] if $node->can('function_name') and $node->function_name eq 'negative';
Branch: 1 branch
BranchTrueFalseTotal
if $node->can("function_name") and $node->function_name eq "negative" 0 5 5
Condition66%2/3
$node->can("function_name") and $node->function_name eq "negative"
!ll&&!rl&&r
320
MC/DC0%0/2
$node->can("function_name") and $node->function_name eq "negative"
$node->can("function_name") $node->function_name eq "negative"
Truth table66%2/3
A and B
A= $node->can("function_name")B= $node->function_name eq "negative"
ABresult
0X0
100
111
427 5 return $self->new_call('negative', [$node]);
428 }
429  
430 9 1;
431  
432 __END__
« FormulaEngine/Namespace/Default.pm ↑ Summary FormulaEngine/Compiler.pm »