Parser.yp

filter: Statementstmt 90.1% 91 / 101 Branchbran 87.5% 70 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.3% 28 / 31 Podpod 100% 4 / 4 totaltotal 89.3% 193 / 216 SCAR 37.9
CC
42
Cov
89%
_lexer
13.0
__ANON__
6.0
_mod_generic
6.0
CRAP 44.4
« blib/lib/Validate/SPF/Parser.pm ↑ Summary blib/lib/Validate/SPF.pm »
1 %{#
2 # Validate::SPF::Parser source file
3 #
4 # Author: Anton Gerasimov
5 #
6  
7 12 ▶use Regexp::Common qw( net );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 my $input;
10  
11 0 my %errors = (
12 E_DEFAULT => "Just error",
13 E_SYNTAX => "Syntax error near token '%s'",
14 E_INVALID_VERSION => "Invalid SPF version",
15 E_IPADDR_EXPECTED => "Expected ip or network address",
16 E_DOMAIN_EXPECTED => "Expected domain name",
17 E_UNEXPECTED_BITMASK => "Unexpected bitmask",
18 E_UNEXPECTED_IPADDR => "Unexpected ip address",
19 E_UNEXPECTED_DOMAIN => "Unexpected domain name",
20 );
21  
22 %}
23  
24 %%
25  
26 10 ▶spf
Subroutine new: called CC=2 SCAR=7.0
Pod documented
27 : chunks
28 10 ▶ { $_[1] }
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 10 10
29 ;
30  
31 version
32 : VERSION
33 {
34 3 ▶ $_[1] eq 'v=spf1' and
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] eq "v=spf1" and return $_[0]->_ver_generic($_[1]) 1 2 3
35 return $_[0]->_ver_generic( $_[1] );
36  
37 2 $_[0]->raise_error( 'E_INVALID_VERSION', $_[1] );
38 }
39 ;
40  
41 chunks
42 0 : chunks chunk
43 0 ▶ { push( @{$_[1]}, $_[2] ) if defined $_[2]; $_[1] }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[2] 0 0 0
44 | chunk
45 61 ▶ { defined $_[1] ? [ $_[1] ] : [ ] }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 61 0 61
46 ;
47  
48 chunk
49 : version
50 | mechanism
51 | modifier
52 ;
53  
54 mechanism
55 : with_ipaddress
56 | with_domain_bitmask
57 | with_bitmask
58 | with_domain
59 ;
60  
61 modifier
62 : MODIFIER '=' DOMAIN
63 {
64 0 ▶ $_[0]->_mod_generic( $_[1], $_[3] );
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 }
66 ;
67  
68 # ptr, exists, include, mx, a, all
69 with_domain
70 : MECHANISM
71 {
72 6 ▶ $_[0]->raise_error( 'E_IPADDR_EXPECTED', $_[1] )
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /ip[46]/i 2 4 6
73 if $_[1] =~ /ip[46]/i;
74 6 ▶ $_[0]->raise_error( 'E_DOMAIN_EXPECTED', $_[1] )
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /\A(exists|include)\Z/i 0 6 6
75 if $_[1] =~ /\A(exists|include)\Z/i;
76  
77 6 ▶ $_[0]->_mech_domain( '+', $_[1], $_[1] =~ /all/i ? undef : '@' );
Branch: 1 branch
BranchTrueFalseTotal
$_[1] =~ /all/i ? : 1 5 6
78 }
79 | QUALIFIER MECHANISM
80 {
81 23 ▶ $_[0]->raise_error( 'E_IPADDR_EXPECTED', $_[1] . $_[2] )
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] =~ /ip[46]/i 4 19 23
82 if $_[2] =~ /ip[46]/i;
83 23 ▶ $_[0]->raise_error( 'E_DOMAIN_EXPECTED', $_[1] . $_[2] )
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] =~ /\A(exists|include)\Z/i 2 21 23
84 if $_[2] =~ /\A(exists|include)\Z/i;
85  
86 23 ▶ $_[0]->_mech_domain( $_[1], $_[2], $_[2] =~ /all/i ? undef : '@' );
Branch: 1 branch
BranchTrueFalseTotal
$_[2] =~ /all/i ? : 5 18 23
87 }
88 | MECHANISM ':' DOMAIN
89 {
90 7 ▶ my $ctx = $_[1] . ':' . $_[3];
Subroutine __ANON__: called CC=2 SCAR=6.9
91  
92 7 ▶ $_[0]->raise_error( 'E_UNEXPECTED_DOMAIN', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /all/i 1 6 7
93 if $_[1] =~ /all/i;
94  
95 7 $_[0]->_mech_domain( '+', $_[1], $_[3] );
96 }
97 | QUALIFIER MECHANISM ':' DOMAIN
98 {
99 14 ▶ my $ctx = $_[1] . $_[2] . ':' . $_[4];
Subroutine __ANON__: called CC=2 SCAR=6.9
100  
101 14 ▶ $_[0]->raise_error( 'E_UNEXPECTED_DOMAIN', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] =~ /all/i 1 13 14
102 if $_[2] =~ /all/i;
103  
104 14 $_[0]->_mech_domain( $_[1], $_[2], $_[4] );
105 }
106 ;
107  
108 # mx, a
109 with_bitmask
110 : MECHANISM '/' BITMASK
111 {
112 4 ▶ my $ctx = $_[1] . '/' . $_[3];
Subroutine __ANON__: called CC=3 SCAR=11.0
113  
114 4 ▶ $_[0]->raise_error( 'E_IPADDR_EXPECTED', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /ip[46]/i 2 2 4
115 if $_[1] =~ /ip[46]/i;
116  
117 4 ▶ $_[0]->raise_error( 'E_UNEXPECTED_BITMASK', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /\A(ptr|all|exists|include)\Z/i 2 2 4
118 if $_[1] =~ /\A(ptr|all|exists|include)\Z/i;
119  
120 4 $_[0]->_mech_domain_bitmask( '+', $_[1], '@', $_[3] );
121 }
122 | QUALIFIER MECHANISM '/' BITMASK
123 {
124 6 ▶ my $ctx = $_[1] . $_[2] . '/' . $_[4];
Subroutine __ANON__: called CC=3 SCAR=11.0
125  
126 6 ▶ $_[0]->raise_error( 'E_IPADDR_EXPECTED', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] =~ /ip[46]/i 0 6 6
127 if $_[2] =~ /ip[46]/i;
128  
129 6 ▶ $_[0]->raise_error( 'E_UNEXPECTED_BITMASK', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] =~ /\A(ptr|all|exists|include)\Z/i 4 2 6
130 if $_[2] =~ /\A(ptr|all|exists|include)\Z/i;
131  
132 6 $_[0]->_mech_domain_bitmask( $_[1], $_[2], '@', $_[4] );
133 }
134 ;
135  
136 # mx, a
137 with_domain_bitmask
138 : MECHANISM ':' DOMAIN '/' BITMASK
139 {
140 5 ▶ my $ctx = $_[1] . ':' . $_[3] . '/' . $_[5];
Subroutine __ANON__: called CC=2 SCAR=6.9
141  
142 5 ▶ $_[0]->raise_error( 'E_UNEXPECTED_BITMASK', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /\A(ptr|all|exists|include)\Z/i 2 3 5
143 if $_[1] =~ /\A(ptr|all|exists|include)\Z/i;
144  
145 5 $_[0]->_mech_domain_bitmask( '+', $_[1], $_[3], $_[5] );
146 }
147 | QUALIFIER MECHANISM ':' DOMAIN '/' BITMASK
148 {
149 6 ▶ my $ctx = $_[1] . $_[2] . ':' . $_[4] . '/' . $_[6];
Subroutine __ANON__: called CC=2 SCAR=6.9
150  
151 6 ▶ $_[0]->raise_error( 'E_UNEXPECTED_BITMASK', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] =~ /\A(ptr|all|exists|include)\Z/i 4 2 6
152 if $_[2] =~ /\A(ptr|all|exists|include)\Z/i;
153  
154 6 $_[0]->_mech_domain_bitmask( $_[1], $_[2], $_[4], $_[6] );
155 }
156 ;
157  
158 # ip4, ip6
159 with_ipaddress
160 : MECHANISM ':' IPADDRESS
161 {
162 8 ▶ my $ctx = $_[1] . ':' . $_[3];
Subroutine __ANON__: called CC=2 SCAR=6.9
163  
164 8 ▶ $_[0]->raise_error( 'E_UNEXPECTED_IPADDR', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /\A(a|mx|ptr|all|exists|include)\Z/i 6 2 8
165 if $_[1] =~ /\A(a|mx|ptr|all|exists|include)\Z/i;
166  
167 8 $_[0]->_mech_ipaddr_bitmask( '+', $_[1], $_[3], undef );
168 }
169 | QUALIFIER MECHANISM ':' IPADDRESS
170 {
171 10 ▶ my $ctx = $_[1] . $_[2] . ':' . $_[4];
Subroutine __ANON__: called CC=2 SCAR=6.9
172  
173 10 ▶ $_[0]->raise_error( 'E_UNEXPECTED_IPADDR', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] =~ /\A(a|mx|ptr|all|exists|include)\Z/i 5 5 10
174 if $_[2] =~ /\A(a|mx|ptr|all|exists|include)\Z/i;
175  
176 10 $_[0]->_mech_ipaddr_bitmask( $_[1], $_[2], $_[4], undef );
177 }
178 | MECHANISM ':' IPADDRESS '/' BITMASK
179 {
180 4 ▶ my $ctx = $_[1] . ':' . $_[3] . '/' . $_[5];
Subroutine __ANON__: called CC=2 SCAR=6.9
181  
182 4 ▶ $_[0]->raise_error( 'E_UNEXPECTED_IPADDR', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /\A(a|mx|ptr|all|exists|include)\Z/i 1 3 4
183 if $_[1] =~ /\A(a|mx|ptr|all|exists|include)\Z/i;
184  
185 4 $_[0]->_mech_ipaddr_bitmask( '+', $_[1], $_[3], $_[5] );
186 }
187 | QUALIFIER MECHANISM ':' IPADDRESS '/' BITMASK
188 {
189 14 ▶ my $ctx = $_[1] . $_[2] . ':' . $_[4] . '/' . $_[6];
Subroutine __ANON__: called CC=2 SCAR=6.9
190  
191 14 ▶ $_[0]->raise_error( 'E_UNEXPECTED_IPADDR', $ctx )
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] =~ /\A(a|mx|ptr|all|exists|include)\Z/i 11 3 14
192 if $_[2] =~ /\A(a|mx|ptr|all|exists|include)\Z/i;
193  
194 14 $_[0]->_mech_ipaddr_bitmask( $_[1], $_[2], $_[4], $_[6] );
195 }
196 10 ;
197  
198 %%
199  
200 sub parse {
201 10 ▶ my ( $self, $text ) = @_;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
202  
203 117 $input = $self->YYData->{INPUT} = $text;
204 117 delete $self->YYData->{ERRMSG};
205  
206 117 return $self->YYParse( yylex => \&_lexer, yyerror => \&_error );
207 }
208  
209 sub error {
210 57 ▶ my ( $self ) = @_;
Subroutine error: called CC=1 SCAR=0.0
Pod documented
211 57 return $self->YYData->{ERRMSG};
212 }
213  
214 sub _build_error {
215 57 ▶ my ( $self, $code, $context, @extra ) = @_;
Subroutine _build_error: called CC=2 SCAR=7.1
216  
217 57 ▶ $code = 'E_DEFAULT' unless exists $errors{$code};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $errors{$code} 0 57 57
218  
219 $self->YYData->{ERRMSG} = {
220 57 text => sprintf( $errors{$code} => @extra ),
221 code => $code,
222 context => $context,
223 };
224 }
225  
226 sub raise_error {
227 49 ▶ my ( $self, @params ) = @_;
Subroutine raise_error: called CC=1 SCAR=0.0
Pod documented
228  
229 49 $self->_build_error( @params );
230 49 $self->YYError;
231 }
232  
233 sub _error {
234 57 ▶ my ( $self ) = @_;
Subroutine _error: called CC=2 SCAR=6.9
235  
236 57 ▶ unless ( exists $self->YYData->{ERRMSG} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->YYData->{"ERRMSG"}) 8 49 57
237 8 substr( $input, index( $input, $self->YYCurval ), 0, '<*>' );
238  
239 8 $self->_build_error( 'E_SYNTAX', $input, $self->YYCurval );
240 }
241  
242 57 return;
243 }
244  
245 sub _lexer {
246 489 ▶ my ( $parser ) = @_;
Subroutine _lexer: called CC=13 SCAR=25.7
247  
248 489 $parser->YYData->{INPUT} =~ s/^\s*//;
249  
250 489 for ( $parser->YYData->{INPUT} ) {
251 # printf( "[debug] %s\n", $_ );
252  
253 489 ▶ s/^(v\=spf\d)\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^(v\=spf\d)\b//i and return "VERSION", $1 3 486 489
254 and return ( 'VERSION', $1 );
255  
256 486 ▶ s/^(\/)\b//i
Branch: 1 branch
BranchTrueFalseTotal
s[^(/)\b][]i and return "/", "/" 39 447 486
257 and return ( '/', '/' );
258 447 ▶ s/^(\:)\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^(\:)\b//i and return ":", ":" 69 378 447
259 and return ( ':', ':' );
260 378 ▶ s/^(\=)\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^(\=)\b//i and return "=", "=" 1 377 378
261 and return ( '=', '=' );
262  
263 # qualifiers
264 377 ▶ s/^([-~\+\?])\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^([-~\+\?])\b//i and return "QUALIFIER", $1 74 303 377
265 and return ( 'QUALIFIER', $1 );
266  
267 # mechanisms
268 303 ▶ s/^(all|ptr|a|mx|ip4|ip6|exists|include)\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^(all|ptr|a|mx|ip4|ip6|exists|include)\b//i and return "MECHANISM", $1 108 195 303
269 and return ( 'MECHANISM', $1 );
270  
271 # modifiers
272 195 ▶ s/^(redirect|exp)\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^(redirect|exp)\b//i and return "MODIFIER", $1 0 195 195
273 and return ( 'MODIFIER', $1 );
274  
275 195 ▶ s/^($RE{net}{IPv4}{dec}|$RE{net}{IPv6}{-sep=>':'})\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^($Validate::SPF::Parser::RE{"net"}{"IPv4"}{"dec"}|$Validate::SPF::Parser::RE{"net"}{"IPv6"}{join $;, "-sep", ":"})\b//i and return "IPADDRESS", $1 36 159 195
276 and return ( 'IPADDRESS', $1 );
277  
278 159 ▶ s/^([_\.a-z\d][\-a-z\d]*\.[\.\-a-z\d]*[a-z\d]?)\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^([_\.a-z\d][\-a-z\d]*\.[\.\-a-z\d]*[a-z\d]?)\b//i and return "DOMAIN", $1 32 127 159
279 and return ( 'DOMAIN', $1 );
280  
281 127 ▶ s/^(\d{1,3})\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^(\d{1,3})\b//i and return "BITMASK", $1 39 88 127
282 and return ( 'BITMASK', $1 );
283  
284 # garbage
285 88 ▶ s/^(.+)\b//i
Branch: 1 branch
BranchTrueFalseTotal
s/^(.+)\b//i and return "UNKNOWN", $1 7 81 88
286 and return ( 'UNKNOWN', $1 );
287 }
288  
289 # EOF
290 81 return ( '', undef );
291 }
292  
293 # generic modifier
294 sub _mod_generic {
295 0 ▶ my ( $self, $mod, $domain ) = @_;
Subroutine _mod_generic: not called CC=2 SCAR=17.9
296  
297 return +{
298 0 ▶ type => 'mod',
Branch: 1 branch
BranchTrueFalseTotal
$domain ? : 0 0 0
299 modifier => lc $mod,
300 (
301 $domain
302 ? ( domain => $domain ) :
303 ( )
304 ),
305 };
306 }
307  
308 # generic version
309 sub _ver_generic {
310 1 ▶ my ( $self, $ver ) = @_;
Subroutine _ver_generic: called CC=1 SCAR=0.0
311  
312 return +{
313 1 type => 'ver',
314 version => lc $ver,
315 };
316 }
317  
318  
319 # generic mechanism
320 sub _mech_generic {
321 107 ▶ my ( $self, $qualifier, $mech, $domain, $ipaddr, $bitmask ) = @_;
Subroutine _mech_generic: called CC=5 SCAR=16.1
322  
323 return +{
324 107 ▶ type => 'mech',
Branch: 4 branches
BranchTrueFalseTotal
$domain ? : 65 42 107
$ipaddr ? : 36 71 107
defined $bitmask ? : 18 18 36
defined $bitmask ? : 39 68 107
325 qualifier => $qualifier,
326 mechanism => lc $mech,
327 (
328 $domain
329 ? ( domain => $domain ) :
330 ( )
331 ),
332 (
333 $ipaddr
334 ? ( ( defined $bitmask ? 'network' : 'ipaddress' ) => $ipaddr )
335 : ( )
336 ),
337 (
338 defined $bitmask
339 ? ( bitmask => $bitmask )
340 : ( )
341 ),
342 };
343 }
344  
345 sub _mech_domain {
346 50 ▶ my ( $self, $qualifier, $mech, $domain ) = @_;
Subroutine _mech_domain: called CC=1 SCAR=0.0
347  
348 50 return $self->_mech_generic( $qualifier, $mech, $domain, undef, undef );
349 }
350  
351 sub _mech_domain_bitmask {
352 21 ▶ my ( $self, $qualifier, $mech, $domain, $bitmask ) = @_;
Subroutine _mech_domain_bitmask: called CC=1 SCAR=0.0
353  
354 21 return $self->_mech_generic( $qualifier, $mech, $domain, undef, $bitmask );
355 }
356  
357 sub _mech_ipaddr_bitmask {
358 36 ▶ my ( $self, $qualifier, $mech, $ipaddr, $bitmask ) = @_;
Subroutine _mech_ipaddr_bitmask: called CC=1 SCAR=0.0
359  
360 36 return $self->_mech_generic( $qualifier, $mech, undef, $ipaddr, $bitmask );
361 }
« blib/lib/Validate/SPF/Parser.pm ↑ Summary blib/lib/Validate/SPF.pm »