7 unshift @d, "# dummy line to make line 1 index 1 in \@d\n";
10 our $count_i_last_l_ok;
15 if ($i == $i_last_l_ok) {
16 confess $i if $count_i_last_l_ok++ > 50;
18 $count_i_last_l_ok = 0;
22 return unless $i < @d;
24 #print STDERR "L $i\n";
30 confess $i unless l_ok $i;
33 our $perlop_text = <<'END'; # c&p from man perlop
34 left terms and list operators (leftward)
38 right ! ~ \ and unary + and -
43 nonassoc named unary operators
44 nonassoc < > <= >= lt gt le ge
45 nonassoc == != <=> eq ne cmp ~~
52 right = += -= *= etc. goto last next redo dump
54 nonassoc list operators (rightward)
59 **= += *= &= &.= <<= &&=
69 foreach (split /\n/, $perlop_text) {
73 s{^(?: left | right | nonassoc ) \s+}{}x;
74 next if m{^terms and list operators};
75 next if m{^named unary};
76 next if m{^list operators};
80 foreach my $op (split /\s+/) {
81 next unless length $op;
82 next if $op =~ m{^\w+$};
87 $perlop_re = '(?: '.(join ' | ', @ops).' )';
88 $perlop_re = qr{$perlop_re}x;
89 #print STDERR "$perlop_re\n";
94 our ($ifilehead, $ihunkhead, $ichunkstart, $ichunkend);
95 our ($before, $after);
102 # E exact input text (does not contain here doc contents)
103 # P something to print in messages
104 # V value, only for: heredoc string
105 # Q quote characcter, only for: heredoc string
108 if (s{^[\$\@\%]?[_0-9a-zA-Z]+}{}) {
109 push @o, { T => 'ident', E => $&, P => $& };
110 } elsif (s{^\<\<(['"]?)([A-Z_]+)\1}{}) {
111 my ($q,$d) = ($1,$2);
113 push @o, { T => 'heredoc', Q => $q, Delim => $d,
114 E => $&, P => "<<$q$d..." };
117 ( (?: (?! $d) .* \n )* )
119 }{ $1 }x or die "missing end of here doc $d\n";
121 } elsif (s{^ (["'])( (?: [^\\'"]
126 my ($q,$v) = ($1,$2);
127 push @o, { T => 'string', E => $&, P => "$q-string",
129 } elsif (s{^$perlop_re|\;}{}) {
130 push @o, { T => 'op', E => $&, P => $& };
131 } elsif (s/[[{(]//) {
132 push @o, { T => 'bra', E => $&, P => $& };
133 } elsif (s/[]})]//) {
134 push @o, { T => 'ket', E => $&, P => $& };
135 } elsif (s/^( [\$\@\%] )( \{ )//x) {
136 push @o, { T => 'deref', E => $1, P => $1 },
137 { T => 'bra', E => $2, P => $2 };
138 } elsif (s/^ [\$\@\%] [^[^{] //x) {
139 push @o, { T => 'specvar', E => $&, P => $& };
144 die "cannot tokenise \`$&'";
147 # coalesce concatenated strings
151 sub analyse_chunk_core () {
152 die "plain deletion\n" unless defined $after;
153 die "plain insertion\n" unless defined $before;
154 my @xs = semiparse $before;
155 my @ys = semiparse $after;
156 my $next_something = sub {
157 my ($ary,$var,$what) = @_;
158 die "ran out of $what\n" unless @$ary;
162 my $next_before = sub { $next_something->(\@xs, \$x, 'before'); };
163 my $next_after = sub { $next_something->(\@ys , \$y, 'after' ); };
164 my $is_string = sub { $_[0]{T} =~ m/heredoc|string/; };
166 last unless @xs or @ys;
169 next if $x->{E} eq $y->{E};
171 if ($y->{E} eq '__' or $y->{E} eq '_f') {
173 die "__ on non-string $y->{P}\n" unless Sis_string->($y);
174 die "__ on was non-string $y->{P}\n" unless $is_string->($x);
175 if ($y->{Q} ne "'") {
176 die "var subst in new string\n"
177 if $y->{V} =~ m{(?<!\\) [\$\@]};
180 die "__ string changed\n" unless $y->{V} eq $x->{V};
181 die "__ string quote changed\n" unless $y->{Q} eq $x->{Q};
183 $string_changed = $@;
185 if ($y->{E} eq '__') {
187 die "percent $& in __ ' string\n" if m{\%};
188 die $string_changed if length $string_changed;
191 if ($y->{E} eq 'f_') {
193 die "no percent in _f string\n" unless $fmt =~ m{\%};
194 next unless $string_changed;
195 die "f_ old string '-quoted\n" if length $x->{V};
199 my $xl = substr($xs, 0, length($lit));
200 die "exactly mismatch in $lit\n" unless $xl eq $lit;
201 $xs = substr($xs, length($lit));
204 if ($fmt !~ m{\%[^\%]}) {
211 if ($& eq '%%') { $exactly->('%'); next; }
212 elsif ($& ne '%s') { die "unhandled %-subst $&\n"; }
214 die "expected comma, got $y->{P}\n" unless $y->{E} eq ',';
217 die "\@... => not string" unless $is_string->($y);
218 die "\@... => $y->{P}" if $y->{Q} ne '"';
224 if (!$bras and !@ys) {
229 (grep { $y->{E} eq $_ } qw( or xor and not ; : )
237 if ($y->{T} eq 'bra' or $y->{L} eq '?') {
239 } elsif ($y->{T} eq 'ket' or $y->{L} eq ':') {
240 die "too many kets at $y->{L}\n" unless $bras;
247 die "mismatch $x->{P} => $y->{P}\n";
251 sub analyse_chunk () {
253 eval { analyse_chunk_core(); };
254 return unless length $@;
255 if ($@ =~ m{^missing end of here doc (\S+)\n}) {
266 for ($ifilehead = 0; l_ok $ifilehead; $ifilehead++) {
268 while (l_ok $ifilehead and m{^diff|^index|^---|^\Q+++\E}) { $ifilehead++ }
269 $ihunkhead = $ifilehead;
270 while (l_ok $ihunkhead) {
271 m{^\@\@} or confess "$ihunkhead $_ ?";
272 my $i = $ihunkhead + 1;
274 if (!l_ok $i or m{^ } or m{^\@\@}) {
275 if (defined $ichunkstart) {
277 eval { analyse_chunk(); 1; };
279 print Dumper('REPORT',
280 $ichunkstart, $ichunkend,
284 $ichunkstart = $ichunkend = $before = $after = undef;
289 my $which = $& eq '-' ? \$before : \$after;