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