4 # Copyright (C)2018 Ian Jackson
5 # GPLv3+, NO WARRANTY, see below.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 GetOptions("debug|D+" => \$debug
32 push @{ $debug[$i] }, $s if $debug;
36 unshift @d, "# dummy line to make line 1 index 1 in \@d\n";
38 our $i_last_l_ok = -1;
39 our $count_i_last_l_ok;
44 if ($i == $i_last_l_ok) {
45 confess $i if $count_i_last_l_ok++ > 50;
47 $count_i_last_l_ok = 0;
51 return unless $i < @d;
53 #print STDERR "L $i\n";
59 confess $i unless l_ok $i;
62 our $perlop_text = <<'END'; # c&p from man perlop
63 left terms and list operators (leftward)
67 right ! ~ \ and unary + and -
72 nonassoc named unary operators
73 nonassoc < > <= >= lt gt le ge
74 nonassoc == != <=> eq ne cmp ~~
81 right = += -= *= etc. goto last next redo dump
83 nonassoc list operators (rightward)
88 **= += *= &= &.= <<= &&=
98 foreach (split /\n/, $perlop_text) {
102 s{^(?: left | right | nonassoc ) \s+}{}x;
103 next if m{^terms and list operators};
104 next if m{^named unary};
105 next if m{^list operators};
109 foreach my $op (split /\s+/) {
110 next unless length $op;
111 next if $op =~ m{^\w+$};
116 $perlop_re = '(?: '.(join ' | ', @ops).' )';
117 $perlop_re = qr{$perlop_re}x;
118 #print STDERR "$perlop_re\n";
123 our ($ifilehead, $ifirsthunkhead);
124 our ($ihunkhead, $ihunkend);
125 our ($ichunkstart, $ichunkend);
126 our ($before, $after);
128 sub is_string ($) { $_[0]{T} =~ m/heredoc|string/; }
129 sub is_trans ($) { grep { $_[0]{E} eq $_ } qw(__ f_ i_); }
146 # E exact input text (does not contain here doc contents)
147 # P something to print in messages
148 # V value, only for: heredoc string
149 # Q quote characcter, only for: heredoc string
152 if (s{^[\$\@\%]?[_0-9a-zA-Z]+}{}) {
153 push @o, { T => 'ident', E => $&, P => $& };
154 } elsif (s{^\<\<(['"]?)([A-Z_]+)\1}{}) {
155 my ($q,$d) = ($1,$2);
157 push @o, { T => 'heredoc', Q => $q, Delim => $d,
158 E => $&, P => "<<$q$d$q" };
161 ( (?: (?! $d \n ) .* \n )*? )
169 $o[$#o]{Invented} = 1;
171 } elsif (s{^ (["'])( (?: [^\\'"]
176 my ($q,$v) = ($1,$2);
177 push @o, { T => 'string', E => $&, P => "$q$q",
179 } elsif (s{^$perlop_re|^\;}{}) {
180 push @o, { T => 'op', E => $&, P => $& };
181 } elsif (s/^[[{(]//) {
182 push @o, { T => 'bra', E => $&, P => $& };
183 } elsif (s/^[]})]//) {
184 push @o, { T => 'ket', E => $&, P => $& };
185 } elsif (s/^( [\$\@\%] )( \{ )//x) {
186 push @o, { T => 'deref', E => $1, P => $1 },
187 { T => 'bra', E => $2, P => $2 };
188 } elsif (s/^ [\$\@\%] [^[^{] //x) {
189 push @o, { T => 'specvar', E => $&, P => $& };
194 die "cannot tokenise \`$&'";
197 for (my $i=0; $i+2 < @o; $i++) {
198 next unless $o[$i+1]{E} eq '.';
199 my @inputs = @o[$i, $i+2];
200 #print STDERR Dumper(\@inputs);
201 next if grep { !is_string($_) } @inputs;
202 my $q = $inputs[0]{Q};
203 next if grep { $_->{Q} ne $q } @inputs;
204 next if grep { $_->{Invented} } @inputs;
205 my $new = { T => 'joinedstrings',
206 E => (join '.', map { $_->{E} } @inputs),
207 P => (join '.', map { $_->{P} } @inputs),
208 V => (join '', map { $_->{V} } @inputs),
211 @o = (@o[0..$i-1], $new, @o[$i+3..$#o]);
212 $i--; # counteracts $i++
214 debug $ichunkstart, "semiparsed: ".join ' ', map { $_->{P} } @o;
215 # debug $ichunkstart, "semiparsed V: ".join ' ', map { defined $_->{V} ? ">$_->{V}<" : '-' } @o;
222 sub analyse_chunk_core () {
223 die "plain deletion\n" unless defined $after;
224 die "plain insertion\n" unless defined $before;
225 my @xs = semiparse $before;
226 my @ys = semiparse $after;
227 @analysed_x = @analysed_y = ();
228 my $next_something = sub {
229 my ($ary,$anal,$var,$what) = @_;
230 die "ran out of $what\n" unless @$ary;
232 push @$anal, $r->{P};
236 my $next_x = sub { $next_something->(\@xs, \@analysed_x, \$x, 'before'); };
237 my $next_y = sub { $next_something->(\@ys, \@analysed_y, \$y, 'after' ); };
238 our @y_expect_suffix = ();
240 while (my $e = shift @y_expect_suffix) {
243 or die "suffix mismatch, expected $e got $y->{E}\n";
245 last unless @xs or @ys;
248 next if $x->{E} eq $y->{E};
249 next if $x->{E} eq 'sprintf' and $y->{E} eq 'f_';
250 next if $x->{E} eq 'die' and $y->{E} eq 'confess';
251 next if $x->{E} eq 'die' and $y->{E} eq 'fail';
255 and is_trans($ys[1])) {
257 $next_y->(); # __ f_ i_
258 @y_expect_suffix = ')';
259 } elsif ($y->{E} eq '('
263 and (grep { $_ eq $analysed_y[-1] } (qw( => [ { ? : . ),
265 $next_y->(); # __ f_ i_
266 @y_expect_suffix = ')';
272 die "__ on non-string $y->{P}\n" unless is_string($y);
273 die "__ on was non-string $x->{P}\n" unless is_string($x);
274 if ($y->{Q} ne "'") {
275 die "var subst in new string\n"
276 if $y->{V} =~ m{(?<!\\) [\$\@]};
279 die "__ string changed\n" unless $y->{V} eq $x->{V};
280 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
282 $string_changed = $@;
286 die "percent $& in __ ' string\n" if m{\%};
287 die $string_changed if length $string_changed;
291 die $string_changed if length $string_changed;
296 die "no percent in f_ string\n" unless $fmt =~ m{\%};
297 next unless $string_changed;
298 die "f_ old string '-quoted\n" if $x->{Q} ne '"';
301 my ($lit, $what) = @_;
302 my $xl = substr($xs, 0, length($lit));
304 debug $ichunkstart, "not exactly x: ..".qp($xs);
305 debug $ichunkstart, "not exactly y: ".qp($lit);
306 my $next = @ys ? $ys[0]{P} : '(end)';
307 die "string contents mismatch near $what before $next\n";
309 $xs = substr($xs, length($lit));
312 #print STDERR Dumper($fmt, $xs, \@xs, @ys);
313 if ($fmt !~ m{\%[^\%]}) {
314 $exactly->($fmt, '(tail)');
316 die "text deleted from end of string: ".qp($xs)."\n"
320 $exactly->($`, '(literal)');
322 if ($& eq '%%') { $exactly->('%', '%%'); next; }
323 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
325 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
330 # X has "<earlier>" . <something>
331 # Y has "<earlier>%s" [other args] , <something>
332 $next_x->(); # eat the '.'
337 die "\@... => not string" unless is_string($y);
338 die "\@... => $y->{P}" if $y->{Q} ne '"';
339 $exactly->($y->{V}, $y->{P});
344 if (!$bras and !@ys) {
349 (grep { $y->{E} eq $_ } qw( or xor and not ; :
350 if unless while when )
354 # lookahead shows close of containing scope
355 # or lower precedence operator
360 $xs =~ s{^\s+}{} if $bras;
361 if (is_string($y) and $y->{Q} eq '"') {
362 $exactly->($y->{V}, $y->{P});
365 $exactly->($y->{E}, $y->{P});
366 if ($y->{T} eq 'bra' or $y->{E} eq '?') {
368 } elsif ($y->{T} eq 'ket' or $y->{E} eq ':') {
369 die "too many kets at $y->{E}\n" unless $bras;
376 die "mismatch $x->{P} => $y->{P}\n";
380 sub analyse_chunk () {
382 eval { analyse_chunk_core(); };
383 return unless length $@;
384 if ($@ =~ m{^missing end of here doc (\S+)\n}) {
386 # (this never happens now, but in the future we might
387 # want this code again eg to try adding to the chunk)
398 our $last_filehead = -1;
400 sub report_on_hunk () {
401 return unless @report;
402 if ($last_filehead != $ifilehead) {
403 foreach (my $i=$ifilehead; $i<$ifirsthunkhead; $i++) {
406 $last_filehead = $ifilehead;
408 my $dummy_r = { S => (scalar @d)+1, E => (scalar @d)+1 };
410 for (my $i=$ihunkhead; ; $i++) {
412 $r //= shift @report;
414 last if $i < $r->{E};
415 confess unless $r->{Done} == 03;
419 last unless $i<$ihunkend;
421 foreach my $ds (@{ $debug[$i] }) {
437 confess unless $r = $dummy_r;
440 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
442 $ifirsthunkhead = $ifilehead;
443 while (l_ok $ifirsthunkhead and
444 m{^diff|^index|^---|^\Q+++\E}) {
447 $ihunkhead = $ifirsthunkhead;
448 while (l_ok $ihunkhead) {
449 m{^\@\@} or confess "$ihunkhead $_ ?";
450 my $i = $ihunkhead + 1;
452 if (!l_ok $i or m{^ } or m{^\@\@}) {
453 if (defined $ichunkstart) {
455 eval { analyse_chunk(); 1; };
457 debug $ichunkstart, "done x: @analysed_x";
458 debug $ichunkstart, "done y: @analysed_y";
459 push @report, { M => $@,
463 $ichunkstart = $ichunkend = $before = $after = undef;
468 my $which = $& eq '-' ? \$before : \$after;