chiark / gitweb /
auditor wip semiparse here doc
[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         foreach my $op (split /\s+/) {
80             next unless length $op;
81             next if $op =~ m{^\w+$};
82             $op =~ s/\W/\\$&/g;
83             push @ops, $op;
84         }
85     }
86     $perlop_re = '(?: '.(join ' | ', @ops).' )';
87     $perlop_re = qr{$perlop_re}x;
88     #print STDERR "$perlop_re\n";
89 }
90
91 prep_perlop();
92
93 our ($ifilehead, $ihunkhead, $ichunkstart, $ichunkend);
94 our ($before, $after);
95
96 sub semiparse ($) {
97     ($_) = @_;
98     my @o;
99     for (;;) {
100         s{^\s+}{};
101         if (s{^[\$\@\%]?[_0-9a-zA-Z]+}{}) {
102             push @o, { T => 'ident', L => $& };
103         } elsif (s{^\<\<('?)([A-Z_]+)\1}{}) {
104             my ($q,$d) = ($1,$2);
105             push @o, { T => 'heredoc', Q => $q, Delim => $d };
106             s{^
107                 (             .* \n    )
108                 ( (?: (?! $d) .* \n )* )
109               }{ $1 } or die "missing end of here doc $d\n";
110             $o[$#o]{V} = $2;
111         } elsif (s{^ (["'])( (?:  [^\\] | \\ \1  )* )}{}x) {
112             my ($q,$v) = ($1,$2);
113             push @o, { T => 'string', Q => $q, V => $v };
114         } elsif (s{^$perlop_re}{}) {
115             push @o, { T => 'op', L => $& };
116         } elsif (s/[[{(]//) {
117             push @o, { T => 'bra', L => $& };
118         } elsif (s/[]})]//) {
119             push @o, { T => 'ket', L => $& };
120         } elsif (!length) {
121             last;
122         } else {
123             m{^.{0,10}};
124             die "cannot tokenise \`$&'";
125         }
126     }
127     return @o;
128 }           
129
130 sub analyse_chunk_core () {
131     die "plain deletion\n" unless defined $after;
132     die "plain insertion\n" unless defined $before;
133     my @before = semiparse $before;
134     my @after = semiparse $after;
135     print Dumper($ichunkstart, $ichunkend, \@before, \@after);
136     flush STDOUT;
137 }
138
139 sub analyse_chunk () {
140     for (;;) {
141         eval { analyse_chunk_core(); };
142         return unless length $@;
143         if ($@ =~ m{^missing end of here doc (\S+)\n}) {
144             # fudge this
145             $before .= "\n$1\n";
146             $after .= "\n$1\n";
147             analyse_chunk_core();
148         } else {
149             die $@;
150         }
151     }
152 }
153
154 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
155     m{^diff} or next;
156     while (l_ok $ifilehead and m{^diff|^index|^---|^\Q+++\E}) { $ifilehead++ }
157     $ihunkhead = $ifilehead;
158     while (l_ok $ihunkhead) {
159         m{^\@\@} or confess "$ihunkhead $_ ?";
160         my $i = $ihunkhead + 1;
161         for (; ; $i++) {
162             if (!l_ok $i or m{^ } or m{^\@\@}) {
163                 if (defined $ichunkstart) {
164                     $ichunkend = $i;
165                     eval { analyse_chunk(); 1; };
166                     if (length $@) {
167                         print Dumper($ichunkstart, $ichunkend,
168                                      $before, $after,
169                                      $@);
170                     }
171                     $ichunkstart = $ichunkend = $before = $after = undef;
172                 }
173                 l_ok $i or last;
174                 m{^\@\@} and last;
175             } elsif (m{^[-+]}) {
176                 my $which = $& eq '-' ? \$before : \$after;
177                 $ichunkstart //= $i;
178                 $$which //= '';
179                 $$which .= $';
180             } else {
181                 confess "$i $_ ?";
182             }
183         }
184         $ichunkend = $i;
185         $ihunkhead = $i;
186     }
187 }