File: GlobalRules.pm

package info (click to toggle)
libcircle-be-perl 0.173320-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 388 kB
  • sloc: perl: 6,042; makefile: 2; sh: 1
file content (449 lines) | stat: -rw-r--r-- 10,119 bytes parent folder | download | duplicates (3)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#  You may distribute under the terms of the GNU General Public License
#
#  (C) Paul Evans, 2008-2017 -- leonerd@leonerd.org.uk

package Circle::GlobalRules;

use strict;
use warnings;

our $VERSION = '0.173320';

use Text::Balanced qw( extract_delimited extract_quotelike );

use base qw( Circle::Rule::Store ); # for the attributes

use Circle::TaggedString;

sub unquote_qr
{
   my $re = shift;

   $re = "$re";

   # Perl tries to put (?-xism:RE) around our pattern. Lets attempt to remove
   # it if we can
   # Recent perls use (?^:RE) instead
   $re =~ s/^\(\?-xism:(.*)\)$/$1/;
   $re =~ s/^\(\?\^:(.*)\)$/$1/;

   return ( $2, $1 ) if $re =~ m/^\(\?([ixsm]*)-?[xism]*:(.*)\)$/;
   return ( $2, $1 ) if $re =~ m/^\(\?\^([ixsm]*):(.*)\)$/;

   # Failed. Lets just be safe then
   return ( $re, "" );
}

# Not an object class. Instead, just a store of rule subs

sub register
{
   my ( $rulestore ) = @_;

   $rulestore->register_cond( matches => __PACKAGE__ );

   $rulestore->register_action( rewrite => __PACKAGE__ );
   $rulestore->register_action( format => __PACKAGE__ );
   $rulestore->register_action( unformat => __PACKAGE__ );
   $rulestore->register_action( level => __PACKAGE__ );
   $rulestore->register_action( highlight => __PACKAGE__ );
}

###### CONDITIONS

### MATCHES

sub parse_cond_matches
   : Rule_description("Look for regexp or substring matches in the text")
   : Rule_format('/regexp/ or "literal"')
{
   shift; # class
   my ( $spec ) = @_;

   if( $spec =~ m{^/} ) {
      # Try to pull the flags
      my ( $content, $flags ) = $spec =~ m{^/(.*)/([i]*)$} or die "Unrecognised regexp string $spec\n";

      return qr/$content/i if $flags eq "i";
      return qr/$content/;
   }
   elsif( $spec =~ m{^"} ) {
      my ( $content ) = $spec =~ m{^"(.*)"$} or die "Unrecognised literal string $spec\n";

      return qr/\Q$content/;
   }
   else {
      die "Unrecognised string type $spec";
   }
}

sub deparse_cond_matches
{
   shift; # class
   my ( $re ) = @_;

   my ( $pattern, $flags ) = unquote_qr( $re );
   return "/$pattern/$flags";
}

sub eval_cond_matches
{
   shift; # class
   my ( $event, $results, $re ) = @_;

   defined( my $text = $event->{text} ) or return 0;
   $text = "$text";  # stringify a String::Tagged

   pos( $text ) = 0;

   my $matched;

   while( $text =~ m/$re/g ) {
      my @matchgroups;
      for ( 0 .. $#+ ) {
         my ( $start, $end ) = ( $-[$_], $+[$_] );
         my $len = $end - $start;

         push @matchgroups, [ $start, $len ];
      }

      $results->push_result( "matchgroups", \@matchgroups );
      $matched = 1;
   }

   return $matched;
}

###### ACTIONS

### REWRITE

sub parse_action_rewrite
   : Rule_description("Rewrite text of the line or matched parts")
   : Rule_format('line|matches|match(number) "string"|s/pattern/replacement/')
{
   shift; # class
   my ( $spec ) = @_;

   $spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n";
   my $type = $1;

   my $groupnum;

   if( $type eq "line" ) {
      $groupnum = -1;
   }
   elsif( $type eq "matches" ) {
      $groupnum = 0;
   }
   elsif( $type eq "match" ) {
      $spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n";
      $groupnum = $1;
   }
   else {
      die "Unrecognised format type $type\n";
   }

   my ( undef, $remains, undef, $op, $delim, $lhs, undef, undef, $rhs, undef, $mods ) = extract_quotelike( $spec )
      or die 'Expected "string" or s/pattern/replacement/';
   $spec = $remains;
   $op = $delim if $op eq "";

   if( $op eq '"' ) {
      # Literal
      return ( $groupnum, literal => $lhs );
   }
   elsif( $op eq "s" ) {
      # s/foo/bar/
      my $global = $mods =~ s/g//;
      # TODO: Range check that mods contains only /ism
      return ( $groupnum, subst => qr/(?$mods:$lhs)/, $rhs, $global );
   }
   else {
      die 'Expected "string" or s/pattern/replacement/';
   }
}

sub deparse_action_rewrite
{
   shift; # class
   my ( $groupnum, $kind, $lhs, $rhs, $global ) = @_;

   my $type = $groupnum == -1 ? "line" :
              $groupnum ==  0 ? "matches" :
                                "match($groupnum)";

   if( $kind eq "literal" ) {
      return "$type \"$lhs\"";
   }
   elsif( $kind eq "subst" ) {
      my ( $pattern, $flags ) = unquote_qr( $lhs );
      return "$type s/$pattern/$rhs/$flags" . ( $global ? "g" : "" );
   }
}

sub eval_action_rewrite
{
   shift; # class
   my ( $event, $results, $groupnum, $kind, $lhs, $rhs, $global ) = @_;

   my @location;
   if( $groupnum == -1 ) {
      @location = ( 0, -1 );
   }
   else {
      foreach my $groups ( @{ $results->get_result( "matchgroups" ) } ) {
         my $group = $groups->[$groupnum] or next;
         @location = @$group;
         last; # can only do the first one
      }
   }

   ref $event->{text} or $event->{text} = Circle::TaggedString->new( $event->{text} );
   my $text = $event->{text}->substr( $location[0], $location[1] );

   if( $kind eq "literal" ) {
      $text = $lhs;
   }
   elsif( $kind eq "subst" ) {
      $text =~ s/$lhs/$rhs/  if !$global;
      $text =~ s/$lhs/$rhs/g if  $global;
   }

   $event->{text}->set_substr( $location[0], $location[1], $text );
}

### FORMAT

sub parse_action_format
   : Rule_description("Apply formatting to the line or matched parts")
   : Rule_format('line|matches|match(number) key="value" [key="value" ...]')
{
   shift; # class
   my ( $spec ) = @_;

   $spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n";
   my $type = $1;

   my $groupnum;

   if( $type eq "line" ) {
      $groupnum = -1;
   }
   elsif( $type eq "matches" ) {
      $groupnum = 0;
   }
   elsif( $type eq "match" ) {
      $spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n";
      $groupnum = $1;
   }
   else {
      die "Unrecognised format type $type\n";
   }

   my %format;
   while( $spec =~ s/^(\w+)=// ) {
      my $name = $1;

      my $value = extract_delimited( $spec, q{"'} );
      s/^["']//, s/["']$// for $value;

      $format{$name} = $value;

      $spec =~ s/^\s+//;
   }

   if( length $spec ) {
      die "Unrecognised format spec $spec\n";
   }

   return ( $groupnum, \%format );
}

sub deparse_action_format
{
   shift; # class
   my ( $groupnum, $formathash ) = @_;

   return unless %$formathash;

   my $type = $groupnum == -1 ? "line" :
              $groupnum ==  0 ? "matches" :
                                "match($groupnum)";

   return "$type ".join( " ", map { qq($_="$formathash->{$_}") } sort keys %$formathash );
}

sub eval_action_format
{
   shift; # class
   my ( $event, $results, $groupnum, $formathash ) = @_;

   my $str = $event->{text};
   ref $str or $str = Circle::TaggedString->new( $str );

   if( $groupnum == -1 ) {
      $str->apply_tag( 0, -1, $_, $formathash->{$_} ) for keys %$formathash;
   }
   else {
      foreach my $groups ( @{ $results->get_result( "matchgroups" ) } ) {
         my $group = $groups->[$groupnum] or next;
         my ( $start, $len ) = @$group;

         $str->apply_tag( $start, $len, $_, $formathash->{$_} ) for keys %$formathash;
      }
   }
}

### UNFORMAT

sub parse_action_unformat
   : Rule_description("Remove formatting from the line or matched parts")
   : Rule_format('line|matches|match(number) key [key ...]')
{
   shift; # class
   my ( $spec ) = @_;

   $spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n";
   my $type = $1;

   my $groupnum;

   if( $type eq "line" ) {
      $groupnum = -1;
   }
   elsif( $type eq "matches" ) {
      $groupnum = 0;
   }
   elsif( $type eq "match" ) {
      $spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n";
      $groupnum = $1;
   }
   else {
      die "Unrecognised format type $type\n";
   }

   my @tags;
   while( $spec =~ s/^(\w+)// ) {
      my $name = $1;

      push @tags, $name;

      $spec =~ s/^\s+//;
   }

   if( length $spec ) {
      die "Unrecognised format spec $spec\n";
   }

   return ( $groupnum, \@tags );
}

sub deparse_action_unformat
{
   shift; # class
   my ( $groupnum, $taglist ) = @_;

   my $type = $groupnum == -1 ? "line" :
              $groupnum ==  0 ? "matches" :
                                "match($groupnum)";

   my $ret = $type;
   $ret .= " $_" for @$taglist;

   return $ret;
}

my @alltags = qw( fg bg b u i );

sub eval_action_unformat
{
   shift; # class
   my ( $event, $results, $groupnum, $taglist ) = @_;

   $taglist = \@alltags unless @$taglist;

   my $str = $event->{text};
   ref $str or $str = Circle::TaggedString->new( $str );

   if( $groupnum == -1 ) {
      $str->unapply_tag( 0, -1, $_ ) for @$taglist;
   }
   else {
      foreach my $groups ( @{ $results->get_result( "matchgroups" ) } ) {
         my $group = $groups->[$groupnum] or next;
         my ( $start, $len ) = @$group;

         $str->unapply_tag( $start, $len, $_ ) for @$taglist;
      }
   }
}

### LEVEL

sub parse_action_level
   : Rule_description("Set the activity level for the targetted item")
   : Rule_format('$level')
{
   shift; # class
   my ( $spec ) = @_;

   $spec =~ s/^(\d)// or die "Expected level number as first argument\n";
   my $level = $1;

   $level >= 0 and $level <= 3 or die "Expected 'level' between 0 and 3\n";

   return ( $level );
}

sub deparse_action_level
{
   shift; # class
   my ( $level ) = @_;

   return "$level";
}

sub eval_action_level
{
   shift; # class
   my ( $event, $results, $level ) = @_;

   $event->{level} = $level;
}

## HIGHLIGHT

sub parse_action_highlight
   : Rule_description("Highlight matched regions and set activity level to 3")
   : Rule_format('')
{
   my $self = shift;
   return;
}

sub deparse_action_highlight
{
   my $self = shift;
   return;
}

sub eval_action_highlight
{
   my $self = shift;
   my ( $event, $results ) = @_;

   my $str = $event->{text};
   ref $str or $str = Circle::TaggedString->new( $str );

   foreach my $matchgroup ( @{ $results->get_result( "matchgroups" ) } ) {
      my ( $start, $len ) = @{$matchgroup->[0]}[0,1];

      $str->apply_tag( $start, $len, b => 1 );
      $str->apply_tag( $start, $len, fg => "highlight" );
   }

   $event->{level} = 3;
}

0x55AA;