/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 687 - (show annotations)
Sun Sep 24 17:49:05 2006 UTC (17 years, 7 months ago) by dpavlin
File size: 4231 byte(s)
 r969@llin:  dpavlin | 2006-09-24 19:47:01 +0200
 use Log::Log4perl for logging

1 package WebPAC::Parser;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common WebPAC::Normalize/;
7
8 use PPI;
9 use PPI::Dumper;
10 use Data::Dump qw/dump/;
11 use File::Slurp;
12
13
14 =head1 NAME
15
16 WebPAC::Parser - parse perl normalization configuration files and mungle it
17
18 =head1 VERSION
19
20 Version 0.01
21
22 =cut
23
24 our $VERSION = '0.01';
25
26 =head1 SYNOPSIS
27
28 FIXME
29
30 =head1 FUNCTIONS
31
32 =head2 new
33
34 Create new parser object.
35
36 my $parser = new WebPAC::Parser(
37 config => new WebPAC::Config(),
38 base_path => '/optional/path/to/conf',
39 );
40
41 =cut
42
43 sub new {
44 my $class = shift;
45 my $self = {@_};
46 bless($self, $class);
47
48 my $log = $self->_get_logger();
49
50 $log->logdie("config isn't WebPAC::Config") unless ($self->{config} && $self->{config}->isa('WebPAC::Config'));
51
52 $log->logdie("can't iterate_inputs over this config object") unless ($self->{config}->can('iterate_inputs'));
53
54 my $source;
55
56 $self->{config}->iterate_inputs( sub {
57 my $input = shift;
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]");
63 $source .= $s;
64 } );
65
66 $log->debug("collected ", length($source), " bytes of source");
67
68 $self->{source} = $source;
69
70 $self ? return $self : return undef;
71 }
72
73 =head2 parse
74
75 =cut
76
77 sub parse {
78 my $self = shift;
79
80 my $log = $self->_get_logger();
81
82 $log->logdie('no source found in object') unless ($self->{source});
83
84 my $Document = PPI::Document->new( \$self->{source} ) || $log->logdie("can't parse source:\n", $self->{source});
85
86 $Document->prune('PPI::Token::Whitespace');
87 #$Document->prune('PPI::Token::Operator');
88
89 # Find all the named subroutines
90
91 my $eval_create;
92
93 $Document->find( sub {
94 my ($Document,$Element) = @_;
95
96 $Element->isa('PPI::Token::Word') or return '';
97 $Element->content eq 'lookup' or return '';
98
99 $log->debug("expansion: ", $Element->snext_sibling);
100
101 my $args = $Element->snext_sibling;
102
103 my @e = $args->child(0)->elements;
104 $log->logdie("hum, expect at least 8 elements, got ", scalar @e, " in $args") if ($#e < 8);
105
106 if ($log->is_debug) {
107 my $report = "found " . scalar @e . " elements:\n";
108
109 foreach my $i ( 0 .. $#e ) {
110 $report .= sprintf("# %-2d: %-30s %s\n", $i, ( $e[$i] || 'undef' ), $e[$i]->class );
111 }
112
113 $log->debug($report);
114 }
115
116 my $key_element = $e[8]->clone;
117
118 $log->logdie("key element must be PPI::Structure::Block") unless $key_element->isa('PPI::Structure::Block');
119
120 $log->debug("key part: ", $key_element);
121
122 my @key;
123
124 $key_element->find( sub {
125 my $e = $_[1] || die "no element?";
126 $e->isa('PPI::Token::Word') or return '';
127 $e->content eq 'rec' or return '';
128
129 my $kf = $e->snext_sibling;
130
131 $log->debug("key fragment = $kf");
132
133 push @key, eval $kf;
134 $log->logdie("can't eval { $kf }: $@") if ($@);
135
136 return 1;
137 });
138
139 my $key = join('-', @key ) || $log->logdie("no key found!");
140
141 $log->debug("key = $key");
142
143 my $create = '
144 $coderef = ' . $e[7] . $e[8] . ';
145 foreach my $v ($coderef->()) {
146 next unless (defined($v) && $v ne \'\');
147 push @{ $lookup->{\'' . $key . '\'}->{$v} }, $mfn;
148 }
149 ';
150
151 $log->debug("create: $create");
152
153 $create =~ s/\s+/ /gs;
154 $eval_create->{ $e[3] }->{ $e[5] } .= $create;
155
156 if ($#e < 10) {
157 $e[8]->insert_after( $e[8]->clone );
158 $e[8]->insert_after( $e[7]->clone );
159 $e[8]->insert_after( $e[6]->clone );
160 }
161
162 $e[7]->remove;
163 $e[8]->insert_before( new PPI::Token::Quote::Single( "'$key'" ) );
164 $e[8]->remove;
165
166
167 $log->debug(">>> ", $Element->snext_sibling);
168 });
169
170 $log->info("create: ", dump($eval_create) );
171 $log->info("lookup: ", $Document->serialize );
172
173 if ($self->{debug}) {
174 my $Dumper = PPI::Dumper->new( $Document );
175 $Dumper->print;
176 }
177
178 return 1;
179 }
180
181 =head1 AUTHOR
182
183 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
184
185 =head1 COPYRIGHT & LICENSE
186
187 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
188
189 This program is free software; you can redistribute it and/or modify it
190 under the same terms as Perl itself.
191
192 =cut
193
194 1; # End of WebPAC::Parser

  ViewVC Help
Powered by ViewVC 1.1.26