/[webpac2]/trunk/lib/WebPAC/Parser.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Parser.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 707 by dpavlin, Mon Sep 25 15:26:12 2006 UTC revision 800 by dpavlin, Sun Feb 4 23:10:18 2007 UTC
# Line 13  use base qw/WebPAC::Common/; Line 13  use base qw/WebPAC::Common/;
13    
14  =head1 NAME  =head1 NAME
15    
16  WebPAC::Parser - parse perl normalization configuration files and mungle it  WebPAC::Parser - parse perl normalization configuration files (rules) and mungle it
17    
18  =head1 VERSION  =head1 VERSION
19    
20  Version 0.05  Version 0.08
21    
22  =cut  =cut
23    
24  our $VERSION = '0.05';  our $VERSION = '0.08';
25    
26  =head1 SYNOPSIS  =head1 SYNOPSIS
27    
28  This module will parse L<WebPAC::Normalize/lookup> directives and generate source  This module will parse L<WebPAC::Normalize/lookup> directives and generate source
29  to produce lookups and normalization.  to produce lookups and normalization. It will also parse other parts of
30    source to produce some of DWIM (I<Do What I Mean>) magic
31    (like producing MARC oputput using L<WebPAC::Output::MARC> if there are C<marc_*>
32    rules in normalisation).
33    
34  It's written using L<PPI>, pure-perl parser for perl and heavily influenced by  It's written using L<PPI>, pure-perl parser for perl and heavily influenced by
35  reading about LISP. It might be a bit over-the board, but at least it removed  reading about LISP. It might be a bit over-the board, but at least it removed
# Line 148  sub lookup_create_rules { Line 151  sub lookup_create_rules {
151          return $self->{_lookup_create}->{ _q($database) }->{ _q($input) };          return $self->{_lookup_create}->{ _q($database) }->{ _q($input) };
152  }  }
153    
154    =head2 normalize_rules
155    
156      my $source = $parser->normalize_rules($database, $input);
157    
158    =cut
159    
160    sub normalize_rules {
161            my $self = shift;
162            my ($database,$input) = @_;
163            $input = _input_name($input);
164            return unless (
165                    defined( $self->{_normalize_source}->{ _q($database) } ) &&
166                    defined( $self->{_normalize_source}->{ _q($database) }->{ _q($input) } )
167            );
168            return $self->{_normalize_source}->{ _q($database) }->{ _q($input) };
169    }
170    
171    
172    =head2 have_rules
173    
174      my $do_marc = $parser->have_rules('marc', $database, $input);
175      my $do_index = $parser->have_rules('search', $database);
176    
177    This function will return hash containing count of all found C<marc_*> or
178    C<search> directives. Input name is optional.
179    
180    =cut
181    
182    sub have_rules {
183            my $self = shift;
184    
185            my $log = $self->_get_logger();
186            my $type = shift @_ || $log->logconfess("need at least type");
187            my $database = shift @_ || $log->logconfess("database is required");
188            my $input = shift @_;
189    
190            $input = _input_name($input);
191    
192    
193            return unless defined( $self->{_have_rules}->{ _q($database) } );
194    
195            my $database_rules = $self->{_have_rules}->{ _q($database ) };
196    
197            if (defined($input)) {
198    
199                    return unless (
200                            defined( $database_rules->{ _q($input) } ) &&
201                            defined( $database_rules->{ _q($input) }->{ $type } )
202                    );
203    
204                    return $database_rules->{ _q($input) }->{ $type };
205            }
206    
207            my $usage;
208    
209            foreach my $i (keys %{ $database_rules }) {
210                    next unless defined( $database_rules->{$i}->{$type} );
211    
212                    foreach my $t (keys %{ $database_rules->{ $i }->{$type} }) {
213                            $usage->{ $t } += $database_rules->{ $i }->{ $t };
214                    }
215            }
216    
217            return $usage;
218    
219    }
220    
221    
222  =head1 PRIVATE  =head1 PRIVATE
223    
224  =head2 _read_sources  =head2 _read_sources
# Line 165  sub _read_sources { Line 236  sub _read_sources {
236    
237          my $nr = 0;          my $nr = 0;
238    
239          my @lookups;          my @sources;
240    
241            my $lookup_src_cache;
242    
243          $self->{config}->iterate_inputs( sub {          $self->{config}->iterate_inputs( sub {
244                  my ($input, $database) = @_;                  my ($input, $database) = @_;
# Line 188  sub _read_sources { Line 261  sub _read_sources {
261    
262                          $self->{valid_inputs}->{$database}->{$input_name}++;                          $self->{valid_inputs}->{$database}->{$input_name}++;
263    
264                          push @lookups, sub {                          push @sources, sub {
265                                  $self->_parse_lookups( $database, $input_name, $full, $s );                                  warn "### $database $input_name, $full ###\n";
266                                    $self->_parse_source( $database, $input_name, $full, $s );
267                          };                          };
268    
269                          $nr++;                          $nr++;
# Line 198  sub _read_sources { Line 272  sub _read_sources {
272    
273          $log->debug("found $nr source files");          $log->debug("found $nr source files");
274    
275          # parse all lookups          # parse all sources
276          $_->() foreach (@lookups);          $_->() foreach (@sources);
277    
278          return $nr;          return $nr;
279  }  }
280    
281  =head2 _parse_lookups  =head2 _parse_source
282    
283    $parser->_parse_lookups($database,$input,$path,$source);    $parser->_parse_source($database,$input,$path,$source);
284    
285  Called for each normalize source (rules) in each input by L</read_sources>  Called for each normalize source (rules) in each input by L</_read_sources>
286    
287  It will report invalid databases and inputs in error log after parsing.  It will report invalid databases and inputs in error log after parsing.
288    
289  =cut  =cut
290    
291  sub _parse_lookups {  sub _parse_source {
292          my $self = shift;          my $self = shift;
293          my ($database, $input, $path, $source) = @_;          my ($database, $input, $path, $source) = @_;
294    
# Line 232  sub _parse_lookups { Line 306  sub _parse_lookups {
306          my $Document = PPI::Document->new( \$source ) || $log->logdie("can't parse source:\n", $self->{source});          my $Document = PPI::Document->new( \$source ) || $log->logdie("can't parse source:\n", $self->{source});
307    
308          $Document->prune('PPI::Token::Whitespace');          $Document->prune('PPI::Token::Whitespace');
309            $Document->prune('PPI::Token::Comment');
310          #$Document->prune('PPI::Token::Operator');          #$Document->prune('PPI::Token::Operator');
311    
312          # Find all the named subroutines          # Find all the named subroutines
# Line 296  sub _parse_lookups { Line 371  sub _parse_lookups {
371    
372                          $log->debug("key = $key");                          $log->debug("key = $key");
373    
                         my $create = "save_into_lookup('$key', $e[7] $e[8] );\n";  
   
                         $log->debug("create: $create");  
   
374                          return $self->_lookup_error("invalid database $e[3] in $path" ) unless $self->valid_database( $e[3] );                          return $self->_lookup_error("invalid database $e[3] in $path" ) unless $self->valid_database( $e[3] );
375                          return $self->_lookup_error("invalid input $e[5] of database $e[3] in $path", ) unless $self->valid_database_input( $e[3], $e[5] );                          return $self->_lookup_error("invalid input $e[5] of database $e[3] in $path", ) unless $self->valid_database_input( $e[3], $e[5] );
376    
377                            my $create = qq{
378                                    save_into_lookup($e[3],$e[5],'$key', $e[7] $e[8] );
379                            };
380    
381                            $log->debug("create: $create");
382    
383                          # save code to create this lookup                          # save code to create this lookup
384                          $self->{_lookup_create}->{ _q($e[3]) }->{ _q($e[5]) } .= $create;                          $self->{_lookup_create}->{ _q($e[3]) }->{ _q($e[5]) } .= $create;
385                          $self->{_lookup_create_key}->{ _q($e[3]) }->{ _q($e[5]) }->{ _q($key) }++;                          $self->{_lookup_create_key}->{ _q($e[3]) }->{ _q($e[5]) }->{ _q($key) }++;
# Line 333  sub _parse_lookups { Line 410  sub _parse_lookups {
410          $log->debug("create: ", dump($self->{_lookup_create}) );          $log->debug("create: ", dump($self->{_lookup_create}) );
411          $log->debug("normalize: $normalize_source");          $log->debug("normalize: $normalize_source");
412    
413          $self->{_normalize_source}->{$database}->{$input} = $normalize_source;          $self->{_normalize_source}->{$database}->{$input} .= $normalize_source;
414    
415          if ($self->{debug}) {          if ($self->{debug}) {
416                  my $Dumper = PPI::Dumper->new( $Document );                  my $Dumper = PPI::Dumper->new( $Document );
# Line 342  sub _parse_lookups { Line 419  sub _parse_lookups {
419    
420          $log->error("Parser errors:\n", join("\n",@{ $self->{_lookup_errors} }) ) if ($self->{_lookup_errors});          $log->error("Parser errors:\n", join("\n",@{ $self->{_lookup_errors} }) ) if ($self->{_lookup_errors});
421    
422            $Document->find( sub {
423                            my ($Document,$Element) = @_;
424    
425                            $Element->isa('PPI::Token::Word') or return '';
426                            if ($Element->content =~ m/^(marc|search)/) {
427                                    my $what = $1;
428                                    $log->debug("found $what rules in $database/$input");
429                                    $self->{_have_rules}->{ $database }->{ $input }->{ $what }->{ $Element->content }++;
430                            } else {
431                                    return '';
432                            }
433            });
434    
435          return 1;          return 1;
436  }  }
437    

Legend:
Removed from v.707  
changed lines
  Added in v.800

  ViewVC Help
Powered by ViewVC 1.1.26