Generator.pm untested

filter: Statementstmt 0.0% 0 / 120 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 13 Podpod n/a totaltotal 0.0% 0 / 177 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package ABNF::Generator;
2  
3 =pod
4  
5 =head1 NAME
6  
7 B<ABNF::Generator> - abstract base class for ABNF-based generators
8  
9 =head1 INHERITANCE
10  
11 B<ABNF::Generator> is the root of the Honest and Liar generators
12  
13 =head1 DESCRIPTION
14  
15 B<ABNF::Generator> is the abstract base class for ABNF-based generators.
16  
17 Also it provides function B<asStrings> to stringified generated sequences
18  
19 =head1 METHODS
20  
21 =cut
22  
23 use 5.014;
24  
25 use strict;
26 use warnings;
27 no warnings "recursion";
28  
29 use Carp;
30 use Readonly;
31 use Method::Signatures;
32 use Data::Dumper;
33  
34 use Parse::ABNF;
35 use List::Util qw(shuffle);
36  
37 use ABNF::Grammar qw($BASIC_RULES splitRule);
38 use ABNF::Validator;
39  
40 use base qw(Exporter);
41 our @EXPORT_OK = qw($CONVERTERS $BASIC_RULES $RECURSION_LIMIT);
42  
43 Readonly our $CHOICE_LIMIT => 128;
44  
45 Readonly our $CONVERTERS => {
46 "hex" => sub { hex($_[0]) },
47 "bin" => sub { oct($_[0]) },
48 "decimal" => sub { int($_[0]) },
49 };
50  
51 =pod
52  
53 =head1 ABNF::Generator->C<new>($grammar, $validator?)
54  
55 Creates a new B<ABNF::Generator> object.
56  
57 $grammar isa B<ABNF::Grammar>.
58  
59 $validator isa B<ABNF::Validator>.
60  
61 Children classes can get acces for them by $self->{_grammar} and $self->{_validator}
62  
63 =cut
64  
65 method new(ABNF::Grammar $grammar, ABNF::Validator $validator?) {
66 my $class = ref($self) || $self;
67  
68 croak "Cant create instance of abstract class" if $class eq 'ABNF::Generator';
69  
70 $self = {
71 _cache => {},
72 _grammar => $grammar,
73 _validator => $validator || ABNF::Validator->new($grammar)
74 };
75  
76 bless($self, $class);
77  
78 $self->_init();
79  
80 return $self;
81 }
82  
83 method _init() {
84 $self->{handlers} = {
85 Range => $self->can("_range"),
86 String => $self->can("_string"),
87 Literal => $self->can("_literal"),
88 Repetition => $self->can("_repetition"),
89 ProseValue => $self->can("_proseValue"),
90 Reference => $self->can("_reference"),
91 Group => $self->can("_group"),
92 Choice => $self->can("_choice"),
93 Rule => $self->can("_rule"),
94 };
95 }
96  
97 =pod
98  
99 =head1 $generator->C<_range>($rule, $recursion)
100  
101 Generates chain for range element.
102  
103 Abstract method, most of all children must overload it.
104  
105 $recursion is a structure to controle recursion depth.
106  
107 =cut
108  
109 method _range($rule, $recursion) {
110 croak "Range handler is undefined yet";
111 }
112  
113 =pod
114  
115 =head1 $generator->C<_string>($rule, $recursion)
116  
117 Generates chain for string element.
118  
119 Abstract method, most of all children must overload it
120  
121 $recursion is a structure to controle recursion depth.
122  
123 =cut
124  
125 method _string($rule, $recursion) {
126 croak "String handler is undefined yet";
127 }
128  
129 =pod
130  
131 =head1 $generator->C<_literal>($rule, $recursion)
132  
133 Generates chain for literal element.
134  
135 Abstract method, most of all children must overload it
136  
137 $recursion is a structure to controle recursion depth.
138  
139 =cut
140  
141 method _literal($rule, $recursion) {
142 croak "Literal handler is undefined yet";
143 }
144  
145 =pod
146  
147 =head1 $generator->C<_repetition>($rule, $recursion)
148  
149 Generates chain for repetition element.
150  
151 Abstract method, most of all children must overload it
152  
153 $recursion is a structure to controle recursion depth.
154  
155 =cut
156  
157 method _repetition($rule, $recursion) {
158 croak "Repetition handler is undefined yet";
159 }
160  
161 =pod
162  
163 =head1 $generator->C<_reference>($rule, $recursion)
164  
165 Generates chain for reference element.
166  
167 Abstract method, most of all children must overload it
168  
169 $recursion is a structure to controle recursion depth.
170  
171 =cut
172  
173 method _reference($rule, $recursion) {
174 croak "Reference handler is undefined yet";
175 }
176  
177 =pod
178  
179 =head1 $generator->C<_group>($rule, $recursion)
180  
181 Generates chain for group element.
182  
183 Abstract method, most of all children must overload it
184  
185 $recursion is a structure to controle recursion depth.
186  
187 =cut
188  
189 method _group($rule, $recursion) {
190 croak "Group handler is undefined yet";
191 }
192  
193 =pod
194  
195 =head1 $generator->C<_choice>($rule, $recursion)
196  
197 Generates chain for choce element.
198  
199 Abstract method, most of all children must overload it
200  
201 $recursion is a structure to controle recursion depth.
202  
203 =cut
204  
205 method _choice($rule, $recursion) {
206 croak "Choice handler is undefined yet";
207 }
208  
209 =pod
210  
211 =head1 $generator->C<_rule>($rule, $recursion)
212  
213 Generates chain for rule element, usually -- basic element in chain.
214  
215 Abstract method, most of all children must overload it
216  
217 $recursion is a structure to controle recursion depth.
218  
219 =cut
220  
221 method _rule($rule, $recursion) {
222 croak "Rule handler is undefined yet";
223 }
224  
225 =pod
226  
227 =head1 $generator->C<_generateChain>($rule, $recursion)
228  
229 Generates one chain per different rule in $rule.
230  
231 $rule is structure that Return from B<ABNF::Grammar::rule> and like in B<Parse::ABNF>.
232  
233 $rule might be a command name.
234  
235 $recursion is a structure to controle recursion depth.
236  
237 at init it have only one key -- level == 0.
238  
239 You can create new object perl call or use one.
240  
241 See use example in ABNF::Generator::Honest in method _choice
242  
243 =cut
244  
245 method _generateChain($rule, $recursion) {
246  
247 my @result = ();
248  
249 if ( ref($rule) ) {
250 croak "Bad rule " . Dumper($rule) unless UNIVERSAL::isa($rule, "HASH");
251 } elsif ( exists($BASIC_RULES->{$rule}) ) {
252 $rule = $BASIC_RULES->{$rule};
253 } else {
254 $rule = $self->{_grammar}->rule($rule);
255 }
256  
257 $self->{handlers}->{ $rule->{class} }
258 or die "Unknown class " . $rule->{class};
259  
260 return $self->{handlers}->{ $rule->{class} }->($self, $rule, $recursion);
261 }
262  
263 =pod
264  
265 =head1 $generator->C<generate>($rule, $tail="")
266  
267 Generates one sequence string for command $rule.
268  
269 Using cache $self->{_cache}->{$rule} for this rule, that speeds up this call.
270  
271 $rule is a command name.
272  
273 $tail is a string added to result if it absent.
274  
275 dies if there is no command like $rule.
276  
277 =cut
278  
279 method generate(Str $rule, Str $tail="") {
280 croak "Unexisted command" unless $self->{_grammar}->hasCommand($rule);
281  
282 $self->{_cache}->{$rule} ||= [];
283 unless ( @{$self->{_cache}->{$rule}} ) {
284 $self->{_cache}->{$rule} = _asStrings( $self->_generateChain($rule, {level => 0}) );
285 }
286 my $result = pop($self->{_cache}->{$rule});
287
288 my $rx = eval { qr/$tail$/ };
289 croak "Bad tail" if $@;
290 return $result =~ $rx ? $result : $result . $tail;
291 }
292  
293 =pod
294  
295 =head1 $generator->C<withoutArguments>($name, $tail="")
296  
297 Return an strings starts like command $name and without arguments.
298  
299 $tail is a string added to a result.
300  
301 dies if there is no command like $rule.
302  
303 =cut
304  
305 method withoutArguments(Str $name, Str $tail="") {
306 croak "Unexisted command" unless $self->{_grammar}->hasCommand($name);
307  
308 my ($prefix, $args) = splitRule( $self->{_grammar}->rule($name) );
309
310 my $rx = eval { qr/$tail$/ };
311 croak "Bad tail" if $@;
312 return $prefix =~ $rx ? $prefix : $prefix . $tail;
313 }
314  
315 =pod
316  
317 =head1 $generator->C<hasCommand>($name)
318  
319 Return 1 if there is a $name is command, 0 otherwise
320  
321 =cut
322  
323 method hasCommand(Str $name) {
324 $self->{_grammar}->hasCommand($name);
325 }
326  
327 =pod
328  
329 =head1 FUNCTIONS
330  
331 =head1 C<_asStrings>($generated)
332  
333 Return stringification of genereted sequences from C<_generateChain>.
334  
335 Uses in generate call to stringify chains.
336  
337 =cut
338  
339 func _asStrings($generated) {
340 given ( $generated->{class} ) {
341 when ( "Atom" ) { return [ $generated->{value} ] }
342  
343 when ( "Sequence" ) {
344 my $value = $generated->{value};
345 return [] unless @$value;
346  
347 my $begin = _asStrings($value->[0]);
348  
349 for ( my $pos = 1; $pos < @$value; $pos++ ) {
350 my @new_begin = ();
351 my $ends = _asStrings($value->[$pos]);
352 next unless @$ends;
353  
354 my @ibegin = splice([shuffle(@$begin)], 0, $CHOICE_LIMIT);
355 my @iends = splice([shuffle(@$ends)], 0, $CHOICE_LIMIT);
356 foreach my $end ( @iends ) {
357 foreach my $begin ( @ibegin ) {
358 push(@new_begin, $begin . $end);
359 }
360 }
361
362 $begin = \@new_begin;
363 }
364  
365 return $begin;
366 }
367  
368 when ( "Choice" ) {
369 return [
370 map { @{_asStrings($_)} } @{$generated->{value}}
371 ];
372 }
373  
374 default { die "Unknown class " . $generated->{class} . Dumper $generated }
375 }
376 }
377  
378 1;
379  
380 =pod
381  
382 =head1 AUTHOR / COPYRIGHT / LICENSE
383  
384 Copyright (c) 2013 Arseny Krasikov <nyaapa@cpan.org>.
385  
386 This module is licensed under the same terms as Perl itself.
387  
388 =cut