File Coverage

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

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