/[cvs]/nfo/perl/libs/Regexp/Group.pm
ViewVC logotype

Annotation of /nfo/perl/libs/Regexp/Group.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Thu Feb 20 21:11:43 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.2: +5 -2 lines
renamed module

1 joko 1.1 ## ------------------------------------------------------------------------
2 joko 1.3 ## $Id: Group.pm,v 1.2 2003/02/14 14:17:42 joko Exp $
3 joko 1.1 ## ------------------------------------------------------------------------
4 joko 1.2 ## $Log: Group.pm,v $
5 joko 1.3 ## Revision 1.2 2003/02/14 14:17:42 joko
6     ## + more warnings and verbosing
7     ##
8 joko 1.2 ## Revision 1.1 2002/12/22 14:16:39 joko
9     ## + initial check-in
10     ##
11 joko 1.1 ## ------------------------------------------------------------------------
12    
13    
14     package Regexp::Group;
15    
16     use strict;
17     use warnings;
18    
19 joko 1.3 use base qw( DesignPattern::Logger );
20 joko 1.1
21    
22     use Data::Dumper;
23    
24     use Data::Compare::Struct qw( isEmpty );
25    
26     my @patterns;
27    
28     # ------------ common perl object constructor ------------
29     sub new {
30     my $invocant = shift;
31     my $class = ref($invocant) || $invocant;
32     my @args = ();
33     @_ && (@args = @_);
34     #$logger->debug( __PACKAGE__ . "->new( @args )" ); # this is not "common"!
35     my $self = { @_ };
36     bless $self, $class;
37     $self->{caller} = caller;
38    
39     #print Dumper(caller(2));
40     #exit;
41    
42     $self->_init();
43     return $self;
44     }
45    
46     sub _init {
47     my $self = shift;
48    
49     # TODO/REVIEW: compile patterns?
50     #@patterns = map{ qr/$_/ } @patterns;
51    
52     # dereference main/raw data (payload)
53     $self->{data} = ${$self->{data}};
54     }
55    
56     sub scan {
57     my $self = shift;
58     my $stepkeys = shift;
59     my $coderef = shift;
60    
61     #my $nodecount_default = shift;
62     my $nodecount_default = 19;
63    
64     #print "SCAN", "\n";
65     #print $self->{pattern}, "\n";
66     $self->{result} = [];
67     $self->{result_raw} = [];
68    
69     # configure tracing
70     # respects additional trace-options passed to _trace-method
71     $self->{TRACE_OPTIONS} = 1;
72     # disables _any_ tracing - even if sub-conditions evaluate to true values
73     $self->{TRACE_DISABLED} = 0;
74    
75     $self->log('starting to scan');
76    
77     #for (1..20) {
78    
79     my $patterncount = 0;
80     my $match_in_loop;
81    
82     my $steps;
83     if ($stepkeys) {
84     $steps = $stepkeys;
85     $patterncount++;
86     } else {
87     $steps = $self->{metadata}->{steps};
88     }
89    
90     #print "cb-2: $coderef", "\n";
91    
92     foreach my $stepkey_current (@{$steps}) {
93     $self->_scan_step($stepkey_current, $coderef);
94     $patterncount++;
95     }
96    
97     #}
98    
99     # ... propagate them to processing
100    
101     # assign result to property of object's current instance
102     #push @{$self->{result}}, $result;
103    
104     #$self->{code}->($self->{caller}, $self);
105    
106     }
107    
108     sub _scan_step {
109    
110     my $self = shift;
111     my $stepkey = shift;
112     my $callback = shift;
113    
114     #print "cb-3: $callback", "\n";
115    
116     #$self->log("running step '$stepkey'", 'debug');
117     $self->log("running step '$stepkey'");
118    
119     my $dataref = $self->{data};
120     my $metadata = $self->{metadata}->{$stepkey};
121     my $pattern = $self->{patterns}->{$stepkey};
122     my $coderef = $self->{coderefs}->{$stepkey};
123    
124     #print Dumper($coderef);
125     #print Dumper($self->{data});
126     #exit;
127    
128     $self->trace("data used", $dataref, 0, undef, { tag => '', exit => 0 });
129     $self->trace("metadata used", $metadata, 0, undef, { tag => '', exit => 0 });
130     $self->trace("pattern used", $pattern, 0, undef, { tag => '', exit => 0 });
131     $self->trace("coderef used", $coderef, 0, undef, { tag => '', exit => 0 });
132    
133     #print "pattern: $pattern", "\n";
134     #exit;
135    
136     # if metadata variable "noscan" is supplied, don't kick off the regex-engine, just call the coderef
137     # TODO: log this event
138     if ($metadata->{noscan} && $metadata->{noscan} == 1) {
139     $coderef->($self, undef, undef, $callback);
140     return;
141     }
142    
143     # dereference data if its still referenced
144     my $data = ${$self->{data}};
145    
146 joko 1.2 if (!$data) {
147     $self->log("data is empty: stepkey=$stepkey", 'warning');
148     return;
149     }
150    
151 joko 1.1 #$self->{data} =~ s{
152     $data =~ s{
153    
154     $pattern # the pattern itself - as contained in a string - actually gets interpolated here
155    
156     }{
157    
158     print "." if $self->{verbose};
159    
160     $self->log("match in step '$stepkey'");
161    
162     # have a clean item to fill in slots
163     my $result_item = [];
164    
165     # 1. build matched structure from slots - grouping functionality for slots
166    
167     # V1 - hand-rolled
168     #my $result = [$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20];
169     #print Dumper($result);
170    
171     my $count = $metadata->{nodecount};
172    
173     # TODO: re-renable this!
174     #$count ||= $nodecount_default;
175    
176     # V2 - iterate!
177     # push dummy item to slot '0' to mimic known behaviour of accessing $1, $2, $3, ....
178     push @{$result_item}, 'n/a';
179     for my $i (1..$count) {
180     my $slot = '$' . $i;
181     push @{$result_item}, eval($slot);
182     }
183    
184    
185     # 2. remember built result (raw) in object - grouping functionality for items
186     $self->log("pushing raw result");
187     push @{$self->{result_raw}}, $result_item;
188    
189    
190     # 4. check/modify further metadata
191     # e.g. 'column_positions' means: "this step is a ColumnProvider"
192     #print Dumper($metadata);
193     if (my $positions = $metadata->{column_positions}) {
194     $self->trace('positions', $positions, 0);
195    
196     #print Dumper($result_item);
197    
198     # build column-names array from position-metadata
199     my @column_names;
200     map {
201     push @column_names, $result_item->[$_];
202     } @$positions;
203    
204     # store additional metadata
205     $metadata->{column_names} = \@column_names;
206     }
207    
208     $self->trace('column-names', $metadata->{column_names}, 0);
209    
210    
211     #return '';
212     #$&;
213     push(@{$self->{matches}->{parsed_chunks}}, $&);
214     #undef;
215    
216     my $matchpos = pos($data);
217     $self->{matches}->{position} = $matchpos;
218     push @{$self->{matches}->{positions}}, $matchpos;
219    
220    
221     # 3. call coderef to get _processed_ result if given (callback)
222     $self->log("calling coderef '$coderef'");
223     if ($coderef) {
224     my $result_processed = {};
225     if ($coderef->($self, $result_item, $result_processed, $callback)) {
226     $self->trace("pushing processed result", $result_processed, 0);
227     push @{$self->{result}}, $result_processed if $result_processed;
228     } else {
229     $self->log('processing of container failed', 'warning');
230     return;
231     }
232     } else {
233     # TODO: croak 'no coderef';
234     }
235    
236     # pass back matched content if we are not the ones to delete something (pass-back-if-not-remover)
237     # with this mechanism, we can iterate over the same content again and again and don't loose data while doing this
238     # without this, multiple scan stepping would not be possible since each step would either remove its
239     # matched content - or not: no flexibility.
240     # as it seems we need flexibility at any point ;-( in order not to break our necks with code-maintenance ;-)
241     # this also can get configured here (this flag is contained in the metadata-part of your filter-declaration)
242     $& if !$metadata->{remover};
243    
244     }xge;
245    
246    
247     #$self->{data_rest} = \$data;
248     $self->{data} = \$data;
249    
250     #$self->trace('matches', $self->getMatches(), 1);
251     #$self->trace('matches-positions', $self->getMatchPositions(), 1);
252 joko 1.2
253     #print "\r" if $self->{verbose};
254     print " " x 50 if $self->{verbose};
255     print "\r" if $self->{verbose};
256 joko 1.1
257     }
258    
259     sub continue {
260     my $self = shift;
261     my $scankey = shift;
262     #print "CONTINUE!", "\n";
263     #$self->{data} = $self->{data_rest};
264     return $self->scan($scankey);
265     }
266    
267     sub getMatches {
268     my $self = shift;
269     return $self->{result_raw};
270     }
271    
272     sub getResults {
273     my $self = shift;
274     return $self->{result};
275     }
276    
277     sub getDataRest {
278     my $self = shift;
279     return $self->{data_rest};
280     }
281    
282     sub getMatchCount {
283     my $self = shift;
284    
285     my $matches = $self->getMatches();
286     return if isEmpty($matches);
287    
288     my $count = $#{$matches};
289     $count++;
290     return $count;
291     }
292    
293     sub getMatchPositions {
294     my $self = shift;
295     if (!$self->getMatchCount()) {
296     $self->{matches}->{positions} = [];
297     }
298     return $self->{matches}->{positions};
299     }
300    
301     sub getMatchPosition {
302     my $self = shift;
303     return $self->{matches}->{position};
304     }
305    
306     1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed