| 1 | package Syntax::Kamelon::Builder; | ||
| 2 | |||
| 3 | our $VERSION = '0.25'; | ||
| 4 | |||
| 5 | 9 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 9 | ▶ | use Carp qw(cluck); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 9 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 9 | ▶ | use base qw(Syntax::Kamelon::XMLData); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | my $regchars = "\\^.\$|()[]{}*+?~!%^&/"; | ||
| 12 | |||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | my %args = (@_); | ||
| 17 | |||
| 18 | my $engine = delete $args{engine}; | ||
| 19 | my $self = $class->SUPER::new(%args); | ||
| 20 | |||
| 21 | $self->{CURCONTEXT} = ''; | ||
| 22 | $self->{CURRULE} = ''; | ||
| 23 | $self->{ENGINE} = $engine; | ||
| 24 | if (defined $self->FileName) { | ||
| 25 | return $self->Setup; | ||
| 26 | } | ||
| 27 | return undef; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub AttributeGet { | ||
| 31 | my ($self, $attribute) = @_; | ||
| 32 | unless ((defined $attribute) and (length($attribute) > 0)) { | ||
| 33 | return $self->AttributeGetContext; | ||
| 34 | } | ||
| 35 | if (exists $self->{ATTRIBUTES}->{$attribute}) { | ||
| 36 | return $self->{ATTRIBUTES}->{$attribute} | ||
| 37 | } else { | ||
| 38 | return $self->AttributeGetContext; | ||
| 39 | } | ||
| 40 | return 'Normal' | ||
| 41 | } | ||
| 42 | |||
| 43 | sub AttributeGetContext { | ||
| 44 | my $self = shift; | ||
| 45 | if (exists $self->ContextData->{$self->CurContext}->{attribute}) { | ||
| 46 | my $attribute = $self->ContextData->{$self->CurContext}->{attribute}; | ||
| 47 | if (exists $self->{ATTRIBUTES}->{$attribute}) { | ||
| 48 | return $self->{ATTRIBUTES}->{$attribute} | ||
| 49 | } | ||
| 50 | } | ||
| 51 | return 'Normal' | ||
| 52 | } | ||
| 53 | |||
| 54 | sub AttributeGetF { | ||
| 55 | my ($self, $attribute) = @_; | ||
| 56 | my $token = $self->AttributeGet($attribute); | ||
| 57 | return $self->Engine->Formatter->FormatTable($token) | ||
| 58 | } | ||
| 59 | |||
| 60 | sub ContextExists { | ||
| 61 | my ($self, $context) = @_; | ||
| 62 | return (exists $self->{CONTEXTDATA}->{$context}) | ||
| 63 | } | ||
| 64 | |||
| 65 | sub CurContext { | ||
| 66 | my $self = shift; | ||
| 67 | if (@_) { $self->{CURCONTEXT} = shift; } | ||
| 68 | return $self->{CURCONTEXT}; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub CurContextIsDynamic { | ||
| 72 | my $self = shift; | ||
| 73 | my $cc = $self->{CURCONTEXT}; | ||
| 74 | my $d = $self->{CONTEXTDATA}->{$cc}; | ||
| 75 | if ((exists $d->{dynamic}) and (lc($d->{dynamic}) eq 'true')) { | ||
| 76 | return 1 | ||
| 77 | } | ||
| 78 | return '' | ||
| 79 | } | ||
| 80 | |||
| 81 | sub CurRule { | ||
| 82 | my $self = shift; | ||
| 83 | if (@_) { $self->{CURRULE} = shift; } | ||
| 84 | return $self->{CURRULE}; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub Delim2Reg { | ||
| 88 | my ($self, $delim) = @_; | ||
| 89 | my $reg = ''; | ||
| 90 | my @d = keys %$delim; | ||
| 91 | while (@d) { | ||
| 92 | my $k = shift @d; | ||
| 93 | $reg = $reg . quotemeta($k); | ||
| 94 | $reg = $reg . '|' if @d | ||
| 95 | } | ||
| 96 | $reg = qr/$reg/; | ||
| 97 | return $reg | ||
| 98 | } | ||
| 99 | |||
| 100 | sub Engine { | ||
| 101 | my $self = shift; | ||
| 102 | return $self->{ENGINE}; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub LogWarning { | ||
| 106 | my ($self, $warning) = @_; | ||
| 107 | if ($self->{ENGINE}->{VERBOSE}) { | ||
| 108 | my $msg = 'Syntax: ' . $self->Syntax; | ||
| 109 | if ($self->{CURCONTEXT}) { | ||
| 110 | $msg = $msg . ', Context: ' . $self->{CURCONTEXT}; | ||
| 111 | if ($self->{CURRULE} ne '') { | ||
| 112 | $msg = $msg . ', Rule: ' . $self->{CURRULE} | ||
| 113 | } | ||
| 114 | } | ||
| 115 | my $call = $self->{ENGINE}->{LOGCALL}; | ||
| 116 | &$call("$msg, $warning"); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | sub RuleGetArgs { | ||
| 121 | my $self = shift; | ||
| 122 | my $rule = shift; | ||
| 123 | my %booltable = ( | ||
| 124 | false => 0, | ||
| 125 | true => 1, | ||
| 126 | yes => 1, | ||
| 127 | no => 0, | ||
| 128 | 1 => 1, | ||
| 129 | 0 => 0, | ||
| 130 | ); | ||
| 131 | my %boolopt = ( | ||
| 132 | dynamic => 0, | ||
| 133 | firstNonSpace => 0, | ||
| 134 | includeAttrib => 0, | ||
| 135 | insensitive => 0, | ||
| 136 | lookAhead => 0, | ||
| 137 | minimal => 0, | ||
| 138 | ); | ||
| 139 | my %default = (%boolopt, | ||
| 140 | context => '#stay', | ||
| 141 | ); | ||
| 142 | my @r = (); | ||
| 143 | while (@_) { | ||
| 144 | my $n = shift; | ||
| 145 | my $d; | ||
| 146 | if (exists($rule->{$n})) { | ||
| 147 | $d = $rule->{$n}; | ||
| 148 | if (exists $boolopt{$n}) { | ||
| 149 | $d = $booltable{lc($d)}; | ||
| 150 | } | ||
| 151 | } elsif (exists($default{$n})) { | ||
| 152 | $d = $default{$n}; | ||
| 153 | } | ||
| 154 | push @r, $d | ||
| 155 | } | ||
| 156 | return @r | ||
| 157 | } | ||
| 158 | |||
| 159 | sub RuleGetChar { | ||
| 160 | my ($self, $char) = @_; | ||
| 161 | if (length($char) eq 1) { return $char } | ||
| 162 | if ($char =~ /^\\.$/) { | ||
| 163 | eval "\$char = \"$char\""; | ||
| 164 | } | ||
| 165 | return $char | ||
| 166 | } | ||
| 167 | |||
| 168 | my %tests = ( | ||
| 169 | AnyChar => 'testAnyChar', | ||
| 170 | DetectChar => 'testDetectChar', | ||
| 171 | Detect2Chars => 'testDetect2Chars', | ||
| 172 | DetectIdentifier => 'testDetectIdentifier', | ||
| 173 | DetectSpaces => 'testDetectSpaces', | ||
| 174 | Float => 'testFloat', | ||
| 175 | HlCChar => 'testHlCChar', | ||
| 176 | HlCHex => 'testHlCHex', | ||
| 177 | HlCOct => 'testHlCOct', | ||
| 178 | HlCStringChar => 'testHlCStringChar', | ||
| 179 | Int => 'testInt', | ||
| 180 | keyword => 'testKeyword', | ||
| 181 | LineContinue => 'testLineContinue', | ||
| 182 | RangeDetect => 'testRangeDetect', | ||
| 183 | RegExpr => 'testRegExpr', | ||
| 184 | RegExprS => 'testRegExprSimple', | ||
| 185 | StringDetect => 'testStringDetect', | ||
| 186 | WordDetect => 'testWordDetect', | ||
| 187 | ); | ||
| 188 | |||
| 189 | sub Setup { | ||
| 190 | my $self = shift; | ||
| 191 | |||
| 192 | my $casesensitive = 1; | ||
| 193 | unless ($self->KeywordsCase eq 'undef') { | ||
| 194 | $casesensitive = $self->KeywordsCase | ||
| 195 | } | ||
| 196 | if (exists $self->Language->{'casesensitive'}) { | ||
| 197 | $casesensitive = $self->Booleanize($self->Language->{'casesensitive'}) | ||
| 198 | } | ||
| 199 | $self->KeywordsCase($casesensitive); | ||
| 200 | #turn lists into hashes for faster and easier lookup. | ||
| 201 | my $lists = $self->Lists; | ||
| 202 | for (keys %$lists) { | ||
| 203 | my $list = $_; | ||
| 204 | my $l = $lists->{$list}; | ||
| 205 | my %h = (); | ||
| 206 | for (@$l) { | ||
| 207 | my $elm = $_; | ||
| 208 | unless ($casesensitive) { $elm = lc $elm } | ||
| 209 | $h{$elm} = 1; | ||
| 210 | } | ||
| 211 | $lists->{$list} = \%h; | ||
| 212 | } | ||
| 213 | |||
| 214 | my %parser = ( | ||
| 215 | basecontext => $self->BaseContext, | ||
| 216 | contexts => {}, | ||
| 217 | deliminators => $self->Deliminators, | ||
| 218 | lists => $lists, | ||
| 219 | syntax => $self->Syntax, | ||
| 220 | ); | ||
| 221 | |||
| 222 | #setup contexts | ||
| 223 | my $cond = $self->ContextData; | ||
| 224 | for (keys %$cond) { | ||
| 225 | $self->CurContext($_); | ||
| 226 | $self->SetupContext(\%parser, $_, $cond->{$_}) | ||
| 227 | } | ||
| 228 | $self->CurContext(''); | ||
| 229 | return \%parser; | ||
| 230 | } | ||
| 231 | |||
| 232 | my %parses = ( | ||
| 233 | AnyChar => \&SetupRuleAnyChar, | ||
| 234 | DetectChar => \&SetupRuleDetectChar, | ||
| 235 | Detect2Chars => \&SetupRuleDetect2Chars, | ||
| 236 | DetectIdentifier => \&SetupRuleDetectIdentifier, | ||
| 237 | DetectSpaces => \&SetupRuleDefault, | ||
| 238 | Float => \&SetupRuleNumber, | ||
| 239 | HlCChar => \&SetupRuleDefault, | ||
| 240 | HlCHex => \&SetupRuleNumber, | ||
| 241 | HlCOct => \&SetupRuleNumber, | ||
| 242 | HlCStringChar => \&SetupRuleDefault, | ||
| 243 | IncludeRules => \&SetupRuleDefault, | ||
| 244 | Int => \&SetupRuleNumber, | ||
| 245 | keyword => \&SetupRuleKeyword, | ||
| 246 | LineContinue => \&SetupRuleLineContinue, | ||
| 247 | RangeDetect => \&SetupRuleRangeDetect, | ||
| 248 | RegExpr => \&SetupRuleRegExpr, | ||
| 249 | StringDetect => \&SetupRuleStringDetect, | ||
| 250 | WordDetect => \&SetupRuleWordDetect, | ||
| 251 | ); | ||
| 252 | |||
| 253 | sub SetupContext { | ||
| 254 | my ($self, $parser, $ctx, $data) = @_; | ||
| 255 | my $eng = $self->{ENGINE}; | ||
| 256 | my %inf = %$data; | ||
| 257 | delete $inf{items}; | ||
| 258 | my %newcontext = ( | ||
| 259 | attribute => $eng->{FORMATTER}->FormatTable('Normal'), | ||
| 260 | callbacks => [], | ||
| 261 | debug => [], | ||
| 262 | info => \%inf, | ||
| 263 | dynamic => 0, | ||
| 264 | emptycontext => sub {}, | ||
| 265 | endcontext => sub {}, | ||
| 266 | fallthroughcontext => undef, | ||
| 267 | ); | ||
| 268 | if (exists $data->{'attribute'}) { | ||
| 269 | my $attr = $self->AttributeGetF($data->{'attribute'}); | ||
| 270 | $newcontext{attribute} = $attr; | ||
| 271 | } | ||
| 272 | if (exists $data->{'lineEmptyContext'}) { | ||
| 273 | my $e = $data->{'lineEmptyContext'}; | ||
| 274 | $e = $self->SetupContextShifter($parser, $e); | ||
| 275 | $newcontext{emptycontext} = $e; | ||
| 276 | } | ||
| 277 | if (exists $data->{'lineEndContext'}) { | ||
| 278 | my $e = $data->{'lineEndContext'}; | ||
| 279 | $e = $self->SetupContextShifter($parser, $e); | ||
| 280 | $newcontext{endcontext} = $e; | ||
| 281 | } | ||
| 282 | if (exists $data->{'fallthrough'}) { | ||
| 283 | my $e = $data->{'fallthrough'}; | ||
| 284 | if ($e eq 'true') { | ||
| 285 | if (exists $data->{'fallthroughContext'}) { | ||
| 286 | my $e = $data->{'fallthroughContext'}; | ||
| 287 | $e = $self->SetupContextShifter($parser, $e); | ||
| 288 | $newcontext{fallthroughcontext} = $e; | ||
| 289 | } | ||
| 290 | } | ||
| 291 | } | ||
| 292 | if (exists $data->{'dynamic'}) { | ||
| 293 | my $e = $data->{'dynamic'}; | ||
| 294 | if ($e eq 'true') { | ||
| 295 | $newcontext{dynamic} = 1; | ||
| 296 | } | ||
| 297 | } | ||
| 298 | |||
| 299 | my $it = $data->{'items'}; | ||
| 300 | my ($r, $d) = $self->SetupContextRules($parser, $ctx, @$it); | ||
| 301 | $newcontext{callbacks} = $r; | ||
| 302 | $newcontext{debug} = $d; | ||
| 303 | $parser->{contexts}->{$ctx} = \%newcontext; | ||
| 304 | } | ||
| 305 | |||
| 306 | sub SetupContextRules { | ||
| 307 | my $self = shift; | ||
| 308 | my $parser = shift; | ||
| 309 | my $ctx = shift; | ||
| 310 | if (exists $parser->{$ctx}) { | ||
| 311 | my $p = $parser->{$ctx}; | ||
| 312 | return $p->{callbacks}, $p->{debug} | ||
| 313 | } | ||
| 314 | my @rules = (); | ||
| 315 | my @debug = (); | ||
| 316 | my $eng = $self->Engine; | ||
| 317 | my $num = 0; | ||
| 318 | while (@_) { | ||
| 319 | my $i = shift; | ||
| 320 | $num ++; | ||
| 321 | $self->CurRule($num); | ||
| 322 | |||
| 323 | # #setup debug info | ||
| 324 | # my $path = $self->Syntax . "::$ctx" . "::$num"; | ||
| 325 | # $i->{path} = $path; | ||
| 326 | |||
| 327 | my $type = $i->{'type'}; | ||
| 328 | if ($type eq 'IncludeRules') { | ||
| 329 | my $context = $i->{'context'}; | ||
| 330 | my ($inclattr) = $self->RuleGetArgs($i, qw/ includeAttrib /); | ||
| 331 | if ($context =~ s/##(.*)//) { #it refers to another syntax | ||
| 332 | my $language = $1; | ||
| 333 | my $p = $eng->{POSTCREATE}; | ||
| 334 | push @$p, $language; | ||
| 335 | if ($language ne $self->Syntax) { #and it refers to another syntax indeed | ||
| 336 | if ($inclattr) { # includeAttr is set | ||
| 337 | my $attr = $self->AttributeGetF($self->ContextData->{$context}->{'attribute'}); | ||
| 338 | push @rules, [$eng->can("IncludeSyntaxIA"), $language, $context, $attr]; | ||
| 339 | push @debug, $i; | ||
| 340 | } else { | ||
| 341 | push @rules, [$eng->can("IncludeSyntax"), $language, $context]; | ||
| 342 | push @debug, $i; | ||
| 343 | } | ||
| 344 | } else { #it can be treated as an include rules | ||
| 345 | my $data = $self->ContextData->{$context}; | ||
| 346 | my $items = $data->{'items'}; | ||
| 347 | $self->CurContext($context); | ||
| 348 | if ($inclattr) { | ||
| 349 | my ($r, $d) = $self->SetupContextRules($parser, $ctx, @$items); | ||
| 350 | push @rules, [$eng->can("IncludeRules"), $r, $d, $inclattr]; | ||
| 351 | push @debug, $i; | ||
| 352 | } else { #if not includeAttr set the rules are literally included. | ||
| 353 | my ($r, $d) = $self->SetupContextRules($parser, $ctx, @$items); | ||
| 354 | push @rules, $r; | ||
| 355 | push @debug, $d; | ||
| 356 | } | ||
| 357 | $self->CurRule($num); | ||
| 358 | $self->CurContext($ctx) | ||
| 359 | } | ||
| 360 | } else { # it is a normal include rules | ||
| 361 | my $data = $self->ContextData->{$context}; | ||
| 362 | my $items = $data->{'items'}; | ||
| 363 | my $cback = $self->CurContext; | ||
| 364 | $self->CurContext($context); | ||
| 365 | if ($inclattr) { | ||
| 366 | my ($r, $d) = $self->SetupContextRules($parser, $ctx, @$items); | ||
| 367 | my $attr = $self->AttributeGetF($self->ContextData->{$context}->{'attribute'}); | ||
| 368 | push @rules, [$eng->can("IncludeRules"), $context, $r, $d, $attr]; | ||
| 369 | push @debug, $i; | ||
| 370 | } else { #if not includeAttr set the rules are literally included. | ||
| 371 | my ($r, $d) = $self->SetupContextRules($parser, $ctx, @$items); | ||
| 372 | push @rules, @$r; | ||
| 373 | push @debug, @$d; | ||
| 374 | } | ||
| 375 | $self->CurRule($num); | ||
| 376 | $self->CurContext($cback); | ||
| 377 | } | ||
| 378 | |||
| 379 | } else { | ||
| 380 | #get general options | ||
| 381 | my ($lookahead, $column, $firstnonspace, $command, $overstrike, $replace, $beginreg, $endreg) = | ||
| 382 | $self->RuleGetArgs($i, qw/ lookAhead column firstNonSpace command overstrike replace beginRegion endRegion /); | ||
| 383 | |||
| 384 | #get method and rulse specfic options | ||
| 385 | my $l = $parses{$type}; | ||
| 386 | my ($method, @options) = &$l($self, $i); | ||
| 387 | if (defined $method) { #returning an undefined method indicates an integrity problem. the rule is skipped. | ||
| 388 | my $formatter = $eng->Formatter; | ||
| 389 | $i->{method} = $method; | ||
| 390 | |||
| 391 | #set the test method | ||
| 392 | unshift @options, $eng->can($method); | ||
| 393 | |||
| 394 | #add context | ||
| 395 | my ($context) = $self->RuleGetArgs($i, qw/ context /); | ||
| 396 | $context = $self->SetupContextShifter($parser, $context); | ||
| 397 | push @options, $context; | ||
| 398 | |||
| 399 | #add attribute | ||
| 400 | my ($attribute) = $self->RuleGetArgs($i, qw/ attribute /); | ||
| 401 | unless (defined $attribute) { $attribute = '' }; | ||
| 402 | $attribute = $self->AttributeGetF($attribute); | ||
| 403 | push @options, $attribute; | ||
| 404 | |||
| 405 | #prepend column call if needed | ||
| 406 | if (defined $column) { | ||
| 407 | unshift @options, $column; | ||
| 408 | unshift @options, $eng->can('testCommonColumn'); | ||
| 409 | } | ||
| 410 | |||
| 411 | #prepend firstNonSpace call if needed | ||
| 412 | if ($firstnonspace) { | ||
| 413 | unshift @options, $eng->can('testCommonFirstNonSpace'); | ||
| 414 | } | ||
| 415 | |||
| 416 | #Add result parsers. Note: Last one to be called is first one to be pushed. | ||
| 417 | my $rparser = 'ParseResult'; | ||
| 418 | if ($lookahead) { | ||
| 419 | $rparser = $rparser . 'LookAhead' | ||
| 420 | } | ||
| 421 | push @options, $eng->can($rparser); | ||
| 422 | if (defined $overstrike) { | ||
| 423 | while (length $overstrike < 128) { | ||
| 424 | $overstrike = $overstrike . $overstrike | ||
| 425 | } | ||
| 426 | push @options, $overstrike; | ||
| 427 | push @options, $eng->can('ParseResultOverStrike'); | ||
| 428 | |||
| 429 | } | ||
| 430 | if (defined $replace) { | ||
| 431 | push @options, $replace; | ||
| 432 | push @options, $eng->can('ParseResultReplace'); | ||
| 433 | } | ||
| 434 | if (defined $command) { | ||
| 435 | push @options, $command, $eng->can('ParseResultCommand'); | ||
| 436 | } | ||
| 437 | |||
| 438 | #add region marker parsers | ||
| 439 | if ($formatter->Foldingdepth) { | ||
| 440 | if (defined $beginreg) { | ||
| 441 | push @options, $beginreg; | ||
| 442 | push @options, $eng->can('ParseResultBeginRegion'); | ||
| 443 | } | ||
| 444 | if (defined $endreg) { | ||
| 445 | push @options, $endreg; | ||
| 446 | push @options, $eng->can('ParseResultEndRegion'); | ||
| 447 | } | ||
| 448 | } | ||
| 449 | push @rules, \@options; | ||
| 450 | push @debug, $i; | ||
| 451 | } | ||
| 452 | } | ||
| 453 | } | ||
| 454 | $self->CurRule(''); | ||
| 455 | return \@rules, \@debug | ||
| 456 | } | ||
| 457 | |||
| 458 | sub SetupContextShifter { | ||
| 459 | my ($self, $parser, $tcontext) = @_; | ||
| 460 | my $eng = $self->Engine; | ||
| 461 | if ($tcontext =~ s/##(.*)//) { | ||
| 462 | my $syntax = $1; | ||
| 463 | unless ($self->SyntaxExists($syntax)) { | ||
| 464 | $self->LogWarning("Syntax '$syntax' does not exist, reverting to #stay"); | ||
| 465 | return sub {} | ||
| 466 | } | ||
| 467 | my $p = $eng->{POSTCREATE}; | ||
| 468 | push @$p, $syntax; | ||
| 469 | return sub { | ||
| 470 | my $hl = $eng->GetLexer($syntax); | ||
| 471 | if ($tcontext eq '') { $tcontext = $hl->{basecontext} } | ||
| 472 | $eng->StackPush($hl, $tcontext); | ||
| 473 | } | ||
| 474 | } elsif ($tcontext =~ /^#pop/i) { | ||
| 475 | my $count = 0; | ||
| 476 | while ($tcontext =~ s/^#pop//i) { $count++ } | ||
| 477 | my $default = sub { for (1 .. $count) { $eng->StackPull }}; | ||
| 478 | return $default if $tcontext eq ''; | ||
| 479 | |||
| 480 | if ($tcontext =~ s/^\!//) { | ||
| 481 | if ($tcontext =~ s/##(.*)//) { | ||
| 482 | my $lx = $1; | ||
| 483 | unless ($self->SyntaxExists($lx)) { | ||
| 484 | $self->LogWarning("Syntax '$lx' does not exist, only doing the #pop part"); | ||
| 485 | return $default | ||
| 486 | } | ||
| 487 | return sub { | ||
| 488 | for (1 .. $count) { $eng->StackPull } | ||
| 489 | my $hl = $eng->GetLexer($lx); | ||
| 490 | if ($tcontext eq '') { $tcontext = $hl->{basecontext} } | ||
| 491 | $eng->StackPush($hl, $tcontext); | ||
| 492 | } | ||
| 493 | } else { | ||
| 494 | unless ($self->ContextExists($tcontext)) { | ||
| 495 | $self->LogWarning("Context '$tcontext' does not exist, only doing the #pop part"); | ||
| 496 | return $default | ||
| 497 | } | ||
| 498 | return sub { | ||
| 499 | for (1 .. $count) { $eng->StackPull } | ||
| 500 | $eng->StackPush($parser, $tcontext) | ||
| 501 | } | ||
| 502 | } | ||
| 503 | } | ||
| 504 | return $default; | ||
| 505 | } elsif ($tcontext =~ /^#stay/i) { | ||
| 506 | return sub {} | ||
| 507 | } else { | ||
| 508 | if ($self->ContextExists($tcontext)) { | ||
| 509 | return sub { $eng->StackPush($parser, $tcontext) } | ||
| 510 | } else { | ||
| 511 | $self->LogWarning("Context '$tcontext' does not exist, reverting to #stay"); | ||
| 512 | return sub {} | ||
| 513 | } | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | sub SetupRuleAnyChar { | ||
| 518 | my ($self, $rule) = @_; | ||
| 519 | my @o = $self->RuleGetArgs($rule, qw/String insensitive/ ); | ||
| 520 | my $method = $tests{$rule->{'type'}}; | ||
| 521 | my $string = shift @o; | ||
| 522 | unless ((defined $string) and ($string ne '')) { #the regex did not compile, the rule is useless | ||
| 523 | $self->LogWarning("Option string is not defined or is empty"); | ||
| 524 | return (undef); | ||
| 525 | } | ||
| 526 | my $i = shift @o; | ||
| 527 | if ($i) { | ||
| 528 | $method = $method . 'I'; | ||
| 529 | $string = lc($string) | ||
| 530 | } | ||
| 531 | return $method, $string | ||
| 532 | } | ||
| 533 | |||
| 534 | sub SetupRuleDefault { | ||
| 535 | my ($self, $rule) = @_; | ||
| 536 | my $method = $tests{$rule->{'type'}}; | ||
| 537 | return $method | ||
| 538 | } | ||
| 539 | |||
| 540 | sub SetupRuleDetectChar { | ||
| 541 | my ($self, $rule) = @_; | ||
| 542 | my ($char, $i, $d) = $self->RuleGetArgs($rule, qw/char insensitive dynamic/ ); | ||
| 543 | my $method = $tests{$rule->{'type'}}; | ||
| 544 | unless ((defined $char) and ($char ne '')) { #the regex did not compile, the rule is useless | ||
| 545 | $self->LogWarning("Option char is not defined or is empty"); | ||
| 546 | return (undef); | ||
| 547 | } | ||
| 548 | $char = $self->RuleGetChar($char); | ||
| 549 | # unless (length($char) eq 1) { #the regex did not compile, the rule is useless | ||
| 550 | # $self->LogWarning("Option char is longer than one character"); | ||
| 551 | # return (undef); | ||
| 552 | # } | ||
| 553 | if ($d and $self->CurContextIsDynamic) { | ||
| 554 | $method = $method . 'D'; | ||
| 555 | } | ||
| 556 | if ($i) { | ||
| 557 | $method = $method . 'I'; | ||
| 558 | $char = lc($char) | ||
| 559 | } | ||
| 560 | return $method, $char | ||
| 561 | } | ||
| 562 | |||
| 563 | sub SetupRuleDetect2Chars { | ||
| 564 | my ($self, $rule) = @_; | ||
| 565 | my ($char, $char1, $i, $d) = $self->RuleGetArgs($rule, qw/char char1 insensitive dynamic/ ); | ||
| 566 | my $method = $tests{$rule->{'type'}}; | ||
| 567 | unless ((defined $char) and ($char ne '')) { #the regex did not compile, the rule is useless | ||
| 568 | $self->LogWarning("Option char is not defined or is empty"); | ||
| 569 | return (undef); | ||
| 570 | } | ||
| 571 | unless ((defined $char1) and ($char1 ne '')) { #the regex did not compile, the rule is useless | ||
| 572 | $self->LogWarning("Option char1 is not defined or is empty"); | ||
| 573 | return (undef); | ||
| 574 | } | ||
| 575 | $char = $self->RuleGetChar($char); | ||
| 576 | # unless (length($char) eq 1) { #the regex did not compile, the rule is useless | ||
| 577 | # $self->LogWarning("Option char is longer than one character"); | ||
| 578 | # return (undef); | ||
| 579 | # } | ||
| 580 | # $char1 = $self->RuleGetChar($char1); | ||
| 581 | # unless (length($char1) eq 1) { #the regex did not compile, the rule is useless | ||
| 582 | # $self->LogWarning("Option char1 is longer than one character"); | ||
| 583 | # return (undef); | ||
| 584 | # } | ||
| 585 | if ($d and $self->CurContextIsDynamic) { | ||
| 586 | $method = $method . 'D'; | ||
| 587 | } | ||
| 588 | if ($i) { | ||
| 589 | $method = $method . 'I'; | ||
| 590 | $char = lc($char); | ||
| 591 | $char1 = lc($char1); | ||
| 592 | } | ||
| 593 | return $method, $char, $char1 | ||
| 594 | } | ||
| 595 | |||
| 596 | sub SetupRuleDetectIdentifier { | ||
| 597 | my ($self, $rule) = @_; | ||
| 598 | my $method = $tests{$rule->{'type'}}; | ||
| 599 | return $method, $self->Delim2Reg($self->Deliminators) | ||
| 600 | } | ||
| 601 | |||
| 602 | sub SetupRuleKeyword { | ||
| 603 | my ($self, $rule) = @_; | ||
| 604 | my @o = $self->RuleGetArgs($rule, qw/String weakDeliminator additionalDeliminator/); | ||
| 605 | my $string = shift @o; | ||
| 606 | unless ((defined $string) and ($string ne '')) { | ||
| 607 | $self->LogWarning("Option String is not defined or is empty"); | ||
| 608 | return (undef); | ||
| 609 | } | ||
| 610 | my $method = $tests{$rule->{'type'}}; | ||
| 611 | unless ($self->KeywordsCase) { $method = $method . 'I' } | ||
| 612 | my $lsts = $self->Lists; | ||
| 613 | unless (exists $lsts->{$string}) { | ||
| 614 | $method = undef; | ||
| 615 | $self->LogWarning("List $string does not exist"); | ||
| 616 | } | ||
| 617 | my $d = $self->WordWrapDeliminators; | ||
| 618 | my %delim = %$d; | ||
| 619 | my $weak = shift @o; | ||
| 620 | $self->MergeWeakDeliminators(\%delim, $weak) if defined $weak; | ||
| 621 | my $additional = shift @o; | ||
| 622 | $self->MergeAdditionalDeliminators(\%delim, $additional) if defined $additional; | ||
| 623 | return $method, $lsts->{$string}, $self->Delim2Reg(\%delim); | ||
| 624 | } | ||
| 625 | |||
| 626 | sub SetupRuleLineContinue { | ||
| 627 | my ($self, $rule) = @_; | ||
| 628 | my ($char) = $self->RuleGetArgs($rule, 'char' ); | ||
| 629 | if (defined $char) { | ||
| 630 | $self->RuleGetChar($char); | ||
| 631 | } else { | ||
| 632 | $char = '\\' | ||
| 633 | } | ||
| 634 | unless (length($char) eq 1) { #the regex did not compile, the rule is useless | ||
| 635 | $self->LogWarning("Option char is longer than one character"); | ||
| 636 | return (undef); | ||
| 637 | } | ||
| 638 | if (index($regchars, $char) >= 0) { $char = "\\$char" }; | ||
| 639 | |||
| 640 | my $method = $tests{$rule->{'type'}}; | ||
| 641 | return $method, $char | ||
| 642 | } | ||
| 643 | |||
| 644 | sub SetupRuleNumber { | ||
| 645 | my ($self, $rule) = @_; | ||
| 646 | my ($weak, $additional) = $self->RuleGetArgs($rule, qw/weakDeliminator additionalDeliminator/); | ||
| 647 | my $d = $self->Deliminators; | ||
| 648 | my %delim = %$d; | ||
| 649 | $self->MergeWeakDeliminators(\%delim, $weak) if defined $weak; | ||
| 650 | $self->MergeAdditionalDeliminators(\%delim, $additional) if defined $additional; | ||
| 651 | my $method = $tests{$rule->{'type'}}; | ||
| 652 | return $method, $self->Delim2Reg(\%delim) | ||
| 653 | } | ||
| 654 | |||
| 655 | sub SetupRuleRangeDetect { | ||
| 656 | my ($self, $rule) = @_; | ||
| 657 | my ($char, $char1, $i, $d) = $self->RuleGetArgs($rule, qw/char char1 insensitive/ ); | ||
| 658 | my $method = $tests{$rule->{'type'}}; | ||
| 659 | unless ((defined $char) and ($char ne '')) { | ||
| 660 | $self->LogWarning("Option char is not defined or is empty"); | ||
| 661 | return (undef); | ||
| 662 | } | ||
| 663 | unless ((defined $char1) and ($char1 ne '')) { | ||
| 664 | $self->LogWarning("Option char1 is not defined or is empty"); | ||
| 665 | return (undef); | ||
| 666 | } | ||
| 667 | $char = $self->RuleGetChar($char); | ||
| 668 | unless (length($char) eq 1) { | ||
| 669 | $self->LogWarning("Option char is longer than one character"); | ||
| 670 | return (undef); | ||
| 671 | } | ||
| 672 | $char1 = $self->RuleGetChar($char1); | ||
| 673 | unless (length($char1) eq 1) { | ||
| 674 | $self->LogWarning("Option char1 is longer than one character"); | ||
| 675 | return (undef); | ||
| 676 | } | ||
| 677 | if ($i) { | ||
| 678 | $method = $method . 'I'; | ||
| 679 | $char = lc($char); | ||
| 680 | $char1 = lc($char1); | ||
| 681 | } | ||
| 682 | return $method, $char, $char1 | ||
| 683 | } | ||
| 684 | |||
| 685 | sub SetupRuleRegExpr { | ||
| 686 | my ($self, $rule) = @_; | ||
| 687 | my ($reg, $i, $d, $minimal) = $self->RuleGetArgs($rule, qw/ String insensitive dynamic minimal/ ); | ||
| 688 | unless ((defined $reg) and ($reg ne '')) { | ||
| 689 | $self->LogWarning("Option string is not defined or is empty"); | ||
| 690 | return (undef); | ||
| 691 | } | ||
| 692 | if ($minimal) { | ||
| 693 | my $string = ''; | ||
| 694 | my $lastchar = ''; | ||
| 695 | while ($reg ne '') { | ||
| 696 | if ($string =~ s/^(\*|\+)//) { | ||
| 697 | $string = "$string$1"; | ||
| 698 | if ($lastchar ne "\\") { | ||
| 699 | $string = "$string?"; | ||
| 700 | } | ||
| 701 | $lastchar = $1; | ||
| 702 | } else { | ||
| 703 | if ($reg =~ s/^(.)//) { | ||
| 704 | $string = "$string$1"; | ||
| 705 | $lastchar = $1; | ||
| 706 | } | ||
| 707 | } | ||
| 708 | } | ||
| 709 | $reg = $string | ||
| 710 | } | ||
| 711 | my $prepend; | ||
| 712 | my $method = $tests{$rule->{'type'}}; | ||
| 713 | unless ($reg ne '') { | ||
| 714 | $self->LogWarning("Option string is not defined or is empty"); | ||
| 715 | return (undef); | ||
| 716 | } | ||
| 717 | if ($reg =~ s/^\^//) { | ||
| 718 | $prepend = 'testCommonLineStart' | ||
| 719 | } elsif ($reg =~ s/^\\(b)//) { | ||
| 720 | $prepend = 'testCommonLastCharBb' | ||
| 721 | } elsif ($reg =~ s/^\\(B)//) { | ||
| 722 | $prepend = 'testCommonLastCharBB' | ||
| 723 | } | ||
| 724 | unless ($d and $self->CurContextIsDynamic) { | ||
| 725 | $reg = "^($reg)"; | ||
| 726 | no warnings; | ||
| 727 | if ($i) { | ||
| 728 | $reg = eval { qr/$reg/i }; | ||
| 729 | } else { | ||
| 730 | $reg = eval { qr/$reg/ }; | ||
| 731 | } | ||
| 732 | if ($@) { #the regex did not compile, the rule is useless | ||
| 733 | $self->LogWarning($@); | ||
| 734 | return (undef); | ||
| 735 | } | ||
| 736 | } | ||
| 737 | if ($d and $self->CurContextIsDynamic) { | ||
| 738 | $method = $method . 'D' | ||
| 739 | } | ||
| 740 | if ($i) { | ||
| 741 | $method = $method . 'I'; | ||
| 742 | } | ||
| 743 | my @out = ($reg); | ||
| 744 | if (defined $prepend) { | ||
| 745 | unshift @out, $prepend, $self->{ENGINE}->can($method) ; | ||
| 746 | } else { | ||
| 747 | unshift @out, $method | ||
| 748 | } | ||
| 749 | return @out | ||
| 750 | } | ||
| 751 | |||
| 752 | sub SetupRuleStringDetect { | ||
| 753 | my ($self, $rule) = @_; | ||
| 754 | my @o = $self->RuleGetArgs($rule, qw/String insensitive dynamic/ ); | ||
| 755 | my $method = $tests{$rule->{'type'}}; | ||
| 756 | my $string = shift @o; | ||
| 757 | my $i = shift @o; | ||
| 758 | my $d = shift @o; | ||
| 759 | unless ((defined $string) and ($string ne '')) { | ||
| 760 | $self->LogWarning("Option string is not defined or is empty"); | ||
| 761 | return (undef); | ||
| 762 | } | ||
| 763 | if ($d and $self->CurContextIsDynamic) { | ||
| 764 | $method = $method . 'D' | ||
| 765 | } | ||
| 766 | if ($i) { | ||
| 767 | $method = $method . 'I'; | ||
| 768 | $string = lc($string); | ||
| 769 | } | ||
| 770 | return $method, $string | ||
| 771 | } | ||
| 772 | |||
| 773 | sub SetupRuleWordDetect { | ||
| 774 | my ($self, $rule) = @_; | ||
| 775 | my @o = $self->RuleGetArgs($rule, qw/String insensitive dynamic weakDeliminator additionalDeliminator/ ); | ||
| 776 | my $method = $tests{$rule->{'type'}}; | ||
| 777 | my $string = shift @o; | ||
| 778 | my $i = shift @o; | ||
| 779 | my $d = shift @o; | ||
| 780 | unless ((defined $string) and ($string ne '')) { | ||
| 781 | $self->LogWarning("Option string is not defined or is empty"); | ||
| 782 | return (undef); | ||
| 783 | } | ||
| 784 | if ($d and $self->CurContextIsDynamic) { | ||
| 785 | $method = $method . 'D' | ||
| 786 | } | ||
| 787 | if ($i) { | ||
| 788 | $method = $method . 'I'; | ||
| 789 | $string = lc($string); | ||
| 790 | } | ||
| 791 | my $wwd = $self->WordWrapDeliminators; | ||
| 792 | my %delim = %$wwd; | ||
| 793 | my $weak = shift @o; | ||
| 794 | $self->MergeWeakDeliminators(\%delim, $weak) if defined $weak; | ||
| 795 | my $additional = shift @o; | ||
| 796 | $self->MergeAdditionalDeliminators(\%delim, $additional) if defined $additional; | ||
| 797 | return $method, $string, $self->Delim2Reg(\%delim); | ||
| 798 | } | ||
| 799 | |||
| 800 | sub SyntaxExists { | ||
| 801 | my ($self, $syntax) = @_; | ||
| 802 | return (exists $self->Engine->{INDEXER}->{INDEX}->{$syntax}) | ||
| 803 | } | ||
| 804 | |||
| 805 | # sub XMLGetLanguage { | ||
| 806 | # my ($self, $token, $parser) = @_; | ||
| 807 | # my $args = $token->[2]; | ||
| 808 | # $self->Language($args->{'name'}); | ||
| 809 | # } | ||
| 810 | |||
| 811 | |||
| 812 | 1; | ||
| 813 | |||
| 814 | __END__ | ||