blib/lib/Decl.pm

filter: Statementstmt 87.1% 224 / 257 Branchbran 72.4% 71 / 98 Conditioncond 52.5% 21 / 40 MC/DCmcdc 23.0% 6 / 26 Subroutinesub 82.6% 38 / 46 Podpod 100% 29 / 29 totaltotal 78.4% 389 / 496 CC 72 SCAR 17.7
CC
72
Cov
80%
id
20.0
find_data
10.4
register_builder
10.4
CRAP 5.8
« blib/lib/Decl/Parser.pm ↑ Summary lib/Decl/Semantics/Repeat.pm »
1 package Decl;
2
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use base qw(Decl::EventContext Decl::Node);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Filter::Util::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #use Parse::Indented;
8 #use Parse::RecDescent::Simple;
9 13 ▶use Decl::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Decl::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Decl::DefaultParsers;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Decl::StandardFilters;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Decl::NodalValuator;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 =head1 NAME
20
21 Decl - Provides a declarative framework for Perl
22
23 =head1 VERSION
24
25 Version 0.11
26
27 =cut
28
29 13 our $VERSION = '0.11';
30
31 3 ▶$SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
32 3 ▶ return if $_[0] =~ /Deep recursion.*Parser/; # TODO: Jezus, Maria es minden szentek.
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /Deep recursion.*Parser/ 0 3 3
33 #require Carp; Carp::cluck
34 3 warn $_[0];
35 13 };
36
37
38 =head1 SYNOPSIS
39
40 This module is a framework for writing Perl code in a declarative manner. What that means right now is that instead of seeing a script as a
41 series of actions to be carried out, you can view the script as a set of objects to be instantiated, then invoked. The syntax for building
42 these objects is intended to be concise and flexible, mostly staying out of your way. Perl code is used to declare actions to be taken once
43 the structure is built, as well as any actions to be taken interactively as the script runs.
44
45 The original motivation for designing this framework was to provide a more rational way of defining a L<Wx> user interface. As it is, the
46 data structures making up a Wx GUI are built with painstakingly detailed (and boring) imperative code. There are XML-based GUI specification
47 frameworks, but I wanted to write my own that wasn't XML-based because I hate typing XML even more than I hate writing setup code.
48
49 Back when I did a lot of GUI work, I'd usually write some pseudocode to describe parts of the UI, then translate it into code by hand.
50 So this year, while noodling around about some tools I'd find useful in my translation business, I thought, well,
51 why not just write a class to interpret that pseudocode description directly?
52
53 Once I started getting into that in earnest, I realized that the Wx-specific functionality could be spun out into an application-specific
54 (in my new parlance, a "semantic") domain, leaving a core set of functionality that was a general declarative framework. I then realized that
55 the same framework could easily be used to work with domains other than Wx GUIs, such as building PDFs, building Flash applications, doing
56 things with Word documents... All kinds of things. All of those things are currently in pieces on the workbench - except for the Word
57 module, which is ready, if not for prime time, then at least for deep cable midnight airing.
58
59 Here's a GUI example using something like the Wx domain. This is a pretty simple example, but it gives you a taste of what I'm talking about.
60 Since Decl runs as a source filter, the example below is a working Perl script that replaces roughly 80 lines of the Wx
61 example code it was adapted from. And yes, it runs in my test suite right now.
62
63 use Wx::Declarative;
64
65 dialog (xsize=250, ysize=110) "Wx::Declarative dialog sample"
66 field celsius (size=100, x=20, y=20) "0"
67 button celsius (x=130, y=20) "Celsius" { $^fahrenheit = ($^celsius / 100.0) * 180 + 32; }
68 field fahrenheit (size=100, x=20, y=50) "32"
69 button fahrenheit (x=130, y=50) "Fahrenheit" { $^celsius = (($^fahrenheit - 32) / 180.0) * 100; }
70
71 The main things to look at are as follows: first, yes - syntactically significant indentation. I know it's suspiciously Pythonic, I know all
72 the arguments citing the danger of getting things to line up, and I don't care; this is the way I have always written my pseudocode, and
73 odds are you're no different and you know it. If it makes you feel better, the indentation detection algorithm is pretty flexible, and Perl
74 code within curly braces is exempt from indentation significance. (Not that this example has any multiline code, but you see what I mean.)
75
76 Second, fields are declared here and their content is exposed as magic variables in the code snippets. You will immediately see that code
77 embedded in a declarative structure goes through a modification pass before being C<eval>'d into a sub. So there is a possibility that I
78 have screwed that modification pass up. I don't have an answer for this right now; the point is quick and easy, not perfection (yet).
79 Caveat emptor. It's still a neat feature.
80
81 There is a standard parser and standard data structure available for tags to use if it suits your purpose - but there's no mandate to use them,
82 and the parser tools are open for use. They're still a little raw, but pretty powerful.
83
84 A declarative object can report its own source code, and that source code can compile into an equivalent declarative object. This means that dynamically
85 constructed objects or applications can be written out as executable code, and code has introspective capability while in the loaded state. C<Decl>
86 also has a macro system that allows the construction of code during the build phase; a macro always dumps as its source, not the result of the expansion, so
87 you can capture dynamic behavior that runs dynamically every time.
88
89 =head1 TUTORIAL
90
91 For more information about how to use C<Decl>, you'll probably want to see the tutorial in L<Decl::Semantics>
92 instead of this file; the rest of this presentation is devoted to the internal workings of C<Decl>.
93 (Old literate programming habits, I guess.)
94 Honestly, you can probably just stop here, because if you're not reading the source along with the POD it probably won't make any sense anyway.
95 Go read the tutorial. Not that I've finished it.
96
97 =head1 SETTING UP THE CLASS STRUCTURE
98
99 =head2 import, yes_i_am_declarative, import_one
100
101 The C<import> function is called when the package is imported. It's used for the filter support; don't call it.
102
103 If semantic classes are supplied in the C<use> command, we're going to instantiate and scan them here. They'll be used to decorate the
104 parse tree appropriately.
105
106 =cut
107
108 13 our %build_handlers = ();
109 13 our %build_flags = ();
110 13 our @semantic_classes = ();
111
112 0 ▶sub yes_i_am_declarative { 1 } # This is probably a childish way of doing this.
Subroutine yes_i_am_declarative: not called CC=1 SCAR=6.9
Pod documented
113 13 our $initial_load;
114 sub import
115 24 ▶{
Subroutine import: called CC=7 SCAR=19.5
116 24 my($type, @arguments) = @_;
117
118 24 ▶ if (not defined $initial_load) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Decl::initial_load) 12 12 24
119 12 $initial_load = 1;
120
121 12 ▶ if (!@arguments || $arguments[0] ne '-nofilter') {
Branch: 1 branch
BranchTrueFalseTotal
if (not @arguments or $arguments[0] ne '-nofilter') { } 1 11 12
Condition66%2/3
not @arguments or $arguments[0] ne "-nofilter"
l!l&&r!l&&!r
1011
MC/DC50%1/2
not @arguments or $arguments[0] ne "-nofilter"
not @arguments $arguments[0] ne "-nofilter"
Truth table66%2/3
not(A) or B
A= not @argumentsB= $arguments[0] ne "-nofilter"
ABresult
1X1
011
000
122 1 filter_add(bless { start => 1 });
123 } else {
124 11 ▶ shift @arguments if @arguments;
Branch: 1 branch
BranchTrueFalseTotal
if @arguments 11 0 11
125 }
126 12 ▶ push @arguments, "Decl::Semantics" unless grep { $_ eq "Decl::Semantics" } @arguments;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "Decl::Semantics";} @arguments 1 11 12
127 }
128
129 13 ▶ use lib "./lib"; # This allows us to test semantic modules without disturbing their production variants that are installed.
Subroutine BEGIN: called CC=1 SCAR=0.0
130 24 foreach my $import_module (@arguments) {
131 12 import_one($import_module);
132 }
133 }
134 12 ▶sub import_one {
Subroutine import_one: called CC=4 SCAR=16.0
Pod documented
135 12 my ($import_module) = @_;
136
137 #print "importing $import_module\n";
138 0 ▶ unless (grep { defined $_ and $import_module eq $_ } @semantic_classes) { # Only try to import each semantic class once.
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {defined $_ and $import_module eq $_;} @Decl::semantic_classes) 12 0 12
Condition0%0/3
defined $_ && $import_module eq $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $import_module eq $_
defined $_ $import_module eq $_
Truth table0%0/3
A && B
A= defined $_B= $import_module eq $_
ABresult
0X0
100
111
139 12 ▶ eval "use $import_module;";
Subroutine BEGIN: called CC=1 SCAR=0.0
140 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 12 12
141 0 warn $@;
142 } else {
143 12 push @semantic_classes, $import_module;
144 12 eval 'foreach (' . $import_module . '->decl_include()) { import_one $_ }';
145 }
146 }
147 }
148
149 =head2 class_builders(), find_tagdef($parent, $tag), build_handler ($parent, $tag), register_builder ($node)
150
151 Given a tag name, C<class_build_handler> returns a hashref of information about how the tag expects to be treated:
152
153 * The class its objects should be blessed into, as a coderef to generate the object ('Decl::Node' is the default)
154 * Its line parser, by name ('default-line' is the default)
155 * Its body parser, by name ('default-body' is the default)
156 * A second-level hashref of hashrefs providing overriding semantics for descendants of this tag.
157
158 If you also provide a hashref, it is assigned to the tag name.
159
160 The C<app_build_handler> does the same thing, but specific to the given application - this allows dynamic tag definition.
161
162 Finally, C<build_handler> is a read-only lookup for a tag in the context of its ancestry that climbs the tree to find the contextual
163 semantics for the tag.
164
165 =cut
166
167 13 our $class_builders; # Note: this is initalized below, after the default parsers are set up.
168
169 0 ▶sub class_builders { $class_builders; }
Subroutine class_builders: not called CC=1 SCAR=6.9
Pod documented
170
171 1856 ▶sub find_tagdef {
Subroutine find_tagdef: called CC=9 SCAR=22.2
Pod documented
172 1856 my ($self, $parent, $tag) = @_;
173
174 1856 ▶ my $apptag = $self->{build_handlers} ? $self->{build_handlers}->nodes($tag) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'build_handlers'} ? : 308 1548 1856
175 1856 my $classtag = $class_builders->nodes($tag);
176
177 1856 ▶ my $apptagd = defined $apptag ? $apptag->nodes($parent->{domain}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $apptag ? : 4 1852 1856
178 1856 ▶ my $classtagd = defined $classtag ? $classtag->nodes($parent->{domain}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $classtag ? : 90 1766 1856
179
180 1856 ▶ my $tagdef = $apptagd || $classtagd;
Condition100%3/3
$apptagd || $classtagd
l!l&&r!l&&!r
4901762
MC/DC100%2/2
$apptagd || $classtagd
$apptagd $classtagd
Truth table100%3/3
A || B
A= $apptagdB= $classtagd
ABresult
1X1
011
000
181
182 1856 ▶ $tagdef = $apptag->nodes if not defined $tagdef and defined $apptag;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $tagdef and defined $apptag 0 1856 1856
Condition66%2/3
not defined $tagdef and defined $apptag
!ll&&!rl&&r
9417620
MC/DC0%0/2
not defined $tagdef and defined $apptag
not defined $tagdef defined $apptag
Truth table66%2/3
not(A) and B
A= not defined $tagdefB= defined $apptag
ABresult
0X0
100
111
183 1856 ▶ $tagdef = $classtag->nodes if not defined $tagdef and defined $classtag; #TODO: man, this really doesn't seem right.
Branch: 1 branch
BranchTrueFalseTotal
if not defined $tagdef and defined $classtag 0 1856 1856
Condition66%2/3
not defined $tagdef and defined $classtag
!ll&&!rl&&r
9417620
MC/DC0%0/2
not defined $tagdef and defined $classtag
not defined $tagdef defined $classtag
Truth table66%2/3
not(A) and B
A= not defined $tagdefB= defined $classtag
ABresult
0X0
100
111
184
185 1856 return $tagdef;
186 }
187
188 2733 ▶sub build_handler {
Subroutine build_handler: called CC=9 SCAR=22.3
Pod documented
189 2733 my ($self, $parent, $tag) = @_;
190
191 2733 ▶ if (defined $parent->{parsemode} and $parent->{parsemode} eq 'vanilla') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent->{"parsemode"} and $parent->{"parsemode"} eq "vanilla") 1523 1210 2733
Condition100%3/3
defined $parent->{"parsemode"} and $parent->{"parsemode"} eq "vanilla"
!ll&&!rl&&r
4707401523
MC/DC100%2/2
defined $parent->{"parsemode"} and $parent->{"parsemode"} eq "vanilla"
defined $parent->{"parsemode"} $parent->{"parsemode"} eq "vanilla"
Truth table100%3/3
A and B
A= defined $parent->{"parsemode"}B= $parent->{"parsemode"} eq "vanilla"
ABresult
0X0
100
111
192 1523 ▶ return (defined $parent->{vanilla_class} ? $parent->{vanilla_class} : 'Decl::Node', undef, 'vanilla');
Branch: 1 branch
BranchTrueFalseTotal
defined $parent->{'vanilla_class'} ? : 70 1453 1523
193 }
194
195 1210 my $flag;
196 1210 ($tag, $flag) = Decl::Node::splittag ($tag);
197
198 1210 my $tagdef = $self->find_tagdef($parent, $tag);
199 1210 ▶ return ($tagdef->label, $tagdef->tag, $tagdef->parameter('body'), $tagdef->parameter('line'), $tagdef->parameter('vanilla')) if defined $tagdef;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tagdef 94 1116 1210
200
201 1116 my $vanilla_class = 'Decl::Node';
202
203 1116 ▶ return ($vanilla_class, undef, 'vanilla') unless blessed($parent);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($parent) 470 646 1116
204 646 my $ancestry = $parent->ancestry();
205 646 foreach (@$ancestry) {
206 646 my $t = $self->find_tagdef($parent, $_);
207 646 ▶ if (defined $t and $t->parameter('vanilla')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t and $t->parameter("vanilla")) 0 646 646
Condition33%1/3
defined $t and $t->parameter("vanilla")
!ll&&!rl&&r
64600
MC/DC0%0/2
defined $t and $t->parameter("vanilla")
defined $t $t->parameter("vanilla")
Truth table33%1/3
A and B
A= defined $tB= $t->parameter("vanilla")
ABresult
0X0
100
111
208 0 $vanilla_class = $t->parameter('vanilla');
209 0 last;
210 }
211 }
212 646 return ($vanilla_class, undef, 'vanilla', undef, $vanilla_class);
213 }
214
215 158 ▶sub register_builder {
Subroutine register_builder: called CC=8 SCAR=23.4
Pod documented
216 158 my ($self, $class, $domain, $tags) = @_;
217 158 ▶ my $bh_list = ref($self) ? $self->{build_handlers} : $class_builders;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 2 156 158
218 158 foreach my $tag_to_add ($tags->nodes()) {
219 278 ▶ my $tag = $bh_list->first($tag_to_add->tag) || $bh_list->load($tag_to_add->tag);
Condition33%1/3
$bh_list->first($tag_to_add->tag) || $bh_list->load($tag_to_add->tag)
l!l&&r!l&&!r
02780
MC/DC0%0/2
$bh_list->first($tag_to_add->tag) || $bh_list->load($tag_to_add->tag)
$bh_list->first($tag_to_add->tag) $bh_list->load($tag_to_add->tag)
Truth table33%1/3
A || B
A= $bh_list->first($tag_to_add->tag)B= $bh_list->load($tag_to_add->tag)
ABresult
1X1
011
000
220 278 my $domain_tag = $tag->nodes($domain);
221 278 ▶ if (not defined $domain_tag) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $domain_tag) 278 0 278
222 278 $domain_tag = $tag->load($domain);
223 }
224 278 my $within = $tags->nodes('within');
225 278 ▶ if ($within) {
Branch: 1 branch
BranchTrueFalseTotal
if ($within) 0 278 278
226 0 my $target_within = $domain_tag->load($within->myline());
227 0 $domain_tag = $target_within;
228 }
229 278 $domain_tag->set_label($class);
230 278 $domain_tag->{parmlist} = \@{$tag_to_add->{parmlist}}; # TODO: maybe a real Node copier at some point? This is hardly going to be the first transformation
231 278 $domain_tag->{parameters} = \%{$tag_to_add->{parameters}}; # where this is going to be needed...
232 278 foreach ($tag_to_add->nodes()) {
233 0 ▶ next if $_->is('within');
Branch: 1 branch
BranchTrueFalseTotal
if $_->is("within") 0 0 0
234 0 $domain_tag->load ($_->describe());
235 }
236 }
237 #print STDERR $self->{build_handlers}->describe() if ref($self);
238 }
239
240 =head2 makenode($ancestry, $code)
241
242 Finds the right build handler for the tag in question, then builds the right class of node with the code given.
243
244 =cut
245
246 979 ▶sub makenode {
Subroutine makenode: called CC=5 SCAR=16.1
Pod documented
247 979 my ($self, $parent, $tag, $body) = @_;
248
249 979 my ($build_class, $domain, $parsemode, $linemode, $vanilla_class) = $self->build_handler($parent, $tag);
250 979 my $newnode = $build_class->new($body);
251 979 ▶ if ($vanilla_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vanilla_class) { } 320 659 979
252 320 $newnode->{parsemode} = 'vanilla';
253 320 $newnode->{vanilla_class} = $vanilla_class;
254 } else {
255 659 $newnode->{parsemode} = $parsemode;
256 }
257 979 ▶ if ($newnode->flag('.')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($newnode->flag('.')) { } 12 967 979
elsif ($newnode->flag('*')) { } 1 966 967
elsif ($newnode->flag('+')) { } 0 966 966
258 12 $newnode->{parsemode} = 'text';
259 } elsif ($newnode->flag('*')) {
260 1 $newnode->{parsemode} = 'vanilla';
261 } elsif ($newnode->flag('+')) {
262 0 $newnode->{parsemode} = '';
263 }
264 979 $newnode->{domain} = $domain;
265 979 $newnode;
266 }
267
268 =head2 remakenode($node)
269
270 If it turns out that things have changed semantically since we split a node out, and the node hasn't been built yet
271 (this is specifically to support the "use" tag), then we can signal that the node should be remade, and we'll build
272 and substitute a new node based on the new semantic environment and using the information available to us in the
273 initially created node.
274
275 =cut
276
277 0 ▶sub remakenode {
Subroutine remakenode: not called CC=1 SCAR=6.9
Pod documented
278 0 my ($self, $node) = @_;
279
280 0 my $bh = $self->build_handler($self->parent, $self->tag); #$node->ancestry);
281 0 my $replacement = $bh->{node}->([$node->tag . $node->flag . " " . $node->line, $node->body]);
282 0 $replacement->{parent} = $node->parent;
283 0 return $replacement;
284 }
285
286
287 =head1 FILTERING SOURCE CODE
288
289 By default, C<Decl> runs as a filter. That means it intercepts code coming in and can change it before Perl starts parsing. Needless to say,
290 filters act very cautiously, because the only thing that can parse Perl correctly is Perl (and sometimes even Perl has doubts). So this filter basically just
291 wraps the entire input source in a call to C<new>, which is then parsed and called after the filter returns.
292
293 =head2 filter
294
295 The C<filter> function is called by the source code filtering process. You probably don't want to call it. But if you've ever wondered
296 how difficult it is to write a source code filter, read it. Hint: I<it really isn't difficult>.
297
298 =cut
299
300 sub filter
301 4 ▶{
Subroutine filter: called CC=4 SCAR=13.9
Pod documented
302 4 my $self = shift;
303 4 my $status;
304
305 4 ▶ if (($status = filter_read()) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (($status = &filter_read()) > 0) { } 2 2 4
elsif (not $self->{'start'}) { } 1 1 2
306 2 ▶ if ($$self{start}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"start"}) 1 1 2
307 1 $$self{start} = 0;
308 1 $_ = "my \$root = " . __PACKAGE__ . "->new();\n\$root->load(<<'DeclarativeEOF');\n$_";
309 }
310 } elsif (!$$self{start}) { # Called on EOF if we ever saw any code.
311 1 $_ = "\nDeclarativeEOF\n\n\$root->start();\n\n";
312 1 $$self{start} = 1; # Otherwise we'll repeat the EOF forever.
313 1 $status = 1;
314 }
315
316 4 $status;
317 }
318
319
320 =head1 PARSERS
321
322 The parsing process in C<Decl> is recursive. The basic form is a tagged line followed by indented text, followed by another tagged line
323 with indented text, and so on. Alternatively, the indented part can be surrounded by brackets.
324
325 tag [rest of line]
326 indented text
327 indented text
328 indented text
329 tag [rest of line] {
330 bracketed text
331 bracketed text
332 }
333
334 By default, each tag parses its indented text in the same way, and it's turtles all the way down. Bracketed text, however, is normally I<not> parsed as
335 declarative (or "nodal") structure, but is left untouched for special handling, typically being parsed by Perl and wrapped as a closure.
336
337 To force content to be handled as text instead of nodal structure, put a period on the end of the tag. Some tags are defined with this as the default;
338 for these you can force normal nodal structure with a '!', or data-only nodal structure with a '*'.
339
340 However, all this is merely the default. Any tag may also specify a different parser for its own indented text, or may carry out some transformation on the
341 text before invoking the parser. It's up to the tag. The C<data> tag, for instance, treats each indented line as a row in a table.
342
343 Once the body is handled, the "rest of line" is also parsed into data useful for the node. Again, there is a default parser, which takes a line of the
344 following form:
345
346 tag name (parameter, parameter=value) [option, option=value] "label or other string text" parser < { bracketed text }
347
348 Any element of that line may be omitted, except for the tag.
349
350 =head2 init_parsers()
351
352 Sets up the registry and builds our default line and body parsers.
353
354 =cut
355
356 29 ▶sub init_parsers {
Subroutine init_parsers: called CC=1 SCAR=0.0
Pod documented
357 29 my ($self) = @_;
358 29 $self->{parsers} = {};
359
360 #$self->{parsers}->{"default-line"} = $self->init_default_line_parser();
361 #$self->{parsers}->{"default-body"} = $self->init_default_body_parser();
362 #$self->{parsers}->{"locator"} = $self->init_locator_parser();
363 }
364
365 13 our %default_parsers = ();
366 13 $default_parsers{'default-line'} = Decl::DefaultParsers::init_default_line_parser(undef);
367 13 $default_parsers{'default-body'} = Decl::DefaultParsers::init_default_body_parser(undef);
368 13 $default_parsers{'locator'} = Decl::DefaultParsers::init_locator_parser(undef);
369
370
371 13 $class_builders = Decl->new_data_with_label('*cbh'); # Have to initialize this after the default parsers are defined...
372
373 =head2 parser($name)
374
375 Retrieves a parser from the registry.
376
377 =cut
378
379 2078 ▶sub parser {
Subroutine parser: called CC=2 SCAR=7.0
Pod documented
380 2078 my ($self, $parsername) = @_;
381 2078 my $possible = $self->{parsers}->{$parsername};
382 2078 ▶ return $possible if $possible;
Branch: 1 branch
BranchTrueFalseTotal
if $possible 0 2078 2078
383 2078 $default_parsers{$parsername};
384 }
385
386 =head2 parse_line ($node)
387
388 Given a node, finds the line parser for it, and runs it on the node's line.
389
390 =cut
391
392 980 ▶sub parse_line {
Subroutine parse_line: called CC=4 SCAR=14.5
Pod documented
393 980 my ($self, $node, $line) = @_;
394
395 980 my ($class, $domain, $bodyp, $linep) = $self->build_handler($node->parent, $node->tag);
396 980 ▶ return if defined $linep and $linep eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if defined $linep and $linep eq "none" 0 980 980
Condition66%2/3
defined $linep and $linep eq "none"
!ll&&!rl&&r
931490
MC/DC0%0/2
defined $linep and $linep eq "none"
defined $linep $linep eq "none"
Truth table66%2/3
A and B
A= defined $linepB= $linep eq "none"
ABresult
0X0
100
111
397 980 ▶ my $p = $self->parser($linep || 'default-line');
Condition50%1/2
$linep || 'default-line'
l!l
0980
MC/DC0%0/1
$linep || 'default-line'
$linep
Truth table50%1/2
A
A= $linep
Aresult
11
00
398 980 $p->execute($node, $line); # TODO: error handler for incorrect parser specification.
399 }
400
401 =head2 parse($node, $body)
402
403 Given a node and body text for it, finds the body parser appropriate to the node's tag and runs it on the node and the body text specified.
404
405 =cut
406
407 795 ▶sub parse {
Subroutine parse: called CC=6 SCAR=18.0
Pod documented
408 795 my ($self, $node, $body) = @_;
409
410 795 ▶ return if $node->{parsemode} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $node->{"parsemode"} eq "text" 21 774 795
411
412 774 my ($class, $domain, $bodyp, $linep) = $self->build_handler($node->parent, $node->tag);
413 774 ▶ $bodyp = 'default-body' if $bodyp eq 'text' and $node->{parsemode} eq 'vanilla';
Branch: 1 branch
BranchTrueFalseTotal
if $bodyp eq "text" and $node->{"parsemode"} eq "vanilla" 1 773 774
Condition66%2/3
$bodyp eq "text" and $node->{"parsemode"} eq "vanilla"
!ll&&!rl&&r
77301
MC/DC50%1/2
$bodyp eq "text" and $node->{"parsemode"} eq "vanilla"
$bodyp eq "text" $node->{"parsemode"} eq "vanilla"
Truth table66%2/3
A and B
A= $bodyp eq "text"B= $node->{"parsemode"} eq "vanilla"
ABresult
0X0
100
111
414 774 ▶ $bodyp = 'default-body' if $bodyp eq 'vanilla';
Branch: 1 branch
BranchTrueFalseTotal
if $bodyp eq "vanilla" 773 1 774
415 774 ▶ my $p = $self->parser($bodyp || 'default-body');
Condition50%1/2
$bodyp || 'default-body'
l!l
7740
MC/DC0%0/1
$bodyp || 'default-body'
$bodyp
Truth table50%1/2
A
A= $bodyp
Aresult
11
00
416 774 $p->execute($self, $node, $body);
417 }
418
419 =head2 parse_using($string, $parser)
420
421 Given a string and the name of a parser, calls the parser on the string and returns the result.
422
423 =cut
424
425 320 ▶sub parse_using {
Subroutine parse_using: called CC=2 SCAR=6.9
Pod documented
426 320 my ($self, $string, $parser) = @_;
427 320 my $p = $self->parser($parser);
428 320 ▶ return undef unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 1 319 320
429 319 return $p->execute($string);
430 }
431
432 =head1 TEMPLATE ENGINE
433
434 The macro system in Decl uses a template engine implemented in Decl::Template. However, the plain vanilla "valuator" (the
435 function used by a given template engine instance to find values for fields with particular names/specs) is replaced in the
436 Decl node environment by a much more powerful valuator. That valuator is implemented in Decl::NodalValuator.
437
438 We instantiate a template engine with a nodal valuator for use by the macro system here.
439
440 =cut
441
442 13 our $template_engine = Decl::NodalValuator::instantiate();
443
444 =head1 BUILDING AND MANAGING THE APPLICATION
445
446 You'd think this would be up at the top, but we had to do a lot of work just to be ready to instantiate a C<Decl> object.
447
448 =head2 new, new_data, new_data_with_label
449
450 The C<new> function is of course called to create a new C<Decl> object. If you pass it some code, it will load that code
451 immediately.
452
453 The C<new_data> is used if you don't want anything to have any semantics or action. It's used for some internal data structures.
454 "Describe" works the same way, not specifying the root tag. This may not be what you want.
455
456 Finally C<new_data_with_label> allows you to provide a different *-tag for the data; this could be useful for debugging. Or I might
457 get rid of it. I don't know yet. It's only used internally in this module anyway.
458
459 =cut
460
461 29 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
462 29 my $class = shift;
463 29 my $self = $class->SUPER::new('*root');
464 29 $self->{id_list} = {};
465 29 $self->{next_id} = 1;
466 29 $self->{root} = $self;
467
468 29 $self->init_parsers;
469
470 29 $self->{build_handlers} = Decl->new_data_with_label("*bh");
471
472 29 $self->{semantics} = {};
473 29 $self->{semtags} = {};
474 29 $self->{controller} = '';
475
476 29 foreach (@semantic_classes) { $self->initiate_semantic_class($_); }
477
478 #print STDERR $class_builders->describe; die;
479
480 29 $self->event_context_init;
481
482 29 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 17 12 29
483 17 $self->load($_[0]);
484 }
485 29 return $self;
486 }
487
488 42 ▶sub new_data_with_label {
Subroutine new_data_with_label: called CC=1 SCAR=0.0
Pod documented
489 42 my $class = shift;
490 42 my $label = shift;
491 42 my $self = $class->new_data(@_);
492 42 $self->{tag} = $label;
493 42 return $self;
494 }
495
496 200 ▶sub new_data {
Subroutine new_data: called CC=2 SCAR=6.9
Pod documented
497 200 my $class = shift;
498 200 my $self = $class->SUPER::new('*data');
499 200 $self->{id_list} = {};
500 200 $self->{next_id} = 1;
501 200 $self->{root} = $self;
502
503 200 $self->{semantics} = {};
504 200 $self->{semtags} = {};
505 200 $self->{controller} = '';
506 200 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 158 42 200
507 158 $self->load($_[0]);
508 }
509 200 $self->{parsemode} = 'vanilla';
510 200 return $self;
511 }
512
513 =head2 initiate_semantic_class
514
515 Does what it says on the tin.
516
517 =cut
518
519 58 ▶sub initiate_semantic_class {
Subroutine initiate_semantic_class: called CC=4 SCAR=14.0
Pod documented
520 58 my ($self, $class) = @_;
521 58 ▶ return unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 58 58
522 58 ▶ return if defined $self->{semtags}->{$class};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"semtags"}{$class} 29 29 58
523 29 my $s = $class->new($self);
524 29 $self->{semtags}->{$class} = $s->tag;
525 29 ▶ $self->{controller} = $s->tag unless $self->{controller};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"controller"} 29 0 29
526 29 $self->{semantics}->{$s->tag} = $s;
527 }
528
529 =head2 semantic_handler ($tag)
530
531 Returns the instance of a semantic module, such as 'core' or 'wx'.
532
533 =cut
534
535 23 ▶sub semantic_handler { $_[0]->{semantics}->{$_[1]} }
Subroutine semantic_handler: called CC=1 SCAR=0.0
Pod documented
536
537
538 =head2 start
539
540 This is called from outside to kick off the process defined in this application. The way we handle this is just to ask the first semantic class to start
541 itself. The idea there being that it's probably going to be Wx or something that provides the interface. (It could also be a Web server or something.)
542
543 The core semantics just execute all the top-level items that are flagged callable.
544
545 =cut
546
547 11 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
548 11 my ($self, $tag) = @_;
549
550 11 $self->{callable} = 1;
551 11 $self->go();
552 #$tag = $self->{controller} unless $tag;
553 #$self->{semantics}->{$tag}->start;
554 }
555
556
557 =head2 id($idstring)
558
559 Wx works with numeric IDs for events, and I presume the other event-based systems do, too. I don't like numbers; they're hard to read and tell apart.
560 So C<Decl> registers event names for you, assigning application-wide unique numeric IDs you can use in your payload objects.
561
562 =cut
563
564 0 ▶sub id {
Subroutine id: not called CC=4 SCAR=30.0
Pod documented
565 0 my ($self, $str) = @_;
566
567 0 ▶ if (not defined $str or not $str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str and $str) 0 0 0
Condition0%0/3
defined $str and $str
!ll&&!rl&&r
000
MC/DC0%0/2
defined $str and $str
defined $str $str
Truth table0%0/3
A and B
A= defined $strB= $str
ABresult
0X0
100
111
568 0 my $retval = $self->{next_id} ++;
569 0 return $retval;
570 }
571 0 ▶ if (not defined $self->{id_list}->{$str}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"id_list"}{$str}) 0 0 0
572 0 $self->{id_list}->{$str} = $self->{next_id} ++;
573 }
574 0 return $self->{id_list}->{$str};
575 }
576
577
578 =head2 root()
579
580 Returns $self; for nodes, returns the parent. The upshot is that by calling C<root> we can get the root of the tree, fast.
581
582 =cut
583
584 4826 ▶sub root { $_[0] }
Subroutine root: called CC=1 SCAR=0.0
Pod documented
585
586 =head2 mylocation()
587
588 Special case: returns a slash. (It's the root.)
589
590 =cut
591
592 0 ▶sub mylocation { '/'; }
Subroutine mylocation: not called CC=1 SCAR=6.9
Pod documented
593
594 =head2 describe([$use])
595
596 Returns a reconstructed set of source code used to compile this present C<Decl> object. If it was assembled
597 in parts, you still get the whole thing back. Macro results are not included in this dump (they're presumed to be the result
598 of macros in the tree itself, so they should be regenerated the next time anyway).
599
600 If you specify a true value for $use, the dump will include a "use" statement at the start in order to make the result an
601 executable Perl script.
602 The dump is always in filter format (if you built it with -nofilter) and contains C<Decl>'s best guess of the
603 semantic modules used. If you're using a "use lib" to affect your %INC, the result won't work right unless you modify it,
604 but if it's all standard modules, the dump result, after loading, should work the same as the original entry.
605
606 =cut
607
608 4 ▶sub describe {
Subroutine describe: called CC=7 SCAR=20.4
Pod documented
609 4 my ($self, $macro_ok, $use) = @_;
610
611 4 ▶ $macro_ok = 0 unless defined $macro_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $macro_ok 2 2 4
612
613 4 my $description = '';
614 4 ▶ $description = "use Decl qw(" . join (", ", @semantic_classes) . ");\n\n" if $use;
Branch: 1 branch
BranchTrueFalseTotal
if $use 0 4 4
615
616 4 foreach ($self->elements) {
617 27 ▶ if (not ref $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_) { } 12 15 27
elsif ($_->{'macroresult'} and not $macro_ok) { } 0 15 15
618 12 $description .= $_;
619 ▶ } elsif ($_->{macroresult} and not $macro_ok) {
Condition33%1/3
$_->{"macroresult"} and not $macro_ok
!ll&&!rl&&r
1500
MC/DC0%0/2
$_->{"macroresult"} and not $macro_ok
$_->{"macroresult"} not $macro_ok
Truth table33%1/3
A and not(B)
A= $_->{"macroresult"}B= not $macro_ok
ABresult
0X0
100
111
620 0 next;
621 } else {
622 15 $description .= $_->describe($macro_ok);
623 }
624 }
625
626 4 return $description;
627 }
628
629 =head2 find_data
630
631 The C<find_data> function finds a top-level data node.
632
633 =cut
634
635 4 ▶sub find_data {
Subroutine find_data: called CC=5 SCAR=23.4
Pod documented
636 4 my ($self, $data) = @_;
637 4 ▶ foreach ($self->nodes) { return ($_, $_->tag) if $_->name eq $data; }
Branch: 1 branch
BranchTrueFalseTotal
if $_->name eq $data 4 0 4
638 0 ▶ foreach ($self->nodes) { return ($_, $_->tag) if $_->is($data); }
Branch: 1 branch
BranchTrueFalseTotal
if $_->is($data) 0 0 0
639 0 return (undef, undef);
640 }
641
642
643 =head2 write, log
644
645 Normal nodes send these to their parents if not otherwise set for the node; at the top level, unless otherwise set, we print to STDOUT or STDERR.
646
647 =cut
648
649 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
650 0 my $self = shift;
651 0 print STDOUT @_;
652 }
653 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
654 0 my $self = shift;
655 0 print STDERR @_;
656 }
657
658 =head1 FILTER REGISTRY
659
660 A C<filter> in Decl is just a function that takes one string and returns another. (TODO: something iterator- and stream-aware, I suppose.)
661 It's used for text blocks. A filter call can take additional parameters as well, but doesn't have to.
662
663 Filters are called using C<call_filter> on any given node; a search is made for the appropriate filter and it's invoked, if found. If it's not found,
664 then a globally registered filter is called (this permits libraries to contain filters). This filter registry is where that is managed.
665
666 =head2 register_filter ($name, $coderef, $origin)
667
668 During load, a module can register a filter with C<register_filter>. (It can happen any other time, too, of course.) To find a registered filter,
669 you can call register_filter without a code reference, and if there is such a filter registered under the name, it will be returned.
670
671 The C<$origin> parameter is something you can use for debugging.
672
673 Decl->register_filter('myfilter', sub { ... }, 'where I defined this');
674
675 =cut
676
677 13 our %registered_filters = ();
678 13 our %registered_filter_origins = ();
679 39 ▶sub register_filter {
Subroutine register_filter: called CC=3 SCAR=11.3
Pod documented
680 39 my ($class, $name, $coderef, $origin) = @_;
681 39 ▶ if (defined $coderef) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $coderef) 39 0 39
682 39 $registered_filters{$name} = $coderef;
683 39 $registered_filter_origins{$name} = $origin;
684 }
685 39 ▶ wantarray ? ($registered_filters{$name}, $registered_filter_origins{$name}) : $registered_filters{$name};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 39 39
686 }
687 13 Decl::DefaultFilters->init_default_filters();
688
689 =head2 registered_filters()
690
691 Returns a sorted list of all global filter names.
692
693 =cut
694
695 0 ▶sub registered_filters { sort keys %registered_filters }
Subroutine registered_filters: not called CC=1 SCAR=6.9
Pod documented
696
697 =head1 AUTHOR
698
699 Michael Roberts, C<< <michael at vivtek.com> >>
700
701 =head1 BUGS
702
703 Please report any bugs or feature requests to C<bug-decl at rt.cpan.org>, or through
704 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Decl>. I will be notified, and then you'll
705 automatically be notified of progress on your bug as I make changes.
706
707
708
709
710 =head1 SUPPORT
711
712 You can find documentation for this module with the perldoc command.
713
714 perldoc Decl
715
716
717 You can also look for information at:
718
719 =over 4
720
721 =item * RT: CPAN's request tracker
722
723 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Decl>
724
725 =item * AnnoCPAN: Annotated CPAN documentation
726
727 L<http://annocpan.org/dist/Decl>
728
729 =item * CPAN Ratings
730
731 L<http://cpanratings.perl.org/d/Decl>
732
733 =item * Search CPAN
734
735 L<http://search.cpan.org/dist/Decl/>
736
737 =back
738
739
740 =head1 ACKNOWLEDGEMENTS
741
742
743 =head1 LICENSE AND COPYRIGHT
744
745 Copyright 2011 Michael Roberts.
746
747 This program is free software; you can redistribute it and/or modify it
748 under the terms of either: the GNU General Public License as published
749 by the Free Software Foundation; or the Artistic License.
750
751 See http://dev.perl.org/licenses/ for more information.
752
753
754 =cut
755
756 13 1; # End of Decl
« blib/lib/Decl/Parser.pm ↑ Summary lib/Decl/Semantics/Repeat.pm »