blib/lib/Decl/Node.pm

filter: Statementstmt 69.2% 407 / 588 Branchbran 60.1% 201 / 334 Conditioncond 39.0% 34 / 87 MC/DCmcdc 24.5% 13 / 53 Subroutinesub 67.6% 88 / 130 Podpod 100% 98 / 98 totaltotal 65.1% 841 / 1290 CC 229 SCAR 25.2
CC
229
Cov
64%
find_ref
72.0
search_data
56.0
find_filter
42.0
CRAP 12.4
« lib/Decl/Semantics/Use.pm ↑ Summary blib/lib/Decl/DefaultParsers.pm »
1 package hashtie;
2 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 #require Tie::Hash;
5 #our @ISA = qw(Tie::ExtraHash); - Apparently only standard in perl >= 5.10. I'm copying it here to remove that dependency, because
6 # let's face it, it's eleven lines of code.
7 # Nota bene: CPAN Testers freaking rock!
8
9 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
10 0 my $pkg = shift;
11 0 $pkg->TIEHASH(@_);
12 }
13 1210 ▶sub TIEHASH { my $p = shift; bless [{}, @_], $p }
Subroutine TIEHASH: called CC=1 SCAR=0.0
14 #sub STORE { $_[0][0]{$_[1]} = $_[2] }
15 #sub FETCH { $_[0][0]{$_[1]} }
16 0 ▶sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
17 0 ▶sub NEXTKEY { each %{$_[0][0]} }
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
18 108 ▶sub EXISTS { exists $_[0][0]->{$_[1]} }
Subroutine EXISTS: called CC=1 SCAR=0.0
19 0 ▶sub DELETE { delete $_[0][0]->{$_[1]} }
Subroutine DELETE: not called CC=1 SCAR=6.9
20 0 ▶sub CLEAR { %{$_[0][0]} = () }
Subroutine CLEAR: not called CC=1 SCAR=6.9
21 0 ▶sub SCALAR { scalar %{$_[0][0]} }
Subroutine SCALAR: not called CC=1 SCAR=6.9
22
23 # My versions of STORE and FETCH.
24 28 ▶sub STORE {
Subroutine STORE: called CC=3 SCAR=11.2
25 28 my ($this, $key, $value) = @_;
26 28 ▶ if ($this->[1]{$key}) { return &{$this->[1]{$key}}(undef, $this->[0], $key, $value); }
Branch: 1 branch
BranchTrueFalseTotal
if ($this->[1]{$key}) 1 27 28
27 27 ▶ return $this->[2]->setvalue($key, $value) if defined $this->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->[2] 27 0 27
28 0 $this->[0]{$key} = $value;
29 }
30
31 52 ▶sub just_store {
Subroutine just_store: called CC=1 SCAR=0.0
32 52 my ($this, $key, $value) = @_;
33 52 $this->[0]{$key} = $value;
34 }
35
36 45 ▶sub FETCH {
Subroutine FETCH: called CC=3 SCAR=11.2
37 45 my ($this, $key, $value) = @_;
38 45 ▶ if ($this->[1]{$key}) { return &{$this->[1]{$key}}(undef, $this->[0], $key); }
Branch: 1 branch
BranchTrueFalseTotal
if ($this->[1]{$key}) 4 41 45
39 41 ▶ return $this->[2]->get_value($key) if defined $this->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $this->[2] 41 0 41
40 0 $this->[0]{$key};
41 }
42
43 148 ▶sub just_get {
Subroutine just_get: called CC=1 SCAR=0.0
44 148 my ($this, $key, $value) = @_;
45 148 $this->[0]{$key};
46 }
47
48 package Decl::Node;
49
50 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
52
53 13 ▶use Iterator::Simple qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 13 ▶use Text::ParseWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 13 ▶use Decl::Semantics::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 13 ▶use Decl::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
59
60 =head1 NAME
61
62 Decl::Node - implements a node in a declarative structure.
63
64 =head1 VERSION
65
66 Version 0.08
67
68 =cut
69
70 13 our $VERSION = '0.08';
71
72
73 =head1 SYNOPSIS
74
75 Each node in a C<Decl> structure is represented by one of these objects. Specific semantics modules subclass these nodes for each of their
76 components.
77
78 =head2 defines(), tags_defined()
79
80 Called by C<Decl> during import, to find out what xmlapi tags this plugin claims to implement. This is a class method, and by default
81 we've got nothing.
82
83 The C<wantsbody> function governs how C<iterator> works.
84
85 =cut
86 0 ▶sub defines { (); }
Subroutine defines: not called CC=1 SCAR=6.9
Pod documented
87 0 ▶sub tags_defined {
Subroutine tags_defined: not called CC=2 SCAR=17.9
Pod documented
88 0 my $self = shift;
89 0 my $tag = Decl->new_data('handle');
90 0 foreach ($self->defines()) {
91 0 $tag->load($_);
92 }
93 0 return $tag;
94 }
95
96 =head2 overloaded ""
97
98 The node class returns tag(class) when expressed as a string.
99
100 =cut
101
102 13 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
103 495 ▶use overload ('""' => sub { $_[0]->tag . '(' . ref($_[0]) . ':' . refaddr($_[0]) . ')' },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
104 20 ▶ '==' => sub { refaddr($_[0]) eq refaddr($_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
105 3 ▶ 'eq' => sub { refaddr($_[0]) eq refaddr($_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
106 0 ▶ '!=' => sub { refaddr($_[0]) ne refaddr($_[1]) });
Subroutine __ANON__: not called CC=1 SCAR=0.2
107
108 =head2 refaddr_or_undef
109
110 This is a cheap trick we're going to use for inserting children after other children.
111
112 =cut
113
114 188 ▶sub refaddr_or_undef {
Subroutine refaddr_or_undef: called CC=2 SCAR=6.9
Pod documented
115 188 my $r = refaddr ($_[0]);
116 188 ▶ $r = $_[0] if not defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 36 152 188
117 188 $r;
118 }
119
120 =head2 new()
121
122 The constructor for a node takes either one or an arrayref containing two texts. If one, it is the entire line-and-body of a node;
123 if the arrayref, the line and the body are already separated. If they're delivered together, they're split before proceeding.
124
125 The line and body are retained, although they may be further parsed later. If the body is parsed, its text is discarded and is reconstructed if it's
126 needed for self-description. (This can be suppressed if a non-standard parser is used that has no self-description facility.)
127
128 The node's I<tag> is the first word in the line. The tag determines everything pertaining to this entire section of the
129 application, including how its contents are parsed.
130
131 =cut
132
133 1210 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
134 1210 my $class = shift;
135 #print STDERR "Adding $class\n";
136 my $self = bless {
137 state => 'unparsed', # Fresh.
138 payload => undef, # Not built.
139 0 ▶ sub => sub {}, # Null action.
Subroutine __ANON__: not called CC=1 SCAR=0.0
140 1210 callable => 0, # Default is not callable.
141 owncode => 0, # Default doesn't have own callable code.
142 macroresult => 0, # Default is explicit text.
143 flag => '', # Indicates special handling of content.
144 name => '',
145 namelist => [],
146 parameters => {},
147 parmlist => [],
148 options => {},
149 optionlist => [],
150 label => '',
151 parser => undef,
152 code => undef,
153 finalcode => undef,
154 errors => [],
155 elements => [],
156 parent => undef,
157 comment => '',
158 bracket => 0,
159 replaced => 0,
160 group => 0,
161 parsemode => '', # Default is to use class nodes. Other valid values: text, vanilla.
162 is_reference=> 0,
163 }, $class;
164
165 1210 my %values = ();
166 1210 my %handlers = ();
167 1210 $self->{hashtie} = tie %values, 'hashtie', \%handlers, $self;
168 1210 $self->{v} = \%values;
169 1210 $self->{h} = \%handlers;
170 1210 $self->{e} = {};
171
172 # Now prepare the body as needed.
173 1210 my ($line, $body);
174 1210 $body = shift;
175 #print STDERR "new: body is " . Dumper ($body);
176 1210 ▶ $body = '' unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 1 1209 1210
177 1210 ▶ if (ref $body eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq 'ARRAY') { } 10 1200 1210
178 #print STDERR "new: body is arrayref\n";
179 {
180 10 my @bodyrest;
181 10 ($line, @bodyrest) = @$body;
182 #print STDERR "new: first line is $line\n";
183 10 $body = \@bodyrest;
184 }
185 } else {
186 1200 ($line, $body) = split /\n/, $body, 2;
187 }
188
189 1210 ▶ $line = 'node' unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 1209 1210
190 1210 my ($fulltag, $rest) = split /\s+/, $line, 2;
191 1210 my ($tag, $flag) = splittag ($fulltag);
192 1210 $self->{tag} = $tag;
193 1210 $self->{flag} = $flag;
194 1210 $self->{line} = $rest;
195 1210 ▶ $self->{line} = '' if not defined $self->{line};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"line"} 939 271 1210
196 1210 $self->{body} = $body;
197
198 1210 return $self;
199 }
200
201 =head2 splittag - class method
202
203 This splits the flag off a tag (e.g. template. => template + .)
204
205 =cut
206
207 2420 ▶sub splittag { $_[0] =~ /^(.*?)([\?\!\*\.\+:]*)$/; }
Subroutine splittag: called CC=1 SCAR=0.0
Pod documented
208
209 =head2 tag(), flag(), is($tag), name(), names(), line(), hasbody(), body(), elements(), truenodes(), payload()
210
211 Accessor functions.
212
213 =cut
214
215 11901 ▶sub tag { $_[0]->{tag} }
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
216 3020 ▶sub flag { $_[1] ? (index ($_[0]->{flag}, $_[1]) >= 0) : $_[0]->{flag} }
Subroutine flag: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 2979 41 3020
217 24530 ▶sub is {
Subroutine is: called CC=3 SCAR=11.0
Pod documented
218 24530 my ($self, $is) = @_;
219 24530 foreach (split /\|/, $is) {
220 24532 ▶ return 1 if $self->{tag} eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tag"} eq $_ 114 24418 24532
221 }
222 24416 return 0;
223 }
224 88 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
225 0 ▶sub names { @{$_[0]->{namelist}} }
Subroutine names: not called CC=1 SCAR=6.9
Pod documented
226 981 ▶sub line { $_[0]->{line} }
Subroutine line: called CC=1 SCAR=0.0
Pod documented
227 0 ▶sub hasbody { defined $_[0]->{body} ? ($_[0]->{body} ? 1 : 0) : 0 }
Subroutine hasbody: not called CC=3 SCAR=24.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
defined $_[0]{'body'} ? : 0 0 0
$_[0]{'body'} ? : 0 0 0
228 2130 ▶sub body { $_[0]->{body} }
Subroutine body: called CC=1 SCAR=0.0
Pod documented
229 822 ▶sub elements { @{$_[0]->{elements}} }
Subroutine elements: called CC=1 SCAR=0.0
Pod documented
230 10880 ▶sub truenodes { grep { ref $_ && (defined $_[1] ? $_->is($_[1]) : 1) } @{$_[0]->{elements}} }
Subroutine truenodes: called CC=3 SCAR=11.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 31372 31372
Condition66%2/3
ref $_ && (defined $_[1] ? $_->is($_[1]) : 1)
!ll&&!rl&&r
203031372
MC/DC50%1/2
ref $_ && (defined $_[1] ? $_->is($_[1]) : 1)
ref $_ (defined $_[1] ? $_->is($_[1]) : 1)
Truth table66%2/3
A && B
A= ref $_B= (defined $_[1] ? $_->is($_[1]) : 1)
ABresult
0X0
100
111
231 992 ▶sub payload { $_[0]->{payload} }
Subroutine payload: called CC=1 SCAR=0.0
Pod documented
232
233 =head2 nodes($flavor)
234
235 The I<true> nodes (C<truenodes()> of a parent are the actual structural children that aren't comments. This function returns
236 the I<functional> nodes - by using a grouping structure, the results of macros, selects, and inserts can appear to be rooted
237 in the parent at precisely the place their progenitor is located.
238
239 If C<$flavor> is specified, C<nodes()> returns only those children with tags equal to C<$flavor>; otherwise, all functional
240 children are returned.
241
242 =cut
243
244 10869 ▶sub nodes {
Subroutine nodes: called CC=7 SCAR=19.6
Pod documented
245 10869 my ($self, $flavor) = @_;
246 10869 my @return = ();
247
248 10869 foreach my $n ($self->truenodes) {
249 31343 ▶ if ($n->{group}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n->{'group'}) { } 0 31343 31343
elsif (defined $flavor ? $n->is($flavor) : 1) { } 7015 24328 31343
250 0 push @return, $n->nodes($flavor);
251 ▶ } elsif (defined $flavor ? $n->is($flavor) : 1) {
Branch: 1 branch
BranchTrueFalseTotal
defined $flavor ? : 24422 6921 31343
252 7015 push @return, $n;
253 }
254 }
255 10869 ▶ return wantarray ? @return : (@return ? $return[0] : undef);
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 8019 2850 10869
@return ? : 194 2656 2850
256 }
257
258 =head2 content_nodes($flavor)
259
260 The I<content> nodes of a parent are the functional nodes returned by C<nodes> minus any that have the flag ':'. This permits nodes to be split
261 into "meta" specifications and child specifications for a given parent. An example might be providing a "style:" parameter for a text structure, or
262 a "path:" parameter for a directory.
263
264 =cut
265
266 11 ▶sub content_nodes {
Subroutine content_nodes: called CC=9 SCAR=28.8
Pod documented
267 11 my ($self, $flavor) = @_;
268 11 my @return = ();
269
270 11 foreach my $n ($self->truenodes) {
271 29 ▶ if ($n->{group} and not $n->flag(':')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n->{'group'} and not $n->flag(':')) { } 0 29 29
elsif (defined $flavor ? $n->is($flavor) : 1 and not $n->flag(':')) { } 29 0 29
Condition33%1/3
$n->{"group"} and not $n->flag(":")
!ll&&!rl&&r
2900
MC/DC0%0/2
$n->{"group"} and not $n->flag(":")
$n->{"group"} not $n->flag(":")
Truth table33%1/3
A and not(B)
A= $n->{"group"}B= not $n->flag(":")
ABresult
0X0
100
111
272 0 push @return, $n->content_nodes($flavor);
273 ▶ } elsif ((defined $flavor ? $n->is($flavor) : 1) and not $n->flag(':')) {
Branch: 1 branch
BranchTrueFalseTotal
defined $flavor ? : 0 29 29
Condition33%1/3
defined $flavor ? $n->is($flavor) : 1 and not $n->flag(":")
!ll&&!rl&&r
0029
MC/DC0%0/2
defined $flavor ? $n->is($flavor) : 1 and not $n->flag(":")
defined $flavor ? $n->is($flavor) : 1 not $n->flag(":")
Truth table33%1/3
A and not(B)
A= defined $flavor ? $n->is($flavor) : 1B= not $n->flag(":")
ABresult
0X0
100
111
274 29 push @return, $n;
275 }
276 }
277 11 ▶ return wantarray ? @return : (@return ? $return[0] : undef);
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 11 0 11
@return ? : 0 0 0
278 }
279
280
281 =head2 parent(), ancestry()
282
283 A list of all the tags of nodes above this one, culminating in this one's tag, returned as an arrayref.
284
285 =cut
286
287 7333 ▶sub parent { $_[0]->{parent} }
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
288 646 ▶sub ancestry {
Subroutine ancestry: called CC=3 SCAR=13.3
Pod documented
289 646 my ($self) = @_;
290 646 my $parent = $self->parent();
291 0 ▶ (defined $parent and $parent != $self->root()) ? [@{$parent->ancestry()}, $self->tag()] : [$self->tag()];
Branch: 1 branch
BranchTrueFalseTotal
defined $parent && $parent != $self->root ? : 0 646 646
Condition33%1/3
defined $parent and $parent != $self->root
!ll&&!rl&&r
64600
MC/DC0%0/2
defined $parent and $parent != $self->root
defined $parent $parent != $self->root
Truth table33%1/3
A and B
A= defined $parentB= $parent != $self->root
ABresult
0X0
100
111
292 }
293
294 =head2 parameter($p), option($o), parmlist(), optionlist(), parameter_n(), option_n(), label(), parser(), code(), gencode(), errors(), bracket(), comment()
295
296 More accessor functions.
297
298 =cut
299
300 355 ▶sub parameter { $_[0]->{parameters}->{$_[1]} || $_[2] || '' }
Subroutine parameter: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"parameters"}{$_[1]} or $_[2] or "" 111 244 355
Condition100%3/3
$_[0]{"parameters"}{$_[1]} or $_[2]
l!l&&r!l&&!r
1092244
MC/DC100%2/2
$_[0]{"parameters"}{$_[1]} or $_[2]
$_[0]{"parameters"}{$_[1]} $_[2]
Truth table100%3/3
A or B
A= $_[0]{"parameters"}{$_[1]}B= $_[2]
ABresult
1X1
011
000
301 8 ▶sub option { $_[0]->{options}->{$_[1]} || $_[2] || '' }
Subroutine option: called CC=3 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"options"}{$_[1]} or $_[2] or "" 8 0 8
Condition33%1/3
$_[0]{"options"}{$_[1]} or $_[2]
l!l&&r!l&&!r
800
MC/DC0%0/2
$_[0]{"options"}{$_[1]} or $_[2]
$_[0]{"options"}{$_[1]} $_[2]
Truth table33%1/3
A or B
A= $_[0]{"options"}{$_[1]}B= $_[2]
ABresult
1X1
011
000
302 2 ▶sub option_n { ($_[0]->optionlist)[$_[1]-1] }
Subroutine option_n: called CC=1 SCAR=0.0
Pod documented
303 8 ▶sub parameter_n { ($_[0]->parmlist)[$_[1]-1] }
Subroutine parameter_n: called CC=1 SCAR=0.0
Pod documented
304 62 ▶sub parmlist { @{$_[0]->{parmlist}} }
Subroutine parmlist: called CC=1 SCAR=0.0
Pod documented
305 66 ▶sub optionlist { @{$_[0]->{optionlist}} }
Subroutine optionlist: called CC=1 SCAR=0.0
Pod documented
306 187 ▶sub label { $_[0]->{label} }
Subroutine label: called CC=1 SCAR=0.0
Pod documented
307 42 ▶sub parser { $_[0]->{parser} }
Subroutine parser: called CC=1 SCAR=0.0
Pod documented
308 119 ▶sub code { $_[0]->{code} }
Subroutine code: called CC=1 SCAR=0.0
Pod documented
309 0 ▶sub gencode { $_[0]->{gencode} }
Subroutine gencode: not called CC=1 SCAR=6.9
Pod documented
310 80 ▶sub bracket { $_[0]->{bracket} }
Subroutine bracket: called CC=1 SCAR=0.0
Pod documented
311 42 ▶sub comment { $_[0]->{comment} }
Subroutine comment: called CC=1 SCAR=0.0
Pod documented
312
313 0 ▶sub errors { @{$_[0]->{errors}} }
Subroutine errors: not called CC=1 SCAR=6.9
Pod documented
314
315 =head2 plist(@parameters)
316
317 Given a list of parameters, returns a hash (not a hashref) of their values, first looking in the parameters, then looking for children
318 of the same name and returning their labels if necessary. This allows us to specify a parameter for a given object either like this:
319
320 object (parm1=value1, parm2 = value2)
321
322 or like this:
323
324 object
325 parm1 "value1"
326 parm2 "value2"
327
328 It just depends on what you find more readable at the time. For this to work during payload build, though, the children have to be built
329 first, which isn't the default - so you have to call $self->build_children before using this in the payload build.
330
331 This is really useful if you're wrapping a module that uses a hash to initialize its object. Like, say, L<LWP::UserAgent>.
332
333 =cut
334
335 1 ▶sub plist {
Subroutine plist: called CC=4 SCAR=13.9
Pod documented
336 1 my $self = shift;
337 1 my %p;
338 1 foreach my $p (@_) {
339 3 ▶ if ($self->parameter($p)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->parameter($p)) { } 2 1 3
elsif (my $pnode = $self->find($p)) { } 1 0 1
340 2 $p{$p} = $self->parameter($p);
341 } elsif (my $pnode = $self->find($p)) {
342 1 $p{$p} = $pnode->label;
343 }
344 }
345
346 1 %p;
347 }
348
349 =head2 parm_css (parameter), set_css_values (hashref, parameter_string), prepare_css_value (hashref, name), get_css_value (hashref, name)
350
351 CSS is characterized by a sort of "parameter tree", where many parameters can be seen as nested in a hierarchy. Take fonts, for example.
352 A font has a size, a name, a bolded flag, and so on. To specify a font, then, we end up with things like font-name, font-size, font-bold, etc.
353 In CSS, we can also group those things together and get something like font="name: Times; size: 20", and that is equivalent to
354 font-name="Times", font-size="20". See?
355
356 This function does the same thing with the parameters of a node. If you give it a name "font" it will find /font-*/ as well, and munge
357 the values into the "font" value. It returns a hashref containing the entire hierarchy of these things, and it will also interpret any
358 string-type parameters in the higher levels, e.g. font="size: 20; name: Times" will go into {size=>20, name=>'Times'}. Honestly, I love
359 this way of handling parameters in CSS.
360
361 If you give a name "font-size" it will also find any font="size: 20" specification and retrieve the appropriate value.
362
363 It I<won't> decompose multiple hierarchical levels starting from a string (e.g. something like font="size: {type: 3}" will not be parsed for
364 font-size-type, because you'd need curly brackets or something anyway, and this ain't JSON, it's just simple CSS-like parameter addressing.
365
366 =cut
367
368 7 ▶sub parm_css {
Subroutine parm_css: called CC=4 SCAR=13.9
Pod documented
369 7 my ($self, $parameter) = @_;
370 7 my $return = {};
371 7 my $top = $parameter;
372 7 $top =~ s/[.\-\/].*$//;
373 7 ▶ hh_set ($return, $top, $self->parameter ($top)) if $self->parameter($top);
Branch: 1 branch
BranchTrueFalseTotal
if $self->parameter($top) 5 2 7
374 7 foreach ($self->parmlist()) {
375 15 ▶ if ($_ =~ /^$top[.\-\/]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ m[^${top}[.\-/]]) 10 5 15
376 10 hh_set ($return, $_, $self->parameter ($_));
377 }
378 }
379 7 return hh_get ($return, $parameter);
380 }
381
382
383 =head2 flags({flag=>numeric value, ...}), oflags({flag=>numeric value, ...})
384
385 A quick utility to produce an OR'd flag set from a list of parameter words. Pass it a hashref containing numeric values for a set of words, and
386 you'll get back the OR'd sum of the flags found in the parameters. The C<flags> function does this for the parameters (round parens) and the C<oflags>
387 function does the same for the options [square brackets].
388
389 =cut
390
391 0 ▶sub flags {
Subroutine flags: not called CC=3 SCAR=24.8
Pod documented
392 0 my ($self, $f) = @_;
393
394 0 my $r = 0;
395
396 0 ▶ while (my ($k, $v) = each %$f) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$f) 0 0 0
397 0 ▶ $r |= $v if $self->parameter ($k);
Branch: 1 branch
BranchTrueFalseTotal
if $self->parameter($k) 0 0 0
398 }
399 0 return $r;
400 }
401 0 ▶sub oflags {
Subroutine oflags: not called CC=3 SCAR=24.8
Pod documented
402 0 my ($self, $f) = @_;
403
404 0 my $r = 0;
405
406 0 for (my ($k, $v) = each %$f) {
407 0 ▶ $r |= $v if $self->option ($k);
Branch: 1 branch
BranchTrueFalseTotal
if $self->option($k) 0 0 0
408 }
409 0 return $r;
410 }
411
412 =head2 list_parameter ($name)
413
414 Sometimes, instead of having e.g. position-x and position-y parameters, it's easier to have something like p=40 20 or dim=20x20. We can use
415 the C<list_parameter> function to obtain a list of any numbers separated by non-number characters. (Note that due to the line parser using
416 commas to separate the parameters themselves, the separator can't be a comma. Unless you want to write a different line parser, in which
417 case, go you!)
418
419 So the separator characters can be: !@#$%^&*|:;~x and space.
420
421 =cut
422
423 0 ▶sub list_parameter { split /[!@\#\$%\^\&\*\|:;~xX ]/, parameter(@_); }
Subroutine list_parameter: not called CC=1 SCAR=6.9
Pod documented
424
425 =head1 BUILDING STRUCTURE
426
427 =head2 load ($string, $after)
428
429 The C<load> method loads declarative specification text into a node by calling the parser appropriate to the node. Multiple loads can be carried out,
430 and will simply add to text already there.
431
432 The return value is the list of objects added to the target, if any.
433
434 =cut
435
436 760 ▶sub load {
Subroutine load: called CC=21 SCAR=30.6
Pod documented
437 760 my ($self, $string, $after) = @_;
438
439 760 my @added;
440
441 760 ▶ if (ref $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string) { } 9 751 760
442 #print STDERR "load: Adding from arrayref!\n" . Dumper($string);
443 9 ▶ if (ref $string ne 'ARRAY') { # In case we're loading already-created nodes.
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string ne 'ARRAY') { } 0 9 9
444 0 $string->{parent} = $self;
445 0 $self->{elements} = [$self->elements, $string];
446 0 push @added, $string;
447 } else {
448 9 my $root = $self->root;
449 9 ▶ $string = [$string] unless ref $$string[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $string->[0] 8 1 9
450 9 foreach my $addition (@$string) {
451 #print STDERR "addition is $addition\n";
452 #print STDERR "line is " . ref($addition) ? $$addition[0] : $addition;
453 10 ▶ my $tag = ref($addition) ? $$addition[0] : $addition;
Branch: 1 branch
BranchTrueFalseTotal
ref $addition ? : 10 0 10
454 10 $tag =~ s/ .*//;
455 #print STDERR ", tag is $tag\n";
456
457 # Make and add the tag by hand (for a text body, this is done by the parser in the 'else' block below).
458 10 my $newtag = $root->makenode($self, $tag, $addition);
459 10 $newtag->{parent} = $self;
460 10 $self->{elements} = [$self->elements, $newtag];
461
462 10 push @added, $newtag;
463 }
464 }
465 } else {
466 # Taken from the Perl recipes:
467 751 my ($white, $leader); # common whitespace and common leading string
468 751 ▶ if ($string =~ /^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\1\2?.*\n)+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\1\2?.*\n)+$/) { } 3 748 751
469 3 ($white, $leader) = ($2, quotemeta($1));
470 } else {
471 748 ($white, $leader) = ($string =~ /^(\s+)/, '');
472 }
473 751 ▶ $leader = '' unless $leader;
Branch: 1 branch
BranchTrueFalseTotal
unless $leader 748 3 751
474 751 ▶ $white = '' unless $white;
Branch: 1 branch
BranchTrueFalseTotal
unless $white 719 32 751
475 751 $white =~ s/^\n*//;
476 751 ▶ $string =~ s/^\s*?$leader(?:$white)?//gm if $leader or $white;
Branch: 1 branch
BranchTrueFalseTotal
if $leader or $white 5 746 751
Condition100%3/3
$leader or $white
l!l&&r!l&&!r
32746
MC/DC100%2/2
$leader or $white
$leader $white
Truth table100%3/3
A or B
A= $leaderB= $white
ABresult
1X1
011
000
477 751 my $root = $self->root();
478 751 @added = $root->parse ($self, $string);
479 }
480
481 760 ▶ if ($after) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after) 6 754 760
482 # Rearrange $self->{elements} if we were given an 'after' node that appears in 'elements'.
483 6 my @newels = ();
484 6 my $found_after = 0;
485 6 foreach my $e (@{$self->{elements}}) {
486 50 ▶ if (refaddr_or_undef($e) eq refaddr_or_undef($after)) {
Branch: 1 branch
BranchTrueFalseTotal
if (refaddr_or_undef($e) eq refaddr_or_undef($after)) { } 6 44 50
487 6 $found_after = 1;
488 6 push @newels, $e;
489 6 foreach my $a (@added) {
490 6 push @newels, $a;
491 }
492 } else {
493 44 ▶ last if grep {refaddr_or_undef($_) eq refaddr_or_undef($e)} @added;
Branch: 1 branch
BranchTrueFalseTotal
if grep {refaddr_or_undef($_) eq refaddr_or_undef($e);} @added 6 38 44
494 38 push @newels, $e;
495 }
496 }
497 6 ▶ $self->{elements} = \@newels if $found_after;
Branch: 1 branch
BranchTrueFalseTotal
if $found_after 6 0 6
498 }
499
500 760 foreach (@added) {
501 932 ▶ $_->build if $_->can('build');
Branch: 1 branch
BranchTrueFalseTotal
if $_->can("build") 932 0 932
502 }
503 #print Dumper($self->sketch);
504 760 ▶ return wantarray ? @added : (@added ? $added[0] : undef);
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 7 753 760
@added ? : 752 1 753
505 }
506
507 =head2 macroinsert ($spec, $after)
508
509 This function adds structure to a given node at runtime that won't show up in the node's C<describe> results. It is used by the macro system (hence
510 the name) but can be used by other runtime structure modifiers that act more or less like macros. The idea is that this structure is meaningful at runtime
511 but is semantically already accounted for in the existing definition, and should I<always> be generated only at runtime.
512
513 =cut
514
515 7 ▶sub macroinsert {
Subroutine macroinsert: called CC=2 SCAR=6.9
Pod documented
516 7 my ($self, $string, $after) = @_;
517 7 my @objects = $self->load($string, $after);
518 7 foreach (@objects) {
519 9 $_->{macroresult} = 1;
520 }
521 7 @objects;
522 }
523
524 =head2 replace_node ($old_node, $new_node)
525
526 There are times when dynamically changing semantics force us to reevaluate an existing node during the build phase. We use C<replace>
527 to replace the existing node with the newly interpeted variant. It works by actual pointer. If the C<old_name> isn't found, nothing will
528 happen.
529
530 =cut
531
532 0 ▶sub replace_node {
Subroutine replace_node: not called CC=4 SCAR=30.0
Pod documented
533 0 my ($self, $old, $new) = @_;
534 0 $old->{replaced} = $new; # Make sure ongoing builds build the right node.
535 0 foreach (@{$self->{elements}}) {
536 0 ▶ next unless ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 0 0
537 0 ▶ $_ = $new if $_ == $old;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $old 0 0 0
538 }
539 }
540
541 =head2 Setting parts of a node: set_name($name), set_label($label), set_parmlist (@list), set_parameter($key, $value), set_optionlist (@list), set_option($key, $value)
542
543 These are handy for building a node from scratch.
544
545 =cut
546
547 0 ▶sub set_name {
Subroutine set_name: not called CC=1 SCAR=6.9
Pod documented
548 0 my $self = shift;
549 0 $self->{name} = $_[0];
550 0 $self->{namelist} = [@_]; # Make a copy!
551 }
552 278 ▶sub set_label {
Subroutine set_label: called CC=1 SCAR=0.0
Pod documented
553 278 my $self = shift;
554 278 $self->{label} = $_[0];
555 }
556 0 ▶sub set_parmlist {
Subroutine set_parmlist: not called CC=1 SCAR=6.9
Pod documented
557 0 my $self = shift;
558 0 $self->{parmlist} = [@_];
559 }
560 0 ▶sub set_parameter {
Subroutine set_parameter: not called CC=1 SCAR=6.9
Pod documented
561 0 my ($self, $key, $value) = @_;
562 0 $self->{parameters}->{$key} = $value;
563 }
564 0 ▶sub set_optionlist {
Subroutine set_optionlist: not called CC=1 SCAR=6.9
Pod documented
565 0 my $self = shift;
566 0 $self->{optionlist} = [@_];
567 }
568 0 ▶sub set_option {
Subroutine set_option: not called CC=1 SCAR=6.9
Pod documented
569 0 my ($self, $key, $value) = @_;
570 0 $self->{options}->{$key} = $value;
571 }
572
573 =head2 The build process: build(), preprocess(), preprocess_line(), decode_line(), parse_body(), build_payload(), build_children(), add_to_parent(), post_build()
574
575 The C<build> function parses the body of the tag, then builds the payload it defines, then calls build on each child if appropriate, then adds itself
576 to its parent. It provides the hooks C<preprocess> (checks for macro nature and expresses if so), C<parse_body> (asks the application to call the appropriate
577 parser for the tag), C<build_payload> (does nothing by default), C<build_children> (calls C<build> on each element), and C<add_to_parent>
578 (does nothing by default).
579
580 If this tag corresponds to a macro, then substitution takes place before parsing, in the preprocess step.
581
582 =cut
583
584 989 ▶sub build {
Subroutine build: called CC=6 SCAR=17.9
Pod documented
585 989 my $self = shift;
586 989 ▶ return $self->{replaced}->build if $self->{replaced};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"replaced"} 0 989 989
587
588 989 ▶ if ($self->{state} ne 'built') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} ne "built") 980 9 989
589 980 ▶ if ($self->root()->{macro_definitions}->{$self->tag}) { # This is required because in some cases, the macro definition may have been
Branch: 1 branch
BranchTrueFalseTotal
if ($self->root->{"macro_definitions"}{$self->tag}) 4 976 980
590 # registered *after* the class was already assigned to the macro instance.
591 # E.g.:
592 # define my_macro
593 # ...
594 # my_macro
595 # (On the same level with the same parent, my_macro has already been split out.)
596 4 bless $self, 'Decl::Semantics::Macro';
597 }
598 980 $self->{force_text} = 0;
599 980 $self->preprocess_line;
600 980 $self->decode_line;
601 980 $self->preprocess;
602 980 ▶ $self->parse_body unless $self->{force_text};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"force_text"} 980 0 980
603 980 $self->build_payload;
604 980 ▶ $self->build_children unless $self->{force_text};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"force_text"} 976 4 980
605 980 $self->add_to_parent;
606 980 $self->post_build;
607
608 980 $self->{state} = 'built';
609 }
610 989 return $self->payload;
611 }
612
613 980 ▶sub preprocess_line {}
Subroutine preprocess_line: called CC=1 SCAR=0.0
Pod documented
614
615 980 ▶sub decode_line { # Was called parse_line, but there was an unfortunate and brain-bending collision with Text::ParseWords. Oy.
Subroutine decode_line: called CC=1 SCAR=0.0
Pod documented
616 980 my $self = shift;
617 980 my $root = $self->root;
618 980 $root->parse_line ($self);
619 }
620
621 980 ▶sub preprocess {}
Subroutine preprocess: called CC=1 SCAR=0.0
Pod documented
622
623 980 ▶sub parse_body {
Subroutine parse_body: called CC=7 SCAR=19.5
Pod documented
624 980 my $self = shift;
625 980 ▶ if ($self->tag =~ /^!/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tag =~ /^!/) { } 0 980 980
626 0 $self->{tag} =~ s/^!//;
627 } else {
628 980 my $root = $self->root;
629 980 ▶ if (ref $self->body eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->body eq 'ARRAY') { } 10 970 980
630 # If we have an arrayref input, we don't need to parse it! (2010-12-05)
631 #print "parse_body: body is an arrayref\n";
632 10 my $list = $self->{body};
633 10 $self->{body} = '';
634 10 foreach (@$list) {
635 9 $self->load ($_);
636 }
637 } else {
638 970 ▶ my @results = $root->parse ($self, $self->body) if $self->body and not $self->{bracket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->body and not $self->{"bracket"} 44 926 970
Condition100%3/3
$self->body and not $self->{"bracket"}
!ll&&!rl&&r
8982844
MC/DC100%2/2
$self->body and not $self->{"bracket"}
$self->body not $self->{"bracket"}
Truth table100%3/3
A and not(B)
A= $self->bodyB= not $self->{"bracket"}
ABresult
0X0
100
111
639 970 ▶ $self->{body} = '' if @results;
Branch: 1 branch
BranchTrueFalseTotal
if @results 24 946 970
640 }
641 }
642 }
643
644 942 ▶sub build_payload {}
Subroutine build_payload: called CC=1 SCAR=0.0
Pod documented
645
646 983 ▶sub build_children {
Subroutine build_children: called CC=3 SCAR=11.2
Pod documented
647 983 my $self = shift;
648
649 983 foreach ($self->nodes) {
650 55 ▶ $_->build if $_->can('build');
Branch: 1 branch
BranchTrueFalseTotal
if $_->can("build") 55 0 55
651 }
652 }
653
654 980 ▶sub add_to_parent {}
Subroutine add_to_parent: called CC=1 SCAR=0.0
Pod documented
655
656 980 ▶sub post_build {}
Subroutine post_build: called CC=1 SCAR=0.0
Pod documented
657
658 =head1 STRUCTURE ACCESS
659
660 =head2 find($locator), findbyname($locator)
661
662 Given a node, finds a descendant using a simple XPath-like language. Once you build a recursive-descent parser facility into your language, this sort
663 of thing gets a whole lot easier. The C<find> function looks by tag; the C<findbyname> treats the tag as a type and thus the name as the search
664 property.
665
666 Generation separators are '.', '/', or ':' depending on how you like it. Offsets by number are in round brackets (), while finding children by name is
667 done with square brackets []. Square brackets [name] find tags named "name". Square brackets [name name2] find name lists (which nodes can have, yes),
668 and square brackets with an = or =~ can also search for nodes by other values.
669
670 You can also pass the results of a parse (the arrayref tree) in as the path; this allows you to build the parse tree using other tools instead of forcing
671 you to build a string (it also allows a single parse result to be used recursively without having to parse it again).
672
673 =cut
674
675 71 ▶sub find {
Subroutine find: called CC=5 SCAR=16.1
Pod documented
676 71 my ($self, $path) = @_;
677
678 71 ▶ $path = $self->root->parse_using ($path, 'locator') unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 25 46 71
679 71 ▶ return $self if @$path == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$path == 0 37 34 71
680
681 34 my $first = shift @$path;
682 34 foreach ($self->nodes) {
683 69 ▶ return $_->find($path) if $_->match($first);
Branch: 1 branch
BranchTrueFalseTotal
if $_->match($first) 34 35 69
684 }
685 0 return undef;
686 }
687 8 ▶sub findbyname {
Subroutine findbyname: called CC=5 SCAR=16.1
Pod documented
688 8 my ($self, $path) = @_;
689
690 8 ▶ $path = $self->root->parse_using ($path, 'locator') unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 2 6 8
691 8 ▶ return $self if @$path == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$path == 0 2 6 8
692
693 6 my $first = shift @$path;
694 6 foreach ($self->nodes) {
695 12 ▶ return $_->findbyname($path) if $_->matchbyname($first);
Branch: 1 branch
BranchTrueFalseTotal
if $_->matchbyname($first) 6 6 12
696 }
697 0 return undef;
698 }
699
700 =head2 match($pathelement), matchbyname($pathelement)
701
702 Returns a true value if the node matches the path element specified; otherwise, returns a false value.
703
704 =cut
705
706 6218 ▶sub match {
Subroutine match: called CC=4 SCAR=13.9
Pod documented
707 6218 my ($self, $pathelement) = @_;
708 6218 ▶ return ($self->tag eq $pathelement) unless ref $pathelement;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pathelement 6178 40 6218
709 40 my ($tag, $name) = @$pathelement;
710 40 ▶ return 1 if $self->tag eq $tag and $self->name eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->tag eq $tag and $self->name eq $name 11 29 40
Condition100%3/3
$self->tag eq $tag and $self->name eq $name
!ll&&!rl&&r
62311
MC/DC100%2/2
$self->tag eq $tag and $self->name eq $name
$self->tag eq $tag $self->name eq $name
Truth table100%3/3
A and B
A= $self->tag eq $tagB= $self->name eq $name
ABresult
0X0
100
111
711 29 return 0;
712 }
713
714 12 ▶sub matchbyname {
Subroutine matchbyname: called CC=4 SCAR=23.7
Pod documented
715 12 my ($self, $pathelement) = @_;
716 12 ▶ return ($self->name eq $pathelement) unless ref $pathelement;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pathelement 12 0 12
717 0 my ($name, $label) = @$pathelement;
718 0 ▶ return 1 if $self->name eq $name and $self->label eq $label;
Branch: 1 branch
BranchTrueFalseTotal
if $self->name eq $name and $self->label eq $label 0 0 0
Condition0%0/3
$self->name eq $name and $self->label eq $label
!ll&&!rl&&r
000
MC/DC0%0/2
$self->name eq $name and $self->label eq $label
$self->name eq $name $self->label eq $label
Truth table0%0/3
A and B
A= $self->name eq $nameB= $self->label eq $label
ABresult
0X0
100
111
719 0 return 0;
720 }
721
722 =head2 first($nodename)
723
724 Given a node, finds a descendant with the given tag anywhere in its descent. Uses the same path notation as C<find>.
725
726 =cut
727
728 6428 ▶sub first {
Subroutine first: called CC=7 SCAR=19.5
Pod documented
729 6428 my ($self, $path) = @_;
730
731 6428 ▶ $path = $self->root->parse_using ($path, 'locator') unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 291 6137 6428
732 6428 ▶ return $self if @$path == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$path == 0 0 6428 6428
733
734 6428 my ($first, @rest) = @$path;
735 6428 foreach ($self->nodes) {
736 6149 ▶ if ($_->match($first)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->match($first)) 12 6137 6149
737 12 my $possible = $_->find(\@rest);
738 12 ▶ return $possible if $possible;
Branch: 1 branch
BranchTrueFalseTotal
if $possible 12 0 12
739 }
740 6137 my $child = $_->first($path);
741 6137 ▶ return $child if $child;
Branch: 1 branch
BranchTrueFalseTotal
if $child 16 6121 6137
742 }
743 6400 return undef;
744 }
745
746 =head2 search($nodename)
747
748 Given a node, finds all descendants with the given tag.
749
750 =cut
751
752 3 ▶sub search {
Subroutine search: called CC=3 SCAR=11.0
Pod documented
753 3 my ($self, $path) = @_;
754 3 my @returns = ();
755 3 foreach ($self->nodes) {
756 2 ▶ push @returns, $_ if $_->tag eq $path;
Branch: 1 branch
BranchTrueFalseTotal
if $_->tag eq $path 2 0 2
757 2 push @returns, $_->search($path);
758 }
759 @returns
760 3 }
761
762 =head2 search_data($type)
763
764 Given a node, finds all its descendents that match the given type in either name or tag.
765 If the type ends in a ':', will only return meta nodes.
766
767 =cut
768
769 0 ▶sub search_data {
Subroutine search_data: not called CC=7 SCAR=40.3
Pod documented
770 0 my ($self, $type) = @_;
771 0 my $flag = '';
772 0 ▶ if ($type =~ /:$/) { # TODO: just : flag?
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /:$/) 0 0 0
773 0 $type =~ s/:$//;
774 0 $flag = ':';
775 }
776 0 my @returns = ();
777 0 foreach ($self->nodes) {
778 0 ▶ if ($_->is($type) || $_->name eq ($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->is($type) or $_->name eq $type) 0 0 0
Condition0%0/3
$_->is($type) or $_->name eq $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->is($type) or $_->name eq $type
$_->is($type) $_->name eq $type
Truth table0%0/3
A or B
A= $_->is($type)B= $_->name eq $type
ABresult
1X1
011
000
779 0 ▶ push @returns, $_ if not $flag or $_->flag($flag);
Branch: 1 branch
BranchTrueFalseTotal
if not $flag or $_->flag($flag) 0 0 0
Condition0%0/3
not $flag or $_->flag($flag)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $flag or $_->flag($flag)
not $flag $_->flag($flag)
Truth table0%0/3
not(A) or B
A= not $flagB= $_->flag($flag)
ABresult
1X1
011
000
780 }
781 0 push @returns, $_->search_data($type . $flag);
782 }
783 0 @returns;
784 }
785
786 =head2 describe, myline, describe_content
787
788 The C<describe> function is used to get our code back out so we can reparse it later if we want to. It includes the body and any children.
789 The C<myline> function just does that without the body and children (just the actual line).
790 The C<describe_content> function does just the body and children (without the actual line).
791
792 We could also use this to check the output of the parser, which notoriously just stops on a line if it encounters something it's not
793 expecting.
794
795 =cut
796
797 41 ▶sub myline {
Subroutine myline: called CC=13 SCAR=26.1
Pod documented
798 41 my ($self) = @_;
799
800 41 my $description = $self->tag . $self->flag;
801 41 foreach (@{$self->{namelist}}) {
802 30 $description .= " " . $_;
803 }
804
805 41 ▶ if ($self->parmlist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parmlist) 4 37 41
806 $description .= " (" .
807 join (', ', map {
808 4 ▶ $self->parameter($_) eq 'yes' ?
Branch: 2 branches
BranchTrueFalseTotal
$self->parameter($_) eq 'yes' ? : 4 12 16
$self->parameter($_) =~ / |"/ ? : 0 12 12
809 $_ :
810 ($self->parameter($_) =~ / |"/ ?
811 $_ . '="' . escapequote ($self->parameter($_)) . '"' :
812 $_ . '=' . $self->parameter($_))
813 } $self->parmlist) .
814 ")";
815 }
816
817 41 ▶ if ($self->optionlist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->optionlist) 4 37 41
818 $description .= " [" .
819 join (', ', map {
820 4 ▶ $self->option($_) eq 'yes' ?
Branch: 2 branches
BranchTrueFalseTotal
$self->option($_) eq 'yes' ? : 8 0 8
$self->option($_) =~ / |"/ ? : 0 0 0
821 $_ :
822 ($self->option($_) =~ / |"/ ?
823 $_ . '="' . escapequote ($self->option($_)) . '"' :
824 $_ . '=' . $self->option($_))
825 } $self->optionlist) .
826 "]";
827 }
828
829 41 ▶ $description .= ' "' . $self->label . '"' if $self->label ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->label ne "" 19 22 41
830 41 ▶ $description .= ' ' . $self->parser . ' <' if $self->parser;
Branch: 1 branch
BranchTrueFalseTotal
if $self->parser 0 41 41
831 41 ▶ $description .= ' ' . $self->code if $self->code;
Branch: 1 branch
BranchTrueFalseTotal
if $self->code 3 38 41
832 41 ▶ $description .= ' ' . $self->bracket if $self->bracket;
Branch: 1 branch
BranchTrueFalseTotal
if $self->bracket 4 37 41
833 41 ▶ $description .= ' ' . $self->comment if $self->comment;
Branch: 1 branch
BranchTrueFalseTotal
if $self->comment 0 41 41
834
835 41 $description;
836 }
837
838 41 ▶sub describe {
Subroutine describe: called CC=1 SCAR=0.0
Pod documented
839 41 my ($self, $macro_ok) = @_;
840
841 41 $self->myline . "\n" . $self->describe_content (' ', $macro_ok);
842 }
843 41 ▶sub describe_content {
Subroutine describe_content: called CC=11 SCAR=24.2
Pod documented
844 41 my ($self, $prefix, $macro_ok) = @_;
845 41 my $description = '';
846 41 ▶ $prefix = '' unless defined $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix 0 41 41
847 41 ▶ $macro_ok = 0 unless defined $macro_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $macro_ok 0 41 41
848
849 41 ▶ if ($self->body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->body) { } 7 34 41
850 7 foreach (split /\n/, $self->body) {
851 19 $description .= "$prefix$_\n";
852 }
853 7 ▶ $description .= "}\n" if $self->bracket;
Branch: 1 branch
BranchTrueFalseTotal
if $self->bracket 4 3 7
854 } else {
855 34 foreach ($self->elements) {
856 29 ▶ if (not ref $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_) { } 0 29 29
elsif ($_->{'macroresult'} and not $macro_ok) { } 3 26 29
857 0 $description .= $_;
858 ▶ } elsif ($_->{macroresult} and not $macro_ok) {
Condition100%3/3
$_->{"macroresult"} and not $macro_ok
!ll&&!rl&&r
2243
MC/DC100%2/2
$_->{"macroresult"} and not $macro_ok
$_->{"macroresult"} not $macro_ok
Truth table100%3/3
A and not(B)
A= $_->{"macroresult"}B= not $macro_ok
ABresult
0X0
100
111
859 3 next;
860 } else {
861 26 foreach (split /\n/, $_->describe($macro_ok)) {
862 42 $description .= "$prefix$_\n";
863 }
864 }
865 }
866 }
867
868 41 $description;
869 }
870
871 =head2 sketch (), sketch_c(), sketch_d()
872
873 Returns a thin structure reflecting the nodal structure of the node in question:
874
875 ['tag',
876 [['child1', []],
877 ['child2', []]]]
878
879 Like that. I'm building it for testing purposes, but it might be useful for something else, too.
880
881 The C<sketch_c> variant also includes the class of each node, and the C<sketch_d> variant runs the
882 whole thing through Dumper first.
883
884 =cut
885
886 40 ▶sub sketch {
Subroutine sketch: called CC=1 SCAR=0.0
Pod documented
887 40 my ($self) = @_;
888
889 40 [$self->tag, [map { $_->sketch() } $self->nodes()]];
890 }
891 0 ▶sub sketch_c {
Subroutine sketch_c: not called CC=1 SCAR=6.9
Pod documented
892 0 my ($self) = @_;
893
894 0 [$self->tag, ref($self), [map { $_->sketch_c() } $self->nodes()]];
895 }
896 0 ▶sub sketch_d { Dumper ($_[0]->sketch_c); }
Subroutine sketch_d: not called CC=1 SCAR=6.9
Pod documented
897
898 =head2 mylocation()
899
900 This reports the node's own location in the code tree.
901
902 =cut
903
904 0 ▶sub mylocation {
Subroutine mylocation: not called CC=2 SCAR=17.9
Pod documented
905 0 my $self = shift;
906 0 my $p = $self->parent->mylocation();
907 0 my $l = $self->tag() . '[' . join(' ', $self->names()) . ']';
908 0 ▶ return '/' . $l if $p eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $p eq "/" 0 0 0
909 0 return $p . '/' . $l;
910 }
911
912 =head2 go($item)
913
914 For callable nodes, this is one way to call them. The default is to call the go methods of all the children of the node, in sequence.
915 The last result is returned as our result (this means that the overall tree may have a return value if you set things up right).
916
917 =cut
918
919 20 ▶sub go {
Subroutine go: called CC=14 SCAR=29.0
Pod documented
920 20 my $self = shift;
921 20 my $callcontext = shift;
922
923 20 $self = $self->deref;
924 20 ▶ return unless defined $self; # TODO: warning
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 20 20
925 20 ▶ return unless $self->{callable};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"callable"} 0 20 20
926 20 ▶ return &{$self->{sub}}($callcontext, @_) if $self->{owncode} && $self->{sub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"owncode"} and $self->{"sub"} 9 11 20
Condition66%2/3
$self->{"owncode"} and $self->{"sub"}
!ll&&!rl&&r
1109
MC/DC50%1/2
$self->{"owncode"} and $self->{"sub"}
$self->{"owncode"} $self->{"sub"}
Truth table66%2/3
A and B
A= $self->{"owncode"}B= $self->{"sub"}
ABresult
0X0
100
111
927
928 11 my $return;
929 my $last_iffy;
930 11 my $master_iffy = undef;
931 11 foreach ($self->content_nodes) {
932 29 ▶ next unless $_->{callable};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"callable"} 15 14 29
933 14 ▶ next if $_->{callable} eq 'sub';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"callable"} eq "sub" 1 13 14
934 13 ▶ next if $_->{event};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"event"} 4 9 13
935 9 ▶ if ($_->{callable} eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'callable'} eq '?') { } 0 9 9
936 0 $last_iffy = $_;
937 0 ▶ $master_iffy = $_ if $_->flag('!') and not defined $master_iffy;
Branch: 1 branch
BranchTrueFalseTotal
if $_->flag("!") and not defined $master_iffy 0 0 0
Condition0%0/3
$_->flag("!") and not defined $master_iffy
!ll&&!rl&&r
000
MC/DC0%0/2
$_->flag("!") and not defined $master_iffy
$_->flag("!") not defined $master_iffy
Truth table0%0/3
A and not(B)
A= $_->flag("!")B= not defined $master_iffy
ABresult
0X0
100
111
938 } else {
939 9 $return = $_->go (@_);
940 9 undef $last_iffy;
941 }
942 }
943 11 ▶ return $master_iffy->go(@_) if defined $master_iffy;
Branch: 1 branch
BranchTrueFalseTotal
if defined $master_iffy 0 11 11
944 11 ▶ return $last_iffy->go(@_) if defined $last_iffy;
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_iffy 0 11 11
945 11 $return;
946 }
947
948 =head2 closure(...)
949
950 For callable nodes, this is the other way to call them; it returns the closure created during initialization. Note that the
951 default closure is really boring.
952
953 =cut
954
955 0 ▶sub closure { $_[0]->{sub} }
Subroutine closure: not called CC=1 SCAR=6.9
Pod documented
956
957
958 =head2 iterate()
959
960 Returns an L<Iterator::Simple> iterator over the body of the node. If the body is a text body, each call returns a line. If the body is a bracketed
961 code body, it is executed to return an iterable object. Yes, this is neat.
962
963 If we're a parser macro, we'll run our special parser over the body instead of the normal parser.
964
965 TODO: shouldn't this be recursive for structured nodes?
966
967 TODO: might want to do something clever with a code ref tag. (I.e. if the tag is a reference but also has a code block, perhaps evaluate the code
968 block to figure out the reference or something. This might be a plate of beans.)
969
970 =cut
971
972 18 ▶sub iterate {
Subroutine iterate: called CC=10 SCAR=24.3
Pod documented
973 18 my $self = shift;
974
975 18 $self = $self->deref;
976 18 ▶ return iter([]) unless defined $self; # TODO: warning
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 18 18
977 18 ▶ return iter([]) unless $self->code or $self->nodes or $self->body;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->code or $self->nodes or $self->body 0 18 18
Condition66%4/6
$self->code or $self->nodes or $self->body
l!l&&r!l&&!r
3150
$self->code or $self->nodes
l!l&&r!l&&!r
0315
MC/DC0%0/3
$self->code or $self->nodes or $self->body
$self->code $self->nodes $self->body
Truth table50%2/4
A or B or C
A= $self->codeB= $self->nodesC= $self->body
ABCresult
1XX1
01X1
0011
0000
978 18 ▶ if ($self->code or $self->bracket) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->code or $self->bracket) { } 1 17 18
elsif ($self->nodes) { } 3 14 17
Condition66%2/3
$self->code or $self->bracket
l!l&&r!l&&!r
0117
MC/DC50%1/2
$self->code or $self->bracket
$self->code $self->bracket
Truth table66%2/3
A or B
A= $self->codeB= $self->bracket
ABresult
1X1
011
000
979 # This is code to be executed, that should return an iterable object.
980 1 my $code;
981 1 ▶ if ($self->code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->code) { } 0 1 1
982 0 $code = $self->code;
983 } else {
984 1 $code = $self->bracket . "\n";
985 1 $code =~ s/^{//;
986 1 $code .= $self->body;
987 }
988 1 my $sub = Decl::Semantics::Code::make_code ($self, $code);
989 1 my $result = &$sub();
990 1 ▶ if (ref $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result) { } 1 0 1
991 1 return iter ($result);
992 } else {
993 0 my @lines = split /\n/, $result;
994 0 return iter (\@lines);
995 }
996 } elsif ($self->nodes) {
997 # Iterate over children.
998 3 return ichain map { $_->iterate } $self->nodes;
999 } else {
1000 # This is text to be iterated over.
1001 14 my @lines = split /\n/, $self->body;
1002 14 return iter (\@lines);
1003 }
1004 }
1005
1006 =head2 text()
1007
1008 This returns a tokenstream on the node's body permitting a consumer to read a series of words interspersed with formatting commands.
1009 The formatting commands are pretty loose - essentially, "blankline" is the only one. Punctuation is treated as letters in words; that is,
1010 only whitespace is elided in the tokenization process.
1011
1012 If the node has been parsed, it probably doesn't have a body any more, so this will return a blank tokenstream. On the other hand, if the node
1013 is callable, it will be called, and the result will be used as input to the tokenstream - same rules as C<iterate> above.
1014
1015 =cut
1016
1017
1018 =head2 express(), content()
1019
1020 The C<content> function returns the iterated content from iterate(), assembled into lines with as few newlines as possible.
1021 The C<express> function is normally an alias for C<content>.
1022
1023 =cut
1024
1025 0 ▶sub express {
Subroutine express: not called CC=1 SCAR=6.9
Pod documented
1026 0 my $self = shift;
1027 0 $self->content(@_);
1028 }
1029 3 ▶sub content {
Subroutine content: called CC=4 SCAR=14.1
Pod documented
1030 3 my ($self, $linebreak) = @_;
1031 3 ▶ $linebreak = "\n" unless $linebreak;
Branch: 1 branch
BranchTrueFalseTotal
unless $linebreak 3 0 3
1032
1033 3 my $i = $self->iterate;
1034 3 my $result = '';
1035 3 my $line;
1036
1037 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $line and do { do { $line = &$i(); return $result unless defined $line; chomp $line; $result .= "$line\n" } } 10 0 10
1038 13 $line = $i->();
1039 13 ▶ return $result unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 3 10 13
1040
1041 10 chomp $line;
1042 10 $result .= "$line\n";
1043 } while (defined $line);
1044
1045 0 return $result;
1046 }
1047
1048 #my $linestart = 1; TODO: figure out why I thought this should be the default. Sigh.
1049 #do {
1050 # $line = $i->();
1051 # if (defined $line) {
1052 # if ($self->parameter('raw')) {
1053 # $result .= $line . "\n";
1054 # } else {
1055 # $line =~ s/\s+$//;
1056 # if ($line ne '') {
1057 # $result .= ($linestart ? '' : ' ') . $line;
1058 # $linestart = 0;
1059 # } else {
1060 # $result .= $linebreak;
1061 # $linestart = 1;
1062 # }
1063 # }
1064 # }
1065 #} while (defined $line);
1066
1067
1068 13 our $ACCEPT_EVENTS = 0;
1069
1070 =head2 event_context
1071
1072 If the node is an event context (e.g. a window or frame or dialog), this should return the payload of the node.
1073 Otherwise, it returns the event_context of the parent node.
1074
1075 =cut
1076
1077 63 ▶sub event_context {
Subroutine event_context: called CC=3 SCAR=13.1
Pod documented
1078 63 ▶ return $_[0] if $ACCEPT_EVENTS;
Branch: 1 branch
BranchTrueFalseTotal
if $Decl::Node::ACCEPT_EVENTS 0 63 63
1079 63 ▶ return $_[0]->parent()->event_context() if $_[0]->parent;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->parent 63 0 63
1080 0 $_[0]->root;
1081 }
1082
1083 =head2 root
1084
1085 Returns the parent - all nodes do this. The top node at C<Decl> returns itself.
1086
1087 =cut
1088
1089 4701 ▶sub root {$_[0]->parent->root}
Subroutine root: called CC=1 SCAR=0.0
Pod documented
1090
1091 =head2 error
1092
1093 Error handling is the part of programming I'm worst at. But you just have to bite the bullet and address your weaknesses,
1094 so here is an error marker function. If there's a problem with a node specification, this marks it. Later we'll do something
1095 sensible with it. TODO: something sensible.
1096
1097 =cut
1098
1099 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
1100 0 my ($self, $error) = @_;
1101 0 ▶ $self->{errors} = [] unless $self->{errors};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"errors"} 0 0 0
1102 0 push @{$self->{errors}}, $error;
1103 #print STDERR "$error\n"; # TODO: bad long-term...
1104 }
1105
1106 =head2 find_data
1107
1108 The C<find_data> function finds a data node starting at a given point in the tree. Right now, it's just going to look for nodes
1109 by name/tag, but more mature locators should follow eventually.
1110
1111 =cut
1112
1113 4 ▶sub find_data {
Subroutine find_data: called CC=7 SCAR=27.5
Pod documented
1114 4 my ($self, $data) = @_;
1115 4 ▶ $data = 'data' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 4 4
1116 0 ▶ foreach ($self->nodes) { return ($_, $_->tag) if $_->name eq $data; }
Branch: 1 branch
BranchTrueFalseTotal
if $_->name eq $data 0 0 0
1117 0 ▶ foreach ($self->nodes) { return ($_, $_->tag) if $_->is($data); }
Branch: 1 branch
BranchTrueFalseTotal
if $_->is($data) 0 0 0
1118 4 ▶ return $self->parent->find_data ($data) if $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent 4 0 4
1119 0 return (undef, undef);
1120 }
1121
1122 =head2 find_context (tag, name)
1123
1124 Here, we search for a node with a given name and tag in almost the same way as C<find_data> - first searching our siblings, then our parent's
1125 siblings, and so on. Used to look for macro definitions, databases, whatever. If either the tag or the name is omitted, it won't be
1126 used for comparison (thus the first tag of any name or the first named tag of any type will be returned).
1127
1128 Note I said "almost". Any node that comes after the caller won't be considered context. (Neither will the caller itself.) Ditto the parent,
1129 grandparent, etc. What that means is that context has to appear in the source before the point where C<find_context> is called.
1130
1131 =cut
1132
1133 3 ▶sub find_context {
Subroutine find_context: called CC=8 SCAR=27.0
Pod documented
1134 3 my ($self, $tag, $name) = @_;
1135 3 ▶ return unless ($self->parent);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parent 0 3 3
1136 3 foreach ($self->parent->nodes) {
1137 3 ▶ last if $_ == $self;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $self 0 3 3
1138 3 ▶ return ($_) if ((not defined $tag) || $_->is($tag)) and ((not defined $name) || $_->name eq $name);
Branch: 1 branch
BranchTrueFalseTotal
if !defined($tag) || $_->is($tag) and !defined($name) || $_->name eq $name 3 0 3
Condition33%3/9
!defined($tag) || $_->is($tag) and !defined($name) || $_->name eq $name
!ll&&!rl&&r
003
not defined $tag or $_->is($tag)
l!l&&r!l&&!r
030
not defined $name or $_->name eq $name
l!l&&r!l&&!r
300
MC/DC0%0/4
!defined($tag) || $_->is($tag) and !defined($name) || $_->name eq $name
not defined $tag $_->is($tag) not defined $name $_->name eq $name
Truth table14%1/7
not(A) or B and not(C) or D
A= not defined $tagB= $_->is($tag)C= not defined $nameD= $_->name eq $name
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
1139 }
1140 0 $self->parent->find_context($tag, $name);
1141 }
1142
1143
1144 =head2 find_ref (tag, name)
1145
1146 The C<find_ref> function looks for tag-and-name combinations that don't have the "is_reference" flag set. It returns the first it finds.
1147 If either tag or name is C<undef>, it ignores that spec.
1148
1149 =cut
1150
1151 0 ▶sub find_ref {
Subroutine find_ref: not called CC=8 SCAR=42.8
Pod documented
1152 0 my ($self, $tag, $name) = @_;
1153 0 foreach ($self->nodes) {
1154 0 ▶ next if $_->{is_reference} or $_->flag('?');
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"is_reference"} or $_->flag("?") 0 0 0
Condition0%0/3
$_->{"is_reference"} or $_->flag("?")
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{"is_reference"} or $_->flag("?")
$_->{"is_reference"} $_->flag("?")
Truth table0%0/3
A or B
A= $_->{"is_reference"}B= $_->flag("?")
ABresult
1X1
011
000
1155 0 ▶ return $_ if ((not defined $tag) || $_->is($tag)) and ((not defined $name) || $_->name eq $name);
Branch: 1 branch
BranchTrueFalseTotal
if !defined($tag) || $_->is($tag) and !defined($name) || $_->name eq $name 0 0 0
Condition0%0/9
!defined($tag) || $_->is($tag) and !defined($name) || $_->name eq $name
!ll&&!rl&&r
000
not defined $tag or $_->is($tag)
l!l&&r!l&&!r
000
not defined $name or $_->name eq $name
l!l&&r!l&&!r
000
MC/DC0%0/4
!defined($tag) || $_->is($tag) and !defined($name) || $_->name eq $name
not defined $tag $_->is($tag) not defined $name $_->name eq $name
Truth table0%0/7
not(A) or B and not(C) or D
A= not defined $tagB= $_->is($tag)C= not defined $nameD= $_->name eq $name
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
1156 }
1157 0 $self->parent->find_ref ($tag, $name);
1158 }
1159
1160 =head2 deref ()
1161
1162 The C<deref> function uses C<find_ref> to dereference a reference tag. If the tag you give it isn't a reference, you'll just get that tag back.
1163 If it's a dangling reference, you'll get C<undef>.
1164
1165 =cut
1166
1167 38 ▶sub deref {
Subroutine deref: called CC=4 SCAR=20.9
Pod documented
1168 38 my ($self) = @_;
1169 38 ▶ return $self unless $self->{is_reference} or $self->flag('?');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_reference"} or $self->flag("?") 38 0 38
Condition33%1/3
$self->{"is_reference"} or $self->flag("?")
l!l&&r!l&&!r
0038
MC/DC0%0/2
$self->{"is_reference"} or $self->flag("?")
$self->{"is_reference"} $self->flag("?")
Truth table33%1/3
A or B
A= $self->{"is_reference"}B= $self->flag("?")
ABresult
1X1
011
000
1170 0 ▶ return $self->parent->find_ref (undef, $self->name) if defined $self->name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->name 0 0 0
1171 0 return $self->parent->find_ref ($self->tag);
1172 }
1173
1174 =head2 set(), get(), get_pair()
1175
1176 These provide a place for object constructors to stash useful information. The C<get> function gets a parameter if the named user variable
1177 hasn't been set. It also allows the specification of a default value.
1178
1179 C<get_pair> gets a pair of named values as an arrayref, with a single arrayref default if neither is found. The individual defaults are assumed
1180 to be 0.
1181
1182 =cut
1183
1184 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
1185 0 my ($self, $var, $value) = @_;
1186 0 $self->{user}->{$var} = $value;
1187 }
1188 0 ▶sub get {
Subroutine get: not called CC=4 SCAR=30.0
Pod documented
1189 0 my ($self, $var, $default) = @_;
1190 0 ▶ return $self->{user}->{$var} if defined $self->{user}->{$var};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"user"}{$var} 0 0 0
1191 0 ▶ return $self->{parameters}->{$var} if defined $self->{parameters}->{$var};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"parameters"}{$var} 0 0 0
1192 0 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 0 0 0
1193 0 ''
1194 }
1195 0 ▶sub get_pair {
Subroutine get_pair: not called CC=3 SCAR=24.8
Pod documented
1196 0 my ($self, $x, $y, $default) = @_;
1197
1198 0 ▶ if ($self->get($x) ne '' || $self->get($y) ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get($x) ne "" or $self->get($y) ne "") 0 0 0
Condition0%0/3
$self->get($x) ne "" or $self->get($y) ne ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get($x) ne "" or $self->get($y) ne ""
$self->get($x) ne "" $self->get($y) ne ""
Truth table0%0/3
A or B
A= $self->get($x) ne ""B= $self->get($y) ne ""
ABresult
1X1
011
000
1199 0 return [($self->get($x, 0)), ($self->get($y, 0))];
1200 }
1201 0 return $default;
1202 }
1203
1204 =head1 VALUES
1205
1206 The value system in a Decl node is getting pretty darned complex. Essentially, though, each node has a value lookup hash that either has scalar values directly
1207 or closures that can be used as proxies for values found in other nodes. (For example, if a node is a macro instantiation, then mostly we're going to be referring
1208 to values in the definition, not in the instance. If a node hasn't explicitly defined a value but its parent has, then when we set that value we'll want to set it
1209 in the parent, not in the child. And so on.)
1210
1211 When we first want to use a given value in a node, we'll call "find_value". That will return a closure that can be called to get or set the value. If the value
1212 can't be set, the closure will simply have no effect. The closure will be stashed locally so that it need only be located once, and we're always assured of being
1213 able to access the same storage location for a given name.
1214
1215 =head2 find_value($var), with helper function get_value_closure
1216
1217 To find a value:
1218
1219 1. Return any previously located closure.
1220 2. If we're a macro instantiation, look at the macro definition.
1221 3. See if there's a local definition for the value; return it if so.
1222 4. See if we have any local constant definitions (our children, evaluated as values).
1223 5. Check our event context.
1224 6. If we're still not in luck, ask our parent to do the same.
1225 7. Otherwise, return "undefined". A set will then create a local variable if necessary.
1226
1227 The closure returned by get_value_closure has the same signature as the varhandlers used by the value tag.
1228 So weird as it sounds, the key and value are in parameters 2 and 3.
1229
1230 =cut
1231
1232 74 ▶sub get_value_closure {
Subroutine get_value_closure: called CC=3 SCAR=11.0
Pod documented
1233 74 my ($self, $value) = @_;
1234 74 ▶ return $self->{h}->{$value} if exists $self->{h}->{$value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"h"}{$value} 0 74 74
1235 74 my $v = $self->{hashtie}->just_get($value);
1236 74 ▶ return $v if ref $v eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "CODE" 43 31 74
1237 74 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1238 74 ▶ $self->{hashtie}->just_store($value, $_[3]) if defined $_[3];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[3] 27 47 74
1239 74 $self->{hashtie}->just_get($value);
1240 }
1241 31 }
1242
1243 95 ▶sub find_value {
Subroutine find_value: called CC=15 SCAR=35.1
Pod documented
1244 95 my ($self, $value) = @_;
1245
1246 #print STDERR "find_value! $self\n";
1247 95 ▶ return $self->{h}->{$value} if exists $self->{h}->{$value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"h"}{$value} 4 91 95
1248 #print STDERR "0\n";
1249 91 ▶ return $self->get_value_closure($value) if exists $self->{v}->{$value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"v"}{$value} 74 17 91
1250 #print STDERR "1\n";
1251
1252 17 my $target = $self;
1253 17 ▶ $target = $self->{instantiates} if $self->{instantiates}; # TODO: maybe. Consider a "context" keyword or sigil or something.
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"instantiates"} 0 17 17
1254
1255 #print STDERR "target is actually $target\n";
1256 17 ▶ if (exists $target->{h}->{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $target->{"h"}{$value}) 0 17 17
1257 #print STDERR "There is a target handler\n";
1258 0 $self->{h}->{$value} = $target->{h}->{$value};
1259 0 return $self->{v}->{$value};
1260 }
1261 17 ▶ if (exists $target->{v}->{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $target->{"v"}{$value}) 0 17 17
1262 #print STDERR "local pointer " . $target->{v}->{$value} . " found\n";
1263 0 $self->{hashtie}->just_store($value, $target->get_value_closure($value));
1264 0 return $self->{hashtie}->just_get($value);
1265 }
1266
1267 17 foreach my $child ($target->nodes) {
1268 4 ▶ if ($child->is($value) or $child->name eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->is($value) or $child->name eq $value) 0 4 4
Condition33%1/3
$child->is($value) or $child->name eq $value
l!l&&r!l&&!r
004
MC/DC0%0/2
$child->is($value) or $child->name eq $value
$child->is($value) $child->name eq $value
Truth table33%1/3
A or B
A= $child->is($value)B= $child->name eq $value
ABresult
1X1
011
000
1269 0 ▶ if ($child->label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->label) 0 0 0
1270 #print STDERR "local child " . $child->describe . " found\n";
1271 0 ▶ $self->{hashtie}->just_store ($value, sub { $child->label });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1272 0 return $self->{hashtie}->just_get ($value);
1273 }
1274 0 ▶ if ($child->describe_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->describe_content) 0 0 0
1275 #print STDERR "local child " . $child->describe . " found\n";
1276 0 ▶ $self->{hashtie}->just_store ($value, sub { $child->describe_content });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1277 0 return $self->{hashtie}->just_get ($value);
1278 }
1279 0 last;
1280 }
1281 }
1282
1283 17 ▶ unless ($target->event_context == $target) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target->event_context == $target) 13 4 17
1284 #print STDERR "We have an event context\n";
1285 13 my $cx = $target->event_context;
1286 13 ▶ if (exists $cx->{h}->{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cx->{"h"}{$value}) 0 13 13
1287 #print STDERR "There is a target handler in the cx\n";
1288 0 $self->{h}->{$value} = $cx->{h}->{$value};
1289 0 return $self->{h}->{$value};
1290 }
1291 #print STDERR "Looking in event context $cx\n";
1292 13 my $context_value = $cx->find_value($value);
1293 #print STDERR "3\n";
1294 13 ▶ if (defined $context_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context_value) 12 1 13
1295 #print STDERR "context value $context_value found\n";
1296 12 $self->{hashtie}->just_store ($value, $context_value);
1297 12 return $context_value;
1298 }
1299 #print STDERR "Was not defined in event context $cx\n";
1300 }
1301
1302 #print STDERR "Looking in parent\n";
1303 5 ▶ return $self->parent->find_value($value) if $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent 1 4 5
1304 #print STDERR "Returning undef\n";
1305 4 return undef;
1306 }
1307
1308
1309 =head2 value($var), setvalue($var, $value)
1310
1311 Accesses the global application value named.
1312
1313 =cut
1314
1315 12 ▶sub value { $_[0]->{v}->{$_[1]} }
Subroutine value: called CC=1 SCAR=0.0
Pod documented
1316 #sub setvalue { $_[0]->{v}->{$_[1]} = $_[2]; }
1317 29 ▶sub setvalue {
Subroutine setvalue: called CC=3 SCAR=11.0
Pod documented
1318 29 my ($self, $value, $newvalue) = @_;
1319 29 ▶ return if $value =~ /^\*/; # Set has no effect on *-values.
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\*/ 0 29 29
1320 29 my $var = $self->find_value($value);
1321 29 ▶ return $var->($self, $self->{v}, $value, $newvalue) if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 28 1 29
1322 1 $self->{hashtie}->just_store($value, $newvalue);
1323 }
1324
1325 =head2 get_value($var)
1326
1327 Given the name of a value, we can find it in various places, which we look at in order:
1328
1329 - A set value in the node asked
1330 - Rinse and repeat for the node's parent.
1331
1332 Names starting with an asterisk find parts of the node itself: *name, *label, *parameter <n>,
1333 *option <n>, *content, and anything else I forgot and add later. A double asterisk gets the same values
1334 from the parent. Triple asterisk, grandparent, etc.
1335
1336 =cut
1337
1338 52 ▶sub get_value {
Subroutine get_value: called CC=7 SCAR=30.2
Pod documented
1339 52 my ($self, $value) = @_;
1340
1341 52 ▶ if ($value =~ /^\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\*/) 0 52 52
1342 0 $value =~ s/^\* *//;
1343 0 ▶ return $self->parent->get_value ($value) if $value =~ /^\*/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\*/ 0 0 0
1344 0 ▶ return $self->name if $value eq 'name';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "name" 0 0 0
1345 0 ▶ return $self->label if $value eq 'label';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "label" 0 0 0
1346 0 ▶ return $self->describe_content('', 0) if $value eq 'content';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "content" 0 0 0
1347 0 return undef;
1348 }
1349
1350 52 my $var = $self->find_value($value);
1351 52 ▶ return if not defined $var;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var 2 50 52
1352 50 $var->($self, $self->{v}, $value);
1353 }
1354
1355 =head2 express_value($valuespec)
1356
1357 A full value spec pipes a given value through a series of filters:
1358
1359 <value>[|<filter>]*
1360
1361 A filter is simply a function that takes one parameter. (This is an oversimplification: the filter can be given parameters that are space-delimited.)
1362
1363 If no lookup value is desired as a starting value, you can also just start the pipe with a filter/function call. marked with an exclamation mark:
1364
1365 !<filter>[|<filter>]*
1366
1367 Clear? Clear.
1368
1369 =cut
1370
1371 0 ▶sub express_value {
Subroutine express_value: not called CC=3 SCAR=24.8
Pod documented
1372 0 my ($self, $valspec) = @_;
1373 0 my @pieces = split /\|/, $valspec; # TODO: a real parser to permit pipe characters within strings.
1374 0 my $value = '';
1375 0 ▶ if ($pieces[0] =~ /^!/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pieces[0] =~ /^!/) { } 0 0 0
1376 0 $pieces[0] =~ s/^! *//;
1377 } else {
1378 0 $value = $self->get_value(shift @pieces);
1379 }
1380 0 ▶ while (my $filter = shift @pieces) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $filter = shift @pieces) 0 0 0
1381 0 $filter =~ s/^\s*//;
1382 0 $filter =~ s/\s*$//;
1383 0 my @words = parse_line ('\s+', 0, $filter);
1384 0 my $filter = shift @words;
1385 0 $value = $self->call_filter($filter, $value, @words);
1386 }
1387 0 $value;
1388 }
1389
1390
1391 =head2 register_varhandler ($event, $handler)
1392
1393 Registers a variable handler in the event context. If there is a handler registered for a name, it will be called instead of the normal
1394 hash read and write. This means you can attach active content to a variable, then treat it just like any other variable in your code.
1395
1396 =cut
1397
1398 4 ▶sub register_varhandler {
Subroutine register_varhandler: called CC=1 SCAR=0.0
Pod documented
1399 4 my ($self, $key, $handler) = @_;
1400 4 $self->{h}->{$key} = $handler;
1401 }
1402
1403
1404 =head2 subs()
1405
1406 Returns all our direct children named 'sub', plus the same thing from our parent. Our answers mask our parent's.
1407
1408 =cut
1409
1410 46 ▶sub subs {
Subroutine subs: called CC=4 SCAR=13.9
Pod documented
1411 46 my $self = shift;
1412 46 ▶ my $subs = $self->parent ? $self->parent()->subs() : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->parent ? : 23 23 46
1413 46 foreach ($self->nodes()) {
1414 86 ▶ next unless $_->tag() eq 'sub';
Branch: 1 branch
BranchTrueFalseTotal
unless $_->tag eq "sub" 84 2 86
1415 2 $_->build();
1416 2 $subs->{$_->name} = $_;
1417 }
1418 46 return $subs;
1419 }
1420
1421 =head2 find_filter(filter), call_filter(filter, value)
1422
1423 Finds a filter by name from a given point in the tree and calls it with a set of parameters.
1424
1425 =cut
1426
1427 0 ▶sub find_filter {
Subroutine find_filter: not called CC=6 SCAR=37.4
Pod documented
1428 0 my ($self, $filter) = @_;
1429
1430 0 ▶ $self = $self->{instantiates} if $self->{instantiates}; # TODO: I think this is probably correct.
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"instantiates"} 0 0 0
1431 0 foreach ($self->nodes()) {
1432 0 ▶ return $_ if $_->is("sub|filter") and $_->name eq $filter;
Branch: 1 branch
BranchTrueFalseTotal
if $_->is("sub|filter") and $_->name eq $filter 0 0 0
Condition0%0/3
$_->is("sub|filter") and $_->name eq $filter
!ll&&!rl&&r
000
MC/DC0%0/2
$_->is("sub|filter") and $_->name eq $filter
$_->is("sub|filter") $_->name eq $filter
Truth table0%0/3
A and B
A= $_->is("sub|filter")B= $_->name eq $filter
ABresult
0X0
100
111
1433 }
1434 0 ▶ return $self->parent->find_filter($filter) if $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent 0 0 0
1435 0 $filter = Decl->register_filter($filter);
1436 0 return $filter;
1437 }
1438
1439 0 ▶sub call_filter {
Subroutine call_filter: not called CC=3 SCAR=24.8
Pod documented
1440 0 my $self = shift;
1441 0 my $filter = shift;
1442 0 my $value = shift;
1443
1444 0 $filter = $self->find_filter($filter);
1445 0 ▶ if (not defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filter) 0 0 0
1446 # TODO: warning
1447 0 return $value;
1448 }
1449 0 ▶ if (ref $filter eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filter eq "CODE") 0 0 0
1450 0 return &$filter ($value, @_);
1451 }
1452 0 $filter->go(undef, $value, @_);
1453 }
1454
1455 #=head2 AUTOLOAD
1456 #
1457 #If a call is made against a node with a payload, the node will try to proxy the payload object's methods using AUTOLOAD.
1458 #
1459 #TODO: some kind of dot notation to permit this to work with inner nodes.
1460 #
1461 #=cut
1462 #
1463 #sub AUTOLOAD {
1464 # my ($self) = @_;
1465 # croak "No method $AUTOLOAD" unless $self->payload and ref($self->payload);
1466 #
1467 # my $name = our $AUTOLOAD;
1468 # $name =~ s/.*://;
1469 # return "No method $AUTOLOAD" unless $self->payload->can($name);
1470 #
1471 # *$AUTOLOAD = eval "sub { my \$self = shift; \$self->payload->$name (\@_) }";
1472 # goto &$AUTOLOAD;
1473 #}
1474
1475
1476 =head1 OUTPUT
1477
1478 =head2 write(), log(), output()
1479
1480 The C<write> function is supported for any node; by default it simply passes its arguments up to its parent. The top of the tree will print everything
1481 to STDOUT - by default. At any point in the tree, though, a node may claim ownership of the output stream by having an option [output]; any C<write>
1482 called below that node's parent will be written to that node's C<write>. Obviously, this is a good way to use files.
1483
1484 The C<log> function is exactly the same, except the default is to write to STDERR and the option to use is [log].
1485
1486 There is another difference: a file used as [output] will by default start from scratch ('w'), while a file used as [log] will append its material ('a').
1487 Either is opened during build, and closed when the program closes.
1488
1489 If it's not in [output] or [log] mode, however, each call to C<write> on a file is independent; the file is closed afterwards and no handle is kept around.
1490 This can be overridden with a (keepopen) parameter or a (>>) parameter for appending. (Any appending file will be opened for appending during build and
1491 closed when the program closes.)
1492
1493 If a file is in keepopen mode, the buffers are flushed after each C<write>/C<log>.
1494
1495 The C<output> function defaults to C<write>. For a macro definition, though, it is used to build the macro to be instantiated.
1496
1497 =cut
1498
1499 0 ▶sub output {
Subroutine output: not called CC=1 SCAR=6.9
Pod documented
1500 0 my $self = shift;
1501 0 $self->write(@_);
1502 }
1503
1504 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
1505 0 my $self = shift;
1506 0 $self->parent->write(@_);
1507 }
1508 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
1509 0 my $self = shift;
1510 0 $self->parent->log(@_);
1511 }
1512
1513 =head1 AUTHOR
1514
1515 Michael Roberts, C<< <michael at vivtek.com> >>
1516
1517 =head1 BUGS
1518
1519 Please report any bugs or feature requests to C<bug-decl at rt.cpan.org>, or through
1520 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Decl>. I will be notified, and then you'll
1521 automatically be notified of progress on your bug as I make changes.
1522
1523 =head1 LICENSE AND COPYRIGHT
1524
1525 Copyright 2010 Michael Roberts.
1526
1527 This program is free software; you can redistribute it and/or modify it
1528 under the terms of either: the GNU General Public License as published
1529 by the Free Software Foundation; or the Artistic License.
1530
1531 See http://dev.perl.org/licenses/ for more information.
1532
1533 =cut
1534
1535 13 1; # End of Decl::Node
« lib/Decl/Semantics/Use.pm ↑ Summary blib/lib/Decl/DefaultParsers.pm »