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/; };
117 # E exact input text (does not contain here doc contents)
118 # P something to print in messages
119 # V value, only for: heredoc string
120 # Q quote characcter, only for: heredoc string
123 if (s{^[\$\@\%]?[_0-9a-zA-Z]+}{}) {
124 push @o, { T => 'ident', E => $&, P => $& };
125 } elsif (s{^\<\<(['"]?)([A-Z_]+)\1}{}) {
126 my ($q,$d) = ($1,$2);
128 push @o, { T => 'heredoc', Q => $q, Delim => $d,
129 E => $&, P => "<<$q$d$q" };
132 ( (?: (?! $d) .* \n )* )
134 }{ $1 }x or die "missing end of here doc $d\n";
136 } elsif (s{^ (["'])( (?: [^\\'"]
141 my ($q,$v) = ($1,$2);
142 push @o, { T => 'string', E => $&, P => "$q-string",
144 } elsif (s{^$perlop_re|^\;}{}) {
145 push @o, { T => 'op', E => $&, P => $& };
146 } elsif (s/[[{(]//) {
147 push @o, { T => 'bra', E => $&, P => $& };
148 } elsif (s/[]})]//) {
149 push @o, { T => 'ket', E => $&, P => $& };
150 } elsif (s/^( [\$\@\%] )( \{ )//x) {
151 push @o, { T => 'deref', E => $1, P => $1 },
152 { T => 'bra', E => $2, P => $2 };
153 } elsif (s/^ [\$\@\%] [^[^{] //x) {
154 push @o, { T => 'specvar', E => $&, P => $& };
159 die "cannot tokenise \`$&'";
162 for (my $i=@o-2; $i>0; --$i) {
163 next unless $o[$i+1]{E} eq '.';
164 my @inputs = @o[$i, $i+2];
165 next if grep { !is_string($_) } @inputs;
166 my $q = $inputs[0]{Q};
167 next if grep { $_->{Q} ne $q } @inputs;
168 my $new = { T => 'joinedstrings',
169 E => (join '.', map { $_->{E} } @inputs),
170 P => (join '.', map { $_->{P} } @inputs),
171 V => (join '', map { $_->{V} } @inputs),
174 @o = (@o[0..$i-1], $new, @o[$i+3..$#o]);
175 print STDERR Dumper(\@o);
177 debug $ichunkstart, "semiparsed: ".join ' ', map { $_->{P} } @o;
181 sub analyse_chunk_core () {
182 die "plain deletion\n" unless defined $after;
183 die "plain insertion\n" unless defined $before;
184 my @xs = semiparse $before;
185 my @ys = semiparse $after;
186 my $next_something = sub {
187 my ($ary,$var,$what) = @_;
188 die "ran out of $what\n" unless @$ary;
192 my $next_x = sub { $next_something->(\@xs, \$x, 'before'); };
193 my $next_y = sub { $next_something->(\@ys, \$y, 'after' ); };
195 last unless @xs or @ys;
198 next if $x->{E} eq $y->{E};
201 if ($ye eq '__' or $ye eq 'f_') {
203 die "__ on non-string $y->{P}\n" unless is_string($y);
204 die "__ on was non-string $y->{P}\n" unless is_string($x);
205 if ($y->{Q} ne "'") {
206 die "var subst in new string\n"
207 if $y->{V} =~ m{(?<!\\) [\$\@]};
210 die "__ string changed\n" unless $y->{V} eq $x->{V};
211 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
213 $string_changed = $@;
217 die "percent $& in __ ' string\n" if m{\%};
218 die $string_changed if length $string_changed;
223 die "no percent in _f string\n" unless $fmt =~ m{\%};
224 next unless $string_changed;
225 die "f_ old string '-quoted\n" if $x->{Q} ne '"';
228 my ($lit, $what) = @_;
229 my $xl = substr($xs, 0, length($lit));
231 debug $ichunkstart, "not exactly x: $xl";
232 debug $ichunkstart, "not exactly y: $lit";
233 my $next = @ys ? $ys[0]{P} : '(end)';
234 die "string contents mismatch near $what before $next\n";
236 $xs = substr($xs, length($lit));
239 if ($fmt !~ m{\%[^\%]}) {
240 $exactly->($fmt, '(tail)');
244 $exactly->($`, '(literal)');
246 if ($& eq '%%') { $exactly->('%', '%%'); next; }
247 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
249 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
252 die "\@... => not string" unless is_string($y);
253 die "\@... => $y->{P}" if $y->{Q} ne '"';
254 $exactly->($y->{V}, $y->{P});
259 if (!$bras and !@ys) {
264 (grep { $y->{E} eq $_ } qw( or xor and not ; : )
271 #debug $ichunkstart, "TOKEN $y->{P}\n";
272 $exactly->($y->{E}, $y->{P});
273 if ($y->{T} eq 'bra' or $y->{E} eq '?') {
275 } elsif ($y->{T} eq 'ket' or $y->{E} eq ':') {
276 die "too many kets at $y->{E}\n" unless $bras;
283 die "mismatch $x->{P} => $y->{P}\n";
287 sub analyse_chunk () {
289 eval { analyse_chunk_core(); };
290 return unless length $@;
291 if ($@ =~ m{^missing end of here doc (\S+)\n}) {
303 our $last_filehead = -1;
305 sub report_on_hunk () {
306 return unless @report;
307 if ($last_filehead != $ifilehead) {
308 foreach (my $i=$ifilehead; $i<$ifirsthunkhead; $i++) {
311 $last_filehead = $ifilehead;
313 my $dummy_r = { S => (scalar @d)+1, E => (scalar @d)+1 };
315 for (my $i=$ihunkhead; ; $i++) {
317 $r //= shift @report;
319 last if $i < $r->{E};
320 confess unless $r->{Done} == 03;
324 last unless $i<$ihunkend;
326 foreach my $ds (@{ $debug[$i] }) {
342 confess unless $r = $dummy_r;
345 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
347 $ifirsthunkhead = $ifilehead;
348 while (l_ok $ifirsthunkhead and
349 m{^diff|^index|^---|^\Q+++\E}) {
352 $ihunkhead = $ifirsthunkhead;
353 while (l_ok $ihunkhead) {
354 m{^\@\@} or confess "$ihunkhead $_ ?";
355 my $i = $ihunkhead + 1;
357 if (!l_ok $i or m{^ } or m{^\@\@}) {
358 if (defined $ichunkstart) {
360 eval { analyse_chunk(); 1; };
362 push @report, { M => $@,
366 $ichunkstart = $ichunkend = $before = $after = undef;
371 my $which = $& eq '-' ? \$before : \$after;