Grammar.pm

filter: Statementstmt 33.9% 38 / 112 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 70.3% 19 / 27 Podpod n/a totaltotal 23.4% 57 / 243 CC 44 SCAR 36.7
CC
44
Cov
19%
new
82.9
replaceRule
64.9
replaceBasicRule
64.9
CRAP 39.1
↑ Summary Generator.pm »
1 package ABNF::Grammar;
2  
3 =pod
4  
5 =head1 NAME
6  
7 B<ABNF-Grammar> - validator and generator for ABNF grammars.
8  
9 B<ABNF::Grammar> - class for inner representation ABNF-grammar.
10  
11 =head1 VERSION
12  
13 This document describes B<ABNF::Grammar> version 0.08
14  
15 =head1 SYNOPSIS
16  
17 use ABNF::Grammar qw(Grammar);
18  
19 use ABNF::Generator qw(asStrings);
20  
21 use ABNF::Generator::Honest qw(Honest);
22  
23 use ABNF::Generator::Liar qw(Liar);
24  
25 use ABNF::Validator qw(Validator);
26  
27 my $grammar = Grammar->new("smtp.bnf", qw(ehlo helo mail rcpt data rset vrfy noop quit data data-terminate));
28 my $valid = Validator->new($grammar);
29 my $liar = Liar->new($grammar, $valid);
30 my $honest = Honest->new($grammar, $valid);
31  
32 $valid->validate("vrfy", "string");
33  
34 my @strings = $liar->withoutArguments("vrfy");
35  
36 my $string = $liar->unExistedCommand("vrfy");
37  
38 my $string = $liar->endlessCommand("vrfy");
39  
40 my $string = $liar->generate("helo");
41  
42 my $string = $honest->generate("helo");
43  
44 =head1 DESCRIPTION
45  
46 This module parses IETF ABNF (STD 68, RFC 5234, 4234, 2234) grammars
47 via B<Parse::ABNF> and provides tools to :
48  
49 =over 4
50  
51 =item * verify validity of string
52  
53 =item * generate valid messages
54  
55 =item * generate invalid messages
56  
57 =back
58  
59 =head1 METHODS
60  
61 =cut
62  
63 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 4 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 4 ▶use Parse::ABNF;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 4 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 4 ▶use base "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
77 our @EXPORT_OK = qw(splitRule Grammar $BASIC_RULES);
78 our $VERSION = "0.08";
79  
80 Readonly our $BASIC_RULES => do {
81 my $res = {};
82 foreach my $rule ( @{$Parse::ABNF::CoreRules} ) {
83 die "Multiple definitions for $rule->{name}" if exists($res->{$rule->{name}});
84 $res->{$rule->{name}} = $rule;
85 }
86  
87 $res;
88 };
89  
90 =pod
91  
92 =head1 ABNF::Grammar->C<new>($fname, @commands)
93  
94 Creates a new B<ABNF::Grammar> object.
95  
96 Read ABNF rules from file with $fname.
97  
98 @commands consists of main command names for generation and validation.
99  
100 =cut
101  
102 0 ▶method new(Str $fname, @commands) {
Subroutine BEGIN: called CC=1 SCAR=5.8
Subroutine new: not called CC=9 SCAR=44.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str"))->check($fname) 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
$Method::Signatures::mutc{"cache"}{"Str"} "Method::Signatures"->_make_constraint("Str")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Str"}B= "Method::Signatures"->_make_constraint("Str")
ABresult
1X1
011
000
103  
104 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
105  
106 0 $self = {_commands => { map {$_ => 1} @commands} };
107  
108 0 bless($self, $class);
109  
110  
111 0 ▶ open(my $file, $fname)
Branch: 1 branch
BranchTrueFalseTotal
open my $file, $fname or croak("Cant open $fname") 0 0 0
112 or croak "Cant open $fname";
113  
114 0 my $content = join("", <$file>) . "\n";
115  
116 0 ▶ close($file)
Branch: 1 branch
BranchTrueFalseTotal
close $file or carp("Cant close $fname") 0 0 0
117 or carp "Cant close $fname";
118  
119 0 $self->_init($content);
120  
121 0 foreach my $command ( @commands ) {
122 0 ▶ croak "Grammar doesn't have command $command" unless exists($self->{_rules}->{$command});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_rules"}{$command} 0 0 0
123 }
124  
125 0 return $self;
126 }
127  
128 =pod
129  
130 =head1 ABNF::Grammar->C<fromString>($content, @commands)
131  
132 Creates a new B<ABNF::Grammar> object.
133  
134 Get ABNF rules from string $rule
135  
136 @commands consists of main command names for generation and validation.
137  
138 =cut
139  
140 0 ▶method fromString(Str $content, @commands) {
Subroutine BEGIN: called CC=1 SCAR=5.8
Subroutine fromString: not called CC=7 SCAR=39.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str"))->check($content) 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
$Method::Signatures::mutc{"cache"}{"Str"} "Method::Signatures"->_make_constraint("Str")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Str"}B= "Method::Signatures"->_make_constraint("Str")
ABresult
1X1
011
000
141  
142 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
143  
144 0 $self = {_commands => { map {$_ => 1} @commands} };
145  
146 0 bless($self, $class);
147  
148 0 $self->_init($content . "\n");
149  
150 0 foreach my $command ( @commands ) {
151 0 ▶ croak "Grammar doesn't have command $command" unless exists($self->{_rules}->{$command});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_rules"}{$command} 0 0 0
152 }
153  
154 0 return $self;
155 }
156  
157 0 ▶method _init($content) {
Subroutine BEGIN: called CC=1 SCAR=5.3
Subroutine _init: not called CC=6 SCAR=35.9
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
if scalar @_ > 1 0 0 0
158  
159 0 my $parser = Parse::ABNF->new();
160 0 ▶ my $rules = $parser->parse($content)
Branch: 1 branch
BranchTrueFalseTotal
my $rules = $parser->parse($content) or croak("Bad rules") 0 0 0
161 or croak "Bad rules";
162  
163 0 foreach my $rule ( @$rules ) {
164 0 ▶ croak "Multiple definitions for $rule->{name}" if exists($self->{_rules}->{$rule->{name}});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_rules"}{$rule->{"name"}} 0 0 0
165 0 $self->{_rules}->{$rule->{name}} = $rule;
166 }
167  
168 }
169  
170 =pod
171  
172 =head1 $grammar->C<rule>($name)
173  
174 Return rule form $name with name $name.
175  
176 Result structure is identical to B<Parse::ABNF> structure.
177  
178 For debug only.
179  
180 Do not modify result structure.
181  
182 =cut
183  
184 0 ▶method rule(Str $name) {
Subroutine BEGIN: called CC=1 SCAR=6.0
Subroutine rule: not called CC=6 SCAR=36.0
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str"))->check($name) 0 0 0
if scalar @_ > 1 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
$Method::Signatures::mutc{"cache"}{"Str"} "Method::Signatures"->_make_constraint("Str")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Str"}B= "Method::Signatures"->_make_constraint("Str")
ABresult
1X1
011
000
185 0 ▶ croak "Unexisted rule $name" unless exists($self->{_rules}->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_rules"}{$name} 0 0 0
186 0 $self->{_rules}->{$name};
187 }
188  
189 =pod
190  
191 =head1 $grammar->C<rules>()
192  
193 Return all rules.
194  
195 Result structures is identical to B<Parse::ABNF> structure.
196  
197 For debug only.
198  
199 Do not modify result structure.
200  
201 =cut
202  
203 0 ▶method rules() {
Subroutine BEGIN: called CC=1 SCAR=4.1
Subroutine rules: not called CC=2 SCAR=14.6
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 0 0
204 0 $self->{_rules};
205 }
206  
207 =pod
208  
209 =head1 $grammar->C<replaceRule>($rule, $value)
210  
211 Replace $rule with $value.
212  
213 For debug use only.
214  
215 dies if there is no rule like $rule.
216  
217 =cut
218  
219 0 ▶method replaceRule(Str $rule, $value) {
Subroutine BEGIN: called CC=1 SCAR=6.2
Subroutine replaceRule: not called CC=8 SCAR=41.7
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str"))->check($rule) 0 0 0
unless scalar @_ > 1 0 0 0
if scalar @_ > 2 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
$Method::Signatures::mutc{"cache"}{"Str"} "Method::Signatures"->_make_constraint("Str")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Str"}B= "Method::Signatures"->_make_constraint("Str")
ABresult
1X1
011
000
220 0 ▶ croak "Unexisted rule $rule" unless exists($self->{_rules}->{$rule});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_rules"}{$rule} 0 0 0
221 0 ▶ croak "new value name must be equal to rule" unless $value->{name} eq $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless $value->{"name"} eq $rule 0 0 0
222 0 $self->{_rules}->{$rule} = $value;
223 }
224  
225 =pod
226  
227 =head1 $grammar->C<replaceBasicRule>($rule, $value)
228  
229 Replace $rule with $value.
230  
231 For debug use only.
232  
233 dies if there is no rule like $rule.
234  
235 =cut
236  
237 0 ▶method replaceBasicRule(Str $rule, $value) {
Subroutine BEGIN: called CC=1 SCAR=6.2
Subroutine replaceBasicRule: not called CC=8 SCAR=41.7
Branch: 4 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str"))->check($rule) 0 0 0
unless scalar @_ > 1 0 0 0
if scalar @_ > 2 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
$Method::Signatures::mutc{"cache"}{"Str"} "Method::Signatures"->_make_constraint("Str")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Str"}B= "Method::Signatures"->_make_constraint("Str")
ABresult
1X1
011
000
238 0 ▶ croak "Unexisted rule $rule" unless exists($BASIC_RULES->{$rule});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ABNF::Grammar::BASIC_RULES->{$rule} 0 0 0
239 0 ▶ croak "new value name must be equal to rule" unless $value->{name} eq $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless $value->{"name"} eq $rule 0 0 0
240 0 $BASIC_RULES->{$rule} = $value;
241 }
242  
243  
244 =pod
245  
246 =head1 $grammar->C<hasCommand>($name)
247  
248 Return 1 if $name is command, 0 otherwise.
249  
250 =cut
251  
252 0 ▶method hasCommand(Str $name) {
Subroutine BEGIN: called CC=1 SCAR=6.0
Subroutine hasCommand: not called CC=5 SCAR=32.4
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 0 0
unless ($Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str"))->check($name) 0 0 0
if scalar @_ > 1 0 0 0
Condition0%0/3
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
l!l&&r!l&&!r
000
MC/DC0%0/2
$Method::Signatures::mutc{"cache"}{"Str"} ||= "Method::Signatures"->_make_constraint("Str")
$Method::Signatures::mutc{"cache"}{"Str"} "Method::Signatures"->_make_constraint("Str")
Truth table0%0/3
A ||= B
A= $Method::Signatures::mutc{"cache"}{"Str"}B= "Method::Signatures"->_make_constraint("Str")
ABresult
1X1
011
000
253 0 exists $self->{_commands}->{$name};
254 }
255  
256 =pod
257  
258 =head1 $grammar->C<commands>()
259  
260 Return all grammar commands as arrayref.
261  
262 =cut
263  
264 4 ▶method commands() {
Subroutine BEGIN: called CC=1 SCAR=0.0
265 [ keys $self->{_commands} ]
266 }
267  
268 =pod
269  
270 =head1 FUNCTIONS
271  
272 =head1 C<splitRule>($rule)
273  
274 In scalar context return prefix only, in list -- prefix and arguments rules.
275  
276 $rule is structure that returns from C<rule> and like in B<Parse::ABNF>.
277  
278 =cut
279  
280 func splitRule($rule) {
281 my $value = $rule->{value};
282 my $prefix = "";
283  
284 if (
285 $value->{class} eq 'Group'
286 && $value->{value}->[0]->{class} eq 'Literal'
287 ) {
288 $prefix = $value->{value}->[0]->{value};
289 $value = dclone($value);
290 shift($value->{value});
291 if (
292 $value->{value}->[0]->{class} eq 'Reference'
293 && $value->{value}->[0]->{name} eq 'SP'
294 ) {
295 $prefix .= "\x20";
296 shift($value->{value});
297 }
298  
299 if (
300 $value->{value}->[-1]->{class} eq 'Reference'
301 && $value->{value}->[-1]->{name} eq 'CRLF'
302 ) {
303 pop($value->{value});
304 }
305 }
306  
307 return wantarray ? ($prefix, $value) : $prefix;
308 }
309  
310 =pod
311  
312 =head1 C<Grammar>()
313  
314 Return __PACKAGE__ to reduce class name :3
315  
316 =cut
317  
318 func Grammar() {
319 return __PACKAGE__;
320 }
321  
322  
323 1;
324  
325 __END__
↑ Summary Generator.pm »