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=@o-2; $i>0; --$i) {
164 next unless $o[$i+1]{E} eq '.';
165 my @inputs = @o[$i, $i+2];
166 next if grep { !is_string($_) } @inputs;
167 my $q = $inputs[0]{Q};
168 next if grep { $_->{Q} ne $q } @inputs;
169 my $new = { T => 'joinedstrings',
170 E => (join '.', map { $_->{E} } @inputs),
171 P => (join '.', map { $_->{P} } @inputs),
172 V => (join '', map { $_->{V} } @inputs),
175 @o = (@o[0..$i-1], $new, @o[$i+3..$#o]);
177 debug $ichunkstart, "semiparsed: ".join ' ', map { $_->{P} } @o;
184 sub analyse_chunk_core () {
185 die "plain deletion\n" unless defined $after;
186 die "plain insertion\n" unless defined $before;
187 my @xs = semiparse $before;
188 my @ys = semiparse $after;
189 @analysed_x = @analysed_y = ();
190 my $next_something = sub {
191 my ($ary,$anal,$var,$what) = @_;
192 die "ran out of $what\n" unless @$ary;
194 push @$anal, $r->{P};
198 my $next_x = sub { $next_something->(\@xs, \@analysed_x, \$x, 'before'); };
199 my $next_y = sub { $next_something->(\@ys, \@analysed_y, \$y, 'after' ); };
200 our @y_expect_suffix = ();
202 while (my $e = shift @y_expect_suffix) {
205 or die "suffix mismatch, expected $e got $y->{E}\n";
207 last unless @xs or @ys;
210 next if $x->{E} eq $y->{E};
214 and is_trans($ys[1])) {
217 @y_expect_suffix = ')';
223 die "__ on non-string $y->{P}\n" unless is_string($y);
224 die "__ on was non-string $y->{P}\n" unless is_string($x);
225 if ($y->{Q} ne "'") {
226 die "var subst in new string\n"
227 if $y->{V} =~ m{(?<!\\) [\$\@]};
230 die "__ string changed\n" unless $y->{V} eq $x->{V};
231 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
233 $string_changed = $@;
237 die "percent $& in __ ' string\n" if m{\%};
238 die $string_changed if length $string_changed;
242 die $string_changed if length $string_changed;
247 die "no percent in _f string\n" unless $fmt =~ m{\%};
248 next unless $string_changed;
249 die "f_ old string '-quoted\n" if $x->{Q} ne '"';
252 my ($lit, $what) = @_;
253 my $xl = substr($xs, 0, length($lit));
255 debug $ichunkstart, "not exactly x: ..\"$xs\"";
256 debug $ichunkstart, "not exactly y: $lit";
257 my $next = @ys ? $ys[0]{P} : '(end)';
258 die "string contents mismatch near $what before $next\n";
260 $xs = substr($xs, length($lit));
263 if ($fmt !~ m{\%[^\%]}) {
264 $exactly->($fmt, '(tail)');
268 $exactly->($`, '(literal)');
270 if ($& eq '%%') { $exactly->('%', '%%'); next; }
271 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
273 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
276 die "\@... => not string" unless is_string($y);
277 die "\@... => $y->{P}" if $y->{Q} ne '"';
278 $exactly->($y->{V}, $y->{P});
283 if (!$bras and !@ys) {
288 (grep { $y->{E} eq $_ } qw( or xor and not ; :
289 if unless while when )
293 # lookahead shows close of containing scope
294 # or lower precedence operator
300 $exactly->($y->{E}, $y->{P});
301 if ($y->{T} eq 'bra' or $y->{E} eq '?') {
303 } elsif ($y->{T} eq 'ket' or $y->{E} eq ':') {
304 die "too many kets at $y->{E}\n" unless $bras;
311 die "mismatch $x->{P} => $y->{P}\n";
315 sub analyse_chunk () {
317 eval { analyse_chunk_core(); };
318 return unless length $@;
319 if ($@ =~ m{^missing end of here doc (\S+)\n}) {
331 our $last_filehead = -1;
333 sub report_on_hunk () {
334 return unless @report;
335 if ($last_filehead != $ifilehead) {
336 foreach (my $i=$ifilehead; $i<$ifirsthunkhead; $i++) {
339 $last_filehead = $ifilehead;
341 my $dummy_r = { S => (scalar @d)+1, E => (scalar @d)+1 };
343 for (my $i=$ihunkhead; ; $i++) {
345 $r //= shift @report;
347 last if $i < $r->{E};
348 confess unless $r->{Done} == 03;
352 last unless $i<$ihunkend;
354 foreach my $ds (@{ $debug[$i] }) {
370 confess unless $r = $dummy_r;
373 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
375 $ifirsthunkhead = $ifilehead;
376 while (l_ok $ifirsthunkhead and
377 m{^diff|^index|^---|^\Q+++\E}) {
380 $ihunkhead = $ifirsthunkhead;
381 while (l_ok $ihunkhead) {
382 m{^\@\@} or confess "$ihunkhead $_ ?";
383 my $i = $ihunkhead + 1;
385 if (!l_ok $i or m{^ } or m{^\@\@}) {
386 if (defined $ichunkstart) {
388 eval { analyse_chunk(); 1; };
390 debug $ichunkstart, "done x: @analysed_x";
391 debug $ichunkstart, "done y: @analysed_y";
392 push @report, { M => $@,
396 $ichunkstart = $ichunkend = $before = $after = undef;
401 my $which = $& eq '-' ? \$before : \$after;