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
111187
3
use 5.022;
11
1
1
1
2
1
50
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
0
16
no warnings qw(experimental::vlb);
15
1
1
1
1
3
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
16
use Cwd 'abs_path';
18
1
1
1
2
0
14
use File::Temp qw/ tempfile tempdir /;
19
1
1
1
261
1
2988
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
9
  my ($expression) = @_;
40
14
14
8
103
  return eval { qr /$expression/ };
41}
42
43sub quote_re {
44
14
9
  my ($expression) = @_;
45
14
13
  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
16
  return $expression;
60}
61
62sub file_to_lists {
63
3
3
  my ($re) = @_;
64
3
3
  my @patterns;
65  my %hints;
66
3
0
  my $fh;
67
3
28
  if (open($fh, '<:utf8', $re)) {
68
3
6
    local $/=undef;
69
3
18
    my $file=<$fh>;
70
3
7
    close $fh;
71
3
3
    my $line_number = 0;
72
3
3
    my $hint = '';
73
3
15
    for (split /\R/, $file) {
74
17
6
      ++$line_number;
75
17
19
      chomp;
76
17
20
      if (/^#(?:\s(.+)|)/) {
77
6
14
        $hint = $1 if ($hint eq '' && defined $1);
78
6
5
        next;
79      }
80
11
13
      $hint = '' unless $_ ne '';
81
11
7
      my $pattern = $_;
82
11
30
      next unless s/^(.+)/(?:$1)/;
83
7
11
      my $quoted = quote_re($1);
84
7
7
      unless (test_re $quoted) {
85
1
3
        my $error = $@;
86
1
45
        my $home = dirname(__FILE__);
87
1
25
        $error =~ s/$home.*?\.pm line \d+\./$re line $line_number (bad-regex)/;
88
1
10
        print STDERR $error;
89
1
3
        $_ = '(?:\$^ - 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
2
        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
7
        push @patterns, $_;
99
6
11
        $hints{$_} = $hint;
100      }
101
7
10
      $hint = '';
102    }
103  }
104
105  return {
106
3
14
    patterns => \@patterns,
107    hints => \%hints,
108  };
109}
110
111sub file_to_list {
112
2
1243
  my ($re) = @_;
113
2
5
  my $lists = file_to_lists($re);
114
115
2
2
2
6
  return @{$lists->{'patterns'}};
116}
117
118sub list_to_re {
119
2
1
  my (@list) = @_;
120
2
5
5
2
3
5
  @list = map { my $quoted = quote_re($_); test_re($quoted) ? $quoted : '' } @list;
121
2
5
2
6
  @list = grep { $_ ne '' } @list;
122
2
2
  return '$^' unless scalar @list;
123
2
6
  return join "|", (@list);
124}
125
126sub not_empty {
127
51
42
  my ($thing) = @_;
128
51
170
  return defined $thing && $thing ne ''
129}
130
131sub valid_word {
132  # shortest_word is an absolute
133
22
13
  our ($shortest, $longest, $shortest_word, $longest_word);
134
22
22
  $shortest = $shortest_word if $shortest_word;
135
22
32
  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
18
  our ($upper_pattern, $lower_pattern, $punctuation_pattern);
144
22
66
18
153
  my $word_pattern = join '|', (grep { defined $_ && /./ } ($upper_pattern, $lower_pattern, $punctuation_pattern));
145
22
22
  $word_pattern = q<\\w|'> unless $word_pattern;
146
22
38
  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
23
  $longest = '' unless defined $longest;
153
22
77
  $word_match = "(?:$word_pattern){$shortest,$longest}";
154
22
182
  return qr/\b$word_match\b/;
155}
156
157sub load_dictionary {
158
12
2019
  my ($dict) = @_;
159
12
2
  our ($word_match, $longest, $shortest, $longest_word, $shortest_word, %dictionary);
160
12
13
  $longest_word = CheckSpelling::Util::get_val_from_env('INPUT_LONGEST_WORD', undef);
161
12
11
  $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef);
162
12
8
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
163
12
17
  $ignore_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_IGNORE_PATTERN', q<[^a-zA-Z']>);
164
12
40
  $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]');
165
12
30
  $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]');
166
12
23
  $not_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_LOWER_PATTERN', '[^a-z]');
167
12
23
  $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
27
  %dictionary = ();
170
171
12
425
  open(DICT, '<:utf8', $dict);
172
12
55
  while (!eof(DICT)) {
173
31
34
    my $word = <DICT>;
174
31
21
    chomp $word;
175
31
76
    next unless $word =~ $word_match;
176
28
22
    my $l = length $word;
177
28
21
    $longest = -1 unless not_empty($longest);
178
28
27
    $longest = $l if $l > $longest;
179
28
21
    $shortest = $l if $l < $shortest;
180
28
62
    $dictionary{$word}=1;
181  }
182
12
24
  close DICT;
183
184
12
10
  $word_match = valid_word();
185}
186
187sub hunspell_dictionary {
188
3
5
  my ($dict) = @_;
189
3
3
  my $name = $dict;
190
3
6
  $name =~ s{/src/index/hunspell/index\.dic$}{};
191
3
10
  $name =~ s{.*/}{};
192
3
27
  my $aff = $dict;
193
3
4
  my $encoding;
194
3
13
  $aff =~ s/\.dic$/.aff/;
195
3
35
  if (open AFF, '<', $aff) {
196
3
20
    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
9
    close AFF;
202  }
203  return {
204
3
297
    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
11428
  my ($configuration) = @_;
214
9
12
  our ($word_match, %unique, $patterns_re, @forbidden_re_list, $forbidden_re, @candidates_re_list, $candidates_re);
215
9
20
  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
15
  our $timeout = CheckSpelling::Util::get_val_from_env('splitter_timeout', 30);
218
9
8
  our %forbidden_re_descriptions;
219
9
9
  if ($hunspell_dictionary_path) {
220
3
36
    our @hunspell_dictionaries = ();
221
1
1
1
1
1
1
1
1
1
3
204
949
19
5
1
13
7
2
16
156
    if (eval 'use Text::Hunspell; 1') {
222
3
115
      my @hunspell_dictionaries_list = glob("$hunspell_dictionary_path/*.dic");
223
3
5
      for my $hunspell_dictionary_file (@hunspell_dictionaries_list) {
224
3
8
        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
16
  my (@patterns_re_list, %in_patterns_re_list);
231
9
57
  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
11
    $patterns_re = undef;
237  }
238
239
9
37
  if (-e "$configuration/forbidden.txt") {
240
1
2
    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
7
    $forbidden_re = undef;
246  }
247
248
9
39
  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
2
    $candidates_re = list_to_re @candidates_re_list;
252  } else {
253
8
7
    $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
7
  my $disable_flags = CheckSpelling::Util::get_file_from_env('INPUT_DISABLE_CHECKS', '');
259
9
6
  our $disable_word_collating = $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/;
260
9
9
  our $disable_minified_file = $disable_flags =~ /(?:^|,|\s)minified-file(?:,|\s|$)/;
261
9
5
  our $disable_single_line_file = $disable_flags =~ /(?:^|,|\s)single-line-file(?:,|\s|$)/;
262
263
9
10
  our $check_file_names = CheckSpelling::Util::get_file_from_env('check_file_names', '');
264
265
9
5
  our $use_magic_file = CheckSpelling::Util::get_val_from_env('INPUT_USE_MAGIC_FILE', '');
266
267
9
8
  $word_match = valid_word();
268
269
9
20
  our $base_dict = CheckSpelling::Util::get_file_from_env('dict', "$configuration/words");
270
9
43
  $base_dict = '/usr/share/dict/words' unless -e $base_dict;
271
9
8
  load_dictionary($base_dict);
272}
273
274sub split_line {
275
1164
567
  our (%dictionary, $word_match, $disable_word_collating);
276
1164
519
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
277
1164
442
  our @hunspell_dictionaries;
278
1164
403
  our $shortest;
279
1164
623
  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
559
  my $rsqm = "\xE2\x80\x99";
284
285
1164
632
  my ($words, $unrecognized) = (0, 0);
286
1164
755
  my ($line, $unique_ref, $unique_unrecognized_ref, $unrecognized_line_items_ref) = @_;
287
1164
6136
    $line =~ s/(?:$rsqm|&apos;|&#39;|\%27|&#8217;|&#x2019;|&rsquo;|\\u2019|\x{2019}|')+/'/g;
288
1164
2726
    $line =~ s/(?:$ignore_pattern)+/ /g;
289
1164
1758
    while ($line =~ s/($upper_pattern{2,})($upper_pattern$lower_pattern{2,})/ $1 $2 /g) {}
290
1164
4448
    while ($line =~ s/((?:$lower_pattern|$punctuation_pattern)+)($upper_pattern)/$1 $2/g) {}
291
1164
1523
    for my $token (split /\s+/, $line) {
292
5857
5176
      next unless $token =~ /$pattern/;
293
4695
3701
      $token =~ s/^(?:'|$rsqm)+//g;
294
4695
4443
      $token =~ s/(?:'|$rsqm)+s?$//g;
295
4695
2578
      my $raw_token = $token;
296
4695
2597
      $token =~ s/^[^Ii]?'+(.*)/$1/;
297
4695
2265
      $token =~ s/(.*?)'+$/$1/;
298
4695
6915
      next unless $token =~ $word_match;
299
4554
3879
      if (defined $dictionary{$token}) {
300
1028
450
        ++$words;
301
1028
569
        $unique_ref->{$token}=1;
302
1028
808
        next;
303      }
304
3526
2302
      if (@hunspell_dictionaries) {
305
3504
1621
        my $found = 0;
306
3504
1804
        for my $hunspell_dictionary (@hunspell_dictionaries) {
307          my $token_encoded = defined $hunspell_dictionary->{'encoding'} ?
308
3504
2698
            encode($hunspell_dictionary->{'encoding'}, $token) : $token;
309
3504
6097
          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
2353
        next if $found;
317      }
318
3526
2362
      my $key = lc $token;
319
3526
2922
      if (defined $dictionary{$key}) {
320
3
3
        ++$words;
321
3
1
        $unique_ref->{$key}=1;
322
3
3
        next;
323      }
324
3523
2222
      unless ($disable_word_collating) {
325
3523
1829
        $key =~ s/''+/'/g;
326
3523
1894
        $key =~ s/'[sd]$//;
327      }
328
3523
2558
      if (defined $dictionary{$key}) {
329
0
0
        ++$words;
330
0
0
        $unique_ref->{$key}=1;
331
0
0
        next;
332      }
333
3523
1633
      ++$unrecognized;
334
3523
2083
      $unique_unrecognized_ref->{$raw_token}=1;
335
3523
4191
      $unrecognized_line_items_ref->{$raw_token}=1;
336    }
337
1164
1686
    return ($words, $unrecognized);
338}
339
340sub skip_file {
341
15
36
  my ($temp_dir, $reason) = @_;
342
15
526
  open(SKIPPED, '>:utf8', "$temp_dir/skipped");
343
15
108
  print SKIPPED $reason;
344
15
504
  close SKIPPED;
345}
346
347sub split_file {
348
15
10947
  my ($file) = @_;
349  our (
350
15
8
    $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
10
  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
17
  my @candidates_re_hits = (0) x scalar @candidates_re_list;
364
15
13
  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
14
  my @forbidden_re_lines = (0) x scalar @forbidden_re_list;
367
15
30
  my $temp_dir = tempdir(DIR=>$sandbox);
368
15
2426
  print STDERR "checking file: $file\n" if defined $ENV{'DEBUG'};
369
15
386
  open(NAME, '>', "$temp_dir/name");
370
15
36
    print NAME $file;
371
15
169
  close NAME;
372
15
50
  my $file_size = -s $file;
373
15
15
  if (defined $largest_file) {
374
15
19
    unless ($check_file_names eq $file) {
375
15
18
      if ($file_size > $largest_file) {
376
1
1
        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
14
  if ($use_magic_file) {
382
8
12197
    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
29457
      my $file_kind = <$file_fh>;
394
8
5164
      close $file_fh;
395
8
124
      if ($file_kind =~ /^(.*?); charset=binary/) {
396
2
25
        skip_file($temp_dir, "it appears to be a binary file (`$1`) (binary-file)\n");
397
2
46
        return $temp_dir;
398      }
399    }
400  }
401
12
119
  open FILE, '<', $file;
402
12
11
  binmode FILE;
403
12
9
  my $head;
404
12
89
  read(FILE, $head, 4096);
405
12
782
  $head =~ s/(?:\r|\n)+$//;
406
12
51
  my $dos_new_lines = () = $head =~ /\r\n/gi;
407
12
67
  my $unix_new_lines = () = $head =~ /\n/gi;
408
12
113
  my $mac_new_lines = () = $head =~ /\r/gi;
409
12
53
  local $/;
410
12
102
  if ($unix_new_lines == 0 && $mac_new_lines == 0) {
411
3
24
    $/ = "\n";
412  } elsif ($dos_new_lines >= $unix_new_lines && $dos_new_lines >= $mac_new_lines) {
413
1
6
    $/ = "\r\n";
414  } elsif ($mac_new_lines > $unix_new_lines) {
415
2
8
    $/ = "\r";
416  } else {
417
6
7
    $/ = "\n";
418  }
419
12
24
  seek(FILE, 0, 0);
420
12
21
  ($words, $unrecognized) = (0, 0);
421
12
37
  %unique = ();
422
12
30
  %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
121
  };
430
431
12
334
  open(WARNINGS, '>:utf8', "$temp_dir/warnings");
432
12
9
  our $timeout;
433
12
10
  eval {
434
12
0
88
0
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
435
12
35
    alarm $timeout;
436
437
12
9
    my $offset = 0;
438
12
78
    while (<FILE>) {
439
1167
1090
      if ($. == 1) {
440
12
21
        unless ($disable_minified_file) {
441
12
59
          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
2346
      $_ = decode_utf8($_, FB_DEFAULT);
448
1166
2708
      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
1406
      s/\R$//;
453
1166
1052
      s/^\x{FEFF}// if $. == 1;
454
1166
1013
      next unless /./;
455
1164
709
      my $raw_line = $_;
456
457      # hook for custom line based text exclusions:
458
1164
849
      if (defined $patterns_re) {
459
2
6
10
8
        s/($patterns_re)/"="x length($1)/ge;
460      }
461
1164
697
      my $previous_line_state = $_;
462
1164
574
      my $line_flagged;
463
1164
816
      if ($forbidden_re) {
464
9
5
61
10
        while (s/($forbidden_re)/"="x length($1)/e) {
465
5
3
          $line_flagged = 1;
466
5
11
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
467
5
3
          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
2
            my $test_line = $previous_line_state;
471
7
4
58
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
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
6
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
485
5
6
          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
4
            my $quoted_trigger_re = CheckSpelling::Util::wrap_in_backticks($found_trigger_re);
489
4
4
            if ($description ne '') {
490
3
12
              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
4
            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
1058
      s/^/ /;
502
1164
694
      my %unrecognized_line_items = ();
503
1164
977
      my ($new_words, $new_unrecognized) = split_line($_, \%unique, \%unique_unrecognized, \%unrecognized_line_items);
504
1164
654
      $words += $new_words;
505
1164
569
      $unrecognized += $new_unrecognized;
506
1164
819
      my $line_length = length($raw_line);
507
1164
1505
      for my $token (sort CheckSpelling::Util::case_biased keys %unrecognized_line_items) {
508
1029
497
        my $found_token = 0;
509
1029
453
        my $raw_token = $token;
510
1029
582
        $token =~ s/'/(?:'|\x{2019}|\&apos;|\&#39;)+/g;
511
1029
428
        my $before;
512
1029
1577
        if ($token =~ /^$upper_pattern$lower_pattern/) {
513
5
5
          $before = '(?<=.)';
514        } elsif ($token =~ /^$upper_pattern/) {
515
0
0
          $before = "(?<!$upper_pattern)";
516        } else {
517
1024
596
          $before = "(?<=$not_lower_pattern)";
518        }
519
1029
1118
        my $after = ($token =~ /$upper_pattern$/) ? "(?=$not_upper_or_lower_pattern)|(?=$upper_pattern$lower_pattern)" : "(?=$not_lower_pattern)";
520
1029
2183
        while ($raw_line =~ /(?:\b|$before)($token)(?:\b|$after)/g) {
521
3520
1519
          $line_flagged = 1;
522
3520
1422
          $found_token = 1;
523
3520
4867
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
524
3520
3076
          next unless $match =~ /./;
525
3520
2185
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
526
3520
12205
          print WARNINGS ":$.:$begin ... $end: $wrapped\n";
527        }
528
1029
1154
        unless ($found_token) {
529
3
27
          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
11
            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
1837
      if ($line_flagged && $candidates_re) {
541
1
1
        $_ = $previous_line_state;
542
1
1
18
3
        s/($candidates_re)/"="x length($1)/ge;
543
1
2
        if ($_ ne $previous_line_state) {
544
1
1
          $_ = $previous_line_state;
545
1
2
          for my $i (0 .. $#candidates_re_list) {
546
2
3
            my $candidate_re = $candidates_re_list[$i];
547
2
13
            next unless $candidate_re =~ /./ && $raw_line =~ /$candidate_re/;
548
1
1
6
5
            if (($_ =~ s/($candidate_re)/"="x length($1)/e)) {
549
1
2
              my ($begin, $end) = ($-[0] + 1, $+[0] + 1);
550
1
3
              my $hit = "$.:$begin:$end";
551
1
1
              $_ = $previous_line_state;
552
1
1
5
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
2
              $_ = $previous_line_state;
556            }
557          }
558        }
559      }
560
1164
843
      unless ($disable_minified_file) {
561
1164
888
        s/={3,}//g;
562
1164
796
        $offset += length;
563
1164
1025
        my $ratio = int($offset / $.);
564
1164
643
        my $ratio_threshold = 1000;
565
1164
2952
        if ($ratio > $ratio_threshold) {
566
11
31
          skip_file($temp_dir, "average line width ($ratio) exceeds the threshold ($ratio_threshold) (minified-file)\n");
567        }
568      }
569    }
570
571
12
79
    alarm 0;
572  };
573
12
15
  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
40
  close FILE;
580
12
134
  close WARNINGS;
581
582
12
29
  if ($unrecognized || @candidates_re_hits || @forbidden_re_hits) {
583
11
323
    open(STATS, '>:utf8', "$temp_dir/stats");
584
11
188
      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
124
    close STATS;
592
11
224
    open(UNKNOWN, '>:utf8', "$temp_dir/unknown");
593
11
19
42
34
      print UNKNOWN map { "$_\n" } sort CheckSpelling::Util::case_biased keys %unique_unrecognized;
594
11
92
    close UNKNOWN;
595  }
596
597
12
113
  return $temp_dir;
598}
599
600sub main {
601
2
397
  my ($configuration, @ARGV) = @_;
602
2
0
  our %dictionary;
603
2
3
  unless (%dictionary) {
604
1
1
    init($configuration);
605  }
606
607  # read all input
608
2
2
  my @reports;
609
610
2
2
  for my $file (@ARGV) {
611
2
1
    my $temp_dir = split_file($file);
612
2
4
    push @reports, "$temp_dir\n";
613  }
614
2
6
  print join '', @reports;
615}
616
6171;