chiark / gitweb /
auditor wip rename @xs @ys from @before @after
[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_before = sub { $next_something->(\@xs, \$x, 'before'); };
163     my $next_after  = 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_before->();
168         $next_after->();
169         next if $x->{E} eq $y->{E};
170         my $string_changed;
171         if ($y->{E} eq '__' or $y->{E} eq '_f') {
172             $next_after->();
173             die "__ on non-string $y->{P}\n"     unless Sis_string->($y);
174             die "__ on was non-string $y->{P}\n" unless $is_string->($x);
175             if ($y->{Q} ne "'") {
176                 die "var subst in new string\n"
177                     if $y->{V} =~ m{(?<!\\) [\$\@]};
178             }
179             eval {
180                 die "__ string changed\n"       unless $y->{V} eq $x->{V};
181                 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
182             };
183             $string_changed = $@;
184         }
185         if ($y->{E} eq '__') {
186             $_ = $y->{V};
187             die "percent $& in __ ' string\n" if m{\%};
188             die $string_changed if length $string_changed;
189             next;
190         }
191         if ($y->{E} eq 'f_') {
192             my $fmt = $y->{V};
193             die "no percent in _f string\n" unless $fmt =~ m{\%};
194             next unless $string_changed;
195             die "f_ old string '-quoted\n" if length $x->{V};
196             my $xs = $x->{V};
197             my $exactly = sub {
198                 my ($lit) = @_;
199                 my $xl = substr($xs, 0, length($lit));
200                 die "exactly mismatch in $lit\n" unless $xl eq $lit;
201                 $xs = substr($xs, length($lit));
202             };
203             for (;;) {
204                 if ($fmt !~ m{\%[^\%]}) {
205                     $exactly->($fmt);
206                     $fmt = '';
207                     last;
208                 }
209                 $exactly->($`);
210                 $fmt = $';
211                 if ($& eq '%%') { $exactly->('%'); next; }
212                 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
213                 $next_after->();
214                 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
215                 if ($xs =~ m{^\@}) {
216                     $next_after->();
217                     die "\@... => not string" unless $is_string->($y);
218                     die "\@... => $y->{P}" if $y->{Q} ne '"';
219                     $exactly->($y->{V});
220                     next;
221                 }
222                 my $bras = 0;
223                 for (;;) {
224                     if (!$bras and !@ys) {
225                         last;
226                     }
227                     $next_after->();
228                     if (!$bras and
229                         (grep { $y->{E} eq $_ } qw( or xor and not ; : )
230                          or $y->{T} eq 'ket'
231                         )) {
232                         unshift @ys, $y;
233                         last;
234                     }
235                     $xs =~ s{^\s+}{};
236                     $exactly->($y->{E});
237                     if ($y->{T} eq 'bra' or $y->{L} eq '?') {
238                         $bras++;
239                     } elsif ($y->{T} eq 'ket' or $y->{L} eq ':') {
240                         die "too many kets at $y->{L}\n" unless $bras;
241                         $bras--;
242                     }
243                 }
244             }
245             next;
246         }
247         die "mismatch $x->{P} => $y->{P}\n";
248     }
249 }
250
251 sub analyse_chunk () {
252     for (;;) {
253         eval { analyse_chunk_core(); };
254         return unless length $@;
255         if ($@ =~ m{^missing end of here doc (\S+)\n}) {
256             # fudge this
257             $before .= "\n$1\n";
258             $after .= "\n$1\n";
259             next;
260         } else {
261             die $@;
262         }
263     }
264 }
265
266 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
267     m{^diff} or next;
268     while (l_ok $ifilehead and m{^diff|^index|^---|^\Q+++\E}) { $ifilehead++ }
269     $ihunkhead = $ifilehead;
270     while (l_ok $ihunkhead) {
271         m{^\@\@} or confess "$ihunkhead $_ ?";
272         my $i = $ihunkhead + 1;
273         for (; ; $i++) {
274             if (!l_ok $i or m{^ } or m{^\@\@}) {
275                 if (defined $ichunkstart) {
276                     $ichunkend = $i;
277                     eval { analyse_chunk(); 1; };
278                     if (length $@) {
279                         print Dumper('REPORT',
280                                      $ichunkstart, $ichunkend,
281                                      $before, $after,
282                                      $@);
283                     }
284                     $ichunkstart = $ichunkend = $before = $after = undef;
285                 }
286                 l_ok $i or last;
287                 m{^\@\@} and last;
288             } elsif (m{^[-+]}) {
289                 my $which = $& eq '-' ? \$before : \$after;
290                 $ichunkstart //= $i;
291                 $$which //= '';
292                 $$which .= $';
293             } else {
294                 confess "$i $_ ?";
295             }
296         }
297         $ichunkend = $i;
298         $ihunkhead = $i;
299     }
300 }