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
108320
3
use 5.022;
11
1
1
1
2
0
45
use feature 'unicode_strings';
12
1
1
1
2
1
9
use strict;
13
1
1
1
1
1
18
use warnings;
14
1
1
1
1
1
14
no warnings qw(experimental::vlb);
15
1
1
1
4
0
21
use Encode qw/decode_utf8 encode FB_DEFAULT/;
16
1
1
1
1
1
21
use File::Basename;
17
1
1
1
2
1
16
use Cwd 'abs_path';
18
1
1
1
1
1
13
use File::Temp qw/ tempfile tempdir /;
19
1
1
1
257
6
2959
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
5
105
  return eval { qr /$expression/ };
41}
42
43sub quote_re {
44
14
14
  my ($expression) = @_;
45
14
10
  return $expression if $expression =~ /\?\{/;
46
14
33
  $expression =~ s/
47   \G
48   (
49      (?:[^\\]|\\[^Q])*
50   )
51   (?:
52      \\Q
53      (?:[^\\]|\\[^E])*
54      (?:\\E)?
55   )?
56/
57
28
46
   $1 . (defined($2) ? quotemeta($2) : '')
58/xge;
59
14
15
  return $expression;
60}
61
62sub file_to_lists {
63
3
3
  my ($re) = @_;
64
3
4
  my @patterns;
65  my %hints;
66
3
0
  my $fh;
67
3
26
  if (open($fh, '<:utf8', $re)) {
68
3
6
    local $/=undef;
69
3
18
    my $file=<$fh>;
70
3
8
    close $fh;
71
3
4
    my $line_number = 0;
72
3
2
    my $hint = '';
73
3
13
    for (split /\R/, $file) {
74
17
7
      ++$line_number;
75
17
9
      chomp;
76
17
27
      if (/^#(?:\s(.+)|)/) {
77
6
14
        $hint = $1 if ($hint eq '' && defined $1);
78
6
5
        next;
79      }
80
11
12
      $hint = '' unless $_ ne '';
81
11
10
      my $pattern = $_;
82
11
28
      next unless s/^(.+)/(?:$1)/;
83
7
7
      my $quoted = quote_re($1);
84
7
8
      unless (test_re $quoted) {
85
1
2
        my $error = $@;
86
1
44
        my $home = dirname(__FILE__);
87
1
21
        $error =~ s/$home.*?\.pm line \d+\./$re line $line_number (bad-regex)/;
88
1
10
        print STDERR $error;
89
1
1
        $_ = '(?:\$^ - skipped because bad-regex)';
90
1
1
        $hint = '';
91      }
92
7
9
      if (defined $hints{$_}) {
93
1
2
        my $pattern_length = length $pattern;
94
1
1
        my $wrapped = CheckSpelling::Util::wrap_in_backticks($pattern);
95
1
13
        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
5
        push @patterns, $_;
99
6
10
        $hints{$_} = $hint;
100      }
101
7
11
      $hint = '';
102    }
103  }
104
105  return {
106
3
13
    patterns => \@patterns,
107    hints => \%hints,
108  };
109}
110
111sub file_to_list {
112
2
1147
  my ($re) = @_;
113
2
3
  my $lists = file_to_lists($re);
114
115
2
2
2
6
  return @{$lists->{'patterns'}};
116}
117
118sub list_to_re {
119
2
2
  my (@list) = @_;
120
2
5
5
1
4
2
  @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
6
  return join "|", (@list);
124}
125
126sub not_empty {
127
51
38
  my ($thing) = @_;
128
51
157
  return defined $thing && $thing ne ''
129}
130
131sub valid_word {
132  # shortest_word is an absolute
133
22
9
  our ($shortest, $longest, $shortest_word, $longest_word);
134
22
22
  $shortest = $shortest_word if $shortest_word;
135
22
21
  if ($longest_word) {
136    # longest_word is an absolute
137
20
17
    $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
10
  our ($upper_pattern, $lower_pattern, $punctuation_pattern);
144
22
66
20
159
  my $word_pattern = join '|', (grep { defined $_ && /./ } ($upper_pattern, $lower_pattern, $punctuation_pattern));
145
22
17
  $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
25
  $longest = '' unless defined $longest;
153
22
64
  $word_match = "(?:$word_pattern){$shortest,$longest}";
154
22
177
  return qr/\b$word_match\b/;
155}
156
157sub load_dictionary {
158
12
1998
  my ($dict) = @_;
159
12
6
  our ($word_match, $longest, $shortest, $longest_word, $shortest_word, %dictionary);
160
12
11
  $longest_word = CheckSpelling::Util::get_val_from_env('INPUT_LONGEST_WORD', undef);
161
12
30
  $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
11
  $ignore_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_IGNORE_PATTERN', q<[^a-zA-Z']>);
164
12
38
  $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]');
165
12
26
  $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]');
166
12
20
  $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
19
  $punctuation_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_PUNCTUATION_PATTERN', q<'>);
169
12
26
  %dictionary = ();
170
171
12
1020
  open(DICT, '<:utf8', $dict);
172
12
56
  while (!eof(DICT)) {
173
31
35
    my $word = <DICT>;
174
31
21
    chomp $word;
175
31
75
    next unless $word =~ $word_match;
176
28
24
    my $l = length $word;
177
28
22
    $longest = -1 unless not_empty($longest);
178
28
31
    $longest = $l if $l > $longest;
179
28
17
    $shortest = $l if $l < $shortest;
180
28
62
    $dictionary{$word}=1;
181  }
182
12
22
  close DICT;
183
184
12
10
  $word_match = valid_word();
185}
186
187sub hunspell_dictionary {
188
3
4
  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
3
  my $aff = $dict;
193
3
2
  my $encoding;
194
3
10
  $aff =~ s/\.dic$/.aff/;
195
3
26
  if (open AFF, '<', $aff) {
196
3
16
    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
277
    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
10775
  my ($configuration) = @_;
214
9
8
  our ($word_match, %unique, $patterns_re, @forbidden_re_list, $forbidden_re, @candidates_re_list, $candidates_re);
215
9
22
  our $sandbox = CheckSpelling::Util::get_file_from_env('sandbox', '');
216
9
11
  our $hunspell_dictionary_path = CheckSpelling::Util::get_file_from_env('hunspell_dictionary_path', '');
217
9
12
  our $timeout = CheckSpelling::Util::get_val_from_env('splitter_timeout', 30);
218
9
6
  our %forbidden_re_descriptions;
219
9
9
  if ($hunspell_dictionary_path) {
220
3
28
    our @hunspell_dictionaries = ();
221
1
1
1
1
1
1
1
1
1
3
167
934
17
4
1
13
4
2
15
141
    if (eval 'use Text::Hunspell; 1') {
222
3
108
      my @hunspell_dictionaries_list = glob("$hunspell_dictionary_path/*.dic");
223
3
7
      for my $hunspell_dictionary_file (@hunspell_dictionaries_list) {
224
3
4
        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
12
  my (@patterns_re_list, %in_patterns_re_list);
231
9
55
  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
6
    $patterns_re = undef;
237  }
238
239
9
35
  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
10
    $forbidden_re = undef;
246  }
247
248
9
58
  if (-e "$configuration/candidates.txt") {
249
1
1
    @candidates_re_list = file_to_list "$configuration/candidates.txt";
250
1
2
2
1
2
6
    @candidates_re_list = map { my $quoted = quote_re($_); $in_patterns_re_list{$_} || !test_re($quoted) ? '' : $quoted } @candidates_re_list;
251
1
2
    $candidates_re = list_to_re @candidates_re_list;
252  } else {
253
8
11
    $candidates_re = undef;
254  }
255
256
9
13
  our $largest_file = CheckSpelling::Util::get_val_from_env('INPUT_LARGEST_FILE', 1024*1024);
257
258
9
8
  my $disable_flags = CheckSpelling::Util::get_file_from_env('INPUT_DISABLE_CHECKS', '');
259
9
9
  our $disable_word_collating = $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/;
260
9
6
  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
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
7
  $word_match = valid_word();
268
269
9
19
  our $base_dict = CheckSpelling::Util::get_file_from_env('dict', "$configuration/words");
270
9
45
  $base_dict = '/usr/share/dict/words' unless -e $base_dict;
271
9
11
  load_dictionary($base_dict);
272}
273
274sub split_line {
275
1164
476
  our (%dictionary, $word_match, $disable_word_collating);
276
1164
441
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
277
1164
471
  our @hunspell_dictionaries;
278
1164
462
  our $shortest;
279
1164
636
  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
556
  my $rsqm = "\xE2\x80\x99";
284
285
1164
659
  my ($words, $unrecognized) = (0, 0);
286
1164
793
  my ($line, $unique_ref, $unique_unrecognized_ref, $unrecognized_line_items_ref) = @_;
287
1164
6100
    $line =~ s/(?:$rsqm|&apos;|&#39;|\%27|&#8217;|&#x2019;|&rsquo;|\\u2019|\x{2019}|')+/'/g;
288
1164
2512
    $line =~ s/(?:$ignore_pattern)+/ /g;
289
1164
1863
    while ($line =~ s/($upper_pattern{2,})($upper_pattern$lower_pattern{2,})/ $1 $2 /g) {}
290
1164
4427
    while ($line =~ s/((?:$lower_pattern|$punctuation_pattern)+)($upper_pattern)/$1 $2/g) {}
291
1164
1521
    for my $token (split /\s+/, $line) {
292
5857
5155
      next unless $token =~ /$pattern/;
293
4695
3783
      $token =~ s/^(?:'|$rsqm)+//g;
294
4695
4426
      $token =~ s/(?:'|$rsqm)+s?$//g;
295
4695
2619
      my $raw_token = $token;
296
4695
2649
      $token =~ s/^[^Ii]?'+(.*)/$1/;
297
4695
2262
      $token =~ s/(.*?)'+$/$1/;
298
4695
6574
      next unless $token =~ $word_match;
299
4554
3850
      if (defined $dictionary{$token}) {
300
1028
477
        ++$words;
301
1028
595
        $unique_ref->{$token}=1;
302
1028
849
        next;
303      }
304
3526
2398
      if (@hunspell_dictionaries) {
305
3504
1800
        my $found = 0;
306
3504
1850
        for my $hunspell_dictionary (@hunspell_dictionaries) {
307          my $token_encoded = defined $hunspell_dictionary->{'encoding'} ?
308
3504
2725
            encode($hunspell_dictionary->{'encoding'}, $token) : $token;
309
3504
6153
          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
2403
        next if $found;
317      }
318
3526
2335
      my $key = lc $token;
319
3526
2934
      if (defined $dictionary{$key}) {
320
3
3
        ++$words;
321
3
1
        $unique_ref->{$key}=1;
322
3
4
        next;
323      }
324
3523
2290
      unless ($disable_word_collating) {
325
3523
1931
        $key =~ s/''+/'/g;
326
3523
1780
        $key =~ s/'[sd]$//;
327      }
328
3523
2648
      if (defined $dictionary{$key}) {
329
0
0
        ++$words;
330
0
0
        $unique_ref->{$key}=1;
331
0
0
        next;
332      }
333
3523
1711
      ++$unrecognized;
334
3523
1920
      $unique_unrecognized_ref->{$raw_token}=1;
335
3523
4131
      $unrecognized_line_items_ref->{$raw_token}=1;
336    }
337
1164
1560
    return ($words, $unrecognized);
338}
339
340sub skip_file {
341
15
28
  my ($temp_dir, $reason) = @_;
342
15
437
  open(SKIPPED, '>:utf8', "$temp_dir/skipped");
343
15
96
  print SKIPPED $reason;
344
15
361
  close SKIPPED;
345}
346
347sub split_file {
348
15
10467
  my ($file) = @_;
349  our (
350
15
10
    $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
6
  our %forbidden_re_descriptions;
358
15
8
  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
7
  my $rsqm = "\xE2\x80\x99";
362
363
15
16
  my @candidates_re_hits = (0) x scalar @candidates_re_list;
364
15
11
  my @candidates_re_lines = (0) x scalar @candidates_re_list;
365
15
10
  my @forbidden_re_hits = (0) x scalar @forbidden_re_list;
366
15
6
  my @forbidden_re_lines = (0) x scalar @forbidden_re_list;
367
15
28
  my $temp_dir = tempdir(DIR=>$sandbox);
368
15
2205
  print STDERR "checking file: $file\n" if defined $ENV{'DEBUG'};
369
15
353
  open(NAME, '>', "$temp_dir/name");
370
15
32
    print NAME $file;
371
15
169
  close NAME;
372
15
50
  my $file_size = -s $file;
373
15
20
  if (defined $largest_file) {
374
15
14
    unless ($check_file_names eq $file) {
375
15
17
      if ($file_size > $largest_file) {
376
1
2
        skip_file($temp_dir, "size `$file_size` exceeds limit `$largest_file` (large-file)\n");
377
1
2
        return $temp_dir;
378      }
379    }
380  }
381
14
13
  if ($use_magic_file) {
382
8
11055
    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
28324
      my $file_kind = <$file_fh>;
394
8
4872
      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
43
        return $temp_dir;
398      }
399    }
400  }
401
12
138
  open FILE, '<', $file;
402
12
10
  binmode FILE;
403
12
8
  my $head;
404
12
85
  read(FILE, $head, 4096);
405
12
825
  $head =~ s/(?:\r|\n)+$//;
406
12
57
  my $dos_new_lines = () = $head =~ /\r\n/gi;
407
12
31
  my $unix_new_lines = () = $head =~ /\n/gi;
408
12
116
  my $mac_new_lines = () = $head =~ /\r/gi;
409
12
45
  local $/;
410
12
69
  if ($unix_new_lines == 0 && $mac_new_lines == 0) {
411
3
20
    $/ = "\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
19
  seek(FILE, 0, 0);
420
12
19
  ($words, $unrecognized) = (0, 0);
421
12
30
  %unique = ();
422
12
44
  %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
101
  };
430
431
12
295
  open(WARNINGS, '>:utf8', "$temp_dir/warnings");
432
12
9
  our $timeout;
433
12
10
  eval {
434
12
0
87
0
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
435
12
29
    alarm $timeout;
436
437
12
15
    my $offset = 0;
438
12
73
    while (<FILE>) {
439
1167
995
      if ($. == 1) {
440
12
16
        unless ($disable_minified_file) {
441
12
48
          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
2317
      $_ = decode_utf8($_, FB_DEFAULT);
448
1166
2594
      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
1398
      s/\R$//;
453
1166
1065
      s/^\x{FEFF}// if $. == 1;
454
1166
1063
      next unless /./;
455
1164
728
      my $raw_line = $_;
456
457      # hook for custom line based text exclusions:
458
1164
776
      if (defined $patterns_re) {
459
2
6
11
8
        s/($patterns_re)/"="x length($1)/ge;
460      }
461
1164
648
      my $previous_line_state = $_;
462
1164
547
      my $line_flagged;
463
1164
778
      if ($forbidden_re) {
464
9
5
62
11
        while (s/($forbidden_re)/"="x length($1)/e) {
465
5
4
          $line_flagged = 1;
466
5
8
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
467
5
4
          my $found_trigger_re;
468
5
4
          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
55
7
            if ($test_line =~ s/($forbidden_re_singleton)/"="x length($1)/e) {
472
4
4
              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
2
              next unless $end == $end_test;
476
4
4
              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
5
              $forbidden_re_lines[$i] = $hit unless $forbidden_re_lines[$i];
481
4
5
              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
8
            $found_trigger_re =~ s/^\(\?:(.*)\)$/$1/;
488
4
5
            my $quoted_trigger_re = CheckSpelling::Util::wrap_in_backticks($found_trigger_re);
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
1101
      s/^/ /;
502
1164
669
      my %unrecognized_line_items = ();
503
1164
933
      my ($new_words, $new_unrecognized) = split_line($_, \%unique, \%unique_unrecognized, \%unrecognized_line_items);
504
1164
684
      $words += $new_words;
505
1164
509
      $unrecognized += $new_unrecognized;
506
1164
837
      my $line_length = length($raw_line);
507
1164
1549
      for my $token (sort CheckSpelling::Util::case_biased keys %unrecognized_line_items) {
508
1029
483
        my $found_token = 0;
509
1029
529
        my $raw_token = $token;
510
1029
541
        $token =~ s/'/(?:'|\x{2019}|\&apos;|\&#39;)+/g;
511
1029
448
        my $before;
512
1029
1573
        if ($token =~ /^$upper_pattern$lower_pattern/) {
513
5
3
          $before = '(?<=.)';
514        } elsif ($token =~ /^$upper_pattern/) {
515
0
0
          $before = "(?<!$upper_pattern)";
516        } else {
517
1024
564
          $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
2071
        while ($raw_line =~ /(?:\b|$before)($token)(?:\b|$after)/g) {
521
3520
1665
          $line_flagged = 1;
522
3520
1539
          $found_token = 1;
523
3520
4946
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
524
3520
2957
          next unless $match =~ /./;
525
3520
2192
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
526
3520
12030
          print WARNINGS ":$.:$begin ... $end: $wrapped\n";
527        }
528
1029
1116
        unless ($found_token) {
529
3
28
          if ($raw_line !~ /$token.*$token/ && $raw_line =~ /($token)/) {
530
3
5
            my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
531
3
3
            my $wrapped = CheckSpelling::Util::wrap_in_backticks($raw_token);
532
3
10
            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
1809
      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
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
13
            next unless $candidate_re =~ /./ && $raw_line =~ /$candidate_re/;
548
1
1
6
4
            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
2
              $_ = $previous_line_state;
552
1
1
4
2
              my $replacements = ($_ =~ s/($candidate_re)/"="x length($1)/ge);
553
1
1
              $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
871
      unless ($disable_minified_file) {
561
1164
946
        s/={3,}//g;
562
1164
837
        $offset += length;
563
1164
1057
        my $ratio = int($offset / $.);
564
1164
651
        my $ratio_threshold = 1000;
565
1164
2831
        if ($ratio > $ratio_threshold) {
566
11
21
          skip_file($temp_dir, "average line width ($ratio) exceeds the threshold ($ratio_threshold) (minified-file)\n");
567        }
568      }
569    }
570
571
12
66
    alarm 0;
572  };
573
12
8
  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
36
  close FILE;
580
12
143
  close WARNINGS;
581
582
12
26
  if ($unrecognized || @candidates_re_hits || @forbidden_re_hits) {
583
11
305
    open(STATS, '>:utf8', "$temp_dir/stats");
584
11
140
      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
120
    close STATS;
592
11
220
    open(UNKNOWN, '>:utf8', "$temp_dir/unknown");
593
11
19
38
32
      print UNKNOWN map { "$_\n" } sort CheckSpelling::Util::case_biased keys %unique_unrecognized;
594
11
90
    close UNKNOWN;
595  }
596
597
12
113
  return $temp_dir;
598}
599
600sub main {
601
2
368
  my ($configuration, @ARGV) = @_;
602
2
2
  our %dictionary;
603
2
2
  unless (%dictionary) {
604
1
1
    init($configuration);
605  }
606
607  # read all input
608
2
1
  my @reports;
609
610
2
2
  for my $file (@ARGV) {
611
2
4
    my $temp_dir = split_file($file);
612
2
3
    push @reports, "$temp_dir\n";
613  }
614
2
6
  print join '', @reports;
615}
616
6171;