File Coverage

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

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