Kamelon/Diagnostics.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Kamelon.pm ↑ Summary Kamelon/Builder.pm »
1 package Syntax::Kamelon::Diagnostics;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 our $VERSION = '0.15';
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use Syntax::Kamelon::Indexer;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Syntax::Kamelon::XMLData;
12 use Term::ANSIColor;
13  
14 my $keylength = 20;
15 my $formatdirection = 0;
16  
17 my %styles = (
18 title => color('white bold on_green'),
19 key => color('yellow'),
20 value => color('green'),
21 error => color('yellow bold on_red'),
22 header => color('white bold'),
23 header1 => color('yellow bold'),
24 message => color('blue'),
25 normal => color('white'),
26 );
27  
28 my %parses = (
29 AnyChar => \&RuleDefault,
30 DetectChar => \&RuleDefault,
31 Detect2Chars => \&RuleDefault,
32 DetectIdentifier => \&RuleDefault,
33 DetectSpaces => \&RuleDefault,
34 Float => \&RuleDefault,
35 HlCChar => \&RuleDefault,
36 HlCHex => \&RuleDefault,
37 HlCOct => \&RuleDefault,
38 HlCStringChar => \&RuleDefault,
39 IncludeRules => \&RuleDefault,
40 Int => \&RuleDefault,
41 keyword => \&RuleKeyword,
42 LineContinue => \&RuleDefault,
43 RangeDetect => \&RuleDefault,
44 RegExpr => \&RuleRegExpr,
45 StringDetect => \&RuleDefault,
46 WordDetect => \&RuleDefault,
47 );
48  
49 my @contextkeys = qw(
50 dynamic
51 fallthrough
52 fallthroughContext
53 lineEmptyContext
54 lineEndContext
55 name
56 );
57  
58 my @langkeys = qw(
59 author
60 casesensitive
61 extensions
62 hidden
63 kateversion
64 license
65 mimetype
66 name
67 priority
68 section
69 style
70 version
71 );
72  
73 my @rulekeys = qw(
74 attribute
75 beginRegion
76 char
77 char1
78 column
79 context
80 dynamic
81 endRegion
82 firstNonSpace
83 includeAttrib
84 insensitive
85 lookAhead
86 minimal
87 String
88 );
89  
90 my %tests = (
91 attributes => \&CheckDuplicateAttributes,
92 details => \&ListDetails,
93 integrity => \&CheckIntegrity,
94 listsizes => \&CheckListSizes,
95 );
96  
97 my %operators = (
98 'smaller' => sub {
99 my ($key, $match) = @_;
100 return ($key < $match)
101 },
102 'bigger' => sub {
103 my ($key, $match) = @_;
104 return ($key > $match)
105 },
106 'smaller or equal' => sub {
107 my ($key, $match) = @_;
108 return ($key <= $match)
109 },
110 'bigger or equal' => sub {
111 my ($key, $match) = @_;
112 return ($key >= $match)
113 },
114 'equal' => sub {
115 my ($key, $match) = @_;
116 return ($key eq $match)
117 },
118 'not equal' => sub {
119 my ($key, $match) = @_;
120 return ($key ne $match)
121 },
122 'regular expression' => sub {
123 my ($key, $match) = @_;
124 return ($key =~ /$match/)
125 },
126 );
127  
128 sub new {
129 my $proto = shift;
130 my $class = ref($proto) || $proto;
131 my %args = (@_);
132
133 my $indexer = delete $args{'indexer'};
134 #catch indexer options
135 unless (defined $indexer) {
136 my @iopts = qw(indexfile noindex xmlfolder);
137 my %index = ();
138 for (@iopts) {
139 my $val = delete $args{$_};
140 if (defined $val) {
141 $index{$_} = $val;
142 }
143 }
144 $indexer = Syntax::Kamelon::Indexer->new(%index);
145 }
146
147 my $self = bless {
148 INDENT => 0,
149 INDENTSTRING => ' ',
150 CONTEXTFILTER => [],
151 CURCONTEXT => '',
152 CURRULE => '',
153 CURSYNTAX => '',
154 INDEXER => $indexer,
155 OUT => [],
156 RULEFILTER => [],
157 SHOWDETAILS => 1,
158 SHOWCONTEXT => 1,
159 SHOWLISTS => 0,
160 SHOWRULES => 1,
161 SHOWATTRIBUTES => 0,
162 SYNTAXFILTER => [],
163 }, $class;
164 $self->{OUTCALL} = sub { $self->PrintAnsi(@_) },
165  
166 return $self;
167 }
168  
169 sub AttributeExists {
170 my ($self, $attribute, $xml) = @_;
171 if (exists $xml->Attributes->{$attribute}) {
172 return 1
173 } else {
174 $self->PrintLine("Undefined attribute $attribute, will revert to 'normal'.", 'message')
175 }
176 return 0
177 }
178  
179 sub AvailableContextKeys {
180 my $self = shift;
181 return \@contextkeys;
182 }
183  
184 sub AvailableLanguageKeys {
185 my $self = shift;
186 return \@langkeys;
187 }
188  
189 sub AvailableOperators {
190 my $self = shift;
191 my @list = sort keys %operators;
192 return \@list;
193 }
194  
195 sub AvailableRuleKeys {
196 my $self = shift;
197 return \@rulekeys;
198 }
199  
200 sub AvailableSyntaxes {
201 my $self = shift;
202 my $i = $self->{INDEXER}->{INDEX};
203 return sort keys %$i
204 }
205  
206 sub CheckDuplicateAttributes {
207 my ($self, $xml) = @_;
208 }
209  
210 sub CheckContextAttribute {
211 my ($self, $attribute, $xml) = @_;
212 if (exists $xml->Attributes->{$attribute}) {
213 return 1
214 } else {
215 $self->PrintLine("Undefined attribute, will revert to 'normal'.", 'message')
216 }
217 return 0
218 }
219  
220 sub CheckListSizes {
221 my ($self, $xml) = @_;
222 my $l = $xml->Lists;
223 foreach my $name (sort keys %$l) {
224 my $list = $l->{$name};
225 my $size = @$list;
226 $self->PrintStyle("List $name: size $size\n");
227 }
228 }
229  
230 sub Clear {
231 my $self = shift;
232 $self->{OUT} = [];
233 }
234  
235 sub ContextExists {
236 my ($self, $context, $xml) = @_;
237 if ($context =~ s/##(.*)//) {
238 my $syntax = $1;
239 my $xmld = $self->GetXMLObject($syntax);
240 if (defined $xmld) {
241 if ($context eq '') {
242 $context = $xmld->BaseContext;
243 }
244 unless (exists $xmld->ContextData->{$context}) {
245 $self->PrintLine("Context $context in $syntax does not exist", 'message');
246 return 0
247 }
248 }
249 } elsif ($context =~ /^#pop/i) {
250 } elsif ($context =~ /^#stay/i) {
251 } else {
252 unless (exists $xml->ContextData->{$context}) {
253 $self->PrintLine("Context $context does not exist", 'message');
254 return 0
255 }
256 }
257 return 1
258 }
259  
260 sub ContextFilter {
261 my $self = shift;
262 if (@_) { $self->{CONTEXTFILTER} = shift; }
263 return $self->{CONTEXTFILTER};
264 }
265  
266 sub CurContext {
267 my $self = shift;
268 if (@_) { $self->{CURCONTEXT} = shift; }
269 return $self->{CURCONTEXT};
270 }
271  
272 sub CurRule {
273 my $self = shift;
274 if (@_) { $self->{CURRULE} = shift; }
275 return $self->{CURRULE};
276 }
277  
278 sub CurSyntax {
279 my $self = shift;
280 if (@_) { $self->{CURSYNTAX} = shift; }
281 return $self->{CURSYNTAX};
282 }
283  
284 sub CurXML{
285 my $self = shift;
286 my $s = $self->CurSyntax;
287 unless ($s eq '') {
288 return $self->{INDEXER}->{INDEX}->{$s}
289 }
290 }
291  
292 sub Diagnoze {
293 my ($self, $entry) = @_;
294 $self->{CURSYNTAX} = $entry;
295 my $xml = $self->GetXMLObject($entry);
296 if ($self->FilterSyntax($xml->Language)) {
297 my $txt = $self->{TXT};
298 $self->PrintLine("Syntax: $entry", 'title');
299 $self->PrintLine;
300 if ($self->{SHOWDETAILS}) {
301 $self->PrintLine("Details", 'header1');
302 $self->PrintLine;
303 $self->PrintHash($xml->Language);
304 $self->PrintLine;
305 my %h = (
306 BaseContext => $xml->BaseContext,
307 WeakDeliminators => $xml->WeakDeliminators,
308 AdditionalDeliminators => $xml->AdditionalDeliminators,
309 KeywordsCase => $xml->KeywordsCase,
310 );
311 $self->PrintHash(\%h);
312 $self->PrintLine;
313 }
314 if ($self->{SHOWLISTS}) {
315 $self->PrintLine("Lists", 'header1');
316 $self->IndentUp;
317 my $lists = $xml->Lists;
318 foreach my $l (sort keys %$lists) {
319 my $list = $lists->{$l};
320 my $size = @$list;
321 $self->PrintLine($l, 'header', " Size $size", 'message');
322 $self->PrintLine;
323 $self->IndentUp;
324 for (@$list) {
325 $self->PrintLine($_, 'value');
326 }
327 $self->IndentDown;
328 }
329 $self->IndentDown;
330 }
331 $self->PrintLine("Contexts", 'header1');
332 $self->PrintLine;
333 $self->IndentUp;
334 my $cd = $xml->ContextData;
335 foreach my $k (sort keys %$cd) {
336 $self->{CURCONTEXT} = $k;
337 my $c = $cd->{$k};
338 if ($self->FilterContext($c)) {
339 $self->PrintLine("Name: $k", 'header');
340 $self->IndentUp;
341 if ($self->{SHOWCONTEXT}) {
342 $self->PrintHash($c, qw / items /);
343 $self->PrintLine;
344 $self->CheckContextAttribute($c->{attribute}, $xml);
345 }
346 $self->PrintRelease;
347 if ($self->{SHOWRULES}) {
348 my $num = 1;
349 my $i = $cd->{$k}->{items};
350 for (@$i) {
351 $self->{CURRULE} = $num;
352 my $rule = $_;
353 if ($self->FilterRule($rule)) {
354 $self->PrintLine("Rule: $num", 'header');
355 $self->PrintHash($rule);
356 $self->CurRule($num);
357 my $type = $rule->{type};
358 my $call = $parses{$type};
359 &$call($self, $rule, $xml);
360 $self->PrintLine;
361 $num++;
362 $self->PrintRelease;
363 }
364 }
365 }
366 $self->IndentDown;
367 $self->PrintLine;
368 }
369 }
370 $self->IndentDown;
371 $self->PrintLine;
372 if ($self->{SHOWATTRIBUTES}) {
373 $self->PrintLine("Attributes", 'title');
374 $self->PrintLine;
375 $self->IndentUp;
376 $self->PrintHash($xml->Attributes);
377 $self->IndentDown;
378 $self->PrintRelease;
379 }
380 }
381 }
382  
383 sub Filter {
384 my ($self, $filter, $hash) = @_;
385 unless (@$filter) { return 2 };
386 for (@$filter) {
387 my ($key, $operator, $value, $cont) = @$_;
388 unless (exists $hash->{$key}) { return 0 }
389 my $token = $hash->{$key};
390 my $call = $operators{$operator};
391 if (&$call($token, $value)) {
392 if (($cont eq 'or') or ($cont eq '')) {
393 return 1
394 }
395 } else {
396 if (($cont eq 'and') or ($cont eq '')) {
397 return 0
398 }
399 }
400 }
401 }
402  
403 sub FilterContext {
404 my ($self, $hash) = @_;
405 my $filter = $self->{CONTEXTFILTER};
406 return $self->Filter($filter, $hash)
407 }
408  
409 sub FilterRule {
410 my ($self, $hash) = @_;
411 my $filter = $self->{RULEFILTER};
412 return $self->Filter($filter, $hash)
413 }
414  
415 sub FilterSyntax {
416 my ($self, $hash) = @_;
417 my $filter = $self->{SYNTAXFILTER};
418 return $self->Filter($filter, $hash)
419 }
420  
421 sub FormatStringLength {
422 my ($self, $str, $length) = @_;
423 unless (defined $length) { $length = $keylength }
424 if ($formatdirection) {
425 while (length($str) < $length) {
426 $str = "$str ";
427 }
428 } else {
429 while (length($str) < $length) {
430 $str = " $str";
431 }
432 }
433 return $str;
434 }
435  
436 sub GetIndexer {
437 my $self = shift;
438 return $self->{INDEXER}
439 }
440  
441 sub GetXMLObject {
442 my ($self, $syntax) = @_;
443 my $p = $self->{XMLPOOL};
444 my $id = $self->{INDEXER};
445 my $i = $id->{INDEX};
446 if (exists $p->{$syntax}) {
447 return $p->{$syntax}
448 } elsif (exists $i->{$syntax}) {
449 my $file = $id->{XMLFOLDER} . '/' . $i->{$syntax}->{'file'};
450 my $hl = Syntax::Kamelon::XMLData->new(
451 xmlfile => $file,
452 );
453 $self->{XMLPOOL}->{$syntax} = $hl;
454 return $hl
455 } else {
456 warn "XML file for $syntax is not indexed. Please load manually\n";
457 }
458 return undef;
459 }
460  
461 sub IndentDown {
462 my $self = shift;
463 my $i = $self->{INDENT};
464 if ($i > 0) {
465 $self->{INDENT} = $i - 1;
466 } else {
467 warn "indentation already 0";
468 }
469 }
470  
471 sub IndentUp {
472 my $self = shift;
473 my $i = $self->{INDENT};
474 $self->{INDENT} = $i + 1;
475 }
476  
477 sub IndentString {
478 my $self = shift;
479 if (@_) { $self->{INDENTSTRING} = shift; }
480 my $i = $self->{INDENT};
481 my $s = $self->{INDENTSTRING};
482 my $o = "";
483 if ($i) {
484 for (1 .. $i) { $o = $o . $s }
485 }
486 return $o;
487 }
488  
489  
490 sub OutCall {
491 my $self = shift;
492 if (@_) { $self->{OUTCALL} = shift; }
493 return $self->{OUTCALL};
494 }
495  
496 sub PrintAnsi {
497 my ($self, $txt, $style) = @_;
498 unless (defined($style)) { $style = 'normal' }
499 unless (exists $styles{$style}) {
500 warn "invalid style, $style";
501 $style = 'normal',
502 }
503 print $styles{$style}, $txt, color('reset');
504 }
505  
506 sub PrintClear {
507 my $self = shift;
508 $self->{OUT} = [];
509 }
510  
511 sub PrintHash {
512 my $self = shift;
513 my $h = shift;
514 my %exclude = ();
515 for (@_) {
516 $exclude{$_} = 1;
517 }
518 my $length = $keylength;
519 foreach my $k (sort keys %$h) {
520 unless (exists $exclude{$k}) {
521 my $l = length $k;
522 if ($l > $length) { $length = $l }
523 }
524 }
525 foreach my $k (sort keys %$h) {
526 unless (exists $exclude{$k}) {
527 my $l = $k;
528 $l = $self->FormatStringLength($l, $length);
529 $self->PrintStyle("$l: ", 'key');
530 my $i = $self->{INDENT};
531 $self->{INDENT} = 0;
532 $self->PrintStyle($h->{$k} . "\n", 'value');
533 $self->{INDENT} = $i;
534 }
535 }
536 }
537  
538 sub PrintLine {
539 my $self = shift;
540 my @o = ("\n", 'normal');
541 unless (@_) { $self->PrintStyle(@o) }
542 while (@_) {
543 my $txt = shift;
544 my $style = shift;
545 $self->PrintStyle($txt, $style);
546 $self->PrintStyle(@o);
547 }
548 }
549  
550 sub PrintRelease {
551 my $self = shift;
552 my $o = $self->{OUT};
553 my $call = $self->{OUTCALL};
554 while (@$o) {
555 my $txt = shift @$o;
556 my $style = shift @$o;
557 &$call($txt, $style);
558 }
559
560 }
561  
562 sub PrintStyle {
563 my $self = shift;
564 my $o = $self->{OUT};
565 while (@_) {
566 my $txt = shift;
567 my $style = shift;
568 push @$o, $self->IndentString . $txt, $style;
569 # &$call($self->IndentString . $txt, $style);
570 }
571 }
572  
573 sub RuleAnyChar {
574 }
575  
576 sub RuleDefault {
577 my ($self, $rule, $xml) = @_;
578 my $result = 1;
579 if (exists $rule->{context}) {
580 unless ($self->ContextExists($rule->{context}, $xml)) {
581 $result = 0
582 }
583 }
584 if (exists $rule->{attribute}) {
585 unless ($self->AttributeExists($rule->{attribute}, $xml)) {
586 $result = 0
587 }
588 }
589 return $result
590 }
591  
592 sub RuleDetectChar {
593 my ($self, $rule, $xml) = @_;
594 my $result = 1;
595 unless ($self->RuleDefault) {
596 $result = 0;
597 }
598 return $result
599 }
600  
601 sub RuleDetect2Chars {
602 my ($self, $rule, $xml) = @_;
603 my $result = 1;
604 unless ($self->RuleDefault) {
605 $result = 0;
606 }
607 return $result
608 }
609  
610 sub RuleFilter {
611 my $self = shift;
612 if (@_) { $self->{RULEFILTER} = shift; }
613 return $self->{RULEFILTER};
614 }
615  
616 sub RuleIncludeRules {
617 }
618  
619 sub RuleInt {
620 }
621  
622 sub RuleKeyword {
623 my ($self, $rule, $xml) = @_;
624 my $result = 1;
625 unless ($self->RuleDefault) {
626 $result = 0;
627 }
628 my $name = $rule->{String};
629 my $lists = $xml->{LISTS};
630 unless (exists $lists->{$name}) {
631 $self->PrintLine("List $name does not exist. Rule will be ignored", 'message');
632 $result = 0;
633 }
634 return $result
635 }
636  
637 sub RuleLineContinue {
638 }
639  
640 sub RuleRangeDetect {
641 my ($self, $rule, $xml) = @_;
642 my $result = 1;
643 unless ($self->RuleDefault) {
644 $result = 0;
645 }
646 return $result
647 }
648  
649 sub RuleRegExpr {
650 my ($self, $rule, $xml) = @_;
651 # print "testing RuleRegExpr\n";
652 my $result = 1;
653 unless ($self->RuleDefault) {
654 $result = 0;
655 }
656 my $reg = $rule->{String};
657 eval { qr/$reg/ };
658 if ($@) {
659 my $msg = $@;
660 $msg =~ s/\sat\s\/[^\n]+\n//;
661 $self->PrintLine("$msg Rule will be ignored", 'message');
662 $result = 0;
663 }
664 return $result
665 }
666  
667 sub RuleStringDetect {
668 }
669  
670 sub RuleWordDetect {
671 }
672  
673 sub ShowAttributes {
674 my $self = shift;
675 if (@_) { $self->{SHOWATTRIBUTES} = shift; }
676 return $self->{SHOWATTRIBUTES};
677 }
678  
679 sub ShowContext {
680 my $self = shift;
681 if (@_) { $self->{SHOWCONTEXT} = shift; }
682 return $self->{SHOWCONTEXT};
683 }
684  
685 sub ShowDetails {
686 my $self = shift;
687 if (@_) { $self->{SHOWDETAILS} = shift; }
688 return $self->{SHOWDETAILS};
689 }
690  
691 sub ShowLists {
692 my $self = shift;
693 if (@_) { $self->{SHOWLISTS} = shift; }
694 return $self->{SHOWLISTS};
695 }
696  
697 sub ShowRules {
698 my $self = shift;
699 if (@_) { $self->{SHOWRULES} = shift; }
700 return $self->{SHOWRULES};
701 }
702  
703 sub SyntaxFilter {
704 my $self = shift;
705 if (@_) { $self->{SYNTAXFILTER} = shift; }
706 return $self->{SYNTAXFILTER};
707 }
708  
709  
710  
711 1;
712  
713 __END__
« Kamelon.pm ↑ Summary Kamelon/Builder.pm »