chiark / gitweb /
b9bd4e50e17e1643c894ad961bfbe2e7771f594a
[dgit-junk.git] / i18n-diff-auditor
1 #!/usr/bin/perl -w
2 use strict;
3 use Carp;
4 use Data::Dumper;
5
6 my @d = <>;
7 unshift @d, "# dummy line to make line 1 index 1 in \@d\n";
8
9 our $i_last_l_ok = -1;
10 our $count_i_last_l_ok;
11
12 sub l_ok ($) {
13     my ($i) = @_;
14
15     if ($i == $i_last_l_ok) {
16         confess $i if $count_i_last_l_ok++ > 50;
17     } else {
18         $count_i_last_l_ok = 0;
19         $i_last_l_ok = $i;
20     }
21
22     return unless $i < @d;
23     $_ = $d[$i];
24     #print STDERR "L $i\n";
25     1;
26 }
27
28 sub l ($) {
29     my ($i) = @_;
30     confess $i unless l_ok $i;
31 };
32
33 our $perlop_text = <<'END'; # c&p from man perlop
34            left        terms and list operators (leftward)
35            left        ->
36            nonassoc    ++ --
37            right       **
38            right       ! ~ \ and unary + and -
39            left        =~ !~
40            left        * / % x
41            left        + - .
42            left        << >>
43            nonassoc    named unary operators
44            nonassoc    < > <= >= lt gt le ge
45            nonassoc    == != <=> eq ne cmp ~~
46            left        &
47            left        | ^
48            left        &&
49            left        || //
50            nonassoc    ..  ...
51            right       ?:
52            right       = += -= *= etc. goto last next redo dump
53            left        , =>
54            nonassoc    list operators (rightward)
55            right       not
56            left        and
57            left        or xor
58
59            **=    +=    *=    &=    &.=    <<=    &&=
60                   -=    /=    |=    |.=    >>=    ||=
61                   .=    %=    ^=    ^.=           //=
62                         x=
63 END
64
65 our $perlop_re;
66
67 sub prep_perlop () {
68     my @ops;
69     foreach (split /\n/, $perlop_text) {
70         next unless m{\S};
71         s{\s+$}{};
72         s{^\s+}{};
73         s{^(?: left | right | nonassoc ) \s+}{}x;
74         next if m{^terms and list operators};
75         next if m{^named unary};
76         next if m{^list operators};
77         s{ and unary.*}{};
78         s{ etc\. }{ };
79         s{\?\:}{ ? : };
80         foreach my $op (split /\s+/) {
81             next unless length $op;
82             next if $op =~ m{^\w+$};
83             $op =~ s/\W/\\$&/g;
84             push @ops, $op;
85         }
86     }
87     $perlop_re = '(?: '.(join ' | ', @ops).' )';
88     $perlop_re = qr{$perlop_re}x;
89     #print STDERR "$perlop_re\n";
90 }
91
92 prep_perlop();
93
94 our ($ifilehead, $ihunkhead, $ichunkstart, $ichunkend);
95 our ($before, $after);
96
97 sub semiparse ($) {
98     ($_) = @_;
99     my @o;
100     # entries contain
101     #   T     type
102     #   E     exact input text (does not contain here doc contents)
103     #   P     something to print in messages
104     #   V     value, only for: heredoc string
105     #   Q     quote characcter, only for: heredoc string
106     for (;;) {
107         s{^\s+}{};
108         if (s{^[\$\@\%]?[_0-9a-zA-Z]+}{}) {
109             push @o, { T => 'ident', E => $&, P => $& };
110         } elsif (s{^\<\<(['"]?)([A-Z_]+)\1}{}) {
111             my ($q,$d) = ($1,$2);
112             $q //= '"';
113             push @o, { T => 'heredoc', Q => $q, Delim => $d,
114                        E => $&, P => "<<$q$d..." };
115             s{^
116                 (             .* \n    )
117                 ( (?: (?! $d) .* \n )* )
118                           $d     \n
119               }{ $1 }x or die "missing end of here doc $d\n";
120             $o[$#o]{V} = $2;
121         } elsif (s{^ (["'])( (?: [^\\'"]
122                                | \\ [^"']
123                                | (?! \1 ) [^"]
124                               )*
125                        \1 )}{}x) {
126             my ($q,$v) = ($1,$2);
127             push @o, { T => 'string', E => $&, P => "$q-string",
128                        Q => $q, V => $v};
129         } elsif (s{^$perlop_re|\;}{}) {
130             push @o, { T => 'op', E => $&, P => $& };
131         } elsif (s/[[{(]//) {
132             push @o, { T => 'bra', E => $&, P => $& };
133         } elsif (s/[]})]//) {
134             push @o, { T => 'ket', E => $&, P => $& };
135         } elsif (s/^( [\$\@\%] )( \{ )//x) {
136             push @o, { T => 'deref', E => $1, P => $1 },
137                      { T => 'bra',   E => $2, P => $2 };
138         } elsif (s/^ [\$\@\%] [^[^{] //x) {
139             push @o, { T => 'specvar', E => $&, P => $& };
140         } elsif (!length) {
141             last;
142         } else {
143             m{^.{0,10}};
144             die "cannot tokenise \`$&'";
145         }
146     }
147     # coalesce concatenated strings
148     return @o;
149 }           
150
151 sub analyse_chunk_core () {
152     die "plain deletion\n" unless defined $after;
153     die "plain insertion\n" unless defined $before;
154     my @xs = semiparse $before;
155     my @ys = semiparse $after;
156     my $next_something = sub {
157         my ($ary,$var,$what) = @_;
158         die "ran out of $what\n" unless @$ary;
159         $$var = shift @$ary;
160     };
161     my ($x,$y);
162     my $next_x = sub { $next_something->(\@xs, \$x, 'before'); };
163     my $next_y = sub { $next_something->(\@ys, \$y, 'after' ); };
164     my $is_string = sub { $_[0]{T} =~ m/heredoc|string/; };
165     for (;;) {
166         last unless @xs or @ys;
167         $next_x->();
168         $next_y->();
169         next if $x->{E} eq $y->{E};
170         my $string_changed;
171         my $ye = $y->{E};
172         if ($ye eq '__' or $ye eq 'f_') {
173             $next_y->();
174             die "__ on non-string $y->{P}\n"     unless $is_string->($y);
175             die "__ on was non-string $y->{P}\n" unless $is_string->($x);
176             if ($y->{Q} ne "'") {
177                 die "var subst in new string\n"
178                     if $y->{V} =~ m{(?<!\\) [\$\@]};
179             }
180             eval {
181                 die "__ string changed\n"       unless $y->{V} eq $x->{V};
182                 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
183             };
184             $string_changed = $@;
185         }
186         if ($ye eq '__') {
187             $_ = $y->{V};
188             die "percent $& in __ ' string\n" if m{\%};
189             die $string_changed if length $string_changed;
190             next;
191         }
192         if ($ye eq 'f_') {
193             my $fmt = $y->{V};
194             die "no percent in _f string\n" unless $fmt =~ m{\%};
195             next unless $string_changed;
196             die "f_ old string '-quoted\n" if length $x->{V};
197             my $xs = $x->{V};
198             my $exactly = sub {
199                 my ($lit) = @_;
200                 my $xl = substr($xs, 0, length($lit));
201                 die "exactly mismatch in $lit\n" unless $xl eq $lit;
202                 $xs = substr($xs, length($lit));
203             };
204             for (;;) {
205                 if ($fmt !~ m{\%[^\%]}) {
206                     $exactly->($fmt);
207                     $fmt = '';
208                     last;
209                 }
210                 $exactly->($`);
211                 $fmt = $';
212                 if ($& eq '%%') { $exactly->('%'); next; }
213                 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
214                 $next_y->();
215                 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
216                 if ($xs =~ m{^\@}) {
217                     $next_y->();
218                     die "\@... => not string" unless $is_string->($y);
219                     die "\@... => $y->{P}" if $y->{Q} ne '"';
220                     $exactly->($y->{V});
221                     next;
222                 }
223                 my $bras = 0;
224                 for (;;) {
225                     if (!$bras and !@ys) {
226                         last;
227                     }
228                     $next_y->();
229                     if (!$bras and
230                         (grep { $y->{E} eq $_ } qw( or xor and not ; : )
231                          or $y->{T} eq 'ket'
232                         )) {
233                         unshift @ys, $y;
234                         last;
235                     }
236                     $xs =~ s{^\s+}{};
237                     $exactly->($y->{E});
238                     if ($y->{T} eq 'bra' or $y->{L} eq '?') {
239                         $bras++;
240                     } elsif ($y->{T} eq 'ket' or $y->{L} eq ':') {
241                         die "too many kets at $y->{L}\n" unless $bras;
242                         $bras--;
243                     }
244                 }
245             }
246             next;
247         }
248         die "mismatch $x->{P} => $y->{P}\n";
249     }
250 }
251
252 sub analyse_chunk () {
253     for (;;) {
254         eval { analyse_chunk_core(); };
255         return unless length $@;
256         if ($@ =~ m{^missing end of here doc (\S+)\n}) {
257             # fudge this
258             $before .= "\n$1\n";
259             $after .= "\n$1\n";
260             next;
261         } else {
262             die $@;
263         }
264     }
265 }
266
267 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
268     m{^diff} or next;
269     while (l_ok $ifilehead and m{^diff|^index|^---|^\Q+++\E}) { $ifilehead++ }
270     $ihunkhead = $ifilehead;
271     while (l_ok $ihunkhead) {
272         m{^\@\@} or confess "$ihunkhead $_ ?";
273         my $i = $ihunkhead + 1;
274         for (; ; $i++) {
275             if (!l_ok $i or m{^ } or m{^\@\@}) {
276                 if (defined $ichunkstart) {
277                     $ichunkend = $i;
278                     eval { analyse_chunk(); 1; };
279                     if (length $@) {
280                         print Dumper('REPORT',
281                                      $ichunkstart, $ichunkend,
282                                      $before, $after,
283                                      $@);
284                     }
285                     $ichunkstart = $ichunkend = $before = $after = undef;
286                 }
287                 l_ok $i or last;
288                 m{^\@\@} and last;
289             } elsif (m{^[-+]}) {
290                 my $which = $& eq '-' ? \$before : \$after;
291                 $ichunkstart //= $i;
292                 $$which //= '';
293                 $$which .= $';
294             } else {
295                 confess "$i $_ ?";
296             }
297         }
298         $ichunkend = $i;
299         $ihunkhead = $i;
300     }
301 }