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
95897
3
use 5.022;
11
1
1
1
1
1
54
use feature 'unicode_strings';
12
1
1
1
2
0
8
use strict;
13
1
1
1
1
1
17
use warnings;
14
1
1
1
1
0
15
no warnings qw(experimental::vlb);
15
1
1
1
1
0
22
use Encode qw/decode_utf8 encode FB_DEFAULT/;
16
1
1
1
1
1
21
use File::Basename;
17
1
1
1
2
0
15
use Cwd 'abs_path';
18
1
1
1
1
1
11
use File::Temp qw/ tempfile tempdir /;
19
1
1
1
210
1
2512
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
18
  my ($expression) = @_;
39
19
19
12
127
  return eval { qr /$expression/ };
40}
41
42sub quote_re {
43
19
12
  my ($expression) = @_;
44
19
17
  return $expression if $expression =~ /\?\{/;
45
19
48
  $expression =~ s/
46   \G
47   (
48      (?:[^\\]|\\[^Q])*
49   )
50   (?:
51      \\Q
52      (?:[^\\]|\\[^E])*
53      (?:\\E)?
54   )?
55/
56
38
56
   $1 . (defined($2) ? quotemeta($2) : '')
57/xge;
58
19
24
  return $expression;
59}
60
61sub file_to_list {
62
4
1072
  my ($re) = @_;
63
4
4
  my @file;
64
4
22
  return @file unless open(FILE, '<:utf8', $re);
65
66
4
6
  local $/=undef;
67
4
16
  my $file=<FILE>;
68
4
8
  close FILE;
69
4
2
  my $line_number = 0;
70
4
16
  for (split /\R/, $file) {
71
17
24
    ++$line_number;
72
17
26
    next if /^#/;
73
12
7
    chomp;
74
12
32
    next unless s/^(.+)/(?:$1)/;
75
9
20
    my $quoted = quote_re($1);
76
9
12
    if (test_re $quoted) {
77
8
11
      push @file, $_;
78    } else {
79
1
1
      my $error = $@;
80
1
36
      my $home = dirname(__FILE__);
81
1
23
      $error =~ s/$home.*?\.pm line \d+\./$re line $line_number (bad-regex)/;
82
1
9
      print STDERR $error;
83
1
4
      push @file, '(?:\$^ - skipped because bad-regex)';
84    }
85  }
86
87
4
12
  return @file;
88}
89
90sub list_to_re {
91
3
8
  my (@list) = @_;
92
3
8
8
2
6
8
  @list = map { my $quoted = quote_re($_); test_re($quoted) ? $quoted : '' } @list;
93
3
8
3
8
  @list = grep { $_ ne '' } @list;
94
3
4
  return '$^' unless scalar @list;
95
3
19
  return join "|", (@list);
96}
97
98sub file_to_re {
99
1
449
  my ($re) = @_;
100
1
1
  return list_to_re(file_to_list($re));
101}
102
103sub not_empty {
104
51
37
  my ($thing) = @_;
105
51
155
  return defined $thing && $thing ne ''
106}
107
108sub valid_word {
109  # shortest_word is an absolute
110
22
14
  our ($shortest, $longest, $shortest_word, $longest_word);
111
22
22
  $shortest = $shortest_word if $shortest_word;
112
22
18
  if ($longest_word) {
113    # longest_word is an absolute
114
20
12
    $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
15
  our ($upper_pattern, $lower_pattern, $punctuation_pattern);
121
22
66
17
128
  my $word_pattern = join '|', (grep { defined $_ && /./ } ($upper_pattern, $lower_pattern, $punctuation_pattern));
122
22
20
  $word_pattern = q<\\w|'> unless $word_pattern;
123
22
42
  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
21
  $shortest = 3 unless defined $shortest;
129
22
18
  $longest = '' unless defined $longest;
130
22
69
  $word_match = "(?:$word_pattern){$shortest,$longest}";
131
22
149
  return qr/\b$word_match\b/;
132}
133
134sub load_dictionary {
135
12
1672
  my ($dict) = @_;
136
12
9
  our ($word_match, $longest, $shortest, $longest_word, $shortest_word, %dictionary);
137
12
5
  $longest_word = CheckSpelling::Util::get_val_from_env('INPUT_LONGEST_WORD', undef);
138
12
7
  $shortest_word = CheckSpelling::Util::get_val_from_env('INPUT_SHORTEST_WORD', undef);
139
12
8
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
140
12
11
  $ignore_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_IGNORE_PATTERN', q<[^a-zA-Z']>);
141
12
39
  $upper_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_UPPER_PATTERN', '[A-Z]');
142
12
24
  $lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_LOWER_PATTERN', '[a-z]');
143
12
18
  $not_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_LOWER_PATTERN', '[^a-z]');
144
12
21
  $not_upper_or_lower_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_NOT_UPPER_OR_LOWER_PATTERN', '[^A-Za-z]');
145
12
34
  $punctuation_pattern = CheckSpelling::Util::get_file_from_env_utf8('INPUT_PUNCTUATION_PATTERN', q<'>);
146
12
23
  %dictionary = ();
147
148
12
416
  open(DICT, '<:utf8', $dict);
149
12
43
  while (!eof(DICT)) {
150
31
29
    my $word = <DICT>;
151
31
26
    chomp $word;
152
31
64
    next unless $word =~ $word_match;
153
28
25
    my $l = length $word;
154
28
16
    $longest = -1 unless not_empty($longest);
155
28
25
    $longest = $l if $l > $longest;
156
28
24
    $shortest = $l if $l < $shortest;
157
28
51
    $dictionary{$word}=1;
158  }
159
12
19
  close DICT;
160
161
12
7
  $word_match = valid_word();
162}
163
164sub hunspell_dictionary {
165
3
6
  my ($dict) = @_;
166
3
3
  my $name = $dict;
167
3
4
  $name =~ s{/src/index/hunspell/index\.dic$}{};
168
3
9
  $name =~ s{.*/}{};
169
3
3
  my $aff = $dict;
170
3
2
  my $encoding;
171
3
5
  $aff =~ s/\.dic$/.aff/;
172
3
18
  if (open AFF, '<', $aff) {
173
3
15
    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
6
    close AFF;
179  }
180  return {
181
3
279
    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
7516
  my ($configuration) = @_;
191
9
9
  our ($word_match, %unique, $patterns_re, @forbidden_re_list, $forbidden_re, @candidates_re_list, $candidates_re);
192
9
19
  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
12
  our $timeout = CheckSpelling::Util::get_val_from_env('splitter_timeout', 30);
195
9
12
  if ($hunspell_dictionary_path) {
196
3
24
    our @hunspell_dictionaries = ();
197
1
1
1
1
1
1
1
1
1
3
177
731
20
4
0
12
5
1
16
169
    if (eval 'use Text::Hunspell; 1') {
198
3
87
      my @hunspell_dictionaries_list = glob("$hunspell_dictionary_path/*.dic");
199
3
6
      for my $hunspell_dictionary_file (@hunspell_dictionaries_list) {
200
3
8
        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
11
  my (@patterns_re_list, %in_patterns_re_list);
207
9
39
  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
10
    $patterns_re = undef;
213  }
214
215
9
19
  if (-e "$configuration/forbidden.txt") {
216
1
11
    @forbidden_re_list = file_to_list "$configuration/forbidden.txt";
217
1
1
    $forbidden_re = list_to_re @forbidden_re_list;
218  } else {
219
8
7
    $forbidden_re = undef;
220  }
221
222
9
25
  if (-e "$configuration/candidates.txt") {
223
1
2
    @candidates_re_list = file_to_list "$configuration/candidates.txt";
224
1
2
2
1
2
5
    @candidates_re_list = map { my $quoted = quote_re($_); $in_patterns_re_list{$_} || !test_re($quoted) ? '' : $quoted } @candidates_re_list;
225
1
2
    $candidates_re = list_to_re @candidates_re_list;
226  } else {
227
8
8
    $candidates_re = undef;
228  }
229
230
9
13
  our $largest_file = CheckSpelling::Util::get_val_from_env('INPUT_LARGEST_FILE', 1024*1024);
231
232
9
8
  my $disable_flags = CheckSpelling::Util::get_file_from_env('INPUT_DISABLE_CHECKS', '');
233
9
10
  our $disable_word_collating = $disable_flags =~ /(?:^|,|\s)word-collating(?:,|\s|$)/;
234
9
9
  our $disable_minified_file = $disable_flags =~ /(?:^|,|\s)minified-file(?:,|\s|$)/;
235
9
5
  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
8
  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
15
  our $base_dict = CheckSpelling::Util::get_file_from_env('dict', "$configuration/words");
244
9
22
  $base_dict = '/usr/share/dict/words' unless -e $base_dict;
245
9
9
  load_dictionary($base_dict);
246}
247
248sub split_line {
249
1164
485
  our (%dictionary, $word_match, $disable_word_collating);
250
1164
439
  our ($ignore_pattern, $upper_pattern, $lower_pattern, $not_lower_pattern, $not_upper_or_lower_pattern, $punctuation_pattern);
251
1164
451
  our @hunspell_dictionaries;
252
1164
393
  our $shortest;
253
1164
489
  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
492
  my $rsqm = "\xE2\x80\x99";
258
259
1164
575
  my ($words, $unrecognized) = (0, 0);
260
1164
755
  my ($line, $unique_ref, $unique_unrecognized_ref, $unrecognized_line_items_ref) = @_;
261
1164
5229
    $line =~ s/(?:$rsqm|&apos;|&#39;|\%27|&#8217;|&#x2019;|&rsquo;|\\u2019|\x{2019}|')+/'/g;
262
1164
2389
    $line =~ s/(?:$ignore_pattern)+/ /g;
263
1164
1451
    while ($line =~ s/($upper_pattern{2,})($upper_pattern$lower_pattern{2,})/ $1 $2 /g) {}
264
1164
3817
    while ($line =~ s/((?:$lower_pattern|$punctuation_pattern)+)($upper_pattern)/$1 $2/g) {}
265
1164
1343
    for my $token (split /\s+/, $line) {
266
5851
4975
      next unless $token =~ /$pattern/;
267
4689
3397
      $token =~ s/^(?:'|$rsqm)+//g;
268
4689
4174
      $token =~ s/(?:'|$rsqm)+s?$//g;
269
4689
2536
      my $raw_token = $token;
270
4689
2602
      $token =~ s/^[^Ii]?'+(.*)/$1/;
271
4689
2144
      $token =~ s/(.*?)'+$/$1/;
272
4689
6045
      next unless $token =~ $word_match;
273
4554
3790
      if (defined $dictionary{$token}) {
274
1028
473
        ++$words;
275
1028
534
        $unique_ref->{$token}=1;
276
1028
843
        next;
277      }
278
3526
2215
      if (@hunspell_dictionaries) {
279
3504
1504
        my $found = 0;
280
3504
1726
        for my $hunspell_dictionary (@hunspell_dictionaries) {
281          my $token_encoded = defined $hunspell_dictionary->{'encoding'} ?
282
3504
2318
            encode($hunspell_dictionary->{'encoding'}, $token) : $token;
283
3504
5144
          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
2223
        next if $found;
291      }
292
3526
2131
      my $key = lc $token;
293
3526
2698
      if (defined $dictionary{$key}) {
294
3
2
        ++$words;
295
3
2
        $unique_ref->{$key}=1;
296
3
4
        next;
297      }
298
3523
2221
      unless ($disable_word_collating) {
299
3523
1884
        $key =~ s/''+/'/g;
300
3523
1802
        $key =~ s/'[sd]$//;
301      }
302
3523
2550
      if (defined $dictionary{$key}) {
303
0
0
        ++$words;
304
0
0
        $unique_ref->{$key}=1;
305
0
0
        next;
306      }
307
3523
1568
      ++$unrecognized;
308
3523
1799
      $unique_unrecognized_ref->{$raw_token}=1;
309
3523
4049
      $unrecognized_line_items_ref->{$raw_token}=1;
310    }
311
1164
1389
    return ($words, $unrecognized);
312}
313
314sub skip_file {
315
15
28
  my ($temp_dir, $reason) = @_;
316
15
318
  open(SKIPPED, '>:utf8', "$temp_dir/skipped");
317
15
74
  print SKIPPED $reason;
318
15
297
  close SKIPPED;
319}
320
321sub split_file {
322
15
5546
  my ($file) = @_;
323  our (
324
15
11
    $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
9
  my $rsqm = "\xE2\x80\x99";
335
336
15
18
  my @candidates_re_hits = (0) x scalar @candidates_re_list;
337
15
10
  my @candidates_re_lines = (0) x scalar @candidates_re_list;
338
15
13
  my @forbidden_re_hits = (0) x scalar @forbidden_re_list;
339
15
10
  my @forbidden_re_lines = (0) x scalar @forbidden_re_list;
340
15
25
  my $temp_dir = tempdir(DIR=>$sandbox);
341
15
1933
  print STDERR "checking file: $file\n" if defined $ENV{'DEBUG'};
342
15
194
  open(NAME, '>', "$temp_dir/name");
343
15
23
    print NAME $file;
344
15
113
  close NAME;
345
15
27
  my $file_size = -s $file;
346
15
21
  if (defined $largest_file) {
347
15
11
    unless ($check_file_names eq $file) {
348
15
33
      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
15
  if ($use_magic_file) {
355
8
9995
    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
25760
      my $file_kind = <$file_fh>;
367
8
3990
      close $file_fh;
368
8
96
      if ($file_kind =~ /^(.*?); charset=binary/) {
369
2
24
        skip_file($temp_dir, "it appears to be a binary file (`$1`) (binary-file)\n");
370
2
52
        return $temp_dir;
371      }
372    }
373  }
374
12
74
  open FILE, '<', $file;
375
12
11
  binmode FILE;
376
12
8
  my $head;
377
12
70
  read(FILE, $head, 4096);
378
12
682
  $head =~ s/(?:\r|\n)+$//;
379
12
37
  my $dos_new_lines = () = $head =~ /\r\n/gi;
380
12
28
  my $unix_new_lines = () = $head =~ /\n/gi;
381
12
90
  my $mac_new_lines = () = $head =~ /\r/gi;
382
12
52
  local $/;
383
12
68
  if ($unix_new_lines == 0 && $mac_new_lines == 0) {
384
3
8
    $/ = "\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
5
    $/ = "\r";
389  } else {
390
6
6
    $/ = "\n";
391  }
392
12
18
  seek(FILE, 0, 0);
393
12
17
  ($words, $unrecognized) = (0, 0);
394
12
26
  %unique = ();
395
12
18
  %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
98
  };
403
404
12
194
  open(WARNINGS, '>:utf8', "$temp_dir/warnings");
405
12
7
  our $timeout;
406
12
10
  eval {
407
12
0
77
0
    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
408
12
30
    alarm $timeout;
409
410
12
7
    my $offset = 0;
411
12
61
    while (<FILE>) {
412
1167
952
      if ($. == 1) {
413
12
14
        unless ($disable_minified_file) {
414
12
42
          if ($file_size >= 512 && length($_) == $file_size) {
415
1
7
            skip_file($temp_dir, "file only has a single line (single-line-file)\n");
416
1
3
            last;
417          }
418        }
419      }
420
1166
1867
      $_ = decode_utf8($_, FB_DEFAULT);
421
1166
2337
      if (/[\x{D800}-\x{DFFF}]/) {
422
0
0
        skip_file($temp_dir, "file contains a UTF-16 surrogate -- UTF-16 surrogates are not supported (utf16-surrogate-file)\n");
423
0
0
        last;
424      }
425
1166
1252
      s/\R$//;
426
1166
985
      s/^\x{FEFF}// if $. == 1;
427
1166
994
      next unless /./;
428
1164
685
      my $raw_line = $_;
429
430      # hook for custom line based text exclusions:
431
1164
720
      if (defined $patterns_re) {
432
2
6
10
7
        s/($patterns_re)/"="x length($1)/ge;
433      }
434
1164
600
      my $previous_line_state = $_;
435
1164
520
      my $line_flagged;
436
1164
721
      if ($forbidden_re) {
437
9
4
55
8
        while (s/($forbidden_re)/"="x length($1)/e) {
438
4
1
          $line_flagged = 1;
439
4
8
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
440
4
3
          my $found_trigger_re;
441
4
4
          for my $i (0 .. $#forbidden_re_list) {
442
4
4
            my $forbidden_re_singleton = $forbidden_re_list[$i];
443
4
3
            my $test_line = $previous_line_state;
444
4
3
29
4
            if ($test_line =~ s/($forbidden_re_singleton)/"="x length($1)/e) {
445
3
4
              next unless $test_line eq $_;
446
3
5
              my ($begin_test, $end_test, $match_test) = ($-[0] + 1, $+[0] + 1, $1);
447
3
2
              next unless $begin == $begin_test;
448
3
4
              next unless $end == $end_test;
449
3
3
              next unless $match eq $match_test;
450
3
1
              $found_trigger_re = $forbidden_re_singleton;
451
3
6
              my $hit = "$.:$begin:$end";
452
3
3
              $forbidden_re_hits[$i]++;
453
3
4
              $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
5
          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
14
            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
16
          $previous_line_state = $_;
466        }
467      }
468      # This is to make it easier to deal w/ rules:
469
1164
949
      s/^/ /;
470
1164
595
      my %unrecognized_line_items = ();
471
1164
697
      my ($new_words, $new_unrecognized) = split_line($_, \%unique, \%unique_unrecognized, \%unrecognized_line_items);
472
1164
565
      $words += $new_words;
473
1164
475
      $unrecognized += $new_unrecognized;
474
1164
698
      my $line_length = length($raw_line);
475
1164
1192
      for my $token (sort CheckSpelling::Util::case_biased keys %unrecognized_line_items) {
476
1029
489
        my $found_token = 0;
477
1029
425
        my $raw_token = $token;
478
1029
521
        $token =~ s/'/(?:'|\x{2019}|\&apos;|\&#39;)+/g;
479
1029
395
        my $before;
480
1029
1351
        if ($token =~ /^$upper_pattern$lower_pattern/) {
481
5
3
          $before = '(?<=.)';
482        } elsif ($token =~ /^$upper_pattern/) {
483
0
0
          $before = "(?<!$upper_pattern)";
484        } else {
485
1024
515
          $before = "(?<=$not_lower_pattern)";
486        }
487
1029
959
        my $after = ($token =~ /$upper_pattern$/) ? "(?=$not_upper_or_lower_pattern)|(?=$upper_pattern$lower_pattern)" : "(?=$not_lower_pattern)";
488
1029
1725
        while ($raw_line =~ /(?:\b|$before)($token)(?:\b|$after)/g) {
489
3520
1568
          $line_flagged = 1;
490
3520
1381
          $found_token = 1;
491
3520
4620
          my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
492
3520
2752
          next unless $match =~ /./;
493
3520
1824
          my $wrapped = CheckSpelling::Util::wrap_in_backticks($match);
494
3520
11471
          print WARNINGS ":$.:$begin ... $end: $wrapped\n";
495        }
496
1029
1014
        unless ($found_token) {
497
3
29
          if ($raw_line !~ /$token.*$token/ && $raw_line =~ /($token)/) {
498
3
5
            my ($begin, $end, $match) = ($-[0] + 1, $+[0] + 1, $1);
499
3
2
            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
1657
      if ($line_flagged && $candidates_re) {
509
1
1
        $_ = $previous_line_state;
510
1
1
48
3
        s/($candidates_re)/"="x length($1)/ge;
511
1
3
        if ($_ ne $previous_line_state) {
512
1
1
          $_ = $previous_line_state;
513
1
2
          for my $i (0 .. $#candidates_re_list) {
514
2
2
            my $candidate_re = $candidates_re_list[$i];
515
2
13
            next unless $candidate_re =~ /./ && $raw_line =~ /$candidate_re/;
516
1
1
6
2
            if (($_ =~ s/($candidate_re)/"="x length($1)/e)) {
517
1
3
              my ($begin, $end) = ($-[0] + 1, $+[0] + 1);
518
1
2
              my $hit = "$.:$begin:$end";
519
1
1
              $_ = $previous_line_state;
520
1
1
5
2
              my $replacements = ($_ =~ s/($candidate_re)/"="x length($1)/ge);
521
1
1
              $candidates_re_hits[$i] += $replacements;
522
1
2
              $candidates_re_lines[$i] = $hit unless $candidates_re_lines[$i];
523
1
2
              $_ = $previous_line_state;
524            }
525          }
526        }
527      }
528
1164
719
      unless ($disable_minified_file) {
529
1164
796
        s/={3,}//g;
530
1164
717
        $offset += length;
531
1164
966
        my $ratio = int($offset / $.);
532
1164
546
        my $ratio_threshold = 1000;
533
1164
2544
        if ($ratio > $ratio_threshold) {
534
11
24
          skip_file($temp_dir, "average line width ($ratio) exceeds the threshold ($ratio_threshold) (minified-file)\n");
535        }
536      }
537    }
538
539
12
64
    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, "it could not be parsed file within time limit (slow-file)\n");
545  }
546
547
12
55
  close FILE;
548
12
93
  close WARNINGS;
549
550
12
29
  if ($unrecognized || @candidates_re_hits || @forbidden_re_hits) {
551
11
213
    open(STATS, '>:utf8', "$temp_dir/stats");
552
11
138
      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
72
    close STATS;
560
11
116
    open(UNKNOWN, '>:utf8', "$temp_dir/unknown");
561
11
19
33
25
      print UNKNOWN map { "$_\n" } sort CheckSpelling::Util::case_biased keys %unique_unrecognized;
562
11
48
    close UNKNOWN;
563  }
564
565
12
103
  return $temp_dir;
566}
567
568sub main {
569
2
355
  my ($configuration, @ARGV) = @_;
570
2
1
  our %dictionary;
571
2
3
  unless (%dictionary) {
572
1
1
    init($configuration);
573  }
574
575  # read all input
576
2
2
  my @reports;
577
578
2
1
  for my $file (@ARGV) {
579
2
7
    my $temp_dir = split_file($file);
580
2
4
    push @reports, "$temp_dir\n";
581  }
582
2
7
  print join '', @reports;
583}
584
5851;