8 GetOptions("debug|D+" => \$debug
14 push @{ $debug[$i] }, $s if $debug;
18 unshift @d, "# dummy line to make line 1 index 1 in \@d\n";
20 our $i_last_l_ok = -1;
21 our $count_i_last_l_ok;
26 if ($i == $i_last_l_ok) {
27 confess $i if $count_i_last_l_ok++ > 50;
29 $count_i_last_l_ok = 0;
33 return unless $i < @d;
35 #print STDERR "L $i\n";
41 confess $i unless l_ok $i;
44 our $perlop_text = <<'END'; # c&p from man perlop
45 left terms and list operators (leftward)
49 right ! ~ \ and unary + and -
54 nonassoc named unary operators
55 nonassoc < > <= >= lt gt le ge
56 nonassoc == != <=> eq ne cmp ~~
63 right = += -= *= etc. goto last next redo dump
65 nonassoc list operators (rightward)
70 **= += *= &= &.= <<= &&=
80 foreach (split /\n/, $perlop_text) {
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};
91 foreach my $op (split /\s+/) {
92 next unless length $op;
93 next if $op =~ m{^\w+$};
98 $perlop_re = '(?: '.(join ' | ', @ops).' )';
99 $perlop_re = qr{$perlop_re}x;
100 #print STDERR "$perlop_re\n";
105 our ($ifilehead, $ifirsthunkhead);
106 our ($ihunkhead, $ihunkend);
107 our ($ichunkstart, $ichunkend);
108 our ($before, $after);
110 sub is_string ($) { $_[0]{T} =~ m/heredoc|string/; }
111 sub is_trans ($) { grep { $_[0]{E} eq $_ } qw(__ f_ i_); }
128 # E exact input text (does not contain here doc contents)
129 # P something to print in messages
130 # V value, only for: heredoc string
131 # Q quote characcter, only for: heredoc string
134 if (s{^[\$\@\%]?[_0-9a-zA-Z]+}{}) {
135 push @o, { T => 'ident', E => $&, P => $& };
136 } elsif (s{^\<\<(['"]?)([A-Z_]+)\1}{}) {
137 my ($q,$d) = ($1,$2);
139 push @o, { T => 'heredoc', Q => $q, Delim => $d,
140 E => $&, P => "<<$q$d$q" };
143 ( (?: (?! $d \n ) .* \n )*? )
151 $o[$#o]{Invented} = 1;
153 } elsif (s{^ (["'])( (?: [^\\'"]
158 my ($q,$v) = ($1,$2);
159 push @o, { T => 'string', E => $&, P => "$q$q",
161 } elsif (s{^$perlop_re|^\;}{}) {
162 push @o, { T => 'op', E => $&, P => $& };
163 } elsif (s/^[[{(]//) {
164 push @o, { T => 'bra', E => $&, P => $& };
165 } elsif (s/^[]})]//) {
166 push @o, { T => 'ket', E => $&, P => $& };
167 } elsif (s/^( [\$\@\%] )( \{ )//x) {
168 push @o, { T => 'deref', E => $1, P => $1 },
169 { T => 'bra', E => $2, P => $2 };
170 } elsif (s/^ [\$\@\%] [^[^{] //x) {
171 push @o, { T => 'specvar', E => $&, P => $& };
176 die "cannot tokenise \`$&'";
179 for (my $i=0; $i+2 < @o; $i++) {
180 next unless $o[$i+1]{E} eq '.';
181 my @inputs = @o[$i, $i+2];
182 #print STDERR Dumper(\@inputs);
183 next if grep { !is_string($_) } @inputs;
184 my $q = $inputs[0]{Q};
185 next if grep { $_->{Q} ne $q } @inputs;
186 next if grep { $_->{Invented} } @inputs;
187 my $new = { T => 'joinedstrings',
188 E => (join '.', map { $_->{E} } @inputs),
189 P => (join '.', map { $_->{P} } @inputs),
190 V => (join '', map { $_->{V} } @inputs),
193 @o = (@o[0..$i-1], $new, @o[$i+3..$#o]);
194 $i--; # counteracts $i++
196 debug $ichunkstart, "semiparsed: ".join ' ', map { $_->{P} } @o;
197 # debug $ichunkstart, "semiparsed V: ".join ' ', map { defined $_->{V} ? ">$_->{V}<" : '-' } @o;
204 sub analyse_chunk_core () {
205 die "plain deletion\n" unless defined $after;
206 die "plain insertion\n" unless defined $before;
207 my @xs = semiparse $before;
208 my @ys = semiparse $after;
209 @analysed_x = @analysed_y = ();
210 my $next_something = sub {
211 my ($ary,$anal,$var,$what) = @_;
212 die "ran out of $what\n" unless @$ary;
214 push @$anal, $r->{P};
218 my $next_x = sub { $next_something->(\@xs, \@analysed_x, \$x, 'before'); };
219 my $next_y = sub { $next_something->(\@ys, \@analysed_y, \$y, 'after' ); };
220 our @y_expect_suffix = ();
222 while (my $e = shift @y_expect_suffix) {
225 or die "suffix mismatch, expected $e got $y->{E}\n";
227 last unless @xs or @ys;
230 next if $x->{E} eq $y->{E};
231 next if $x->{E} eq 'sprintf' and $y->{E} eq 'f_';
232 next if $x->{E} eq 'die' and $y->{E} eq 'confess';
233 next if $x->{E} eq 'die' and $y->{E} eq 'fail';
237 and is_trans($ys[1])) {
239 $next_y->(); # __ f_ i_
240 @y_expect_suffix = ')';
241 } elsif ($y->{E} eq '('
245 and (grep { $_ eq $analysed_y[-1] } (qw( => [ { ? : . ),
247 $next_y->(); # __ f_ i_
248 @y_expect_suffix = ')';
254 die "__ on non-string $y->{P}\n" unless is_string($y);
255 die "__ on was non-string $x->{P}\n" unless is_string($x);
256 if ($y->{Q} ne "'") {
257 die "var subst in new string\n"
258 if $y->{V} =~ m{(?<!\\) [\$\@]};
261 die "__ string changed\n" unless $y->{V} eq $x->{V};
262 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
264 $string_changed = $@;
268 die "percent $& in __ ' string\n" if m{\%};
269 die $string_changed if length $string_changed;
273 die $string_changed if length $string_changed;
278 die "no percent in f_ string\n" unless $fmt =~ m{\%};
279 next unless $string_changed;
280 die "f_ old string '-quoted\n" if $x->{Q} ne '"';
283 my ($lit, $what) = @_;
284 my $xl = substr($xs, 0, length($lit));
286 debug $ichunkstart, "not exactly x: ..".qp($xs);
287 debug $ichunkstart, "not exactly y: ".qp($lit);
288 my $next = @ys ? $ys[0]{P} : '(end)';
289 die "string contents mismatch near $what before $next\n";
291 $xs = substr($xs, length($lit));
294 #print STDERR Dumper($fmt, $xs, \@xs, @ys);
295 if ($fmt !~ m{\%[^\%]}) {
296 $exactly->($fmt, '(tail)');
298 die "text deleted from end of string: ".qp($xs)."\n"
302 $exactly->($`, '(literal)');
304 if ($& eq '%%') { $exactly->('%', '%%'); next; }
305 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
307 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
312 # X has "<earlier>" . <something>
313 # Y has "<earlier>%s" [other args] , <something>
314 $next_x->(); # eat the '.'
319 die "\@... => not string" unless is_string($y);
320 die "\@... => $y->{P}" if $y->{Q} ne '"';
321 $exactly->($y->{V}, $y->{P});
326 if (!$bras and !@ys) {
331 (grep { $y->{E} eq $_ } qw( or xor and not ; :
332 if unless while when )
336 # lookahead shows close of containing scope
337 # or lower precedence operator
342 $xs =~ s{^\s+}{} if $bras;
343 if (is_string($y) and $y->{Q} eq '"') {
344 $exactly->($y->{V}, $y->{P});
347 $exactly->($y->{E}, $y->{P});
348 if ($y->{T} eq 'bra' or $y->{E} eq '?') {
350 } elsif ($y->{T} eq 'ket' or $y->{E} eq ':') {
351 die "too many kets at $y->{E}\n" unless $bras;
358 die "mismatch $x->{P} => $y->{P}\n";
362 sub analyse_chunk () {
364 eval { analyse_chunk_core(); };
365 return unless length $@;
366 if ($@ =~ m{^missing end of here doc (\S+)\n}) {
368 # (this never happens now, but in the future we might
369 # want this code again eg to try adding to the chunk)
380 our $last_filehead = -1;
382 sub report_on_hunk () {
383 return unless @report;
384 if ($last_filehead != $ifilehead) {
385 foreach (my $i=$ifilehead; $i<$ifirsthunkhead; $i++) {
388 $last_filehead = $ifilehead;
390 my $dummy_r = { S => (scalar @d)+1, E => (scalar @d)+1 };
392 for (my $i=$ihunkhead; ; $i++) {
394 $r //= shift @report;
396 last if $i < $r->{E};
397 confess unless $r->{Done} == 03;
401 last unless $i<$ihunkend;
403 foreach my $ds (@{ $debug[$i] }) {
419 confess unless $r = $dummy_r;
422 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
424 $ifirsthunkhead = $ifilehead;
425 while (l_ok $ifirsthunkhead and
426 m{^diff|^index|^---|^\Q+++\E}) {
429 $ihunkhead = $ifirsthunkhead;
430 while (l_ok $ihunkhead) {
431 m{^\@\@} or confess "$ihunkhead $_ ?";
432 my $i = $ihunkhead + 1;
434 if (!l_ok $i or m{^ } or m{^\@\@}) {
435 if (defined $ichunkstart) {
437 eval { analyse_chunk(); 1; };
439 debug $ichunkstart, "done x: @analysed_x";
440 debug $ichunkstart, "done y: @analysed_y";
441 push @report, { M => $@,
445 $ichunkstart = $ichunkend = $before = $after = undef;
450 my $which = $& eq '-' ? \$before : \$after;