chiark / gitweb /
i18n: i18n-diff-auditor: further not exactly debugging improvement
[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 semiparse ($) {
114     ($_) = @_;
115     my @o;
116     # entries contain
117     #   T     type
118     #   E     exact input text (does not contain here doc contents)
119     #   P     something to print in messages
120     #   V     value, only for: heredoc string
121     #   Q     quote characcter, only for: heredoc string
122     for (;;) {
123         s{^\s+}{};
124         if (s{^[\$\@\%]?[_0-9a-zA-Z]+}{}) {
125             push @o, { T => 'ident', E => $&, P => $& };
126         } elsif (s{^\<\<(['"]?)([A-Z_]+)\1}{}) {
127             my ($q,$d) = ($1,$2);
128             $q ||= '"';
129             push @o, { T => 'heredoc', Q => $q, Delim => $d,
130                        E => $&, P => "<<$q$d$q" };
131             s{^
132                 (             .* \n    )
133                 ( (?: (?! $d) .* \n )* )
134                           $d     \n
135               }{ $1 }xe or die "missing end of here doc $d\n";
136             $o[$#o]{V} = $2;
137         } elsif (s{^ (["'])( (?: [^\\'"]
138                                | \\ [^"']
139                                | (?! \1 ) [^"]
140                               )*
141                        ) \1 }{}x) {
142             my ($q,$v) = ($1,$2);
143             push @o, { T => 'string', E => $&, P => "$q$q",
144                        Q => $q, V => $v};
145         } elsif (s{^$perlop_re|^\;}{}) {
146             push @o, { T => 'op', E => $&, P => $& };
147         } elsif (s/^[[{(]//) {
148             push @o, { T => 'bra', E => $&, P => $& };
149         } elsif (s/^[]})]//) {
150             push @o, { T => 'ket', E => $&, P => $& };
151         } elsif (s/^( [\$\@\%] )( \{ )//x) {
152             push @o, { T => 'deref', E => $1, P => $1 },
153                      { T => 'bra',   E => $2, P => $2 };
154         } elsif (s/^ [\$\@\%] [^[^{] //x) {
155             push @o, { T => 'specvar', E => $&, P => $& };
156         } elsif (!length) {
157             last;
158         } else {
159             m{^.{0,10}};
160             die "cannot tokenise \`$&'";
161         }
162     }
163     for (my $i=0; $i+2 < @o; $i++) {
164         next unless $o[$i+1]{E} eq '.';
165         my @inputs = @o[$i, $i+2];
166         #print STDERR Dumper(\@inputs);
167         next if grep { !is_string($_) } @inputs;
168         my $q = $inputs[0]{Q};
169         next if grep { $_->{Q} ne $q } @inputs;
170         my $new = { T => 'joinedstrings',
171                     E => (join '.', map { $_->{E} } @inputs),
172                     P => (join '.', map { $_->{P} } @inputs),
173                     V => (join '',  map { $_->{V} } @inputs),
174                     Q => $q,
175                   };
176         @o = (@o[0..$i-1], $new, @o[$i+3..$#o]);
177         $i--; # counteracts $i++
178     }
179     debug $ichunkstart, "semiparsed: ".join ' ', map { $_->{P} } @o;
180     # debug $ichunkstart, "semiparsed V: ".join ' ', map { defined $_->{V} ? ">$_->{V}<" : '-' } @o;
181     return @o;
182 }           
183
184 our @analysed_x;
185 our @analysed_y;
186
187 sub analyse_chunk_core () {
188     die "plain deletion\n" unless defined $after;
189     die "plain insertion\n" unless defined $before;
190     my @xs = semiparse $before;
191     my @ys = semiparse $after;
192     @analysed_x = @analysed_y = ();
193     my $next_something = sub {
194         my ($ary,$anal,$var,$what) = @_;
195         die "ran out of $what\n" unless @$ary;
196         my $r = shift @$ary;
197         push @$anal, $r->{P};
198         $$var = $r;
199     };
200     my ($x,$y);
201     my $next_x = sub { $next_something->(\@xs, \@analysed_x, \$x, 'before'); };
202     my $next_y = sub { $next_something->(\@ys, \@analysed_y, \$y, 'after' ); };
203     our @y_expect_suffix = ();
204     for (;;) {
205         while (my $e = shift @y_expect_suffix) {
206             $next_y->();
207             $y->{E} eq $e
208                 or die "suffix mismatch, expected $e got $y->{E}\n";
209         }
210         last unless @xs or @ys;
211         $next_x->();
212         $next_y->();
213         next if $x->{E} eq $y->{E};
214         next if $x->{E} eq 'sprintf' and $y->{E} eq 'f_';
215         next if $x->{E} eq 'die'     and $y->{E} eq 'confess';
216         if ($y->{E} eq '+'
217             and @ys >= 3
218             and $ys[0]{E} eq '('
219             and is_trans($ys[1])) {
220             $next_y->(); # (
221             $next_y->(); # __ f_ i_
222             @y_expect_suffix = ')';
223         } elsif ($y->{E} eq '('
224             and @ys > 2
225             and is_trans($ys[0])
226             and @analysed_y
227             and (grep { $_ eq $analysed_y[-1] } (qw( => [ { ? : . ),
228                                                  '(', ',') )) {
229             $next_y->(); # __ f_ i_
230             @y_expect_suffix = ')';
231         }
232         my $string_changed;
233         my $ye = $y->{E};
234         if (is_trans($y)) {
235             $next_y->();
236             die "__ on non-string $y->{P}\n"     unless is_string($y);
237             die "__ on was non-string $x->{P}\n" unless is_string($x);
238             if ($y->{Q} ne "'") {
239                 die "var subst in new string\n"
240                     if $y->{V} =~ m{(?<!\\) [\$\@]};
241             }
242             eval {
243                 die "__ string changed\n"       unless $y->{V} eq $x->{V};
244                 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
245             };
246             $string_changed = $@;
247         }
248         if ($ye eq '__') {
249             $_ = $y->{V};
250             die "percent $& in __ ' string\n" if m{\%};
251             die $string_changed if length $string_changed;
252             next;
253         }
254         if ($ye eq 'i_') {
255             die $string_changed if length $string_changed;
256             next;
257         }
258         if ($ye eq 'f_') {
259             my $fmt = $y->{V};
260             die "no percent in _f string\n" unless $fmt =~ m{\%};
261             next unless $string_changed;
262             die "f_ old string '-quoted\n" if $x->{Q} ne '"';
263             my $xs = $x->{V};
264             my $exactly = sub {
265                 my ($lit, $what) = @_;
266                 my $xl = substr($xs, 0, length($lit));
267                 if ($xl ne $lit) {
268                     my $q = sub {
269                         my ($p) = @_;
270                         $p =~ s{\\}{\\\\}g;
271                         $p =~ s{\'}{\\'}g;
272                         $p =~ s{\n}{\\n}g;
273                         $p =~ s{\t}{\\t}g;
274                         return "'$p'";
275                     };
276                     debug $ichunkstart, "not exactly x: ..".$q->($xs);
277                     debug $ichunkstart, "not exactly y:   ".$q->($lit);
278                     my $next = @ys ? $ys[0]{P} : '(end)';
279                     die "string contents mismatch near $what before $next\n";
280                 }
281                 $xs = substr($xs, length($lit));
282             };
283             for (;;) {
284                 if ($fmt !~ m{\%[^\%]}) {
285                     $exactly->($fmt, '(tail)');
286                     $fmt = '';
287                     die "text deleted from end of string\n" if length $xs;
288                     last;
289                 }
290                 $exactly->($`, '(literal)');
291                 $fmt = $';
292                 if ($& eq '%%') { $exactly->('%', '%%'); next; }
293                 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
294                 $next_y->();
295                 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
296                 if (!length $fmt and
297                     !length $xs and
298                     @xs and
299                     $xs[0]{E} eq '.') {
300                     # X has   "<earlier>" .                <something>
301                     # Y has   "<earlier>%s" [other args] , <something>
302                     $next_x->(); # eat the '.'
303                     next;
304                 }
305                 if ($xs =~ m{^\@}) {
306                     $next_y->();
307                     die "\@... => not string" unless is_string($y);
308                     die "\@... => $y->{P}" if $y->{Q} ne '"';
309                     $exactly->($y->{V}, $y->{P});
310                     next;
311                 }
312                 my $bras = 0;
313                 for (;;) {
314                     if (!$bras and !@ys) {
315                         last;
316                     }
317                     $next_y->();
318                     if (!$bras and
319                         (grep { $y->{E} eq $_ } qw( or xor and not ; :
320                                                     if unless while when )
321                          or $y->{E} eq ','
322                          or $y->{T} eq 'ket'
323                         )) {
324                         # lookahead shows close of containing scope
325                         # or lower precedence operator
326                         unshift @ys, $y;
327                         pop @analysed_y;
328                         last;
329                     }
330                     $xs =~ s{^\s+}{} if $bras;
331                     if (is_string($y) and $y->{Q} eq '"') {
332                         $exactly->($y->{V}, $y->{P});
333                         next;
334                     }
335                     $exactly->($y->{E}, $y->{P});
336                     if ($y->{T} eq 'bra' or $y->{E} eq '?') {
337                         $bras++;
338                     } elsif ($y->{T} eq 'ket' or $y->{E} eq ':') {
339                         die "too many kets at $y->{E}\n" unless $bras;
340                         $bras--;
341                     }
342                 }
343             }
344             next;
345         }
346         die "mismatch $x->{P} => $y->{P}\n";
347     }
348 }
349
350 sub analyse_chunk () {
351     for (;;) {
352         eval { analyse_chunk_core(); };
353         return unless length $@;
354         if ($@ =~ m{^missing end of here doc (\S+)\n}) {
355             # fudge this
356             $before .= "\n$1\n";
357             $after .= "\n$1\n";
358             next;
359         } else {
360             die $@;
361         }
362     }
363 }
364
365 our @report;
366 our $last_filehead = -1;
367
368 sub report_on_hunk () {
369     return unless @report;
370     if ($last_filehead != $ifilehead) {
371         foreach (my $i=$ifilehead; $i<$ifirsthunkhead; $i++) {
372             print $d[$i];
373         }
374         $last_filehead = $ifilehead;
375     }
376     my $dummy_r = { S => (scalar @d)+1, E => (scalar @d)+1 };
377     my $r;
378     for (my $i=$ihunkhead; ; $i++) {
379         for (;;) {
380             $r //= shift @report;
381             $r //= $dummy_r;
382             last if $i < $r->{E};
383             confess unless $r->{Done} == 03;
384             $r = undef;
385         }
386
387         last unless $i<$ihunkend;
388
389         foreach my $ds (@{ $debug[$i] }) {
390             print "# $ds\n";
391         }
392
393         if ($i == $r->{S}) {
394             print "!! $r->{M}";
395             $r->{Done} |= 01;
396         }
397         if ($i >= $r->{S}) {
398             print "!";
399             $r->{Done} |= 02;
400         } else {
401             print " ";
402         }
403         print $d[$i];
404     }
405     confess unless $r = $dummy_r;
406 }
407
408 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
409     m{^diff} or next;
410     $ifirsthunkhead = $ifilehead;
411     while (l_ok $ifirsthunkhead and
412            m{^diff|^index|^---|^\Q+++\E}) {
413         $ifirsthunkhead++
414     }
415     $ihunkhead = $ifirsthunkhead;
416     while (l_ok $ihunkhead) {
417         m{^\@\@} or confess "$ihunkhead $_ ?";
418         my $i = $ihunkhead + 1;
419         for (; ; $i++) {
420             if (!l_ok $i or m{^ } or m{^\@\@}) {
421                 if (defined $ichunkstart) {
422                     $ichunkend = $i;
423                     eval { analyse_chunk(); 1; };
424                     if (length $@) {
425                         debug $ichunkstart, "done x: @analysed_x";
426                         debug $ichunkstart, "done y: @analysed_y";
427                         push @report, { M => $@,
428                                         S => $ichunkstart,
429                                         E => $ichunkend };
430                     }
431                     $ichunkstart = $ichunkend = $before = $after = undef;
432                 }
433                 l_ok $i or last;
434                 m{^\@\@} and last;
435             } elsif (m{^[-+]}) {
436                 my $which = $& eq '-' ? \$before : \$after;
437                 $ichunkstart //= $i;
438                 $$which //= '';
439                 $$which .= $';
440             } else {
441                 confess "$i $_ ?";
442             }
443         }
444         $ihunkend = $i;
445         report_on_hunk();
446         $ichunkend = $i;
447         $ihunkhead = $i;
448     }
449 }