File Coverage

File:lib/CheckSpelling/UnknownWordSplitter.pm
Coverage:88.2%

linestmtbrancondsubtimecode
1#! -*-perl-*-
2
3# ~/bin/w
4# Search for potentially misspelled words
5# Output is:
6# misspellled
7# woord (WOORD, Woord, woord, woord's)
8package CheckSpelling::UnknownWordSplitter;
9
10
1
1
109562
3
use 5.022;
11
1
1
1
2
1
50
use feature 'unicode_strings';
12
1
1
1
1
1
8
use strict;
13
1
1
1
2
0
19
use warnings;
14
1
1
1
2
0
33
no warnings qw(experimental::vlb);
15
1
1
1
1
3
2
use utf8;
16
1
1
1
12
0
26
use Encode qw/decode_utf8 encode FB_DEFAULT/;
17
1
1
1
2
1
25
use File::Basename;
18
1
1
1
2
0
16
use Cwd 'abs_path';
19
1
1
1
3
1
15
use File::Temp qw/ tempfile tempdir /;
20
1
1
1
267
1
3249
use CheckSpelling::Util;
21our $VERSION='0.1.0';
22
23my ($longest_word, $shortest_word, $word_match, $forbidden_re, $patterns_re, $candidates_re, $disable_word_collating, $check_file_names);
24my ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
25my ($shortest, $longest) = (255, 0);
26my @forbidden_re_list;
27my %forbidden_re_descriptions;
28my @candidates_re_list;
29my $hunspell_dictionary_path;
30my @hunspell_dictionaries;
31my %dictionary = ();
32my $base_dict;
33my %unique;
34my %unique_unrecognized;
35my ($last_file, $words, $unrecognized) = ('', 0, 0);
36my ($ignore_next_line_pattern);
37
38my $disable_flags;
39
40sub test_re {
41
14
10
  my ($expression) = @_;
42
14
14
7
102
  return eval { qr /$expression/ };
43}
44
45sub quote_re {
46
14
10
  my ($expression) = @_;
47
14
12
  return $expression if $expression =~ /\?\{/;
48
14
32
  $expression =~ s/
49   \G
50   (
51      (?:[^\\]|\\[^Q])*
52   )
53   (?:
54      \\Q
55      (?:[^\\]|\\[^E])*
56      (?:\\E)?
57   )?
58/
59
28
42
   $1 . (defined($2) ? quotemeta($2) : '')
60/xge;
61
14
17
  return $expression;
62}
63
64sub file_to_lists {
65
3
3
  my ($re) = @_;
66
3
3
  my @patterns;
67  my %hints;
68
3
0
  my $fh;
69
3
28
  if (open($fh, '<:utf8', $re)) {
70
3
5
    local $/=undef;
71
3
20
    my $file=<$fh>;
72
3
8
    close $fh;
73
3
1
    my $line_number = 0;
74
3
3
    my $hint = '';
75
3
12
    for (split /\R/, $file) {
76
17
11
      ++$line_number;
77
17
8
      chomp;
78
17
17
      if (/^#(?:\s(.+)|)/) {
79
6
14
        $hint = $1 if ($hint eq '' && defined $1);
80
6
4
        next;
81      }
82
11
11
      $hint = '' unless $_ ne '';
83
11
10
      my $pattern = $_;
84
11
25
      next unless s/^(.+)/(?:$1)/;
85
7
10
      my $quoted = quote_re($1);
86
7
7
      unless (test_re $quoted) {
87
1
1
        my $error = $@;
88
1
44
        my $home = dirname(__FILE__);
89
1
19
        $error =~ s/$home.*?\.pm line \d+\./$re line $line_number (bad-regex)/;
90
1
12
        print STDERR $error;
91
1
2
        $_ = '(?:\$^ - skipped because bad-regex)';
92
1
2
        $hint = '';
93      }
94
7
10
      if (defined $hints{$_}) {
95
1
1
        my $pattern_length = length $pattern;
96
1
2
        my $wrapped = CheckSpelling::Util::wrap_in_backticks($pattern);
97
1
16
        print STDERR "$re:$line_number:1 ... $pattern_length, Warning - duplicate pattern: $wrapped (duplicate-pattern)\n";
98
1
2
        $_ = '(?:\$^ - skipped because duplicate-pattern on $line_number)';
99      } else {
100
6
7
        push @patterns, $_;
101
6
9
        $hints{$_} = $hint;
102      }
103
7
11
      $hint = '';
104    }
105  }
106
107  return {
108
3
11
    patterns => \@patterns,
109    hints => \%hints,
110  };
111}
112
113sub file_to_list {
114
2
1172
  my ($re) = @_;
115
2
4
  my $lists = file_to_lists($re);
116
117
2
2
1
6
  return @{$lists->{'patterns'}};
118}
119
120sub list_to_re {
121
2
2
  my (@list) = @_;
122
2
5
5
1
2
4
  @list = map { my $quoted = quote_re($_); test_re($quoted) ? $quoted : '' } @list;
123
2
5
1
6
  @list = grep { $_ ne '' } @list;
124
2
1
  return '$^' unless scalar @list;
125
2
6
  return join "|", (@list);
126}
127
128sub not_empty {
129
51
43
  my ($thing) = @_;
130
51
161
  return defined $thing && $thing ne ''
131}
132
133sub valid_word {
134  # shortest_word is an absolute
135
22
12
  our ($shortest, $longest, $shortest_word, $longest_word);
136
22
16
  $shortest = $shortest_word if $shortest_word;
137
22
18
  if ($longest_word) {
138    # longest_word is an absolute
139
20
18
    $longest = $longest_word;
140  } elsif (not_empty($longest)) {
141    # we allow for some sloppiness (a couple of stuck keys per word)
142    # it's possible that this should scale with word length
143
1
1
    $longest += 2;
144  }
145
22
15
  our ($upper_pattern, $lower_pattern, $punctuation_pattern);
146
22
66
13
148
  my $word_pattern = join '|', (grep { defined $_ && /./ } ($upper_pattern, $lower_pattern, $punctuation_pattern));
147
22
18
  $word_pattern = q<\\w|'> unless $word_pattern;
148
22
39
  if ((defined $shortest && not_empty($longest)) &&
149      ($shortest > $longest)) {
150
0
0
    $word_pattern = "(?:$word_pattern){3}";
151
0
0
    return qr/$word_pattern/;
152  }
153
22
28
  $shortest = 3 unless defined $shortest;
154
22
20
  $longest = '' unless defined $longest;
155
22
71
  $word_match = "(?:$word_pattern){$shortest,$longest}";
156
22
189
  return qr/\b$word_match\b/;
157}
158
159sub load_dictionary {
160
12
1942
  my ($dict) = @_;
161
12
5
  our ($word_match, $longest, $shortest, $longest_word, $shortest_word, %dictionary);
162
12
12
  $longest_word = CheckSpelling::Util::get_val_from_env('INPUT_LONGEST_WORD', undef);
163
12
6
  $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef);
164
12
7
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
165
12
16
  $ignore_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_IGNORE_PATTERN', q<[^a-zA-Z']>);
166
12
37
  $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]');
167
12
26
  $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]');
168
12
20
  $not_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_LOWER_PATTERN', '[^a-z]');
169
12
16
  $not_upper_or_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_UPPER_OR_LOWER_PATTERN', '[^A-Za-z]');
170
12
18
  $punctuation_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_PUNCTUATION_PATTERN', q<'>);
171
12
26
  %dictionary = ();
172
173
12
535
  open(DICT, '<:utf8', $dict);
174
12
52
  while (!eof(DICT)) {
175
31
33
    my $word = <DICT>;
176
31
30
    chomp $word;
177
31
68
    next unless $word =~ $word_match;
178
28
25
    my $l = length $word;
179
28
17
    $longest = -1 unless not_empty($longest);
180
28
27
    $longest = $l if $l > $longest;
181
28
21
    $shortest = $l if $l < $shortest;
182
28
51
    $dictionary{$word}=1;
183  }
184
12
28
  close DICT;
185
186
12
10
  $word_match = valid_word();
187}
188
189sub hunspell_dictionary {
190
3
5
  my ($dict) = @_;
191
3
3
  my $name = $dict;
192
3
3
  $name =~ s{/src/index/hunspell/index\.dic$}{};
193
3
11
  $name =~ s{.*/}{};
194
3
4
  my $aff = $dict;
195
3
3
  my $encoding;
196
3
7
  $aff =~ s/\.dic$/.aff/;
197
3
24
  if (open AFF, '<', $aff) {
198
3
17
    while (<AFF>) {
199
0
0
      next unless /^SET\s+(\S+)/;
200
0
0
      $encoding = $1 if ($1 !~ /utf-8/i);
201
0
0
      last;
202    }
203
3
7
    close AFF;
204  }
205  return {
206
3
277
    name => $name,
207    dict => $dict,
208    aff => $aff,
209    encoding => $encoding,
210    engine => Text::Hunspell->new($aff, $dict),
211  }
212}
213
214sub init {
215
9
10688
  my ($configuration) = @_;
216
9
9
  our ($word_match, %unique, $patterns_re, @forbidden_re_list, $forbidden_re, @candidates_re_list, $candidates_re);
217
9
19
  our $sandbox = CheckSpelling::Util::get_file_from_env('sandbox', '');
218
9
9
  our $hunspell_dictionary_path = CheckSpelling::Util::get_file_from_env('hunspell_dictionary_path', '');
219
9
13
  our $timeout = CheckSpelling::Util::get_val_from_env('splitter_timeout', 30);
220
9
4
  our %forbidden_re_descriptions;
221
9
9
  if ($hunspell_dictionary_path) {
222
3
32
    our @hunspell_dictionaries = ();
223
1
1
1
1
1
1
1
1
1
3
168
929
19
4
1
15
5
1
17
134
    if (eval 'use Text::Hunspell; 1') {
224
3
101
      my @hunspell_dictionaries_list = glob("$hunspell_dictionary_path/*.dic");
225
3
6
      for my $hunspell_dictionary_file (@hunspell_dictionaries_list) {
226
3
6
        push @hunspell_dictionaries, hunspell_dictionary($hunspell_dictionary_file);
227      }
228    } else {
229
0
0
      print STDERR "Could not load Text::Hunspell for dictionaries (hunspell-unavailable)\n";
230    }
231  }
232
9
11
  my (@patterns_re_list, %in_patterns_re_list);
233
9
54
  if (-e "$configuration/patterns.txt") {
234
0
0
    @patterns_re_list = file_to_list "$configuration/patterns.txt";
235
0
0
    $patterns_re = list_to_re @patterns_re_list;
236
0
0
0
0
    %in_patterns_re_list = map {$_ => 1} @patterns_re_list;
237  } else {
238
9
5
    $patterns_re = undef;
239  }
240
241
9
32
  if (-e "$configuration/forbidden.txt") {
242
1
1
    my $forbidden_re_info = file_to_lists "$configuration/forbidden.txt";
243
1
1
1
1
    @forbidden_re_list = @{$forbidden_re_info->{'patterns'}};
244
1
1
1
3
    %forbidden_re_descriptions = %{$forbidden_re_info->{'hints'}};
245
1
1
    $forbidden_re = list_to_re @forbidden_re_list;
246  } else {
247
8
11
    $forbidden_re = undef;
248  }
249
250
9
36
  if (-e "$configuration/candidates.txt") {
251
1
1
    @candidates_re_list = file_to_list "$configuration/candidates.txt";
252
1
2
2
2
2
3
    @candidates_re_list = map { my $quoted = quote_re($_); $in_patterns_re_list{$_} || !test_re($quoted) ? '' : $quoted } @candidates_re_list;
253
1
1
    $candidates_re = list_to_re @candidates_re_list;
254  } else {
255
8
4
    $candidates_re = undef;
256  }
257
258
9
14
  our $largest_file = CheckSpelling::Util::get_val_from_env('INPUT_LARGEST_FILE', 1024*1024);
259
260
9
9
  my $disable_flags = CheckSpelling::Util::get_file_from_env('INPUT_DISABLE_CHECKS', '');
261
9
8
  our $disable_word_collating = $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/;
262
9
7
  our $disable_minified_file = $disable_flags =~ /(?:^|,|\s)minified-file(?:,|\s|$)/;
263
9
2
  our $disable_single_line_file = $disable_flags =~ /(?:^|,|\s)single-line-file(?:,|\s|$)/;
264
265
9
7
  our $ignore_next_line_pattern = CheckSpelling::Util::get_file_from_env('INPUT_IGNORE_NEXT_LINE', '');
266
9
8
  $ignore_next_line_pattern =~ s/\s+/|/g;
267
268
9
6
  our $check_file_names = CheckSpelling::Util::get_file_from_env('check_file_names', '');
269
270
9
6
  our $use_magic_file = CheckSpelling::Util::get_val_from_env('INPUT_USE_MAGIC_FILE', '');
271
272
9
9
  $word_match = valid_word();
273
274
9
15
  our $base_dict = CheckSpelling::Util::get_file_from_env('dict', "$configuration/words");
275
9
38
  $base_dict = '/usr/share/dict/words' unless -e $base_dict;
276
9
9
  load_dictionary($base_dict);
277}
278
279sub split_line {
280
1165
511
  our (%dictionary, $word_match, $disable_word_collating);
281
1165
420
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
282
1165
405
  our @hunspell_dictionaries;
283
1165
443
  our $shortest;
284
1165
729
  my $shortest_threshold = $shortest + 2;
285
1165
671
  my $pattern = '.';
286  # $pattern = "(?:$upper_pattern){$shortest,}|$upper_pattern(?:$lower_pattern){2,}\n";
287
288  # https://www.fileformat.info/info/unicode/char/2019/
289
1165
476
  my $rsqm = "\xE2\x80\x99";
290
291
1165
707
  my ($words, $unrecognized) = (0, 0);
292
1165
786
  my ($line, $unique_ref, $unique_unrecognized_ref, $unrecognized_line_items_ref) = @_;
293
1165
6163
    $line =~ s/(?:$rsqm|&apos;|&#39;|\%27|&#8217;|&#x2019;|&rsquo;|\\u2019|\x{2019}|')+/'/g;
294
1165
2687
    $line =~ s/(?:$ignore_pattern)+/ /g;
295
1165
1795
    while ($line =~ s/($upper_pattern{2,})($upper_pattern$lower_pattern{2,})/ $1 $2 /g) {}
296
1165
4615
    while ($line =~ s/((?:$lower_pattern|$punctuation_pattern)+)($upper_pattern)/$1 $2/g) {}
297
1165
1583
    for my $token (split /\s+/, $line) {
298
5878
5204
      next unless $token =~ /$pattern/;
299
4714
3713
      $token =~ s/^(?:'|$rsqm)+//g;
300
4714
4475
      $token =~ s/(?:'|$rsqm)+s?$//g;
301
4714
2743
      my $raw_token = $token;
302
4714
2601
      $token =~ s/^[^Ii]?'+(.*)/$1/;
303
4714
2255
      $token =~ s/(.*?)'+$/$1/;
304
4714
6758
      next unless $token =~ $word_match;
305
4561
3872
      if (defined $dictionary{$token}) {
306
1032
456
        ++$words;
307
1032
535
        $unique_ref->{$token}=1;
308
1032
824
        next;
309      }
310
3529
2333
      if (@hunspell_dictionaries) {
311
3504
1635
        my $found = 0;
312
3504
1809
        for my $hunspell_dictionary (@hunspell_dictionaries) {
313          my $token_encoded = defined $hunspell_dictionary->{'encoding'} ?
314
3504
2620
            encode($hunspell_dictionary->{'encoding'}, $token) : $token;
315
3504
6083
          next unless ($hunspell_dictionary->{'engine'}->check($token_encoded));
316
0
0
          ++$words;
317
0
0
          $dictionary{$token} = 1;
318
0
0
          $unique_ref->{$token}=1;
319
0
0
          $found = 1;
320
0
0
          last;
321        }
322
3504
2387
        next if $found;
323      }
324
3529
2270
      my $key = lc $token;
325
3529
2895
      if (defined $dictionary{$key}) {
326
6
4
        ++$words;
327
6
4
        $unique_ref->{$key}=1;
328
6
6
        next;
329      }
330
3523
2304
      unless ($disable_word_collating) {
331
3523
1874
        $key =~ s/''+/'/g;
332
3523
3449
        $key =~ s/'[sd]$// unless length $key >= $shortest_threshold;
333      }
334
3523
2720
      if (defined $dictionary{$key}) {
335
0
0
        ++$words;
336
0
0
        $unique_ref->{$key}=1;
337
0
0
        next;
338      }
339
3523
1625
      ++$unrecognized;
340
3523
1915
      $unique_unrecognized_ref->{$raw_token}=1;
341
3523
4057
      $unrecognized_line_items_ref->{$raw_token}=1;
342    }
343
1165
1661
    return ($words, $unrecognized);
344}
345
346sub skip_file {
347
15
28
  my ($temp_dir, $reason) = @_;
348
15
445
  open(SKIPPED, '>:utf8', "$temp_dir/skipped");
349
15
93
  print SKIPPED $reason;
350
15
356
  close SKIPPED;
351}
352
353sub split_file {
354
15
10465
  my ($file) = @_;
355  our (
356
15
8
    $unrecognized, $shortest, $largest_file, $words,
357    $word_match, %unique, %unique_unrecognized, $forbidden_re,
358    @forbidden_re_list, $patterns_re, %dictionary,
359    $candidates_re, @candidates_re_list, $check_file_names, $use_magic_file, $disable_minified_file,
360    $disable_single_line_file,
361    $ignore_next_line_pattern,
362    $sandbox,
363  );
364
15
31
  $ignore_next_line_pattern = '$^' unless $ignore_next_line_pattern =~ /./;
365
366
15
8
  our %forbidden_re_descriptions;
367
15
7
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
368
369  # https://www.fileformat.info/info/unicode/char/2019/
370
15
19
  my $rsqm = "\xE2\x80\x99";
371
372
15
15
  my @candidates_re_hits = (0) x scalar @candidates_re_list;
373
15
12
  my @candidates_re_lines = (0) x scalar @candidates_re_list;
374
15
12
  my @forbidden_re_hits = (0) x scalar @forbidden_re_list;
375
15
9
  my @forbidden_re_lines = (0) x scalar @forbidden_re_list;
376
15
23
  my $temp_dir = tempdir(DIR=>$sandbox);
377
15
2366
  print STDERR "checking file: $file\n" if defined $ENV{'DEBUG'};
378
15
327
  open(NAME, '>', "$temp_dir/name");
379
15
33
    print NAME $file;
380
15
166
  close NAME;
381
15
46
  my $file_size = -s $file;
382
15
18
  if (defined $largest_file) {
383
15
13
    unless ($check_file_names eq $file) {
384
15
14
      if ($file_size > $largest_file) {
385
1
3
        skip_file($temp_dir, "size `$file_size` exceeds limit `$largest_file` (large-file)\n");
386
1
3
        return $temp_dir;
387      }
388    }
389  }
390
14
14
  if ($use_magic_file) {
391
8
11484
    if (open(my $file_fh, '-|',
392              '/usr/bin/file',
393              '-b',
394              '--mime',
395              '-e', 'cdf',
396              '-e', 'compress',
397              '-e', 'csv',
398              '-e', 'elf',
399              '-e', 'json',
400              '-e', 'tar',
401              $file)) {
402
8
28487
      my $file_kind = <$file_fh>;
403
8
4864
      close $file_fh;
404
8
115
      if ($file_kind =~ /^(.*?); charset=binary/) {
405
2
26
        skip_file($temp_dir, "it appears to be a binary file (`$1`) (binary-file)\n");
406
2
46
        return $temp_dir;
407      }
408    }
409  }
410
12
98
  open FILE, '<', $file;
411
12
10
  binmode FILE;
412
12
5
  my $head;
413
12
86
  read(FILE, $head, 4096);
414
12
791
  $head =~ s/(?:\r|\n)+$//;
415
12
57
  my $dos_new_lines = () = $head =~ /\r\n/gi;
416
12
37
  my $unix_new_lines = () = $head =~ /\n/gi;
417
12
94
  my $mac_new_lines = () = $head =~ /\r/gi;
418
12
47
  local $/;
419
12
66
  if ($unix_new_lines == 0 && $mac_new_lines == 0) {
420
3
10
    $/ = "\n";
421  } elsif ($dos_new_lines >= $unix_new_lines && $dos_new_lines >= $mac_new_lines) {
422
1
5
    $/ = "\r\n";
423  } elsif ($mac_new_lines > $unix_new_lines) {
424
2
7
    $/ = "\r";
425  } else {
426
6
8
    $/ = "\n";
427  }
428
12
17
  seek(FILE, 0, 0);
429
12
21
  ($words, $unrecognized) = (0, 0);
430
12
31
  %unique = ();
431
12
19
  %unique_unrecognized = ();
432
433  local $SIG{__WARN__} = sub {
434
0
0
    my $message = shift;
435
0
0
    $message =~ s/> line/> in $file - line/;
436
0
0
    chomp $message;
437
0
0
    print STDERR "$message\n";
438
12
95
  };
439
440
12
316
  open(WARNINGS, '>:utf8', "$temp_dir/warnings");
441
12
5
  our $timeout;
442
12
9
  eval {
443
12
0
82
0
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
444
12
29
    alarm $timeout;
445
446
12
13
    my $ignore_next_line = 0;
447
12
10
    my $offset = 0;
448
12
85
    while (<FILE>) {
449
1169
1021
      if ($. == 1) {
450
12
18
        unless ($disable_minified_file) {
451
12
45
          if ($file_size >= 512 && length($_) == $file_size) {
452
1
7
            skip_file($temp_dir, "file only has a single line (single-line-file)\n");
453
1
5
            last;
454          }
455        }
456      }
457
1168
2345
      $_ = decode_utf8($_, FB_DEFAULT);
458
1168
2701
      if (/[\x{D800}-\x{DFFF}]/) {
459
0
0
        skip_file($temp_dir, "file contains a UTF-16 surrogate -- UTF-16 surrogates are not supported (utf16-surrogate-file)\n");
460
0
0
        last;
461      }
462
1168
1486
      s/\R$//;
463
1168
1040
      s/^\x{FEFF}// if $. == 1;
464
1168
1002
      next unless /./;
465
1166
729
      my $raw_line = $_;
466
467
1166
604
      my $ignore_this_line = $ignore_next_line;
468
1166
1006
      $ignore_next_line = ($_ =~ /$ignore_next_line_pattern/);
469
1166
763
      next if $ignore_this_line;
470
471      # hook for custom line based text exclusions:
472
1165
743
      if (defined $patterns_re) {
473
2
6
10
16
        s/($patterns_re)/"="x length($1)/ge;
474      }
475
1165
625
      my $initial_line_state = $_;
476
1165
715
      my $previous_line_state = $_;
477
1165
554
      my $line_flagged;
478
1165
810
      if ($forbidden_re) {
479
9
5
58
12
        while (s/($forbidden_re)/"="x length($1)/e) {
480
5
4
          $line_flagged = 1;
481
5
8
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
482
5
3
          my $found_trigger_re;
483
5
5
          for my $i (0 .. $#forbidden_re_list) {
484
7
5
            my $forbidden_re_singleton = $forbidden_re_list[$i];
485
7
4
            my $test_line = $previous_line_state;
486
7
4
57
7
            if ($test_line =~ s/($forbidden_re_singleton)/"="x length($1)/e) {
487
4
5
              next unless $test_line eq $_;
488
4
6
              my ($begin_test, $end_test, $match_test) = ($-[0] + 1, $+[0] + 1, $1);
489
4
4
              next unless $begin == $begin_test;
490
4
4
              next unless $end == $end_test;
491
4
2
              next unless $match eq $match_test;
492
4
3
              $found_trigger_re = $forbidden_re_singleton;
493
4
7
              my $hit = "$.:$begin:$end";
494
4
4
              $forbidden_re_hits[$i]++;
495
4
5
              $forbidden_re_lines[$i] = $hit unless $forbidden_re_lines[$i];
496
4
5
              last;
497            }
498          }
499
5
6
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
500
5
5
          if ($found_trigger_re) {
501
4
8
            my $description = $forbidden_re_descriptions{$found_trigger_re} || '';
502
4
9
            $found_trigger_re =~ s/^\(\?:(.*)\)$/$1/;
503
4
2
            my $quoted_trigger_re = CheckSpelling::Util::truncate_with_ellipsis(CheckSpelling::Util::wrap_in_backticks($found_trigger_re), 99);
504
4
4
            if ($description ne '') {
505
3
14
              print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns rule: $description - $quoted_trigger_re (forbidden-pattern)\n";
506            } else {
507
1
5
              print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry: $quoted_trigger_re (forbidden-pattern)\n";
508            }
509          } else {
510
1
3
            print WARNINGS ":$.:$begin ... $end, Warning - $wrapped matches a line_forbidden.patterns entry (forbidden-pattern)\n";
511          }
512
5
24
          $previous_line_state = $_;
513        }
514
9
8
        $_ = $initial_line_state;
515      }
516      # This is to make it easier to deal w/ rules:
517
1165
1053
      s/^/ /;
518
1165
722
      my %unrecognized_line_items = ();
519
1165
927
      my ($new_words, $new_unrecognized) = split_line($_, \%unique, \%unique_unrecognized, \%unrecognized_line_items);
520
1165
651
      $words += $new_words;
521
1165
476
      $unrecognized += $new_unrecognized;
522
1165
774
      my $line_length = length($raw_line);
523
1165
1467
      for my $token (sort CheckSpelling::Util::case_biased keys %unrecognized_line_items) {
524
1029
483
        my $found_token = 0;
525
1029
474
        my $raw_token = $token;
526
1029
538
        $token =~ s/'/(?:'|\x{2019}|\&apos;|\&#39;)+/g;
527
1029
463
        my $before;
528
1029
1576
        if ($token =~ /^$upper_pattern$lower_pattern/) {
529
5
2
          $before = '(?<=.)';
530        } elsif ($token =~ /^$upper_pattern/) {
531
0
0
          $before = "(?<!$upper_pattern)";
532        } else {
533
1024
578
          $before = "(?<=$not_lower_pattern)";
534        }
535
1029
1153
        my $after = ($token =~ /$upper_pattern$/) ? "(?=$not_upper_or_lower_pattern)|(?=$upper_pattern$lower_pattern)" : "(?=$not_lower_pattern)";
536
1029
2081
        while ($raw_line =~ /(?:\b|$before)($token)(?:\b|$after)/g) {
537
3520
1491
          $line_flagged = 1;
538
3520
1640
          $found_token = 1;
539
3520
5067
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
540
3520
3091
          next unless $match =~ /./;
541
3520
2091
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
542
3520
12014
          print WARNINGS ":$.:$begin ... $end: $wrapped\n";
543        }
544
1029
1161
        unless ($found_token) {
545
3
29
          if ($raw_line !~ /$token.*$token/ && $raw_line =~ /($token)/) {
546
3
6
            my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
547
3
2
            my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token);
548
3
11
            print WARNINGS ":$.:$begin ... $end: $wrapped\n";
549          } else {
550
0
0
            my $offset = $line_length + 1;
551
0
0
            my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token);
552
0
0
            print WARNINGS ":$.:1 ... $offset, Warning - Could not identify whole word $wrapped in line (token-is-substring)\n";
553          }
554        }
555      }
556
1165
1826
      if ($line_flagged && $candidates_re) {
557
1
1
        $_ = $previous_line_state = $initial_line_state;
558
1
1
17
2
        s/($candidates_re)/"="x length($1)/ge;
559
1
2
        if ($_ ne $initial_line_state) {
560
1
1
          $_ = $previous_line_state;
561
1
2
          for my $i (0 .. $#candidates_re_list) {
562
2
1
            my $candidate_re = $candidates_re_list[$i];
563
2
17
            next unless $candidate_re =~ /./ && $raw_line =~ /$candidate_re/;
564
1
1
6
2
            if (($_ =~ s/($candidate_re)/"="x length($1)/e)) {
565
1
2
              my ($begin, $end) = ($-[0] + 1, $+[0] + 1);
566
1
2
              my $hit = "$.:$begin:$end";
567
1
1
              $_ = $previous_line_state;
568
1
1
8
2
              my $replacements = ($_ =~ s/($candidate_re)/"="x length($1)/ge);
569
1
1
              $candidates_re_hits[$i] += $replacements;
570
1
2
              $candidates_re_lines[$i] = $hit unless $candidates_re_lines[$i];
571
1
2
              $_ = $previous_line_state;
572            }
573          }
574        }
575      }
576
1165
847
      unless ($disable_minified_file) {
577
1165
950
        s/={3,}//g;
578
1165
792
        $offset += length;
579
1165
1130
        my $ratio = int($offset / $.);
580
1165
602
        my $ratio_threshold = 1000;
581
1165
3092
        if ($ratio > $ratio_threshold) {
582
11
25
          skip_file($temp_dir, "average line width ($ratio) exceeds the threshold ($ratio_threshold) (minified-file)\n");
583        }
584      }
585    }
586
587
12
71
    alarm 0;
588  };
589
12
11
  if ($@) {
590
0
0
    die unless $@ eq "alarm\n";
591
0
0
    print WARNINGS ":$.:1 ... 1, Warning - Could not parse file within time limit (slow-file)\n";
592
0
0
    skip_file($temp_dir, "it could not be parsed file within time limit (slow-file)\n");
593  }
594
595
12
34
  close FILE;
596
12
122
  close WARNINGS;
597
598
12
43
  if ($unrecognized || @candidates_re_hits || @forbidden_re_hits) {
599
11
292
    open(STATS, '>:utf8', "$temp_dir/stats");
600
11
166
      print STATS "{words: $words, unrecognized: $unrecognized, unknown: ".(keys %unique_unrecognized).
601      ", unique: ".(keys %unique).
602      (@candidates_re_hits ? ", candidates: [".(join ',', @candidates_re_hits)."]" : "").
603      (@candidates_re_lines ? ", candidate_lines: [".(join ',', @candidates_re_lines)."]" : "").
604      (@forbidden_re_hits ? ", forbidden: [".(join ',', @forbidden_re_hits)."]" : "").
605      (@forbidden_re_lines ? ", forbidden_lines: [".(join ',', @forbidden_re_lines)."]" : "").
606      "}";
607
11
120
    close STATS;
608
11
215
    open(UNKNOWN, '>:utf8', "$temp_dir/unknown");
609
11
19
38
31
      print UNKNOWN map { "$_\n" } sort CheckSpelling::Util::case_biased keys %unique_unrecognized;
610
11
88
    close UNKNOWN;
611  }
612
613
12
124
  return $temp_dir;
614}
615
616sub main {
617
2
377
  my ($configuration, @ARGV) = @_;
618
2
1
  our %dictionary;
619
2
2
  unless (%dictionary) {
620
1
1
    init($configuration);
621  }
622
623  # read all input
624
2
3
  my @reports;
625
626
2
2
  for my $file (@ARGV) {
627
2
1
    my $temp_dir = split_file($file);
628
2
3
    push @reports, "$temp_dir\n";
629  }
630
2
6
  print join '', @reports;
631}
632
6331;