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
107265
2
use 5.022;
11
1
1
1
1
1
37
use feature 'unicode_strings';
12
1
1
1
2
1
9
use strict;
13
1
1
1
1
0
17
use warnings;
14
1
1
1
2
0
14
no warnings qw(experimental::vlb);
15
1
1
1
1
2
19
use Encode qw/decode_utf8 encode FB_DEFAULT/;
16
1
1
1
1
1
22
use File::Basename;
17
1
1
1
2
0
15
use Cwd 'abs_path';
18
1
1
1
2
0
12
use File::Temp qw/ tempfile tempdir /;
19
1
1
1
255
1
2933
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
11
  my ($expression) = @_;
40
14
14
6
102
  return eval { qr /$expression/ };
41}
42
43sub quote_re {
44
14
10
  my ($expression) = @_;
45
14
13
  return $expression if $expression =~ /\?\{/;
46
14
30
  $expression =~ s/
47   \G
48   (
49      (?:[^\\]|\\[^Q])*
50   )
51   (?:
52      \\Q
53      (?:[^\\]|\\[^E])*
54      (?:\\E)?
55   )?
56/
57
28
45
   $1 . (defined($2) ? quotemeta($2) : '')
58/xge;
59
14
15
  return $expression;
60}
61
62sub file_to_lists {
63
3
2
  my ($re) = @_;
64
3
4
  my @patterns;
65  my %hints;
66
3
0
  my $fh;
67
3
29
  if (open($fh, '<:utf8', $re)) {
68
3
5
    local $/=undef;
69
3
20
    my $file=<$fh>;
70
3
9
    close $fh;
71
3
1
    my $line_number = 0;
72
3
11
    my $hint = '';
73
3
13
    for (split /\R/, $file) {
74
17
8
      ++$line_number;
75
17
11
      chomp;
76
17
16
      if (/^#(?:\s(.+)|)/) {
77
6
13
        $hint = $1 if ($hint eq '' && defined $1);
78
6
5
        next;
79      }
80
11
11
      $hint = '' unless $_ ne '';
81
11
11
      my $pattern = $_;
82
11
28
      next unless s/^(.+)/(?:$1)/;
83
7
8
      my $quoted = quote_re($1);
84
7
5
      unless (test_re $quoted) {
85
1
1
        my $error = $@;
86
1
43
        my $home = dirname(__FILE__);
87
1
18
        $error =~ s/$home.*?\.pm line \d+\./$re line $line_number (bad-regex)/;
88
1
9
        print STDERR $error;
89
1
1
        $_ = '(?:\$^ - skipped because bad-regex)';
90
1
1
        $hint = '';
91      }
92
7
12
      if (defined $hints{$_}) {
93
1
1
        my $pattern_length = length $pattern;
94
1
1
        my $wrapped = CheckSpelling::Util::wrap_in_backticks($pattern);
95
1
16
        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
7
        $hints{$_} = $hint;
100      }
101
7
12
      $hint = '';
102    }
103  }
104
105  return {
106
3
11
    patterns => \@patterns,
107    hints => \%hints,
108  };
109}
110
111sub file_to_list {
112
2
1130
  my ($re) = @_;
113
2
4
  my $lists = file_to_lists($re);
114
115
2
2
2
5
  return @{$lists->{'patterns'}};
116}
117
118sub list_to_re {
119
2
2
  my (@list) = @_;
120
2
5
5
2
4
5
  @list = map { my $quoted = quote_re($_); test_re($quoted) ? $quoted : '' } @list;
121
2
5
2
5
  @list = grep { $_ ne '' } @list;
122
2
1
  return '$^' unless scalar @list;
123
2
8
  return join "|", (@list);
124}
125
126sub not_empty {
127
51
41
  my ($thing) = @_;
128
51
147
  return defined $thing && $thing ne ''
129}
130
131sub valid_word {
132  # shortest_word is an absolute
133
22
15
  our ($shortest, $longest, $shortest_word, $longest_word);
134
22
17
  $shortest = $shortest_word if $shortest_word;
135
22
17
  if ($longest_word) {
136    # longest_word is an absolute
137
20
22
    $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
14
  our ($upper_pattern, $lower_pattern, $punctuation_pattern);
144
22
66
13
138
  my $word_pattern = join '|', (grep { defined $_ && /./ } ($upper_pattern, $lower_pattern, $punctuation_pattern));
145
22
18
  $word_pattern = q<\\w|'> unless $word_pattern;
146
22
40
  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
22
  $shortest = 3 unless defined $shortest;
152
22
21
  $longest = '' unless defined $longest;
153
22
68
  $word_match = "(?:$word_pattern){$shortest,$longest}";
154
22
186
  return qr/\b$word_match\b/;
155}
156
157sub load_dictionary {
158
12
2000
  my ($dict) = @_;
159
12
10
  our ($word_match, $longest, $shortest, $longest_word, $shortest_word, %dictionary);
160
12
9
  $longest_word = CheckSpelling::Util::get_val_from_env('INPUT_LONGEST_WORD', undef);
161
12
8
  $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef);
162
12
4
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
163
12
15
  $ignore_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_IGNORE_PATTERN', q<[^a-zA-Z']>);
164
12
42
  $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]');
165
12
24
  $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]');
166
12
21
  $not_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_LOWER_PATTERN', '[^a-z]');
167
12
19
  $not_upper_or_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_UPPER_OR_LOWER_PATTERN', '[^A-Za-z]');
168
12
21
  $punctuation_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_PUNCTUATION_PATTERN', q<'>);
169
12
23
  %dictionary = ();
170
171
12
405
  open(DICT, '<:utf8', $dict);
172
12
46
  while (!eof(DICT)) {
173
31
34
    my $word = <DICT>;
174
31
26
    chomp $word;
175
31
72
    next unless $word =~ $word_match;
176
28
20
    my $l = length $word;
177
28
20
    $longest = -1 unless not_empty($longest);
178
28
28
    $longest = $l if $l > $longest;
179
28
18
    $shortest = $l if $l < $shortest;
180
28
55
    $dictionary{$word}=1;
181  }
182
12
23
  close DICT;
183
184
12
9
  $word_match = valid_word();
185}
186
187sub hunspell_dictionary {
188
3
2
  my ($dict) = @_;
189
3
3
  my $name = $dict;
190
3
4
  $name =~ s{/src/index/hunspell/index\.dic$}{};
191
3
12
  $name =~ s{.*/}{};
192
3
4
  my $aff = $dict;
193
3
0
  my $encoding;
194
3
9
  $aff =~ s/\.dic$/.aff/;
195
3
24
  if (open AFF, '<', $aff) {
196
3
17
    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
303
    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
10596
  my ($configuration) = @_;
214
9
9
  our ($word_match, %unique, $patterns_re, @forbidden_re_list, $forbidden_re, @candidates_re_list, $candidates_re);
215
9
23
  our $sandbox = CheckSpelling::Util::get_file_from_env('sandbox', '');
216
9
7
  our $hunspell_dictionary_path = CheckSpelling::Util::get_file_from_env('hunspell_dictionary_path', '');
217
9
35
  our $timeout = CheckSpelling::Util::get_val_from_env('splitter_timeout', 30);
218
9
9
  our %forbidden_re_descriptions;
219
9
12
  if ($hunspell_dictionary_path) {
220
3
30
    our @hunspell_dictionaries = ();
221
1
1
1
1
1
1
1
1
1
3
206
886
41
4
1
16
5
1
18
140
    if (eval 'use Text::Hunspell; 1') {
222
3
103
      my @hunspell_dictionaries_list = glob("$hunspell_dictionary_path/*.dic");
223
3
3
      for my $hunspell_dictionary_file (@hunspell_dictionaries_list) {
224
3
7
        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
10
  my (@patterns_re_list, %in_patterns_re_list);
231
9
52
  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
8
    $patterns_re = undef;
237  }
238
239
9
33
  if (-e "$configuration/forbidden.txt") {
240
1
1
    my $forbidden_re_info = file_to_lists "$configuration/forbidden.txt";
241
1
1
1
2
    @forbidden_re_list = @{$forbidden_re_info->{'patterns'}};
242
1
1
1
2
    %forbidden_re_descriptions = %{$forbidden_re_info->{'hints'}};
243
1
1
    $forbidden_re = list_to_re @forbidden_re_list;
244  } else {
245
8
7
    $forbidden_re = undef;
246  }
247
248
9
34
  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
12
    $candidates_re = undef;
254  }
255
256
9
11
  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
10
  our $disable_word_collating = $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/;
260
9
3
  our $disable_minified_file = $disable_flags =~ /(?:^|,|\s)minified-file(?:,|\s|$)/;
261
9
7
  our $disable_single_line_file = $disable_flags =~ /(?:^|,|\s)single-line-file(?:,|\s|$)/;
262
263
9
5
  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
7
  $word_match = valid_word();
268
269
9
16
  our $base_dict = CheckSpelling::Util::get_file_from_env('dict', "$configuration/words");
270
9
41
  $base_dict = '/usr/share/dict/words' unless -e $base_dict;
271
9
8
  load_dictionary($base_dict);
272}
273
274sub split_line {
275
1164
520
  our (%dictionary, $word_match, $disable_word_collating);
276
1164
443
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
277
1164
410
  our @hunspell_dictionaries;
278
1164
450
  our $shortest;
279
1164
527
  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
474
  my $rsqm = "\xE2\x80\x99";
284
285
1164
661
  my ($words, $unrecognized) = (0, 0);
286
1164
763
  my ($line, $unique_ref, $unique_unrecognized_ref, $unrecognized_line_items_ref) = @_;
287
1164
5954
    $line =~ s/(?:$rsqm|&apos;|&#39;|\%27|&#8217;|&#x2019;|&rsquo;|\\u2019|\x{2019}|')+/'/g;
288
1164
2531
    $line =~ s/(?:$ignore_pattern)+/ /g;
289
1164
1722
    while ($line =~ s/($upper_pattern{2,})($upper_pattern$lower_pattern{2,})/ $1 $2 /g) {}
290
1164
4367
    while ($line =~ s/((?:$lower_pattern|$punctuation_pattern)+)($upper_pattern)/$1 $2/g) {}
291
1164
1559
    for my $token (split /\s+/, $line) {
292
5857
5284
      next unless $token =~ /$pattern/;
293
4695
3895
      $token =~ s/^(?:'|$rsqm)+//g;
294
4695
4467
      $token =~ s/(?:'|$rsqm)+s?$//g;
295
4695
2671
      my $raw_token = $token;
296
4695
2858
      $token =~ s/^[^Ii]?'+(.*)/$1/;
297
4695
2304
      $token =~ s/(.*?)'+$/$1/;
298
4695
6612
      next unless $token =~ $word_match;
299
4554
3871
      if (defined $dictionary{$token}) {
300
1028
501
        ++$words;
301
1028
549
        $unique_ref->{$token}=1;
302
1028
814
        next;
303      }
304
3526
2385
      if (@hunspell_dictionaries) {
305
3504
1648
        my $found = 0;
306
3504
1860
        for my $hunspell_dictionary (@hunspell_dictionaries) {
307          my $token_encoded = defined $hunspell_dictionary->{'encoding'} ?
308
3504
2595
            encode($hunspell_dictionary->{'encoding'}, $token) : $token;
309
3504
6133
          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
2410
        next if $found;
317      }
318
3526
2335
      my $key = lc $token;
319
3526
2870
      if (defined $dictionary{$key}) {
320
3
1
        ++$words;
321
3
2
        $unique_ref->{$key}=1;
322
3
4
        next;
323      }
324
3523
2385
      unless ($disable_word_collating) {
325
3523
1835
        $key =~ s/''+/'/g;
326
3523
1753
        $key =~ s/'[sd]$//;
327      }
328
3523
2639
      if (defined $dictionary{$key}) {
329
0
0
        ++$words;
330
0
0
        $unique_ref->{$key}=1;
331
0
0
        next;
332      }
333
3523
1675
      ++$unrecognized;
334
3523
2044
      $unique_unrecognized_ref->{$raw_token}=1;
335
3523
4154
      $unrecognized_line_items_ref->{$raw_token}=1;
336    }
337
1164
1668
    return ($words, $unrecognized);
338}
339
340sub skip_file {
341
15
30
  my ($temp_dir, $reason) = @_;
342
15
458
  open(SKIPPED, '>:utf8', "$temp_dir/skipped");
343
15
89
  print SKIPPED $reason;
344
15
363
  close SKIPPED;
345}
346
347sub split_file {
348
15
10188
  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
8
  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
15
  my @candidates_re_hits = (0) x scalar @candidates_re_list;
364
15
6
  my @candidates_re_lines = (0) x scalar @candidates_re_list;
365
15
12
  my @forbidden_re_hits = (0) x scalar @forbidden_re_list;
366
15
7
  my @forbidden_re_lines = (0) x scalar @forbidden_re_list;
367
15
25
  my $temp_dir = tempdir(DIR=>$sandbox);
368
15
2237
  print STDERR "checking file: $file\n" if defined $ENV{'DEBUG'};
369
15
339
  open(NAME, '>', "$temp_dir/name");
370
15
32
    print NAME $file;
371
15
177
  close NAME;
372
15
49
  my $file_size = -s $file;
373
15
16
  if (defined $largest_file) {
374
15
13
    unless ($check_file_names eq $file) {
375
15
13
      if ($file_size > $largest_file) {
376
1
2
        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
12
  if ($use_magic_file) {
382
8
10788
    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
27836
      my $file_kind = <$file_fh>;
394
8
4765
      close $file_fh;
395
8
104
      if ($file_kind =~ /^(.*?); charset=binary/) {
396
2
24
        skip_file($temp_dir, "it appears to be a binary file (`$1`) (binary-file)\n");
397
2
40
        return $temp_dir;
398      }
399    }
400  }
401
12
106
  open FILE, '<', $file;
402
12
9
  binmode FILE;
403
12
8
  my $head;
404
12
87
  read(FILE, $head, 4096);
405
12
808
  $head =~ s/(?:\r|\n)+$//;
406
12
48
  my $dos_new_lines = () = $head =~ /\r\n/gi;
407
12
31
  my $unix_new_lines = () = $head =~ /\n/gi;
408
12
99
  my $mac_new_lines = () = $head =~ /\r/gi;
409
12
46
  local $/;
410
12
67
  if ($unix_new_lines == 0 && $mac_new_lines == 0) {
411
3
12
    $/ = "\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
5
    $/ = "\r";
416  } else {
417
6
6
    $/ = "\n";
418  }
419
12
19
  seek(FILE, 0, 0);
420
12
22
  ($words, $unrecognized) = (0, 0);
421
12
30
  %unique = ();
422
12
18
  %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
96
  };
430
431
12
298
  open(WARNINGS, '>:utf8', "$temp_dir/warnings");
432
12
6
  our $timeout;
433
12
8
  eval {
434
12
0
90
0
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
435
12
28
    alarm $timeout;
436
437
12
12
    my $offset = 0;
438
12
79
    while (<FILE>) {
439
1167
1022
      if ($. == 1) {
440
12
12
        unless ($disable_minified_file) {
441
12
40
          if ($file_size >= 512 && length($_) == $file_size) {
442
1
8
            skip_file($temp_dir, "file only has a single line (single-line-file)\n");
443
1
4
            last;
444          }
445        }
446      }
447
1166
2269
      $_ = decode_utf8($_, FB_DEFAULT);
448
1166
2674
      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
1444
      s/\R$//;
453
1166
1043
      s/^\x{FEFF}// if $. == 1;
454
1166
1125
      next unless /./;
455
1164
730
      my $raw_line = $_;
456
457      # hook for custom line based text exclusions:
458
1164
761
      if (defined $patterns_re) {
459
2
6
12
9
        s/($patterns_re)/"="x length($1)/ge;
460      }
461
1164
697
      my $previous_line_state = $_;
462
1164
542
      my $line_flagged;
463
1164
864
      if ($forbidden_re) {
464
9
5
76
10
        while (s/($forbidden_re)/"="x length($1)/e) {
465
5
5
          $line_flagged = 1;
466
5
9
          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
6
            my $forbidden_re_singleton = $forbidden_re_list[$i];
470
7
4
            my $test_line = $previous_line_state;
471
7
4
57
7
            if ($test_line =~ s/($forbidden_re_singleton)/"="x length($1)/e) {
472
4
5
              next unless $test_line eq $_;
473
4
6
              my ($begin_test, $end_test, $match_test) = ($-[0] + 1, $+[0] + 1, $1);
474
4
4
              next unless $begin == $begin_test;
475
4
3
              next unless $end == $end_test;
476
4
2
              next unless $match eq $match_test;
477
4
4
              $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
5
          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
3
            my $quoted_trigger_re = CheckSpelling::Util::truncate_with_ellipsis(CheckSpelling::Util::wrap_in_backticks($found_trigger_re), 99);
489
4
4
            if ($description ne '') {
490
3
14
              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
26
          $previous_line_state = $_;
498        }
499      }
500      # This is to make it easier to deal w/ rules:
501
1164
1082
      s/^/ /;
502
1164
690
      my %unrecognized_line_items = ();
503
1164
983
      my ($new_words, $new_unrecognized) = split_line($_, \%unique, \%unique_unrecognized, \%unrecognized_line_items);
504
1164
659
      $words += $new_words;
505
1164
538
      $unrecognized += $new_unrecognized;
506
1164
880
      my $line_length = length($raw_line);
507
1164
1587
      for my $token (sort CheckSpelling::Util::case_biased keys %unrecognized_line_items) {
508
1029
491
        my $found_token = 0;
509
1029
476
        my $raw_token = $token;
510
1029
570
        $token =~ s/'/(?:'|\x{2019}|\&apos;|\&#39;)+/g;
511
1029
397
        my $before;
512
1029
1545
        if ($token =~ /^$upper_pattern$lower_pattern/) {
513
5
6
          $before = '(?<=.)';
514        } elsif ($token =~ /^$upper_pattern/) {
515
0
0
          $before = "(?<!$upper_pattern)";
516        } else {
517
1024
612
          $before = "(?<=$not_lower_pattern)";
518        }
519
1029
1192
        my $after = ($token =~ /$upper_pattern$/) ? "(?=$not_upper_or_lower_pattern)|(?=$upper_pattern$lower_pattern)" : "(?=$not_lower_pattern)";
520
1029
2055
        while ($raw_line =~ /(?:\b|$before)($token)(?:\b|$after)/g) {
521
3520
1573
          $line_flagged = 1;
522
3520
1376
          $found_token = 1;
523
3520
4973
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
524
3520
2894
          next unless $match =~ /./;
525
3520
2211
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
526
3520
12264
          print WARNINGS ":$.:$begin ... $end: $wrapped\n";
527        }
528
1029
1093
        unless ($found_token) {
529
3
26
          if ($raw_line !~ /$token.*$token/ && $raw_line =~ /($token)/) {
530
3
6
            my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
531
3
3
            my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token);
532
3
12
            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
1862
      if ($line_flagged && $candidates_re) {
541
1
2
        $_ = $previous_line_state;
542
1
1
16
2
        s/($candidates_re)/"="x length($1)/ge;
543
1
2
        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
6
2
            if (($_ =~ s/($candidate_re)/"="x length($1)/e)) {
549
1
2
              my ($begin, $end) = ($-[0] + 1, $+[0] + 1);
550
1
2
              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
1
              $candidates_re_lines[$i] = $hit unless $candidates_re_lines[$i];
555
1
3
              $_ = $previous_line_state;
556            }
557          }
558        }
559      }
560
1164
828
      unless ($disable_minified_file) {
561
1164
973
        s/={3,}//g;
562
1164
783
        $offset += length;
563
1164
1060
        my $ratio = int($offset / $.);
564
1164
608
        my $ratio_threshold = 1000;
565
1164
2797
        if ($ratio > $ratio_threshold) {
566
11
22
          skip_file($temp_dir, "average line width ($ratio) exceeds the threshold ($ratio_threshold) (minified-file)\n");
567        }
568      }
569    }
570
571
12
62
    alarm 0;
572  };
573
12
11
  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
32
  close FILE;
580
12
125
  close WARNINGS;
581
582
12
22
  if ($unrecognized || @candidates_re_hits || @forbidden_re_hits) {
583
11
274
    open(STATS, '>:utf8', "$temp_dir/stats");
584
11
145
      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
113
    close STATS;
592
11
216
    open(UNKNOWN, '>:utf8', "$temp_dir/unknown");
593
11
19
38
27
      print UNKNOWN map { "$_\n" } sort CheckSpelling::Util::case_biased keys %unique_unrecognized;
594
11
88
    close UNKNOWN;
595  }
596
597
12
97
  return $temp_dir;
598}
599
600sub main {
601
2
374
  my ($configuration, @ARGV) = @_;
602
2
1
  our %dictionary;
603
2
3
  unless (%dictionary) {
604
1
3
    init($configuration);
605  }
606
607  # read all input
608
2
2
  my @reports;
609
610
2
2
  for my $file (@ARGV) {
611
2
2
    my $temp_dir = split_file($file);
612
2
4
    push @reports, "$temp_dir\n";
613  }
614
2
5
  print join '', @reports;
615}
616
6171;