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.01';
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 {
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 $log->debug("adding $path to parser [",length($s)," bytes]");
66 $log->debug("collected ", length($source), " bytes of source");
68 $self->{source} = $source;
70 $self ? return $self : return undef;
80 my $log = $self->_get_logger();
82 $log->logdie('no source found in object') unless ($self->{source});
84 my $Document = PPI::Document->new( \$self->{source} ) || $log->logdie("can't parse source:\n", $self->{source});
86 $Document->prune('PPI::Token::Whitespace');
87 #$Document->prune('PPI::Token::Operator');
89 # Find all the named subroutines
93 $Document->find( sub {
94 my ($Document,$Element) = @_;
96 $Element->isa('PPI::Token::Word') or return '';
97 $Element->content eq 'lookup' or return '';
99 print "#*** expansion: ", $Element->snext_sibling,$/;
101 my $args = $Element->snext_sibling;
103 my @e = $args->child(0)->elements;
104 print "hum, expect at least 8 elements, got ", scalar @e, " in $args\n" if ($#e < 8);
106 print "# found ", scalar @e, " elements:\n";
108 foreach my $i ( 0 .. $#e ) {
109 printf("# %-2d: %-30s %s\n", $i, ( $e[$i] || 'undef' ), $e[$i]->class );
112 my $key_element = $e[8]->clone;
114 die "key element must be PPI::Structure::Block" unless $key_element->isa('PPI::Structure::Block');
116 print "## key part: ", $key_element, $/;
120 $key_element->find( sub {
121 my $e = $_[1] || die "no element?";
122 $e->isa('PPI::Token::Word') or return '';
123 $e->content eq 'rec' or return '';
125 my $kf = $e->snext_sibling;
127 print "## key fragment = $kf\n";
130 print "ERROR: can't eval { $kf }: $@" if ($@);
135 my $key = join('-', @key ) || print "ERROR: no key found!";
137 print "key = $key\n";
140 $coderef = ' . $e[7] . $e[8] . ';
141 foreach my $v ($coderef->()) {
142 next unless (defined($v) && $v ne \'\');
143 push @{ $lookup->{\'' . $key . '\'}->{$v} }, $mfn;
147 print "create: $create\n";
149 $create =~ s/\s+/ /gs;
150 $eval_create->{ $e[3] }->{ $e[5] } .= $create;
153 $e[8]->insert_after( $e[8]->clone );
154 $e[8]->insert_after( $e[7]->clone );
155 $e[8]->insert_after( $e[6]->clone );
159 $e[8]->insert_before( new PPI::Token::Quote::Single( "'$key'" ) );
163 print "# >>> ", $Element->snext_sibling, "\n";
166 print "-----\ncreate: ", dump($eval_create), "\n";
167 print "-----\nlookup: ", $Document->serialize, "\n";
170 my $Dumper = PPI::Dumper->new( $Document );
177 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
179 =head1 COPYRIGHT & LICENSE
181 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
183 This program is free software; you can redistribute it and/or modify it
184 under the same terms as Perl itself.
188 1; # End of WebPAC::Parser