Debian Codegraph @

examples/assemble

raw
1 #! /usr/bin/perl -w
2 #
3 # assemble - read lines from STDIN and assemble them into a pattern
4 #
5 # Copyright (C) David Landgren 2004-2008
6
7 use strict;
8 use Getopt::Std;
9 use Regexp::Assemble;
10
11 use Time::HiRes 'time';
12
13 use vars '$VERSION';
14 $VERSION = '0.92';
15
16 getopts( 'abcd:f:i:nprsSt:TuUvw', \my %opt );
17
18 print "$VERSION\n" and exit if $opt{v};
19
20 $opt{d} |= 8 if $opt{T};
21
22 my $ra = Regexp::Assemble->new(
23 chomp => 1,
24 debug => $opt{d} || 0,
25 fold_meta_pairs => exists $opt{f} ? 0 : 1,
26 reduce => exists $opt{r} ? 0 : 1,
27 dup_warn => exists $opt{u} ? 1 : 0,
28 lookahead => exists $opt{a} ? 1 : 0,
29 unroll_plus => exists $opt{U} ? 1 : 0,
30 );
31
32 if( $opt{b} or $opt{c} ) {
33 if( !$opt{b} ) {
34 # filter comments
35 $ra->pre_filter( sub { $_[0] =~ s/\s*#.*$//; 1 } );
36 }
37 elsif( !$opt{c} ) {
38 # filter blank lines
39 $ra->pre_filter( sub { length(shift) } );
40 }
41 else {
42 # filter comments and blank lines.
43 # (removing a comment may can cause a line to become blank
44 $ra->pre_filter( sub {
45 $_[0] =~ s/\s*#.*$//;
46 length($_[0])
47 } );
48 }
49 }
50
51 $ra->add( <> );
52
53 # need to tickle reduction explicitly in the event of -S and -d8
54 $ra->_reduce() if $opt{S} and $opt{d} and ($opt{d} & 8);
55
56 if( $opt{i} or $opt{p} or not ($opt{t} or $opt{S}) ) {
57 print $ra->as_string( indent => $opt{i} || 0 );
58 print "\n" unless $opt{n};
59 }
60
61 if( $opt{s} or $opt{S} ) {
62 warn qq{# nr=@{[$ra->stats_add]} dup=@{[$ra->stats_dup]} raw=@{[$ra->stats_raw]} cooked=@{[$ra->stats_cooked]} len=@{[$ra->stats_length]}\n};
63 }
64
65 if( $opt{t} ) {
66 my $error = 0;
67 my $file = $opt{t};
68 open IN, $file or die "Cannot open $file for input: $!\n";
69 print $ra->as_string, "\n";
70 while( <IN> ) {
71 chomp;
72 if( $opt{w} ) {
73 next if $_ =~ /^$ra$/;
74 }
75 else {
76 next if $_ =~ /$ra/;
77 }
78 print "FAIL <$_>\n";
79 ++$error;
80 }
81 close IN;
82 exit $error ? 1 : 0;
83 }
84
85 =head1 NAME
86
87 assemble - Assemble a list of regular expressions from a file
88
89 =head1 SYNOPSIS
90
91 assemble -abcdfinprsStTuUvw file [...]
92
93 =head1 DESCRIPTION
94
95 Assemble a list of regular expression either from standard input or a
96 file, using the Regexp::Assemble module.
97
98 =head1 OPTIONS
99
100 =over 5
101
102 =item B<-a>
103
104 look Ahead. Insert C<(?=...)> zero-width lookahead assertions in the pattern,
105 where necessary.
106
107 =item B<-b>
108
109 Blank. Ignore blank lines.
110
111 =item B<-c>
112
113 Comment. Basic comment filtering. Strip off perl/shell comments (C<\s*#.*$/>).
114
115 =item B<-d>
116
117 Debug. Turns on debugging output. See L<Regexp::Assemble> for suitable values.
118
119 =item B<-i>
120
121 Indent. Print the regular expression using and indent of n to display
122 nesting. A.k.a pretty-printing. Implies -p.
123
124 =item B<-n>
125
126 No newline. Do not print a newline after the pattern. Useful when
127 interpolating the output into a templating system or similar.
128
129 =item B<-p>
130
131 Print. Print the pattern. This is the default, however, it is
132 required when the -t switch is enabled (because if you want to test
133 patterns ordinarily you don't care what the the assembled pattern
134 looks like).
135
136 =item B<-r>
137
138 Reduce. The default behaviour is to reduce the assembled pattern.
139 Enabling this switch causes the reduction algorithm to be switched
140 off. This can help you determine how much reduction is performed.
141
142 assemble pattern.file | wc
143 # versus
144 assemble -r pattern.file | wc
145
146 =item B<-s>
147
148 Statistics. Print some statistics about the assembled pattern. The
149 output is sent to STDERR (in order to allow the generated pattern
150 to be redirected elsewhere).
151
152 =item B<-S>
153
154 Statistics only. Like B<-s>, except that the pattern itself is not
155 output. Useful with B<-d 8> to see the time taken.
156
157 =item B<-t>
158
159 Test. Test the assembled expression against the contents of a file.
160 Each line is read from the file and is matched against the pattern.
161 Lines that fail to match are printed. In other words, no output is
162 good output. In this mode of operation, error status is 1 in the
163 case of a failure, 0 if all lines matched.
164
165 =item B<-T>
166
167 Time. Print statistics on the time taken to reduce and assemble the
168 pattern. (This is merely a lazy person's synonym for C<-d 8>).
169
170 =item B<-u>
171
172 Unique. Carp if duplicate patterns are found.
173
174 =item B<-U>
175
176 Unroll. Transform C<a+> I<et al> into C<aa*> (which may
177 allow additional reductions).
178
179 =item B<-v>
180
181 Version. Print the version of the assemble script.
182
183 =item B<-w>
184
185 Word/Whole. When testing the contents of a file with C<-t>, bracket
186 the expression with C<^> and C<$> in order to match the whole word
187 or line from the file.
188
189 =back
190
191 =head1 DIAGNOSTICS
192
193 Will print out a summary of the problem if an added pattern causes
194 the assembly to fail.
195
196 =head1 SEE ALSO
197
198 L<Regexp::Assemble>
199
200 =head1 AUTHOR
201
202 Copyright (C) 2004-2008 David Landgren. All rights reserved.
203
204 =head1 LICENSE
205
206 This script is free software; you can redistribute it and/or modify it
207 under the same terms as Perl itself.
208