Kamelon.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/Indexer.pm ↑ Summary Kamelon/Diagnostics.pm »
1 package Syntax::Kamelon;
2  
3 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 0 ▶use Syntax::Kamelon::Builder;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Syntax::Kamelon::Indexer;
11 use Module::Load::Conditional qw[can_load];
12 use Data::Dumper;
13  
14 our $VERSION = '0.26';
15  
16 my @attributes = qw (
17 Alert
18 Annotation
19 Attribute
20 BaseN
21 BuiltIn
22 Char
23 Comment
24 CommentVar
25 Constant
26 ControlFlow
27 DataType
28 DecVal
29 Documentation
30 Error
31 Extension
32 Float
33 Function
34 Import
35 Information
36 Keyword
37 Normal
38 Operator
39 Others
40 Preprocessor
41 RegionMarker
42 SpecialChar
43 SpecialString
44 String
45 Variable
46 VerbatimString
47 Warning
48 );
49  
50 sub new {
51 my $proto = shift;
52 my $class = ref($proto) || $proto;
53 my %args = (@_);
54
55 #catch indexer options
56 my @iopts = qw(indexfile noindex xmlfolder);
57 my %indexer = ();
58 for (@iopts) {
59 my $val = delete $args{$_};
60 if (defined $val) {
61 $indexer{$_} = $val;
62 }
63 }
64  
65 my $format = delete $args{formatter};
66 my $cmnds = delete $args{commands};
67 my $logcall = delete $args{logcall};
68 my $syntax = delete $args{syntax};
69 my $verbose = delete $args{verbose};
70  
71 if (%args) {
72 for (keys %args) {
73 warn "unrecognized option: $_"
74 }
75 }
76 my $self = {}; #forego XS routines for now.
77 bless ($self, $class);
78  
79 unless (defined $cmnds) { $cmnds = {} }
80  
81 #this one must be defined before we configure the formatter
82 $self->{INDEXER} = Syntax::Kamelon::Indexer->new(%indexer);
83 #configure the formatter
84 unless (defined($format)) { $format = ['Base'] }
85 $self->InitFormatter($format);
86  
87 unless (defined $logcall) { $logcall = sub { print STDERR shift, "\n" } }
88 unless (defined($syntax)) { $syntax = '' };
89 unless (defined($verbose)) { $verbose = 0 };
90  
91 $self->{CAPTURED} = [];
92 $self->{COMMANDS} = [];
93 $self->{CURRENTLINE} = '';
94 $self->{HLPOOL} = {};
95 $self->{LOGCALL} = $logcall;
96 $self->{LINENUMBER} = 1;
97 $self->{LINESEGMENT} = '';
98 $self->{OUT} = [];
99 $self->{POSTCREATE} = [];
100 $self->{PULLED} = 0;
101 $self->{SNIPPET} = '';
102 $self->{SNIPPETATTRIBUTE} = $self->Formatter->FormatTable('Normal');
103 $self->{STACK} = [];
104 $self->{SYNTAX} = $syntax;
105 $self->{USEATTRIBSTACK} = [];
106 $self->{VERBOSE} = $verbose;
107 $self->Reset;
108  
109 return $self;
110 }
111  
112 sub AvailableAttributes {
113 my $self = shift;
114 return @attributes
115 }
116  
117 sub AvailableSyntaxes {
118 my $self = shift;
119 return $self->{INDEXER}->AvailableSyntaxes;
120 }
121  
122 sub Capture {
123 my $self = shift;
124 my $c = $self->{CAPTURED};
125 push @$c, @_;
126 }
127  
128 sub CapturedGet {
129 my ($self, $num) = @_;
130 $num --;
131 my $stack = $self->{STACK};
132 if (defined $stack->[0]->[2]->[$num]) {
133 return $stack->[0]->[2]->[$num];
134 }
135 return '';
136 }
137  
138 sub CapturedParse {
139 my ($self, $string) = @_;
140 my $s = '';
141 while ($string ne '') {
142 if ($string =~ s/^([^\%]*)\%(\d)//) {
143 my $r = $self->CapturedGet($2);
144 if ($r ne '') {
145 $s = $s . $1 . $r
146 } else {
147 $s = $s . $1 . '%' . $2;
148 $self->LogWarning("Capture $2 not found");
149 }
150 } else {
151 $string =~ s/^(.)//;
152 $s = "$s$1";
153 }
154 }
155 return $s;
156 }
157  
158 sub CapturedParseC {
159 my ($self, $string) = @_;
160 if ($string =~ s/^(\d)//) {
161 return $self->CapturedGet($1);
162 }
163 $self->LogWarning("Capture $string not found");
164 return $string;
165 }
166  
167 sub CapturedParseR {
168 my ($self, $string) = @_;
169 my $s = '';
170 my @vn = (qw/a b c d e f g h/);
171 my @out = ();
172 my $num = 0;
173 while ($string ne '') {
174 if ($string =~ s/^([^\%]*)\%(\d)//) {
175 my $r = $self->CapturedGet($2);
176 if ($r ne '') {
177 push @out, $r;
178 $s = $s . $1 . "\$" . $vn[$num];
179 } else {
180 $s = $s . $1 . '%' . $2;
181 $self->LogWarning("Capture $2 not found");
182 }
183 $num ++;
184 } else {
185 $string =~ s/^(.)//;
186 $s = "$s$1";
187 }
188 }
189 return ($s, @out);
190 }
191  
192 sub ClearLexers {
193 my $self = shift;
194 $self->{HLPOOL} = {}
195 }
196  
197 sub Column {
198 my $self = shift;
199 my $l = length($self->{LINESEGMENT});
200 return $l
201 }
202  
203 sub CommandExecute {
204 my ($self, $cmnd, $parse) = @_;
205 my $c = $self->{COMMANDS};
206 if (exists $c->{$cmnd}) {
207 my $ref = $c->{$cmnd};
208 my $call;
209 my @o = ($parse);
210 if ($ref =~/ARRAY/) { #it is a method with an owner
211 $call = $ref->[0];
212 unshift @o, $ref->[1];
213 } else { #it is an anonymous sub
214 $call = $ref;
215 }
216 return &$call(@o);
217 }
218 $self->LogWarning("Command $cmnd not found");
219 return $parse;
220 }
221  
222 sub CurrentLine {
223 my $self = shift;
224 return $self->{CURRENTLINE};
225 }
226  
227 sub FirstNonSpace {
228 my ($self, $string) = @_;
229 my $line = $self->{LINESEGMENT};
230 if ($line eq '') { return 1 }
231 if (($line =~ /^\s*$/) and ($string =~ /^[^\s]/)) {
232 return 1
233 }
234 return ''
235 }
236  
237 sub Format {
238 my $self = shift;
239 return $self->Formatter->Format;
240 }
241  
242 sub Formatter {
243 my $self = shift;
244 return $self->{FORMATTER}
245 }
246  
247 sub GetIndexer {
248 my $self = shift;
249 return $self->{INDEXER}
250 }
251  
252 sub GetLexer {
253 my ($self, $syntax) = @_;
254 my $pool = $self->{HLPOOL};
255 my $id = $self->{INDEXER};
256 my $i = $id->{INDEX};
257  
258 return $pool->{$syntax} if exists $pool->{$syntax}; #syntax definition is already loaded
259
260 if (exists $i->{$syntax}) { #create the syntax definition
261 my $file = $id->{XMLFOLDER} . '/' . $i->{$syntax}->{'file'};
262 my $hl = Syntax::Kamelon::Builder->new(
263 engine => $self,
264 xmlfile => $file,
265 );
266 $pool->{$syntax} = $hl;
267 return $hl
268 }
269 $self->LogWarning("Syntax definition for '$syntax' not found");
270 return undef
271 }
272  
273 sub IncludeRules {
274 my ($self, $text, $callbacklist, $debuginfo, $inclattr) = @_;
275 $self->UseAttribStackPush($inclattr);
276 my $r = $self->ParseContext(\$text, $callbacklist, $debuginfo);
277 $self->UseAttribStackPull;
278 return $r;
279 }
280  
281 sub IncludeSyntax {
282 my ($self, $text, $syntax, $context) = @_;
283 my $hl = $self->GetLexer($syntax);
284 if ($context eq '') {
285 $context = $hl->{basecontext};
286 }
287 my $callbacklist = $hl->{contexts}->{$context}->{callbacks};
288 my $debuginfo = $hl->{contexts}->{$context}->{debug};
289 return $self->ParseContext($text, $callbacklist, $debuginfo);
290 }
291  
292 sub IncludeSyntaxIA {
293 my ($self, $text, $syntax, $context, $attr) = @_;
294 my $hl = $self->GetLexer($syntax);
295 if ($context eq '') {
296 $context = $hl->{basecontext};
297 }
298 $self->UseAttribStackPush($attr);
299 my $callbacklist = $hl->{contexts}->{$context}->{callbacks};
300 my $debuginfo = $hl->{contexts}->{$context}->{debug};
301 my $r = $self->ParseContext($text, $callbacklist, $debuginfo);
302 $self->UseAttribStackPull;
303 return $r;
304 }
305  
306 sub InitFormatter {
307 my ($self, $form) = @_;
308 my @fopt = @$form;
309 my $module = shift @fopt;
310 $module = "Syntax::Kamelon::Format::$module";
311 if (can_load(modules => {$module => 0})){
312 $self->{FORMATTER} = $module->new($self, @fopt);
313 } else {
314 warn "unable to load formatter $module\n";
315 }
316 }
317  
318 sub LastcharDeliminator {
319 my ($self, $delim) = @_;
320 return 1 if $self->LineStart;
321 my $last = $self->LastChar;
322 # return 1 if $last eq '';
323 return $last =~ /$delim/
324 }
325  
326 sub LastChar {
327 my $self = shift;
328 my $l = $self->{LINESEGMENT};
329 if ($l eq '') { return "\n" } #last character was a newline
330 return substr($l, length($l) - 1, 1);
331 }
332  
333 sub LineNumber {
334 my $self = shift;
335 $self->{LINENUMBER} = shift if @_;
336 return $self->{LINENUMBER}
337 }
338  
339 sub LineEndContext {
340 my ($self, $shifter) = @_;
341 $self->{PULLED} = 0;
342 &$shifter;
343 if ($self->{PULLED}) {
344 my $top = $self->{STACK}->[0];
345 my ($syn, $con) = @$top;
346 my $shifter2 = $syn->{contexts}->{$con}->{endcontext};
347 $self->LineEndContext($shifter2);
348 }
349 }
350  
351 sub LineStart {
352 my $self = shift;
353 return ($self->{LINESEGMENT} eq '')
354 }
355  
356 sub LogCallGet {
357 my $self = shift;
358 return $self->{LOGCALL};
359 }
360  
361 sub LogCallSet {
362 my $self = shift;
363 if (@_) { $self->{LOGCALL} = shift; }
364 }
365  
366 sub LogWarning {
367 my ($self, $warning) = @_;
368 if ($self->{VERBOSE}) {
369 my $top = $self->StackTop;
370 if (defined $top) {
371 my $lang = $top->[0]->{syntax};
372 my $context = $top->[1];
373 $warning = "Syntax: $lang, Context: $context, $warning\n";
374 } else {
375 $warning = "$warning\n STACK IS EMPTY: PANIC\n"
376 }
377 my $call = $self->{LOGCALL};
378 &$call($warning);
379 }
380 }
381  
382 sub Parse {
383 my ($self, $text) = @_;
384 $self->{SNIPPET} = '';
385 my $form = $self->{FORMATTER};
386 my $out = $self->{OUT};
387 @$out = ();
388 while ($text ne '') {
389 if ($text =~ s/^([^\n]*\n)//) {
390 $self->ParseLine($1);
391 $self->SnippetForce;
392 $form->Parse(@$out);
393 @$out = ();
394 } else {
395 $self->ParseLine($text);
396 $text = '';
397 $self->SnippetForce;
398 $form->Parse(@$out);
399 @$out = ();
400 }
401 }
402 }
403  
404 sub ParseLine {
405 my ($self, $text) = @_;
406 $self->{CURRENTLINE} = $text;
407 while ($text ne '') {
408 my $top = $self->{STACK}->[0];
409 my ($hl, $context) = @$top;
410 my $ctd = $hl->{contexts}->{$context};
411 if ($text =~ s/^(\n)//) { #newline detected
412 if ($self->LineStart) {
413 my $m = $ctd->{emptycontext};
414 &$m;
415 }
416 $self->SnippetForce;
417 $self->LineEndContext($ctd->{endcontext});
418 # my $attr = $ctd->{attribute};
419 # $self->SnippetParse($1, $attr);
420 $self->SnippetForce;
421 $self->{LINESEGMENT} = '';
422 $self->{LINENUMBER} = $self->{LINENUMBER} + 1;
423 } else {
424 my $callbacklist = $ctd->{callbacks};
425 unless ($self->ParseContext(\$text, $callbacklist)) {
426 my $f = $ctd->{fallthroughcontext};
427 if (defined($f)) {
428 &$f;
429 } else {
430 $text =~ s/^(.)//;
431 my $attr = $self->{USEATTRIBSTACK}->[0];
432 unless (defined $attr) {
433 $attr = $ctd->{attribute};
434 }
435 $self->SnippetParse($1, $attr);
436 }
437 }
438 }
439 }
440 }
441  
442 sub ParseContext {
443 my ($self, $text, $callbacklist) = @_;
444 my $r = 0;
445 if ($self->StackTop->[1] ne 'aspsource') {
446 }
447 for (@$callbacklist) {
448 my @i = @$_;
449 my $call = shift @i;
450 return 1 if &$call($self, $text, @i);
451 }
452 return '';
453 }
454  
455 sub ParseRaw {
456 my ($self, $text) = @_;
457 $self->{SNIPPET} = '';
458 my $out = $self->{OUT};
459 @$out = ();
460 # my $call = $modecalls{$self->{MODE}};
461 while ($text ne '') {
462 if ($text =~ s/^([^\n]+\n)//) {
463 # &$call($self, $1);
464 $self->ParseLine($1);
465 } else {
466 # &$call($self, $text);
467 $self->ParseLine($text);
468 $text = ''
469 }
470 }
471 $self->SnippetForce;
472 return @$out;
473 }
474  
475 sub ParseResult {
476 my ($self, $text, $string, $context, $attr) = @_;
477 $$text = substr($$text, length($string));
478 unless (defined($attr)) {
479 my $t = $self->{STACK}->[0];
480 my ($thl, $ctext) = @$t;
481 $attr = $thl->{contexts}->{$ctext}->{attribute};
482 }
483 $self->SnippetParse($string, $attr);
484 &$context;
485 return 1
486 }
487  
488 sub ParseResultChained {
489 my ($self, $text, $string, $context, $attr) = @_;
490 $$text = substr($$text, length($string));
491 unless (defined($attr)) {
492 my $t = $self->{STACK}->[0];
493 my ($thl, $ctext) = @$t;
494 $attr = $thl->{contexts}->{$ctext}->{attribute};
495 }
496 $self->SnippetParse($string, $attr);
497 &$context;
498 my $parser = pop @_;
499 return &$parser($self, $text, $string, @_);
500 }
501  
502 sub ParseResultCommand {
503 my $self = shift;
504 my $text = shift;
505 my $match = shift;
506 my $cmnd = pop @_;
507 $match = $self->CommandExecute($cmnd, $match);
508 my $parser = pop @_;
509 return &$parser($self, $text, $match, @_);
510 }
511  
512 sub ParseResultLookAhead {
513 my ($self, $text, $string, $context, $attr) = @_;
514 &$context;
515 return 1
516 }
517  
518 sub ParseResultChainedLookAhead {
519 my ($self, $text, $string, $context, $attr) = @_;
520 &$context;
521 my $parser = pop @_;
522 return &$parser($self, $text, $string, @_);
523 }
524  
525 sub ParseResultOverStrike {
526 my $self = shift;
527 my $text = shift;
528 my $match = shift;
529 my $ovr = pop @_;
530 my $parser = pop @_;
531 return &$parser->ParseResult($self, $text, substr($ovr, 0, length($match)), @_);
532 }
533  
534 sub ParseResultBeginRegion {
535 my $self = shift;
536 my $region = pop @_;
537 $self->{FORMATTER}->FoldBegin($region);
538 my $parser = pop @_;
539 return &$parser($self, @_);
540 }
541  
542 sub ParseResultEndRegion {
543 my $self = shift;
544 my $region = pop @_;
545 $self->{FORMATTER}->FoldEnd($region);
546 my $parser = pop @_;
547 return &$parser($self, @_);
548 }
549  
550 sub ParseResultReplace {
551 my $self = shift;
552 my $text = shift;
553 my $match = shift;
554 my $replace = pop @_;
555 my $parser = pop @_;
556 return &$parser($self, $text, $replace, @_);
557 }
558  
559 sub Reset {
560 my $self = shift;
561 my $lang = $self->Syntax;
562 $self->{OUT} = [];
563 $self->{SNIPPET} = '';
564 $self->{LINESEGMENT} = '';
565 my $linenumber = $self->Formatter->LineOffset;
566 unless (defined $linenumber) { $linenumber = 1 }
567 $self->{LINENUMBER} = $linenumber;
568 $self->{FORMATTER}->Reset;
569 if ($lang eq '') {
570 $self->{STACK} = [];
571 } else {
572 my $hl = $self->GetLexer($lang);
573 unless (defined $hl) {
574 if ($self->Debug) {
575 croak "Lexer for syntax '$lang' could not be created.";
576 }
577 $self->{STACK} = [];
578 return
579 }
580 my $basecontext = $hl->{basecontext};
581 $self->{STACK} = [
582 [$hl, $basecontext]
583 ];
584 }
585 }
586  
587 sub SnippetForce {
588 my $self = shift;
589 my $parse = $self->{SNIPPET};
590 if ($parse ne '') {
591 my $out = $self->{OUT};
592 # push(@$out, [$parse, $self->{SNIPPETATTRIBUTE}, undef, undef]);
593 push(@$out, $parse, $self->{SNIPPETATTRIBUTE});
594 $self->{SNIPPET} = '';
595 }
596 }
597  
598 sub SnippetParse {
599 my ($self, $snip, $attr) = @_;
600 my $parse = $self->{SNIPPET};
601 my $out = $self->{OUT};
602 if ($attr ne $self->{SNIPPETATTRIBUTE}) {
603 if ($parse ne '') {
604 push(@$out, $parse, $self->{SNIPPETATTRIBUTE});
605 $parse = '';
606 }
607 $self->{SNIPPETATTRIBUTE} = $attr;
608 }
609 $self->{SNIPPET} = $parse . $snip;
610 $self->{LINESEGMENT} = $self->{LINESEGMENT} . $snip;
611 }
612  
613 sub StackPush {
614 my $self = shift;
615 my $stack = $self->{STACK};
616 unshift(@$stack, [@_, $self->{CAPTURED}]);
617 $self->{CAPTURED} = [];
618 }
619  
620 sub StackPull {
621 my $self = shift;
622 my $stack = $self->{STACK};
623 if (@$stack > 1) {
624 $self->{PULLED} = 1;
625 return shift @$stack;
626 } else {
627 $self->LogWarning("Cannot #pop context, already at basecontext")
628 }
629 }
630  
631 sub StackTop {
632 my $self = shift;
633 return $self->{STACK}->[0];
634 }
635  
636 sub StateCompare {
637 my ($self, $state) = @_;
638 my $h = [ $self->stateGet ];
639 return (Dumper($h) eq Dumper($state));
640 }
641  
642 sub StateGet {
643 my $self = shift;
644 my $s = $self->{STACK};
645 return @$s;
646 }
647  
648 sub StateSet {
649 my $self = shift;
650 my $s = $self->{STACK};
651 @$s = (@_);
652 }
653  
654 sub SuggestSyntax {
655 my ($self, $file) = @_;
656 my $id = $self->GetIndexer;
657 my $hsh = $id->Extensions;
658 my $ext;
659 if ($file =~ /(\.[^\.]+)$/) {
660 $ext = $1;
661 }
662 return undef unless defined $ext;
663 my $list = $hsh->{"*$ext"};
664 if (defined $list) {
665 my @l = @$list;
666 my $syntax = shift @l;
667 while (@l) {
668 my $new = shift @l;
669  
670 my $pnew = $id->InfoPriority($new);
671 $pnew = 0 unless defined $pnew;
672 $pnew = 0 unless $pnew =~ /^\d+$/;
673  
674 my $psyntax = $id->InfoPriority($syntax);
675 $psyntax = 0 unless defined $psyntax;
676 $psyntax = 0 unless $psyntax =~ /^\d+$/;
677  
678 $syntax = $new if $pnew > $psyntax;
679 }
680 return $syntax
681 }
682 return undef;
683 }
684  
685 sub Syntax {
686 my ($self, $syntax) = @_;
687 if (defined $syntax) {
688 if ($self->GetIndexer->SyntaxExists($syntax)) {
689 $self->{SYNTAX} = $syntax;
690 $self->Reset;
691 } else {
692 warn "Unknown syntax: '$syntax'"
693 }
694 }
695 return $self->{SYNTAX};
696 }
697  
698 sub testAnyChar {
699 my $self = shift;
700 my $text = shift;
701 my $string = shift;
702 my $test = substr($$text, 0, 1);
703 if (index($string, $test) > -1) {
704 my $parser = pop @_;
705 &$parser($self, $text, $test, @_);
706 return 1
707 }
708 return ''
709 }
710  
711 sub testAnyCharI {
712 my $self = shift;
713 my $text = shift;
714 my $string = shift;
715 my $test = substr($$text, 0, 1);
716 my $bck = $test;
717 $test = lc($test);
718 if (index($string, $test) > -1) {
719 my $parser = pop @_;
720 &$parser($self, $text, $bck, @_);
721 return 1
722 }
723 return ''
724 }
725  
726 sub testCommonColumn {
727 my $self = shift;
728 my $text = shift;
729 my $column = shift;
730 if ($column ne $self->Column) {
731 return '';
732 }
733 my $next = shift;
734 return &$next($self, $text, @_)
735 }
736  
737 sub testCommonFirstNonSpace {
738 my $self = shift;
739 my $text = shift;
740 unless ($self->FirstNonSpace($$text)) {
741 return 0
742 }
743 my $next = shift;
744 return &$next($self, $text, @_)
745 }
746  
747 sub testCommonLastCharBB {
748 my $self = shift;
749 my $lastchar = $self->LastChar;
750 unless ($lastchar =~ /\w/) { return '' }
751 my $text = shift;
752 my $next = shift;
753 return &$next($self, $text, @_)
754 }
755  
756 sub testCommonLastCharBb {
757 my $self = shift;
758 my $lastchar = $self->LastChar;
759 if ($lastchar =~ /\w/) { return '' }
760 my $text = shift;
761 my $next = shift;
762 return &$next($self, $text, @_)
763 }
764  
765 sub testCommonLineStart {
766 my $self = shift;
767 my $text = shift;
768 unless ($self->{LINESEGMENT} eq '') {
769 return ''
770 }
771 my $next = shift;
772 return &$next($self, $text, @_)
773 }
774  
775 sub testDetectChar {
776 my $self = shift;
777 my $text = shift;
778 my $char = shift;
779 my $test = substr($$text, 0, 1);
780 if ($char eq $test) {
781 my $parser = pop @_;
782 &$parser($self, $text, $test, @_);
783 return 1
784 }
785 return ''
786 }
787  
788 sub testDetectCharD {
789 my $self = shift;
790 my $text = shift;
791 my $char = shift;
792 $char = $self->CapturedParseC($char);
793 my $test = substr($$text, 0, 1);
794 if ($char eq $test) {
795 my $parser = pop @_;
796 &$parser($self, $text, $test, @_);
797 return 1
798 }
799 return ''
800 }
801  
802 sub testDetectCharDI {
803 my $self = shift;
804 my $text = shift;
805 my $char = shift;
806 $char = $self->CapturedParseC($char);
807 my $test = substr($$text, 0, 1);
808 if (lc($char) eq lc($test)) {
809 my $parser = pop @_;
810 &$parser($self, $text, $test, @_);
811 return 1
812 }
813 return ''
814  
815 }
816  
817 sub testDetectCharI {
818 my $self = shift;
819 my $text = shift;
820 my $char = shift;
821 my $test = substr($$text, 0, 1);
822 if (lc($char) eq lc($test)) {
823 my $parser = pop @_;
824 &$parser($self, $text, $test, @_);
825 return 1
826 }
827 return ''
828 }
829  
830 sub testDetect2Chars {
831 my $self = shift;
832 my $text = shift;
833 my $char = shift;
834 my $char1 = shift;
835 my $string = $char . $char1;
836 my $test = substr($$text, 0, 2);
837 if ($string eq $test) {
838 my $parser = pop @_;
839 &$parser($self, $text, $test, @_);
840 return 1
841 }
842 return ''
843 }
844  
845 sub testDetect2CharsD {
846 my $self = shift;
847 my $text = shift;
848 my $char = shift;
849 my $char1 = shift;
850 $char = $self->CapturedParseC($char);
851 $char1 = $self->CapturedParseC($char1);
852 my $string = $char . $char1;
853 my $test = substr($$text, 0, 2);
854 if ($string eq $test) {
855 my $parser = pop @_;
856 &$parser($self, $text, $test, @_);
857 return 1
858 }
859 return ''
860 }
861  
862 sub testDetect2CharsDI {
863 my $self = shift;
864 my $text = shift;
865 my $char = shift;
866 my $char1 = shift;
867 $char = $self->CapturedParseC($char);
868 $char1 = $self->CapturedParseC($char1);
869 my $string = $char . $char1;
870 my $test = substr($$text, 0, 2);
871 if (lc($string) eq lc($test)) {
872 my $parser = pop @_;
873 &$parser($self, $text, $test, @_);
874 return 1
875 }
876 return ''
877  
878 }
879  
880 sub testDetect2CharsI {
881 my $self = shift;
882 my $text = shift;
883 my $char = shift;
884 my $char1 = shift;
885 my $string = $char . $char1;
886 my $test = substr($$text, 0, 2);
887 if (lc($string) eq lc($test)) {
888 my $parser = pop @_;
889 &$parser($self, $text, $test, @_);
890 return 1
891 }
892 return ''
893  
894 }
895 sub testDetectIdentifier {
896 my $self = shift;
897 my $text = shift;
898 my $delim = shift;
899 return '' unless $self->LastcharDeliminator($delim);
900 if ($$text =~ /^([a-zA-Z_][a-zA-Z0-9_]*)/) {
901 my $parser = pop @_;
902 &$parser($self, $text, $1, @_);
903 return 1
904 }
905 return ''
906 }
907  
908 sub testDetectSpaces {
909 my $self = shift;
910 my $text = shift;
911 if ($$text =~ /^([\040|\t]+)/) {
912 my $parser = pop @_;
913 &$parser($self, $text, $1, @_);
914 return 1
915 }
916 return ''
917 }
918  
919 sub testFloat {
920 my $self = shift;
921 my $text = shift;
922 my $delim = shift;
923 return '' unless $self->LastcharDeliminator($delim);
924 if ($$text =~ /^((?=\.?\d)\d*(?:\.\d*)?(?:[Ee][+-]?\d+)?)/) {
925 my $parser = pop @_;
926 &$parser($self, $text, $1, @_);
927 return 1
928 }
929 return ''
930 }
931  
932 sub testHlCChar {
933 my $self = shift;
934 my $text = shift;
935 if ($$text =~ /^('.')/) {
936 my $parser = pop @_;
937 &$parser($self, $text, $1, @_);
938 return 1
939 }
940 return ''
941 }
942  
943 sub testHlCHex {
944 my $self = shift;
945 my $text = shift;
946 my $delim = shift;
947 return '' unless $self->LastcharDeliminator($delim);
948 if ($$text =~ /^(0x[0-9a-fA-F]+)/) {
949 my $parser = pop @_;
950 &$parser($self, $text, $1, @_);
951 return 1
952 }
953 return ''
954 }
955  
956 sub testHlCOct {
957 my $self = shift;
958 my $text = shift;
959 my $delim = shift;
960 return '' unless $self->LastcharDeliminator($delim);
961 if ($$text =~ /^(0[0-7]+)/) {
962 my $parser = pop @_;
963 &$parser($self, $text, $1, @_);
964 return 1;
965 }
966 return ''
967 }
968  
969 sub testHlCStringChar {
970 my $self = shift;
971 my $text = shift;
972 if ($$text =~ /^(\\[a|b|e|f|n|r|t|v|'|"|\?])/) {
973 my $parser = pop @_;
974 &$parser($self, $text, $1, @_);
975 return 1
976 }
977 if ($$text =~ /^(\\x[0-9a-fA-F][0-9a-fA-F]?)/) {
978 my $parser = pop @_;
979 &$parser($self, $text, $1, @_);
980 return 1
981 }
982 if ($$text =~ /^(\\[0-7][0-7]?[0-7]?)/) {
983 my $parser = pop @_;
984 &$parser($self, $text, $1, @_);
985 return 1
986 }
987 return ''
988 }
989  
990 sub testInt {
991 my $self = shift;
992 my $text = shift;
993 my $delim = shift;
994 return '' unless $self->LastcharDeliminator($delim);
995 if ($$text =~ /^([+-]?\d+)/) {
996 my $parser = pop @_;
997 &$parser($self, $text, $1, @_);
998 return 1
999 }
1000 return ''
1001 }
1002  
1003 sub testKeyword {
1004 my $self = shift;
1005 my $text = shift;
1006 my $list = shift;
1007 my $delim = shift;
1008 return '' unless $self->LastcharDeliminator($delim);
1009 if ($$text =~ /^([^$delim]+)/) {
1010 my $match = $1;
1011 if (exists $list->{$match}) {
1012 my $parser = pop @_;
1013 &$parser($self, $text, $match, @_);
1014 return 1
1015 }
1016 }
1017 return ''
1018 }
1019  
1020 sub testKeywordI {
1021 my $self = shift;
1022 my $text = shift;
1023 my $list = shift;
1024 my $delim = shift;
1025 return '' unless $self->LastcharDeliminator($delim);
1026 if ($$text =~ /^([^$delim]+)/) {
1027 my $match = $1;
1028 my $test = lc($match);
1029 if (exists $list->{$test}) {
1030 my $parser = pop @_;
1031 &$parser($self, $text, $match, @_);
1032 return 1
1033 }
1034 }
1035 return ''
1036 }
1037  
1038 sub testLineContinue {
1039 my $self = shift;
1040 my $text = shift;
1041 my $char = shift;
1042 if ($$text =~ /^($char)\n/) {
1043 my $parser = pop @_;
1044 &$parser($self, $text, $1, @_);
1045 return 1
1046 }
1047 return ''
1048 }
1049  
1050 sub testRangeDetect {
1051 my $self = shift;
1052 my $text = shift;
1053 my $char = shift;
1054 my $char1 = shift;
1055 if ($$text =~ /^($char[^$char1]+$char1)/) {
1056 my $parser = pop @_;
1057 &$parser($self, $text, $1, @_);
1058 return 1
1059 }
1060 }
1061  
1062 sub testRangeDetectI {
1063 my $self = shift;
1064 my $text = shift;
1065 my $char = shift;
1066 my $char1 = shift;
1067 if ($$text =~ /^($char[^$char1]+$char1)/i) {
1068 my $parser = pop @_;
1069 &$parser($self, $text, $1, @_);
1070 return 1
1071 }
1072 }
1073  
1074 sub testRegExpr {
1075 my $self = shift;
1076 my $text = shift;
1077 my $reg = shift;
1078 if ($$text =~ /$reg/) {
1079 my $match = $1;
1080 chomp $match; #Sometimes a trailing newline is matched.
1081 if ($match eq '') { return '' } #Sometimes a reg gives a positive result of zero length
1082 if ($#- > 1) {
1083 no strict 'refs';
1084 my @cap = map {$$_} 2 .. $#-;
1085 $self->Capture(@cap);
1086 # $self->{STACK}->[0]->[2] = \@cap;
1087 }
1088 my $parser = pop @_;
1089 &$parser($self, $text, $match, @_);
1090 return 1
1091 }
1092 return ''
1093 }
1094  
1095 sub testRegExprD {
1096 my $self = shift;
1097 my $text = shift;
1098 my $reg = shift;
1099
1100 my ($a, $b, $c, $d, $e, $f, $g, $h);
1101 ($reg, $a, $b, $c, $d, $e, $f, $g, $h) = $self->CapturedParseR($reg);
1102 $reg = "^($reg)";
1103 # emergency measurements to avoid exception (szabgab)
1104 eval "\$reg = qr/$reg/;";
1105 if ($@) {
1106 warn $@;
1107 return '';
1108 }
1109 if ($$text =~ /$reg/) {
1110 my $match = $1;
1111 chomp $match; #Sometimes a trailing newline is matched.
1112 if ($match eq '') { return '' } #Sometimes a reg gives a positive result of zero length
1113 if ($#- > 1) {
1114 no strict 'refs';
1115 my @cap = map {$$_} 2 .. $#-;
1116 $self->Capture(@cap);
1117 # $self->{STACK}->[0]->[2] = \@cap;
1118 }
1119 my $parser = pop @_;
1120 &$parser($self, $text, $match, @_);
1121 return 1
1122 }
1123 return ''
1124 }
1125  
1126 sub testRegExprDI {
1127 my $self = shift;
1128 my $text = shift;
1129 my $reg = shift;
1130
1131 my ($a, $b, $c, $d, $e, $f, $g, $h);
1132 ($reg, $a, $b, $c, $d, $e, $f, $g, $h) = $self->CapturedParseR($reg);
1133 $reg = "^($reg)";
1134 # emergency measurements to avoid exception (szabgab)
1135 eval "\$reg = qr/$reg/i;";
1136 if ($@) {
1137 warn $@;
1138 return '';
1139 }
1140 if ($$text =~ /$reg/i) {
1141 my $match = $1;
1142 chomp $match; #Sometimes a trailing newline is matched.
1143 if ($match eq '') { return '' } #And when that happens a reg can give a positive result of zero length
1144 if ($#- > 1) {
1145 no strict 'refs';
1146 my @cap = map {$$_} 2 .. $#-;
1147 $self->Capture(@cap);
1148 # $self->{STACK}->[0]->[2] = \@cap;
1149 }
1150 my $parser = pop @_;
1151 &$parser($self, $text, $match, @_);
1152 return 1
1153 }
1154 return ''
1155 }
1156  
1157 sub testRegExprI {
1158 my $self = shift;
1159 my $text = shift;
1160 my $reg = shift;
1161 if ($$text =~ /$reg/i) {
1162 my $match = $1;
1163 chomp $match; #Sometimes a trailing newline is matched.
1164 if ($match eq '') { return '' } #Sometimes a reg gives a positive result of zero length
1165 if ($#- > 1) {
1166 no strict 'refs';
1167 my @cap = map {$$_} 2 .. $#-;
1168 $self->Capture(@cap);
1169 # $self->{STACK}->[0]->[2] = \@cap;
1170 }
1171 my $parser = pop @_;
1172 &$parser($self, $text, $match, @_);
1173 return 1
1174 }
1175 return ''
1176 }
1177  
1178 sub testStringDetect {
1179 my $self = shift;
1180 my $text = shift;
1181 my $string = shift;
1182 my $test = substr($$text, 0, length($string));
1183 if ($string eq $test) {
1184 my $parser = pop @_;
1185 &$parser($self, $text, $test, @_);
1186 return 1
1187 }
1188 return ''
1189 }
1190  
1191 sub testStringDetectD {
1192 my $self = shift;
1193 my $text = shift;
1194 my $string = shift;
1195 $string = $self->CapturedParse($string);
1196 my $test = substr($$text, 0, length($string));
1197 if ($string eq $test) {
1198 my $parser = pop @_;
1199 &$parser($self, $text, $test, @_);
1200 return 1
1201 }
1202 return ''
1203  
1204 }
1205  
1206 sub testStringDetectDI {
1207 my $self = shift;
1208 my $text = shift;
1209 my $string = shift;
1210 $string = $self->CapturedParse($string);
1211 my $test = substr($$text, 0, length($string));
1212 if (lc($string) eq lc($test)) {
1213 my $parser = pop @_;
1214 &$parser($self, $text, $test, @_);
1215 return 1
1216 }
1217 return ''
1218  
1219 }
1220  
1221 sub testStringDetectI {
1222 my $self = shift;
1223 my $text = shift;
1224 my $string = shift;
1225 my $test = substr($$text, 0, length($string));
1226 if (lc($string) eq lc($test)) {
1227 my $parser = pop @_;
1228 &$parser($self, $text, $test, @_);
1229 return 1
1230 }
1231 return ''
1232 }
1233  
1234 sub testWordDetect {
1235 my $self = shift;
1236 my $text = shift;
1237 my $string = shift;
1238 my $delim = shift;
1239 return '' if (length($string) + 1 > length($$text));
1240 return '' unless $self->LastcharDeliminator($delim);
1241 my $testc = substr($$text, length($string), 1);
1242 return '' unless $testc =~ /$delim/;
1243 my $test = substr($$text, 0, length($string));
1244 if ($string eq $test) {
1245 my $parser = pop @_;
1246 &$parser($self, $text, $test, @_);
1247 return 1
1248 }
1249 return ''
1250 }
1251  
1252 sub testWordDetectD {
1253 my $self = shift;
1254 my $text = shift;
1255 my $string = shift;
1256 my $delim = shift;
1257 $string = $self->CapturedParse($string);
1258 return '' if (length($string) + 1 > length($$text));
1259 return '' unless $self->LastcharDeliminator($delim);
1260 my $testc = substr($$text, length($string), 1);
1261 return '' unless $testc =~ /$delim/;
1262 my $test = substr($$text, 0, length($string));
1263 if ($string eq $test) {
1264 my $parser = pop @_;
1265 &$parser($self, $text, $test, @_);
1266 return 1
1267 }
1268 return ''
1269 }
1270  
1271 sub testWordDetectDI {
1272 my $self = shift;
1273 my $text = shift;
1274 my $string = shift;
1275 my $delim = shift;
1276 $string = lc($self->CapturedParse($string));
1277 return '' if (length($string) + 1 > length($$text));
1278 return '' unless $self->LastcharDeliminator($delim);
1279 my $testc = substr($$text, length($string), 1);
1280 return '' unless $testc =~ /$delim/;
1281 my $test = substr($$text, 0, length($string));
1282 if (lc($string) eq lc($test)) {
1283 my $parser = pop @_;
1284 &$parser($self, $text, $test, @_);
1285 return 1
1286 }
1287 return ''
1288 }
1289  
1290 sub testWordDetectI {
1291 my $self = shift;
1292 my $text = shift;
1293 my $string = shift;
1294 my $delim = shift;
1295 $string = lc($self->CapturedParse($string));
1296 return '' if (length($string) + 1 > length($$text));
1297 return '' unless $self->LastcharDeliminator($delim);
1298 my $testc = substr($$text, length($string), 1);
1299 return '' unless $testc =~ /$delim/;
1300 my $test = substr($$text, 0, length($string));
1301 if (lc($string) eq lc($test)) {
1302 my $parser = pop @_;
1303 &$parser($self, $text, $test, @_);
1304 return 1
1305 }
1306 return ''
1307 }
1308  
1309 sub UseAttribStackPush {
1310 my ($self, $item) = @_;
1311 my $stack = $self->{USEATTRIBSTACK};
1312 unshift(@$stack, $item);
1313 }
1314  
1315 sub UseAttribStackPull {
1316 my ($self, $val) = @_;
1317 my $stack = $self->{USEATTRIBSTACK};
1318 return shift(@$stack);
1319 }
1320  
1321 sub UseAttribStackTop {
1322 my $self = shift;
1323 return $self->{USEATTRIBSTACK}->[0];
1324 }
1325  
1326 1;
1327 __END__
« Kamelon/Indexer.pm ↑ Summary Kamelon/Diagnostics.pm »