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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sun Dec 22 14:16:39 2002 UTC (21 years, 5 months ago) by joko
Branch: MAIN
+ initial check-in

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

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