1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
# Copyright (C) 2008-2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
package GenTest::Grammar;
require Exporter;
@ISA = qw(GenTest);
@EXPORT = qw(
GRAMMAR_FLAG_COMPACT_RULES
);
use strict;
use GenTest;
use GenTest::Constants;
use GenTest::Grammar::Rule;
use GenTest::Random;
use Data::Dumper;
use constant GRAMMAR_RULES => 0;
use constant GRAMMAR_FILE => 1;
use constant GRAMMAR_STRING => 2;
use constant GRAMMAR_FLAGS => 3;
use constant GRAMMAR_FLAG_COMPACT_RULES => 1;
1;
sub new {
my $class = shift;
my $grammar = $class->SUPER::new({
'grammar_file' => GRAMMAR_FILE,
'grammar_string' => GRAMMAR_STRING,
'grammar_flags' => GRAMMAR_FLAGS,
'grammar_rules' => GRAMMAR_RULES
}, @_);
if (defined $grammar->rules()) {
$grammar->[GRAMMAR_STRING] = $grammar->toString();
} else {
$grammar->[GRAMMAR_RULES] = {};
if (defined $grammar->file()) {
my $parse_result = $grammar->parseFromFile($grammar->file());
return undef if $parse_result > STATUS_OK;
}
if (defined $grammar->string()) {
my $parse_result = $grammar->parseFromString($grammar->string());
return undef if $parse_result > STATUS_OK;
}
}
return $grammar;
}
sub file {
return $_[0]->[GRAMMAR_FILE];
}
sub string {
return $_[0]->[GRAMMAR_STRING];
}
sub toString {
my $grammar = shift;
my $rules = $grammar->rules();
return join("\n\n", map { $grammar->rule($_)->toString() } sort keys %$rules);
}
sub parseFromFile {
my ($grammar, $grammar_file) = @_;
open (GF, $grammar_file) or die "Unable to open() grammar $grammar_file: $!";
read (GF, my $grammar_string, -s $grammar_file) or die "Unable to read() $grammar_file: $!";
$grammar->[GRAMMAR_STRING] = $grammar_string;
return $grammar->parseFromString($grammar_string);
}
sub parseFromString {
my ($grammar, $grammar_string) = @_;
#
# provide an #include directive
#
while ($grammar_string =~ s{#include [<"](.*?)[>"]$}{
{
my $include_string;
my $include_file = $1;
open (IF, $1) or die "Unable to open include file $include_file: $!";
read (IF, my $include_string, -s $include_file) or die "Unable to open $include_file: $!";
$include_string;
}}mie) {};
# Strip comments. Note that this is not Perl-code safe, since perl fragments
# can contain both comments with # and the $# expression. A proper lexer will fix this
$grammar_string =~ s{#.*$}{}iomg;
# Join lines ending in \
$grammar_string =~ s{\\$}{ }iomg;
# Strip end-line whitespace
$grammar_string =~ s{\s+$}{}iomg;
# Add terminating \n to ease parsing
$grammar_string = $grammar_string."\n";
my @rule_strings = split (";\s*[\r\n]+", $grammar_string);
my %rules;
foreach my $rule_string (@rule_strings) {
my ($rule_name, $components_string) = $rule_string =~ m{^(.*?)\s*:(.*)$}sio;
$rule_name =~ s{[\r\n]}{}gsio;
$rule_name =~ s{^\s*}{}gsio;
next if $rule_name eq '';
say("Warning: Rule $rule_name is defined twice.") if exists $rules{$rule_name};
my @component_strings = split (m{\|}, $components_string);
my @components;
my %components;
foreach my $component_string (@component_strings) {
# Remove leading whitespace
$component_string =~ s{^\s+}{}sgio;
$component_string =~ s{\s+$}{}sgio;
# Rempove repeating whitespaces
$component_string =~ s{\s+}{ }sgio;
# Split this so that each identifier is separated from all syntax elements
# The identifier can start with a lowercase letter or an underscore , plus quotes
$component_string =~ s{([_a-z0-9'"`\{\}\$\[\]]+)}{|$1|}sgo;
# Revert overzealous splitting that splits things like _varchar(32) into several tokens
$component_string =~ s{([a-z0-9_]+)\|\(\|(\d+)\|\)}{$1($2)|}sgo;
# Remove leading and trailing pipes
$component_string =~ s{^\|}{}sgio;
$component_string =~ s{\|$}{}sgio;
if (
(exists $components{$component_string}) &&
($grammar->[GRAMMAR_FLAGS] & GRAMMAR_FLAG_COMPACT_RULES)
) {
next;
} else {
$components{$component_string}++;
}
my @component_parts = split (m{\|}, $component_string);
#
# If this grammar rule contains Perl code, assemble it between the various
# component parts it was split into. This "reconstructive" step is definitely bad design
# The way to do it properly would be to tokenize the grammar using a full-blown lexer
# which should hopefully come up in a future version.
#
my $nesting_level = 0;
my $pos = 0;
my $code_start;
while (1) {
if ($component_parts[$pos] =~ m{\{}so) {
$code_start = $pos if $nesting_level == 0; # Code segment starts here
my $bracket_count = ($component_parts[$pos] =~ tr/{//);
$nesting_level = $nesting_level + $bracket_count;
}
if ($component_parts[$pos] =~ m{\}}so) {
my $bracket_count = ($component_parts[$pos] =~ tr/}//);
$nesting_level = $nesting_level - $bracket_count;
if ($nesting_level == 0) {
# Resemble the entire Perl code segment into a single string
splice(@component_parts, $code_start, ($pos - $code_start + 1) , join ('', @component_parts[$code_start..$pos]));
$pos = $code_start + 1;
$code_start = undef;
}
}
last if $pos > $#component_parts;
$pos++;
}
push @components, \@component_parts;
}
my $rule = GenTest::Grammar::Rule->new(
name => $rule_name,
components => \@components
);
$rules{$rule_name} = $rule;
}
$grammar->[GRAMMAR_RULES] = \%rules;
return STATUS_OK;
}
sub rule {
return $_[0]->[GRAMMAR_RULES]->{$_[1]};
}
sub rules {
return $_[0]->[GRAMMAR_RULES];
}
sub deleteRule {
delete $_[0]->[GRAMMAR_RULES]->{$_[1]};
}
#
# Check if the grammar is tagged with query properties such as RESULTSET_ or ERROR_1234
#
sub hasProperties {
if ($_[0]->[GRAMMAR_STRING] =~ m{RESULTSET_|ERROR_|QUERY_}so) {
return 1;
} else {
return 0;
}
}
##
## Make a new grammar using the patch_grammar to replace old rules and
## add new rules.
##
sub patch {
my ($self, $patch_grammar) = @_;
my $patch_rules = $patch_grammar->rules();
my $rules = $self->rules();
foreach my $ruleName (keys %$patch_rules) {
$rules->{$ruleName} = $patch_rules->{$ruleName};
}
my $new_grammar = GenTest::Grammar->new(grammar_rules => $rules);
return $new_grammar;
}
sub firstMatchingRule {
my ($self, @ids) = @_;
foreach my $x (@ids) {
return $self->rule($x) if defined $self->rule($x);
}
return undef;
}
##
## The "body" of topGrammar
##
sub topGrammarX {
my ($self, $level, $max, @rules) = @_;
if ($max > 0) {
my $result={};
foreach my $rule (@rules) {
foreach my $c (@{$rule->components()}) {
my @subrules = ();
foreach my $cp (@$c) {
push @subrules,$self->rule($cp) if defined $self->rule($cp);
}
my $componentrules =
$self->topGrammarX($level + 1, $max -1,@subrules);
if (defined $componentrules) {
foreach my $sr (keys %$componentrules) {
$result->{$sr} = $componentrules->{$sr};
}
}
}
$result->{$rule->name()} = $rule;
}
return $result;
} else {
return undef;
}
}
##
## Produce a new grammar which is the toplevel $level rules of this
## grammar
##
sub topGrammar {
my ($self, $levels, @startrules) = @_;
my $start = $self->firstMatchingRule(@startrules);
my $rules = $self->topGrammarX(0,$levels, $start);
return GenTest::Grammar->new(grammar_rules => $rules);
}
##
## Produce a new grammar keeping a masked set of rules. The mask is 16
## bits. If the mask is too short, we use the original mask as a seed
## for a random number generator and generate more 16-bit values as
## needed. The mask is applied in alphapetical order on the rules to
## ensure a deterministicresult since I don't trust the Perl %hashes
## to be always ordered the same twhen they are produced e.g. from
## topGrammar or whatever...
##
sub mask {
my ($self, $mask) = @_;
my $rules = $self->rules();
my %newRuleset;
my $i = 0;
my $prng = GenTest::Random->new(seed => $mask);
## Generate the first 16 bits.
my $mask16 = $prng->uint16(0,0x7fff);
foreach my $rulename (sort keys %$rules) {
my $rule = $self->rule($rulename);
my @newComponents;
foreach my $x (@{$rule->components()}) {
push @newComponents, $x if (1 << ($i++)) & $mask16;
if ($i % 16 == 0) {
# We need more bits!
$i = 0;
$mask = $prng->uint16(0,0x7fff);
}
}
my $newRule;
## If no components were chosen, we chose all to have a working
## grammar.
if ($#newComponents < 0) {
$newRule = $rule;
} else {
$newRule= GenTest::Grammar::Rule->new(name => $rulename,
components => \@newComponents);
}
$newRuleset{$rulename}= $newRule;
}
return GenTest::Grammar->new(grammar_rules => \%newRuleset);
}
1;
|