Advertisement
Rouslan

bracket checker

Jul 21st, 2011
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.93 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use utf8;
  5. binmode STDOUT,":utf8";
  6. use File::Find;
  7.  
  8. my $checkre = qr/(?|
  9.     (\()(?&matched)([\}\]”›»】〉》」』]|$) |
  10.     (\{)(?&matched)([\)\]”›»】〉》」』]|$) |
  11.     (\[)(?&matched)([\)\}”›»】〉》」』]|$) |
  12.     ()(?&matched)([\)\}\]›»】〉》」』]|$) |
  13.     ()(?&matched)([\)\}\]”»】〉》」』]|$) |
  14.     («)(?&matched)([\)\}\]”›】〉》」』]|$) |
  15.     ()(?&matched)([\)\}\]”›»〉》」』]|$) |
  16.     ()(?&matched)([\)\}\]”›»】》」』]|$) |
  17.     ()(?&matched)([\)\}\]”›»】〉」』]|$) |
  18.     ()(?&matched)([\)\}\]”›»】〉》』]|$) |
  19.     ()(?&matched)([\)\}\]”›»】〉》」]|$) |
  20.     (^)(?&matched)([\)\}\]”›»】〉》」』]))
  21. (?(DEFINE)(?<matched>(?:
  22.     \((?&matched)\) |
  23.     \{(?&matched)\} |
  24.     \[(?&matched)\] |
  25.     “(?&matched)|
  26.     ‹(?&matched)|
  27.     «(?&matched)» |
  28.     【(?&matched)|
  29.     〈(?&matched)|
  30.     《(?&matched)|
  31.     「(?&matched)|
  32.     『(?&matched)|
  33.     [^\(\{\[“‹«【〈《「『\)\}\]”›»】〉》」』]++)*+))
  34. /sx;
  35.  
  36. sub check_file {
  37.     if(-f && /\.txt$/) {
  38.         if(open(my $fh,'<:encoding(UTF-8)',$_)) {
  39.             undef $/;
  40.             my $data = <$fh>;
  41.             if($data =~ $checkre) {
  42.                 if(!length $2) {
  43.                     print "File $File::Find::name has unclosed bracket $1 at position $-[1]\n";
  44.                 }
  45.                 elsif(!length $1) {
  46.                     print "File $File::Find::name has unopened bracket $2 at position $-[2]\n";
  47.                 }
  48.                 else {
  49.                     print "File $File::Find::name has mismatched brackets $1 $2 at positions $-[1] and $-[2]\n";
  50.                 }
  51.             }
  52.         } else {
  53.             print STDERR "Cannot open $File::Find::name: $!\n";
  54.         }
  55.     }
  56. }
  57.  
  58. @ARGV = ('.') unless @ARGV;
  59. find(\&check_file,@ARGV);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement