Builder.pm

filter: Statementstmt 31.1% 38 / 122 Branchbran 20.0% 4 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 27.5% 8 / 29 Podpod 10.5% 2 / 19 totaltotal 27.3% 52 / 190 CC 18 SCAR 17.1
CC
18
Cov
30%
getExt
12.4
runTemplate
12.0
findTemplate
12.0
CRAP 5.5
« Builder/Parser.pm ↑ Summary Builder/Args.pm »
1 package Lang::Tree::Builder;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Lang::Tree::Builder::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.1';
11  
12 =head1 NAME
13  
14 Lang::Tree::Builder - Build Classes from a Tree Definition File
15  
16 =head1 SYNOPSIS
17  
18 use Lang::Tree::Builder;
19 my $builder = new Lang::Tree::Builder(%params);
20 $builder->build($config_file);
21  
22 =head1 DESCRIPTION
23  
24 C<Lang::Tree::Builder> takes a set of parameters and a tree definition file
25 and uses L<Template::Toolkit> to generate perl classes and an API.
26  
27 It is intended primarily to take the drudgery out of writing all of
28 the support classes needed to build abstract syntax trees.
29  
30 =head2 new
31  
32 my $builder = new Lang::Tree::Builder(%params);
33  
34 C<%params> are
35  
36 =over 4
37  
38 =item dir
39  
40 The output directory, default C<./>
41  
42 =item prefix
43  
44 A prefix class prepended to all generated classes, default none.
45 If a prefix is provided, it is used literally. i.e. if you want
46 C<MyPrefix::> you must supply the C<::> explicitly.
47  
48 =item lang
49  
50 The language of the generated classes.
51  
52 =back
53  
54 =cut
55  
56 2 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod documented
57 2 my ($class, %params) = @_;
58  
59 2 my %defaults = (
60 dir => '.',
61 prefix => '',
62 lang => 'Perl',
63 );
64  
65 2 foreach my $key (keys %params) {
66 4 ▶ die "unrecognised param: $key" unless exists $defaults{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $defaults{$key} 0 4 4
67 }
68  
69 2 my %body;
70  
71 2 foreach my $key (keys %defaults) {
72 $body{$key} =
73 exists($params{$key})
74 ? $params{$key}
75 6 ▶ : $defaults{$key};
Branch: 1 branch
BranchTrueFalseTotal
exists $params{$key} ? : 4 2 6
76 }
77  
78 2 my $self = bless {%body}, $class;
79 2 $self->{includes} = $self->findIncludes();
80 2 $self->{ext} = $self->getExt();
81 0 $self->initVmethods();
82  
83 # add to this hash for new kinds of targets
84 $self->{TEMPLATES} = {
85 0 visitor => 'Visitor',
86 cvis => 'CopyingVisitor',
87 printer => 'Printer',
88 xml => 'XML',
89 api => 'API',
90 };
91  
92 0 return $self;
93 }
94  
95 {
96 2 my $init = 0;
97  
98 0 ▶ sub initVmethods {
Subroutine initVmethods: not called CC=2 SCAR=17.9
Pod undocumented
99 0 ▶ unless ($init) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($init) 0 0 0
100 0 ▶ $Template::Stash::SCALAR_OPS->{camelcase} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 my ($text) = @_;
102 0 $text =~ s/_(\w)/uc($1)/eg;
103 0 ucfirst($text);
104 0 };
105  
106 0 ▶ $Template::Stash::SCALAR_OPS->{uncamel} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 my ($text) = @_;
108 0 $text =~ s/([A-Z])/'_' . lc($1)/eg;
109 0 $text =~ s/^_//;
110 0 $text;
111 0 };
112  
113 0 ▶ $Template::Stash::SCALAR_OPS->{tolower} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 my ($text) = @_;
115 0 lc($text);
116 0 };
117  
118 0 ▶ $Template::Stash::SCALAR_OPS->{getMethod} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 my ($text) = @_;
120 0 $text =~ s/_(\w)/uc($1)/eg;
121 0 'get' . ucfirst($text);
122 0 };
123  
124 0 ▶ $Template::Stash::LIST_OPS->{sz} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 my ($list) = @_;
126 0 scalar(@$list);
127 0 };
128  
129 0 $init = 1;
130 }
131 }
132 }
133  
134 =head2 build
135  
136 $builder->build($config_file);
137  
138 Builds the tree classes, API and Visitor from definitions
139 in the C<$config_file>.
140  
141 =cut
142  
143 2 ▶sub build {
Subroutine build: not called CC=1 SCAR=4.1
Pod documented
144 0 my ($self, $config_file) = @_;
145  
146 0 my $data = $self->getData($config_file);
147 0 $self->buildClasses($data, $config_file);
148 0 $self->buildSpecialClasses($data, $config_file);
149 }
150  
151 # parses the $config file and returns its $data
152 0 ▶sub getData {
Subroutine getData: not called CC=1 SCAR=6.9
Pod undocumented
153 0 my ($self, $config_file) = @_;
154  
155 0 my $parser = Lang::Tree::Builder::Parser->new(prefix => $self->{prefix});
156 0 return $parser->parseFile($config_file);
157 }
158  
159 # build each class file from the $data
160 0 ▶sub buildClasses {
Subroutine buildClasses: not called CC=2 SCAR=17.9
Pod undocumented
161 0 my ($self, $data, $config_file) = @_;
162  
163 0 foreach my $class ($data->classes) {
164 0 $self->buildClass($data, $config_file, $class);
165 }
166 }
167  
168 # build an individual class file
169 0 ▶sub buildClass {
Subroutine buildClass: not called CC=1 SCAR=6.9
Pod undocumented
170 0 my ($self, $data, $config_file, $class) = @_;
171  
172 0 $self->runTemplate($data, $config_file, 'Class.tt2', $class,
173 class => $class);
174 }
175  
176 0 ▶sub buildSpecialClasses {
Subroutine buildSpecialClasses: not called CC=2 SCAR=17.9
Pod undocumented
177 0 my ($self, $data, $config_file) = @_;
178 0 foreach my $key (keys %{$self->{TEMPLATES}}) {
179 0 $self->buildSpecialClass($data, $config_file, $self->{TEMPLATES}{$key});
180 }
181 }
182  
183 0 ▶sub buildSpecialClass {
Subroutine buildSpecialClass: not called CC=1 SCAR=6.9
Pod undocumented
184 0 my ($self, $data, $config_file, $template) = @_;
185 0 $self->runTemplate($data, $config_file, "$template.tt2",
186 $self->makeSpecialClass($template));
187 }
188  
189 0 ▶sub runTemplate {
Subroutine runTemplate: not called CC=3 SCAR=24.8
Pod undocumented
190 0 my ($self, $data, $config_file, $input_template, $output_class, %extra)
191 = @_;
192  
193 0 ▶ unless ($self->findTemplate($input_template)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->findTemplate($input_template)) 0 0 0
194 0 warn "templates $input_template not found\n";
195 0 return;
196 }
197 0 my $template = $self->makeTemplate();
198 0 ▶ $template->process(
Branch: 1 branch
BranchTrueFalseTotal
$template->process($input_template, $self->makeVars($data, $config_file, %extra), $self->getOutputFilename($output_class)) or die "builder failed: ", $template->error 0 0 0
199 $input_template,
200 $self->makeVars($data, $config_file, %extra),
201 $self->getOutputFilename($output_class)
202 ) or die "builder failed: ", $template->error();
203 }
204  
205 # create the vars hash for the template
206 0 ▶sub makeVars {
Subroutine makeVars: not called CC=1 SCAR=6.9
Pod undocumented
207 0 my ($self, $data, $config_file, %extra) = @_;
208  
209 return {
210 warning => $self->makeWarning($config_file),
211 data => $data,
212 0 %{$self->makeSpecialClasses()},
213 ext => $self->{ext},
214 0 %extra
215 };
216 }
217  
218 0 ▶sub makeSpecialClasses {
Subroutine makeSpecialClasses: not called CC=2 SCAR=17.9
Pod undocumented
219 0 my ($self) = @_;
220 0 my $classes = {};
221 0 foreach my $key (keys %{$self->{TEMPLATES}}) {
222 0 $classes->{$key} = $self->makeSpecialClass($self->{TEMPLATES}->{$key});
223 }
224 0 return $classes;
225 }
226  
227 0 ▶sub makeSpecialClass {
Subroutine makeSpecialClass: not called CC=1 SCAR=6.9
Pod undocumented
228 0 my ($self, $class) = @_;
229 0 new Lang::Tree::Builder::Class(class => ($self->{prefix} . $class));
230 }
231  
232 # create a new Template object
233 0 ▶sub makeTemplate {
Subroutine makeTemplate: not called CC=2 SCAR=17.9
Pod undocumented
234 0 my ($self) = @_;
235  
236 0 ▶ Template->new($self->makeTemplateConfig())
Branch: 1 branch
BranchTrueFalseTotal
"Template"->new($self->makeTemplateConfig) or die "$Template::ERROR\n" 0 0 0
237 || die "$Template::ERROR\n";
238 }
239  
240 # returns the warning message
241 0 ▶sub makeWarning {
Subroutine makeWarning: not called CC=1 SCAR=6.9
Pod undocumented
242 0 my ($self, $config_file) = @_;
243  
244 0 "Automatically generated from $config_file on "
245 . scalar(gmtime)
246 . " GMT.";
247 }
248  
249 # returns a hashref of config for the Template
250 0 ▶sub makeTemplateConfig {
Subroutine makeTemplateConfig: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my ($self) = @_;
252  
253 return {
254 INCLUDE_PATH => $self->{includes},
255 0 INTERPOLATE => 0,
256 POST_CHOMP => 0,
257 EVAL_PERL => 0,
258 };
259 }
260  
261 0 ▶sub findTemplate {
Subroutine findTemplate: not called CC=3 SCAR=24.8
Pod undocumented
262 0 my ($self, $template) = @_;
263 0 foreach my $inc (@{ $self->{includes} }) {
264 0 ▶ return "$inc/$template" if -f "$inc/$template";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$inc/$template" 0 0 0
265 }
266 0 return '';
267 }
268  
269 # returns the filename extension for all generated files
270 2 ▶sub getExt {
Subroutine getExt: called CC=5 SCAR=25.2
Pod undocumented
271 2 my ($self) = @_;
272  
273 2 foreach my $inc (@{ $self->{includes} }) {
274 16 my $extfile = "$inc/EXT";
275 16 ▶ if (-e $extfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $extfile) 0 16 16
276 0 my $fh = new FileHandle($extfile);
277 0 ▶ die "$extfile: $@" unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
278 0 my $ext = '';
279 0 ▶ while (defined(my $line = $fh->getline())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $fh->getline)) 0 0 0
280 0 chomp($line);
281 0 $line =~ s/#.*//;
282 0 $line =~ s/\s+//g;
283 0 $ext .= $line;
284 }
285 0 return $ext;
286 }
287 }
288  
289 2 die "can't find EXT";
290 }
291  
292 # returns the output filename for an argument Lang::Tree::Builder::Class
293 0 ▶sub getOutputFilename {
Subroutine getOutputFilename: not called CC=1 SCAR=6.9
Pod undocumented
294 0 my ($self, $class) = @_;
295  
296 0 return $self->{dir} . '/' . $class->name('/') . $self->{ext};
297 }
298  
299 # returns an arrayref of locations to search for Templates
300 2 ▶sub findIncludes {
Subroutine findIncludes: called CC=1 SCAR=0.0
Pod undocumented
301 2 my ($self) = @_;
302  
303 2 [ map { "$_/Tree/Builder/Templates/$self->{lang}" } @INC ];
304 }
305  
306 =head1 AUTHOR
307  
308 Bill Hails <me@billhails.net>
309  
310 =head1 SEE ALSO
311  
312 Full documentation is in the command line interface L<treebuild>.
313  
314 =cut
315  
316 2 1;
« Builder/Parser.pm ↑ Summary Builder/Args.pm »