chiark / gitweb /
17bd81ce366b49b2cc7d462f33146b1b5e4f9901
[dgit.git] / i18n-diff-auditor
1 #!/usr/bin/perl -w
2 use strict;
3 use Carp;
4 use Data::Dumper;
5 use Getopt::Long;
6
7 our $debug = 0;
8 GetOptions("debug|D+" => \$debug
9            );
10
11 our @debug;
12 sub debug ($$) {
13     my ($i,$s) = @_;
14     push @{ $debug[$i] }, $s if $debug;
15 }
16
17 my @d = <>;
18 unshift @d, "# dummy line to make line 1 index 1 in \@d\n";
19
20 our $i_last_l_ok = -1;
21 our $count_i_last_l_ok;
22
23 sub l_ok ($) {
24     my ($i) = @_;
25
26     if ($i == $i_last_l_ok) {
27         confess $i if $count_i_last_l_ok++ > 50;
28     } else {
29         $count_i_last_l_ok = 0;
30         $i_last_l_ok = $i;
31     }
32
33     return unless $i < @d;
34     $_ = $d[$i];
35     #print STDERR "L $i\n";
36     1;
37 }
38
39 sub l ($) {
40     my ($i) = @_;
41     confess $i unless l_ok $i;
42 };
43
44 our $perlop_text = <<'END'; # c&p from man perlop
45            left        terms and list operators (leftward)
46            left        ->
47            nonassoc    ++ --
48            right       **
49            right       ! ~ \ and unary + and -
50            left        =~ !~
51            left        * / % x
52            left        + - .
53            left        << >>
54            nonassoc    named unary operators
55            nonassoc    < > <= >= lt gt le ge
56            nonassoc    == != <=> eq ne cmp ~~
57            left        &
58            left        | ^
59            left        &&
60            left        || //
61            nonassoc    ..  ...
62            right       ?:
63            right       = += -= *= etc. goto last next redo dump
64            left        , =>
65            nonassoc    list operators (rightward)
66            right       not
67            left        and
68            left        or xor
69
70            **=    +=    *=    &=    &.=    <<=    &&=
71                   -=    /=    |=    |.=    >>=    ||=
72                   .=    %=    ^=    ^.=           //=
73                         x=
74 END
75
76 our $perlop_re;
77
78 sub prep_perlop () {
79     my @ops;
80     foreach (split /\n/, $perlop_text) {
81         next unless m{\S};
82         s{\s+$}{};
83         s{^\s+}{};
84         s{^(?: left | right | nonassoc ) \s+}{}x;
85         next if m{^terms and list operators};
86         next if m{^named unary};
87         next if m{^list operators};
88         s{ and unary.*}{};
89         s{ etc\. }{ };
90         s{\?\:}{ ? : };
91         foreach my $op (split /\s+/) {
92             next unless length $op;
93             next if $op =~ m{^\w+$};
94             $op =~ s/\W/\\$&/g;
95             push @ops, $op;
96         }
97     }
98     $perlop_re = '(?: '.(join ' | ', @ops).' )';
99     $perlop_re = qr{$perlop_re}x;
100     #print STDERR "$perlop_re\n";
101 }
102
103 prep_perlop();
104
105 our ($ifilehead, $ifirsthunkhead);
106 our ($ihunkhead, $ihunkend);
107 our ($ichunkstart, $ichunkend);
108 our ($before, $after);
109
110 sub is_string ($) { $_[0]{T} =~ m/heredoc|string/; }
111 sub is_trans ($) { grep { $_[0]{E} eq $_ } qw(__ f_ i_); }
112
113 sub qp ($) {
114     my ($p) = @_;
115     $p =~ s{\\}{\\\\}g;
116     $p =~ s{\'}{\\'}g;
117     $p =~ s{\n}{\\n}g;
118     $p =~ s{\t}{\\t}g;
119     return "'$p'";
120 };
121
122 sub semiparse ($) {
123     ($_) = @_;
124     my @o;
125     #my $in = $_;
126     # entries contain
127     #   T     type
128     #   E     exact input text (does not contain here doc contents)
129     #   P     something to print in messages
130     #   V     value, only for: heredoc string
131     #   Q     quote characcter, only for: heredoc string
132     for (;;) {
133         s{^\s+}{};
134         if (s{^[\$\@\%]?[_0-9a-zA-Z]+}{}) {
135             push @o, { T => 'ident', E => $&, P => $& };
136         } elsif (s{^\<\<(['"]?)([A-Z_]+)\1}{}) {
137             my ($q,$d) = ($1,$2);
138             $q ||= '"';
139             push @o, { T => 'heredoc', Q => $q, Delim => $d,
140                        E => $&, P => "<<$q$d$q" };
141             if (s{^
142                     (                 .* \n     )
143                     ( (?: (?! $d \n ) .* \n )*? )
144                               $d         \n
145                  }{ $1 }xe) {
146                 $o[$#o]{V} = $2;
147             } else {
148                 m{^.*\n} or confess;
149                 $_ = $&;
150                 $o[$#o]{V} = $';
151                 $o[$#o]{Invented} = 1;
152             }
153         } elsif (s{^ (["'])( (?: [^\\'"]
154                                | \\ [^"']
155                                | (?! \1 ) [^"]
156                               )*
157                        ) \1 }{}x) {
158             my ($q,$v) = ($1,$2);
159             push @o, { T => 'string', E => $&, P => "$q$q",
160                        Q => $q, V => $v};
161         } elsif (s{^$perlop_re|^\;}{}) {
162             push @o, { T => 'op', E => $&, P => $& };
163         } elsif (s/^[[{(]//) {
164             push @o, { T => 'bra', E => $&, P => $& };
165         } elsif (s/^[]})]//) {
166             push @o, { T => 'ket', E => $&, P => $& };
167         } elsif (s/^( [\$\@\%] )( \{ )//x) {
168             push @o, { T => 'deref', E => $1, P => $1 },
169                      { T => 'bra',   E => $2, P => $2 };
170         } elsif (s/^ [\$\@\%] [^[^{] //x) {
171             push @o, { T => 'specvar', E => $&, P => $& };
172         } elsif (!length) {
173             last;
174         } else {
175             m{^.{0,10}};
176             die "cannot tokenise \`$&'";
177         }
178     }
179     for (my $i=0; $i+2 < @o; $i++) {
180         next unless $o[$i+1]{E} eq '.';
181         my @inputs = @o[$i, $i+2];
182         #print STDERR Dumper(\@inputs);
183         next if grep { !is_string($_) } @inputs;
184         my $q = $inputs[0]{Q};
185         next if grep { $_->{Q} ne $q } @inputs;
186         next if grep { $_->{Invented} } @inputs;
187         my $new = { T => 'joinedstrings',
188                     E => (join '.', map { $_->{E} } @inputs),
189                     P => (join '.', map { $_->{P} } @inputs),
190                     V => (join '',  map { $_->{V} } @inputs),
191                     Q => $q,
192                   };
193         @o = (@o[0..$i-1], $new, @o[$i+3..$#o]);
194         $i--; # counteracts $i++
195     }
196     debug $ichunkstart, "semiparsed: ".join ' ', map { $_->{P} } @o;
197     # debug $ichunkstart, "semiparsed V: ".join ' ', map { defined $_->{V} ? ">$_->{V}<" : '-' } @o;
198     return @o;
199 }           
200
201 our @analysed_x;
202 our @analysed_y;
203
204 sub analyse_chunk_core () {
205     die "plain deletion\n" unless defined $after;
206     die "plain insertion\n" unless defined $before;
207     my @xs = semiparse $before;
208     my @ys = semiparse $after;
209     @analysed_x = @analysed_y = ();
210     my $next_something = sub {
211         my ($ary,$anal,$var,$what) = @_;
212         die "ran out of $what\n" unless @$ary;
213         my $r = shift @$ary;
214         push @$anal, $r->{P};
215         $$var = $r;
216     };
217     my ($x,$y);
218     my $next_x = sub { $next_something->(\@xs, \@analysed_x, \$x, 'before'); };
219     my $next_y = sub { $next_something->(\@ys, \@analysed_y, \$y, 'after' ); };
220     our @y_expect_suffix = ();
221     for (;;) {
222         while (my $e = shift @y_expect_suffix) {
223             $next_y->();
224             $y->{E} eq $e
225                 or die "suffix mismatch, expected $e got $y->{E}\n";
226         }
227         last unless @xs or @ys;
228         $next_x->();
229         $next_y->();
230         next if $x->{E} eq $y->{E};
231         next if $x->{E} eq 'sprintf' and $y->{E} eq 'f_';
232         next if $x->{E} eq 'die'     and $y->{E} eq 'confess';
233         if ($y->{E} eq '+'
234             and @ys >= 3
235             and $ys[0]{E} eq '('
236             and is_trans($ys[1])) {
237             $next_y->(); # (
238             $next_y->(); # __ f_ i_
239             @y_expect_suffix = ')';
240         } elsif ($y->{E} eq '('
241             and @ys > 2
242             and is_trans($ys[0])
243             and @analysed_y
244             and (grep { $_ eq $analysed_y[-1] } (qw( => [ { ? : . ),
245                                                  '(', ',') )) {
246             $next_y->(); # __ f_ i_
247             @y_expect_suffix = ')';
248         }
249         my $string_changed;
250         my $ye = $y->{E};
251         if (is_trans($y)) {
252             $next_y->();
253             die "__ on non-string $y->{P}\n"     unless is_string($y);
254             die "__ on was non-string $x->{P}\n" unless is_string($x);
255             if ($y->{Q} ne "'") {
256                 die "var subst in new string\n"
257                     if $y->{V} =~ m{(?<!\\) [\$\@]};
258             }
259             eval {
260                 die "__ string changed\n"       unless $y->{V} eq $x->{V};
261                 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
262             };
263             $string_changed = $@;
264         }
265         if ($ye eq '__') {
266             $_ = $y->{V};
267             die "percent $& in __ ' string\n" if m{\%};
268             die $string_changed if length $string_changed;
269             next;
270         }
271         if ($ye eq 'i_') {
272             die $string_changed if length $string_changed;
273             next;
274         }
275         if ($ye eq 'f_') {
276             my $fmt = $y->{V};
277             die "no percent in _f string\n" unless $fmt =~ m{\%};
278             next unless $string_changed;
279             die "f_ old string '-quoted\n" if $x->{Q} ne '"';
280             my $xs = $x->{V};
281             my $exactly = sub {
282                 my ($lit, $what) = @_;
283                 my $xl = substr($xs, 0, length($lit));
284                 if ($xl ne $lit) {
285                     debug $ichunkstart, "not exactly x: ..".qp($xs);
286                     debug $ichunkstart, "not exactly y:   ".qp($lit);
287                     my $next = @ys ? $ys[0]{P} : '(end)';
288                     die "string contents mismatch near $what before $next\n";
289                 }
290                 $xs = substr($xs, length($lit));
291             };
292             for (;;) {
293                 #print STDERR Dumper($fmt, $xs, \@xs, @ys);
294                 if ($fmt !~ m{\%[^\%]}) {
295                     $exactly->($fmt, '(tail)');
296                     $fmt = '';
297                     die "text deleted from end of string: ".qp($xs)."\n"
298                         if length $xs;
299                     last;
300                 }
301                 $exactly->($`, '(literal)');
302                 $fmt = $';
303                 if ($& eq '%%') { $exactly->('%', '%%'); next; }
304                 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
305                 $next_y->();
306                 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
307                 if (!length $fmt and
308                     !length $xs and
309                     @xs and
310                     $xs[0]{E} eq '.') {
311                     # X has   "<earlier>" .                <something>
312                     # Y has   "<earlier>%s" [other args] , <something>
313                     $next_x->(); # eat the '.'
314                     next;
315                 }
316                 if ($xs =~ m{^\@}) {
317                     $next_y->();
318                     die "\@... => not string" unless is_string($y);
319                     die "\@... => $y->{P}" if $y->{Q} ne '"';
320                     $exactly->($y->{V}, $y->{P});
321                     next;
322                 }
323                 my $bras = 0;
324                 for (;;) {
325                     if (!$bras and !@ys) {
326                         last;
327                     }
328                     $next_y->();
329                     if (!$bras and
330                         (grep { $y->{E} eq $_ } qw( or xor and not ; :
331                                                     if unless while when )
332                          or $y->{E} eq ','
333                          or $y->{T} eq 'ket'
334                         )) {
335                         # lookahead shows close of containing scope
336                         # or lower precedence operator
337                         unshift @ys, $y;
338                         pop @analysed_y;
339                         last;
340                     }
341                     $xs =~ s{^\s+}{} if $bras;
342                     if (is_string($y) and $y->{Q} eq '"') {
343                         $exactly->($y->{V}, $y->{P});
344                         next;
345                     }
346                     $exactly->($y->{E}, $y->{P});
347                     if ($y->{T} eq 'bra' or $y->{E} eq '?') {
348                         $bras++;
349                     } elsif ($y->{T} eq 'ket' or $y->{E} eq ':') {
350                         die "too many kets at $y->{E}\n" unless $bras;
351                         $bras--;
352                     }
353                 }
354             }
355             next;
356         }
357         die "mismatch $x->{P} => $y->{P}\n";
358     }
359 }
360
361 sub analyse_chunk () {
362     for (;;) {
363         eval { analyse_chunk_core(); };
364         return unless length $@;
365         if ($@ =~ m{^missing end of here doc (\S+)\n}) {
366             # fudge this
367             # (this never happens now, but in the future we might
368             # want this code again eg to try adding to the chunk)
369             $before .= "\n$1\n";
370             $after .= "\n$1\n";
371             next;
372         } else {
373             die $@;
374         }
375     }
376 }
377
378 our @report;
379 our $last_filehead = -1;
380
381 sub report_on_hunk () {
382     return unless @report;
383     if ($last_filehead != $ifilehead) {
384         foreach (my $i=$ifilehead; $i<$ifirsthunkhead; $i++) {
385             print $d[$i];
386         }
387         $last_filehead = $ifilehead;
388     }
389     my $dummy_r = { S => (scalar @d)+1, E => (scalar @d)+1 };
390     my $r;
391     for (my $i=$ihunkhead; ; $i++) {
392         for (;;) {
393             $r //= shift @report;
394             $r //= $dummy_r;
395             last if $i < $r->{E};
396             confess unless $r->{Done} == 03;
397             $r = undef;
398         }
399
400         last unless $i<$ihunkend;
401
402         foreach my $ds (@{ $debug[$i] }) {
403             print "# $ds\n";
404         }
405
406         if ($i == $r->{S}) {
407             print "!! $r->{M}";
408             $r->{Done} |= 01;
409         }
410         if ($i >= $r->{S}) {
411             print "!";
412             $r->{Done} |= 02;
413         } else {
414             print " ";
415         }
416         print $d[$i];
417     }
418     confess unless $r = $dummy_r;
419 }
420
421 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
422     m{^diff} or next;
423     $ifirsthunkhead = $ifilehead;
424     while (l_ok $ifirsthunkhead and
425            m{^diff|^index|^---|^\Q+++\E}) {
426         $ifirsthunkhead++
427     }
428     $ihunkhead = $ifirsthunkhead;
429     while (l_ok $ihunkhead) {
430         m{^\@\@} or confess "$ihunkhead $_ ?";
431         my $i = $ihunkhead + 1;
432         for (; ; $i++) {
433             if (!l_ok $i or m{^ } or m{^\@\@}) {
434                 if (defined $ichunkstart) {
435                     $ichunkend = $i;
436                     eval { analyse_chunk(); 1; };
437                     if (length $@) {
438                         debug $ichunkstart, "done x: @analysed_x";
439                         debug $ichunkstart, "done y: @analysed_y";
440                         push @report, { M => $@,
441                                         S => $ichunkstart,
442                                         E => $ichunkend };
443                     }
444                     $ichunkstart = $ichunkend = $before = $after = undef;
445                 }
446                 l_ok $i or last;
447                 m{^\@\@} and last;
448             } elsif (m{^[-+]}) {
449                 my $which = $& eq '-' ? \$before : \$after;
450                 $ichunkstart //= $i;
451                 $$which //= '';
452                 $$which .= $';
453             } else {
454                 confess "$i $_ ?";
455             }
456         }
457         $ihunkend = $i;
458         report_on_hunk();
459         $ichunkend = $i;
460         $ihunkhead = $i;
461     }
462 }