1 package WebPAC::Parser;
6 use base qw/WebPAC::Common WebPAC::Normalize/;
10 use Data::Dump qw/dump/;
16 WebPAC::Parser - parse perl normalization configuration files and mungle it
24 our $VERSION = '0.02';
34 Create new parser object.
36 my $parser = new WebPAC::Parser(
37 config => new WebPAC::Config(),
38 base_path => '/optional/path/to/conf',
48 my $log = $self->_get_logger();
50 $log->logdie("config isn't WebPAC::Config") unless ($self->{config} && $self->{config}->isa('WebPAC::Config'));
52 $log->logdie("can't iterate_inputs over this config object") unless ($self->{config}->can('iterate_inputs'));
56 $self->{config}->iterate_inputs( sub {
57 my ($input, $database) = @_;
58 my $path = $input->{normalize}->{path} || return;
59 my $full = $self->{base_path} ? $self->{base_path} . '/' . $path : $path;
60 $log->logdie("normalization input $full doesn't exist") unless (-e $full);
61 my $s = read_file( $full ) || $log->logdie("can't read $full: $!");
62 my $input_name = $input->{name} || $log->logdie("can't deduce name of input: ", dump( $input ));
63 $log->debug("$database/$input_name: adding $path to parser [",length($s)," bytes]");
65 $self->{valid_inputs}->{$database}->{$input_name}++;
68 $log->debug("collected ", length($source), " bytes of source");
70 $self->{source} = $source;
72 $self ? return $self : return undef;
82 my $log = $self->_get_logger();
84 $log->logdie('no source found in object') unless ($self->{source});
86 $log->debug("valid_inputs = ", dump( $self->{valid_inputs} ));
88 my $Document = PPI::Document->new( \$self->{source} ) || $log->logdie("can't parse source:\n", $self->{source});
90 $Document->prune('PPI::Token::Whitespace');
91 #$Document->prune('PPI::Token::Operator');
93 # Find all the named subroutines
97 $Document->find( sub {
98 my ($Document,$Element) = @_;
100 $Element->isa('PPI::Token::Word') or return '';
101 $Element->content eq 'lookup' or return '';
103 $log->debug("expansion: ", $Element->snext_sibling);
105 my $args = $Element->snext_sibling;
107 my @e = $args->child(0)->elements;
108 $log->logdie("hum, expect at least 8 elements, got ", scalar @e, " in $args") if ($#e < 8);
110 if ($log->is_debug) {
111 my $report = "found " . scalar @e . " elements:\n";
113 foreach my $i ( 0 .. $#e ) {
114 $report .= sprintf("# %-2d: %-30s %s\n", $i, ( $e[$i] || 'undef' ), $e[$i]->class );
117 $log->debug($report);
120 my $key_element = $e[8]->clone;
122 $log->logdie("key element must be PPI::Structure::Block") unless $key_element->isa('PPI::Structure::Block');
124 $log->debug("key part: ", $key_element);
128 $key_element->find( sub {
129 my $e = $_[1] || die "no element?";
130 $e->isa('PPI::Token::Word') or return '';
131 $e->content eq 'rec' or return '';
133 my $kf = $e->snext_sibling;
135 $log->debug("key fragment = $kf");
138 $log->logdie("can't eval { $kf }: $@") if ($@);
143 my $key = join('-', @key ) || $log->logdie("no key found!");
145 $log->debug("key = $key");
148 $coderef = ' . $e[7] . $e[8] . ';
149 foreach my $v ($coderef->()) {
150 next unless (defined($v) && $v ne \'\');
151 push @{ $lookup->{\'' . $key . '\'}->{$v} }, $mfn;
155 $log->debug("create: $create");
157 $log->logdie("invalid database $e[3]" ) unless $self->valid_database( $e[3] );
158 $log->logdie("invalid input $e[5] of database $e[3]", ) unless $self->valid_database_input( $e[3], $e[5] );
160 $eval_create->{ $e[3] }->{ $e[5] } .= $create;
163 $e[8]->insert_after( $e[8]->clone );
164 $e[8]->insert_after( $e[7]->clone );
165 $e[8]->insert_after( $e[6]->clone );
169 $e[8]->insert_before( new PPI::Token::Quote::Single( "'$key'" ) );
173 $log->debug(">>> ", $Element->snext_sibling);
176 $log->info("create: ", dump($eval_create) );
177 $log->info("lookup: ", $Document->serialize );
179 if ($self->{debug}) {
180 my $Dumper = PPI::Dumper->new( $Document );
187 =head2 valid_database
189 my $ok = $parse->valid_database('key');
196 my $database = shift || return;
197 $database =~ s/['"]//g;
199 return defined($self->{valid_inputs}->{$database});
202 =head2 valid_database_input
204 my $ok = $parse->valid_database('database_key','input_name');
208 sub valid_database_input {
211 my ($database,$input) = @_;
212 $database =~ s/['"]//g;
215 return defined($self->{valid_inputs}->{$database}->{$input});
220 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
222 =head1 COPYRIGHT & LICENSE
224 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
226 This program is free software; you can redistribute it and/or modify it
227 under the same terms as Perl itself.
231 1; # End of WebPAC::Parser