| 1 |
#! /usr/bin/perl -w |
| 2 |
# |
| 3 |
# debugging - strategies for dealing with assembled patterns that |
| 4 |
# cannot be compiled. Usual message is |
| 5 |
# "Unmatched ( in regex; marked by <-- HERE in m/a( <-- HERE" |
| 6 |
# |
| 7 |
# Copyright (C) David Landgren 2005 |
| 8 |
|
| 9 |
use strict; |
| 10 |
use Getopt::Std; |
| 11 |
use Regexp::Assemble; |
| 12 |
|
| 13 |
use vars '$VERSION'; |
| 14 |
$VERSION = '0.1'; |
| 15 |
|
| 16 |
getopts( 'v', \my %opt ); |
| 17 |
|
| 18 |
print "$VERSION\n" and exit if $opt{v}; |
| 19 |
|
| 20 |
my $r = Regexp::Assemble->new; |
| 21 |
|
| 22 |
while( <> ) { |
| 23 |
chomp; |
| 24 |
|
| 25 |
# take a copy of the assembly and add the next pattern |
| 26 |
my $clone = $r->clone; |
| 27 |
$clone->add($_); |
| 28 |
|
| 29 |
# if we compile the regexp, does it blow up? |
| 30 |
eval { my $regexp = $clone->re }; |
| 31 |
|
| 32 |
# it blew up |
| 33 |
if( $@ ) { |
| 34 |
report(); |
| 35 |
} |
| 36 |
else { |
| 37 |
# ok, now add it for real to the assembly |
| 38 |
$r->add( $_ ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
sub report { |
| 43 |
my $lsqb = $_ =~ tr/[/[/; |
| 44 |
my $rsqb = $_ =~ tr/]/]/; |
| 45 |
my $lpar = $_ =~ tr/(/(/; |
| 46 |
my $rpar = $_ =~ tr/)/)/; |
| 47 |
|
| 48 |
warn <<WARNING; |
| 49 |
$ARGV($.): [$_] |
| 50 |
counts: [=$lsqb ]=$rsqb (=$lpar )=$rpar |
| 51 |
The above line causes the assembly to fail. Look for unbalanced parens or |
| 52 |
brackets, or counts greater than 1. If the line looks ok, it could be |
| 53 |
because of an interaction with a previously encountered line (try reversing |
| 54 |
the order of the lines to find the other line). |
| 55 |
|
| 56 |
WARNING |
| 57 |
} |
| 58 |
|
| 59 |
=head1 NAME |
| 60 |
|
| 61 |
debugging - a strategy for dealing with uncompilable regexps |
| 62 |
|
| 63 |
=head1 SYNOPSIS |
| 64 |
|
| 65 |
B<debugging> file-of-patterns |
| 66 |
|
| 67 |
=head1 OPTIONS |
| 68 |
|
| 69 |
=over 5 |
| 70 |
|
| 71 |
=item B<-v> |
| 72 |
|
| 73 |
prints out the version of script. |
| 74 |
|
| 75 |
=back |
| 76 |
|
| 77 |
=head1 DESCRIPTION |
| 78 |
|
| 79 |
By default, C<Regexp::Assemble> uses a naive strategy for chopping |
| 80 |
up input. It can get upset if you try to feed it a pattern with |
| 81 |
nested parentheses. For instance, C<a(b(c))> and C<a(b(d))> will |
| 82 |
cause it to fail. If you really want to do that, you'll have to |
| 83 |
come up with a regexp that can chop the above into C<a>, C<(b(c))> |
| 84 |
and C<a>, C<(b(d))>. |
| 85 |
|
| 86 |
Of course, that will only buy you C<a(?:(b(c))|(b(d)))> at the |
| 87 |
present time. Teaching it to produce C<a(b([cd]))> would be |
| 88 |
considerably more complex. Patches welcome. |
| 89 |
|
| 90 |
So, when adding hundreds of patterns, it's not always obvious |
| 91 |
figuring out a way of isolating the offending pattern, especially |
| 92 |
when they are coming out of a file or some other process you don't |
| 93 |
have control over. |
| 94 |
|
| 95 |
The trick, then, consists of adding a pattern one at a time, and |
| 96 |
then standing back and watching whether producing a regexp from the |
| 97 |
list of patterns seen so far will cause things to blow up. So you |
| 98 |
take a copy of the assembly, and add the pattern to the copy. If |
| 99 |
that works, then add it to the real assembly. Repeat until done. |
| 100 |
|
| 101 |
You don't want to do this all the time, because it is of course |
| 102 |
much less efficient. |
| 103 |
|
| 104 |
=head1 SEE ALSO |
| 105 |
|
| 106 |
L<Regexp::Assemble> |
| 107 |
|
| 108 |
=head1 AUTHOR |
| 109 |
|
| 110 |
Copyright (C) 2005 David Landgren. All rights reserved. |
| 111 |
|
| 112 |
=head1 LICENSE |
| 113 |
|
| 114 |
This script is free software; you can redistribute it and/or modify it |
| 115 |
under the same terms as Perl itself. |