Eyapp/Cleaner.pm untested

filter: Statementstmt 0.0% 0 / 212 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 125 MC/DCmcdc 0.0% 0 / 71 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 495 CC 36 SCAR 60.2
CC
36
Cov
0%
ppcontroller
702.0
controller
30.0
_Lexer
30.0
CRAP 413.0
1 package Parse::Eyapp::Cleaner;
2 use warnings;
3 use strict;
4 use Scalar::Util qw{reftype};
5  
6 my $lexlevel = 0; # Used by the lexical analyzer. Controls in which section we are:
7 my (
8 $input,
9 # head (0), body(1) or tail (2)
10 @lineno, # Used by the lexical analyzer. $lineno[0] is the line number for
11 # the beginning of the token, $lineno[1] the end
12 $nberr, # Number of errors up to now
13 );
14  
15 my $filename;
16 my $bypass = 0;
17  
18 sub trim {
19 $_[0] =~ s/\s+$//;
20 $_[0] =~ s/^\s+//;
21 }
22  
23 sub controller {
24 my $ouput = '';
25  
26 local $/= undef;
27 $$input = <>;
28  
29 while () {
30  
31 my ($token, $attr) = _Lexer();
32  
33 last unless $token;
34  
35 if ($token eq '.') {
36 ($token, $attr) = _Lexer();
37 next;
38 }
39 next unless defined(reftype($attr)) && defined($attr->[0]);
40 print $attr->[0] unless ($token =~ /(CODE)|\$/);
41 }
42 print "\n";
43 }
44  
45 {
46  
47 my $output = '';
48  
49 sub _generate {
50 $output .= sprintf "%s"x@_, @_;
51 }
52  
53 sub ppcontroller {
54 $input = shift;
55 my %args = @_;
56  
57 my $skipcomments = $args{skipcomments}? '|COMMENT' : '';
58  
59 my $depth = 0;
60  
61 my $delete_set = 'CODE|BLANKS|DEFAULTACTION|\n|\$'.$skipcomments;
62 $delete_set = qr{$delete_set};
63  
64 my $end_cr_set = qr{\n\s*$|^\s*$};
65  
66  
67 my $ouput = '';
68  
69 my ($ptoken, $pattr) = ('', ['', -1]);
70 while () {
71  
72 my ($token, $attr) = _Lexer();
73  
74 last unless $token;
75  
76 next if $token eq '$';
77 next unless defined(reftype($attr)) && defined($attr->[0]);
78  
79 if ($token eq '.') { # attribute name
80 ($token, $attr) = _Lexer();
81 }
82 elsif ($token eq 'NAME') {
83 trim($attr->[0]);
84 my $g = ($depth == 0)? "\n " : " ";
85 _generate $attr->[0].$g;
86 }
87 elsif ($token =~ /\b(VARIABLE)\b/) {
88 my $g = ($output =~ $end_cr_set)? '': "\n";
89 $attr->[0] =~ s/\s*:\s*$/:/; # remove blanks before and after colon
90 _generate $g.$attr->[0]."\n ";
91 }
92 elsif ($token =~ /\b(IDENT|LITERAL|NUMBER|REGEXP)\b/) {
93 _generate $attr->[0]." ";
94 }
95 elsif ($token =~ /(PREC|STAR\b|PLUS|OPTION|[)(])/) {
96 $depth++ if $token eq '(';
97 $depth-- if $token eq ')';
98 _generate $attr->[0]." ";
99 }
100 elsif ($token =~ /\b(TOKEN|ASSOC|CONFLICT|SYNTACTIC|SEMANTIC|STRICT|START|EXPECT|NAMINGSCHEME|LEXER|UNION)\b/) {
101 my $g = ($output =~ $end_cr_set)? '': "\n";
102 _generate $g.$attr->[0]." ";
103 }
104 elsif ($token =~ /TREE/) {
105 _generate "\n".$attr->[0]."\n";
106 }
107 elsif ($token eq ':') {
108 _generate ":\n ";
109 }
110 elsif ($token eq '|') {
111 $output =~ s/[ \t]*$//;
112 my $g = ($output =~ $end_cr_set)? '': "\n";
113 _generate "$g | ";
114 #_generate "\n ".$attr->[0]." ";
115 }
116 elsif ($token eq ';') {
117 my $g;
118 if ($output =~ m{[:|]\s*$}) {
119 $g = "/* empty */\n";
120 }
121 elsif ($output =~ $end_cr_set) {
122 $g = '';
123 }
124 else {
125 $g = "\n";
126 }
127 _generate "$g;\n";
128 }
129 elsif ($token eq '%%') {
130 my $g = ($output =~ $end_cr_set)? '': "\n";
131 _generate "$g\n%%\n\n";
132 }
133 else {
134 _generate $attr->[0] unless ($token =~ $delete_set);
135 }
136 ($ptoken, $pattr) = ($token, $attr);
137 }
138 _generate "\n";
139  
140 $output =~ s/\s*\Z/\n/;
141 return $output;
142 }
143 } # end closure
144  
145 sub slurp_perl_code {
146 my($level,$from,$code);
147  
148 $from=pos($$input);
149  
150 $level=1;
151 while($$input=~/([{}])/gc) {
152 substr($$input,pos($$input)-1,1) eq '\\' #Quoted
153 and next;
154 $level += ($1 eq '{' ? 1 : -1)
155 or last;
156 }
157 $level
158 and _SyntaxError(2,"Unmatched { opened line $lineno[0]",-1);
159 $code = substr($$input,$from,pos($$input)-$from-1);
160 $lineno[1]+= $code=~tr/\n//;
161 return [ $code, $lineno[0] ];
162 }
163  
164  
165 sub _Lexer {
166
167 #At EOF
168 pos($$input) && (pos($$input) >= length($$input))
169 and return('',[ '', -1 ]);
170  
171 #In TAIL section
172 $lexlevel > 1
173 and do {
174 my($pos)=pos($$input);
175  
176 $lineno[0]=$lineno[1];
177 $lineno[1]=-1;
178 pos($$input)=length($$input);
179 };
180  
181 #Skip blanks
182 $lexlevel == 0
183 ? $$input=~m{\G((?: # Head section: \n separates declarations
184 [\t\ ]+ # Any white space char but \n
185 )+)}xsgc
186 : $$input=~m{\G((?:
187 \s+ # any white space char, including \n
188 )+)}xsgc
189 and do {
190 my($blanks)=$1;
191  
192 #Maybe At EOF
193 pos($$input) >= length($$input)
194 and return('',[ $blanks, -1 ]);
195  
196 $lineno[1]+= $blanks=~tr/\n//;
197 return ('BLANKS', [ $blanks, $lineno[0]]);
198 };
199  
200 $$input=~m{\G((?:
201 \#[^\n]*\s* # Perl like comments
202 | /\*.*?\*/\s* # C like comments
203 )+)}xsgc
204 and do {
205 my($blanks)=$1;
206  
207 #Maybe At EOF
208 pos($$input) >= length($$input)
209 and return('',[ $blanks, -1 ]);
210  
211 $lineno[1]+= $blanks=~tr/\n//;
212 trim($blanks);
213 return ('COMMENT', [ $blanks, $lineno[0]]);
214 };
215  
216 $lineno[0]=$lineno[1];
217  
218 $$input=~/\G([A-Za-z_][A-Za-z0-9_]*\s*:)/gc
219 and return('VARIABLE',[ $1, $lineno[0] ]);
220  
221 $$input=~/\G([A-Za-z_][A-Za-z0-9_]*)/gc
222 and return('IDENT',[ $1, $lineno[0] ]);
223  
224 $$input =~ m{\G(
225 / # opening slash
226 (?:[^/\\]| # an ordinary character
227 \\\\| # escaped \ i.e. \\
228 \\/| # escaped slash i.e. \/
229 \\ # escape i.e. \
230 )*? # non greedy repetitions
231 / # closing slash
232 )
233 }xgc and return('REGEXP',[ $1, $lineno[0] ]);
234  
235  
236 $$input=~/\G( ' # opening apostrophe
237 (?:[^'\\]| # an ordinary character
238 \\\\| # escaped \ i.e. \\
239 \\'| # escaped apostrophe i.e. \'
240 \\ # escape i.e. \
241 )*? # non greedy repetitions
242 ' # closing apostrophe
243 )/gxc
244 and do {
245 my $string = $1;
246  
247 # The string 'error' is reserved for the special token 'error'
248 $string eq "'error'"
249 and do {
250 _SyntaxError(0,"Literal 'error' ".
251 "will be treated as error token",$lineno[0]);
252 return('IDENT',[ 'error', $lineno[0] ]);
253 };
254  
255 my $lines = $string =~ tr/\n//;
256 _SyntaxError(2, "Constant string $string contains newlines",$lineno[0]) if $lines;
257 $lineno[1] += $lines;
258 return('LITERAL',[ $string, $lineno[0] ]);
259 };
260  
261 # New section: body or tail
262 $$input=~/\G(%%)/gc
263 and do {
264 ++$lexlevel;
265 return($1, [ $1, $lineno[0] ]);
266 };
267  
268  
269 $$input=~/\G%begin\s*{/gc
270 and do {
271 return ('BEGINCODE', &slurp_perl_code());
272 };
273  
274 $$input=~/\G{/gc
275 and do {
276 &slurp_perl_code();
277 };
278  
279 if($lexlevel == 0) {# In head section
280 $$input=~/\G(%(left|right|nonassoc))/gc
281 and return('ASSOC',[ $1, $lineno[0] ]);
282  
283 $$input=~/\G(%start)/gc
284 and return('START',[ $1, $lineno[0] ]);
285  
286 $$input=~/\G(%expect)/gc
287 and return('EXPECT',[ $1, $lineno[0] ]);
288  
289 $$input=~/\G(%namingscheme)/gc
290 and return('NAMINGSCHEME',[ $1, $lineno[0] ]);
291  
292 $$input=~/\G%{/gc
293 and do {
294 my($code);
295  
296 $$input=~/\G(.*?)%}/sgc
297 or _SyntaxError(2,"Unmatched %{ opened line $lineno[0]",-1);
298  
299 $code=$1;
300 $lineno[1]+= $code=~tr/\n//;
301 return('HEADCODE',[ $code, $lineno[0] ]);
302 };
303 $$input=~/\G(%token)/gc
304 and return('TOKEN',[ $1, $lineno[0] ]);
305  
306 $$input=~/\G(%conflict)/gc
307 and return('CONFLICT',[ $1, $lineno[0] ]);
308  
309  
310 $$input=~/\G(%strict)/gc
311 and return('STRICT',[ $1, $lineno[0] ]);
312  
313 $$input=~/\G(%semantic\s+token)/gc
314 and return('SEMANTIC',[ $1, $lineno[0] ]);
315  
316 $$input=~/\G(%syntactic\s+token)/gc
317 and return('SYNTACTIC',[ $1, $lineno[0] ]);
318  
319 $$input=~/\G(%type)/gc
320 and return('TYPE',[ $1, $lineno[0] ]);
321  
322 $$input=~/\G%prefix\s+([A-Za-z_][A-Za-z0-9_:]*::)/gc
323 and return('PREFIX',[ $1, $lineno[0] ]);
324  
325 $$input=~/\G(%union)/gc
326 and return('UNION',[ $1, $lineno[0] ]);
327  
328 $$input=~/\G(%lexer)/gc
329 and return('LEXER',[ $1, $lineno[0] ]);
330  
331 $$input=~/\G(%defaultaction)/gc
332 and return('DEFAULTACTION',[ $1, $lineno[0] ]);
333  
334 $$input=~/\G(%tree((?:\s+(?:bypass|alias)){0,2}))/gc
335 and do {
336 my $treeoptions = defined($2)? $2 : '';
337 return('TREE',[ $1, $lineno[0] ])
338 };
339  
340 $$input=~/\G(%metatree)/gc
341 and return('METATREE',[ $1, $lineno[0] ]);
342  
343 $$input=~/\G([0-9]+)/gc
344 and return('NUMBER',[ $1, $lineno[0] ]);
345  
346 }
347 else {# In rule section
348 $$input=~/\G(%prec)/gc
349 and return('PREC',[ $1, $lineno[0] ]);
350  
351 $$input=~/\G((<\s*%name\s*([A-Za-z_][A-Za-z0-9_]*)\s*)?\*\s*>)/gc
352 and return('STAR',[ $1, $lineno[0] ]);
353  
354 $$input=~/\G((%name\s*([A-Za-z_][A-Za-z0-9_]*)\s*)?\*)/gc
355 and return('STAR',[ $1, $lineno[0] ]);
356  
357 $$input=~/\G((<\s*%name\s*([A-Za-z_][A-Za-z0-9_]*)\s*)?\+\s*>)/gc
358 and return('PLUS',[ $1, $lineno[0] ]);
359  
360 $$input=~/\G((%name\s*([A-Za-z_][A-Za-z0-9_]*)\s*)?\+)/gc
361 and return('PLUS',[ $1, $lineno[0] ]);
362  
363 $$input=~/\G((<\s*%name\s*([A-Za-z_][A-Za-z0-9_]*)\s*)?\?\s*)>/gc
364 and return('OPTION',[ $1, $lineno[0] ]);
365  
366 $$input=~/\G((%name\s*([A-Za-z_][A-Za-z0-9_]*)\s*)?\?)/gc
367 and return('OPTION',[ $1, $lineno[0] ]);
368  
369 $$input=~/\G(%no\s+bypass\s+[A-Za-z_][A-Za-z0-9_]*\s*)/gc
370 and do {
371 return('NAME',[ $1, $lineno[0] ]);
372 };
373  
374 $$input=~/\G(%name\s+[\w:]*\n?)/gc
375 and do {
376 return('NAME',[ $1, $lineno[0] ]);
377 };
378 }
379  
380 #Always return something
381 $$input=~/\G(.)/sg
382 or return ('', ['', -1]);
383  
384 $1 eq "\n"
385 and ++$lineno[1];
386  
387 ( $1 ,[ $1, $lineno[0] ]);
388  
389 }
390  
391 sub _SyntaxError {
392 my($level,$message,$lineno)=@_;
393  
394 $message= "*".
395 [ 'Warning', 'Error', 'Fatal' ]->[$level].
396 "* $message, at ".
397 ($lineno < 0 ? "eof" : "line $lineno")." at file $filename\n";
398  
399 $level > 1
400 and die $message;
401  
402 warn $message;
403  
404 $level > 0
405 and ++$nberr;
406  
407 $nberr == 20
408 and die "*Fatal* Too many errors detected.\n"
409 }
410  
411 1;
412