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_); }
118 # E exact input text (does not contain here doc contents)
119 # P something to print in messages
120 # V value, only for: heredoc string
121 # Q quote characcter, only for: heredoc string
124 if (s{^[\$\@\%]?[_0-9a-zA-Z]+}{}) {
125 push @o, { T => 'ident', E => $&, P => $& };
126 } elsif (s{^\<\<(['"]?)([A-Z_]+)\1}{}) {
127 my ($q,$d) = ($1,$2);
129 push @o, { T => 'heredoc', Q => $q, Delim => $d,
130 E => $&, P => "<<$q$d$q" };
133 ( (?: (?! $d) .* \n )* )
135 }{ $1 }x or die "missing end of here doc $d\n";
137 } elsif (s{^ (["'])( (?: [^\\'"]
142 my ($q,$v) = ($1,$2);
143 push @o, { T => 'string', E => $&, P => "$q$q",
145 } elsif (s{^$perlop_re|^\;}{}) {
146 push @o, { T => 'op', E => $&, P => $& };
147 } elsif (s/^[[{(]//) {
148 push @o, { T => 'bra', E => $&, P => $& };
149 } elsif (s/^[]})]//) {
150 push @o, { T => 'ket', E => $&, P => $& };
151 } elsif (s/^( [\$\@\%] )( \{ )//x) {
152 push @o, { T => 'deref', E => $1, P => $1 },
153 { T => 'bra', E => $2, P => $2 };
154 } elsif (s/^ [\$\@\%] [^[^{] //x) {
155 push @o, { T => 'specvar', E => $&, P => $& };
160 die "cannot tokenise \`$&'";
163 for (my $i=0; $i+2 < @o; $i++) {
164 next unless $o[$i+1]{E} eq '.';
165 my @inputs = @o[$i, $i+2];
166 #print STDERR Dumper(\@inputs);
167 next if grep { !is_string($_) } @inputs;
168 my $q = $inputs[0]{Q};
169 next if grep { $_->{Q} ne $q } @inputs;
170 my $new = { T => 'joinedstrings',
171 E => (join '.', map { $_->{E} } @inputs),
172 P => (join '.', map { $_->{P} } @inputs),
173 V => (join '', map { $_->{V} } @inputs),
176 @o = (@o[0..$i-1], $new, @o[$i+3..$#o]);
177 $i--; # counteracts $i++
179 debug $ichunkstart, "semiparsed: ".join ' ', map { $_->{P} } @o;
186 sub analyse_chunk_core () {
187 die "plain deletion\n" unless defined $after;
188 die "plain insertion\n" unless defined $before;
189 my @xs = semiparse $before;
190 my @ys = semiparse $after;
191 @analysed_x = @analysed_y = ();
192 my $next_something = sub {
193 my ($ary,$anal,$var,$what) = @_;
194 die "ran out of $what\n" unless @$ary;
196 push @$anal, $r->{P};
200 my $next_x = sub { $next_something->(\@xs, \@analysed_x, \$x, 'before'); };
201 my $next_y = sub { $next_something->(\@ys, \@analysed_y, \$y, 'after' ); };
202 our @y_expect_suffix = ();
204 while (my $e = shift @y_expect_suffix) {
207 or die "suffix mismatch, expected $e got $y->{E}\n";
209 last unless @xs or @ys;
212 next if $x->{E} eq $y->{E};
213 next if $x->{E} eq 'sprintf' and $y->{E} eq 'f_';
214 next if $x->{E} eq 'die' and $y->{E} eq 'confess';
218 and is_trans($ys[1])) {
221 @y_expect_suffix = ')';
227 die "__ on non-string $y->{P}\n" unless is_string($y);
228 die "__ on was non-string $y->{P}\n" unless is_string($x);
229 if ($y->{Q} ne "'") {
230 die "var subst in new string\n"
231 if $y->{V} =~ m{(?<!\\) [\$\@]};
234 die "__ string changed\n" unless $y->{V} eq $x->{V};
235 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
237 $string_changed = $@;
241 die "percent $& in __ ' string\n" if m{\%};
242 die $string_changed if length $string_changed;
246 die $string_changed if length $string_changed;
251 die "no percent in _f string\n" unless $fmt =~ m{\%};
252 next unless $string_changed;
253 die "f_ old string '-quoted\n" if $x->{Q} ne '"';
256 my ($lit, $what) = @_;
257 my $xl = substr($xs, 0, length($lit));
259 debug $ichunkstart, "not exactly x: \"..$xs\"";
260 debug $ichunkstart, "not exactly y: $lit";
261 my $next = @ys ? $ys[0]{P} : '(end)';
262 die "string contents mismatch near $what before $next\n";
264 $xs = substr($xs, length($lit));
267 if ($fmt !~ m{\%[^\%]}) {
268 $exactly->($fmt, '(tail)');
272 $exactly->($`, '(literal)');
274 if ($& eq '%%') { $exactly->('%', '%%'); next; }
275 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
277 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
282 # X has "<earlier>" . <something>
283 # Y has "<earlier>%s" [other args] , <something>
284 $next_x->(); # eat the '.'
289 die "\@... => not string" unless is_string($y);
290 die "\@... => $y->{P}" if $y->{Q} ne '"';
291 $exactly->($y->{V}, $y->{P});
296 if (!$bras and !@ys) {
301 (grep { $y->{E} eq $_ } qw( or xor and not ; :
302 if unless while when )
306 # lookahead shows close of containing scope
307 # or lower precedence operator
312 $xs =~ s{^\s+}{} if $bras;
313 $exactly->($y->{E}, $y->{P});
314 if ($y->{T} eq 'bra' or $y->{E} eq '?') {
316 } elsif ($y->{T} eq 'ket' or $y->{E} eq ':') {
317 die "too many kets at $y->{E}\n" unless $bras;
324 die "mismatch $x->{P} => $y->{P}\n";
328 sub analyse_chunk () {
330 eval { analyse_chunk_core(); };
331 return unless length $@;
332 if ($@ =~ m{^missing end of here doc (\S+)\n}) {
344 our $last_filehead = -1;
346 sub report_on_hunk () {
347 return unless @report;
348 if ($last_filehead != $ifilehead) {
349 foreach (my $i=$ifilehead; $i<$ifirsthunkhead; $i++) {
352 $last_filehead = $ifilehead;
354 my $dummy_r = { S => (scalar @d)+1, E => (scalar @d)+1 };
356 for (my $i=$ihunkhead; ; $i++) {
358 $r //= shift @report;
360 last if $i < $r->{E};
361 confess unless $r->{Done} == 03;
365 last unless $i<$ihunkend;
367 foreach my $ds (@{ $debug[$i] }) {
383 confess unless $r = $dummy_r;
386 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
388 $ifirsthunkhead = $ifilehead;
389 while (l_ok $ifirsthunkhead and
390 m{^diff|^index|^---|^\Q+++\E}) {
393 $ihunkhead = $ifirsthunkhead;
394 while (l_ok $ihunkhead) {
395 m{^\@\@} or confess "$ihunkhead $_ ?";
396 my $i = $ihunkhead + 1;
398 if (!l_ok $i or m{^ } or m{^\@\@}) {
399 if (defined $ichunkstart) {
401 eval { analyse_chunk(); 1; };
403 debug $ichunkstart, "done x: @analysed_x";
404 debug $ichunkstart, "done y: @analysed_y";
405 push @report, { M => $@,
409 $ichunkstart = $ichunkend = $before = $after = undef;
414 my $which = $& eq '-' ? \$before : \$after;