summaryrefslogtreecommitdiff
path: root/lib/Config/AST/Node.pm
blob: c240bc3af1198fd4d0024ca397698e411dd5a111 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package Config::AST::Node;

use strict;
use warnings;
use parent 'Exporter';
use Text::Locus;
use Clone 'clone';

use Carp;

our %EXPORT_TAGS = ( 'sort' => [ qw(NO_SORT SORT_NATURAL SORT_PATH) ] );
our @EXPORT_OK = qw(NO_SORT SORT_NATURAL SORT_PATH);

=head1 NAME

Config::AST::Node - generic configuration syntax tree node

=head1 SYNOPSIS

use parent 'Config::AST::Node';
    
=head1 DESCRIPTION

This is an abstract class representing a node in the configuration parse
tree. A node can be either a non-leaf node, representing a I<section>, or
a leaf node, representing a I<simple statement>.

=head1 METHODS    

=head2 new(ARG => VAL, ...)

Creates new object. Recognized arguments are:

=over 4

=item B<clone =E<gt>> I<OBJ>

Clone object I<OBJ>, which must be an instance of B<Config::AST::Node>
or its derived class.

=item B<default =E<gt>> I<VAL>

Sets default value.

=item B<locus =E<gt>> I<LOC>

Sets the locus - an object of class B<Text::Locus>, which see.

=item B<file =E<gt>> I<NAME>    

Sets the file name.

=item B<order =E<gt>> I<N>

Sets ordinal number.

=back
    
=cut    
    
sub new {
    my $class = shift;
    local %_ = @_;
    my $v;
    my $self;
    if ($v = delete $_{clone}) {
	$self = Clone::clone($v);
    } else {
	$self = bless { }, $class;
    }
    if (defined($v = delete $_{default})) {
	$self->default($v);
    }
    if (defined($v = delete $_{locus})) {
	$self->locus($v);
    }

    if (defined($v = delete $_{file})) {
	$self->locus($v, delete $_{line} // 0);
    }
    if (defined($v = delete $_{order})) {
	$self->order($v);
    }
    croak "unrecognized arguments" if keys(%_);
    return $self;
}

=head2 $x = $node->locus;

Returns a locus associated with the node.
    
=head2 $node->locus($LOC)

=head2 $node->locus($FILE, $LINE)    

Associates a locus with the node. In the second form, a new locus object
is created for location I<$FILE>:I<$LINE>.
    
=cut

sub locus {
    my $self = shift;
    if (@_ == 1) {
	croak "bad argument type"
	    unless ref($_[0]) eq 'Text::Locus';
	$self->{_locus} = $_[0];
    } elsif (@_ == 2) {
	$self->{_locus} = new Text::Locus(@_);
    } elsif (@_) {
	croak "bad number of arguments";
    }
    return $self->{_locus} ||= new Text::Locus;
}

=head2 $x = $node->order

=head2 $node->order(I<$N>)

Returns or sets and returns ordinal number for the node.

=cut    
    
sub order {
    my ($self, $val) = @_;
    if (defined($val)) {
	$self->{_order} = $val;
    }
    return $self->{_order} // 0;
}

=head2 $x = $node->default

=head2 $node->default(I<$N>)

Returns or sets and returns default value for the node.

=cut    
    
sub default {
    my ($self, $val) = @_;
    if (defined($val)) {
	$self->{_default} = $val;
    }
    return $self->{_default};
}

=head2 $node->is_leaf

Returns true if node is a leaf node
    
=cut    

sub is_leaf { 0 }

=head2 $node->is_null

Returns true if node is a null node

=cut    

sub is_null { 0 }

=head2 $node->is_section

Returns true if node represents a section.

=cut    

sub is_section { ! shift->is_leaf }

=head2 $node->is_value

Returns true if node represents a value (or statement).

=cut

sub is_value { shift->is_leaf }

use constant {
    NO_SORT => sub { @_ },
    SORT_NATURAL => sub {
	    sort { $a->[1]->order <=> $b->[1]->order } @_
    },
    SORT_PATH => sub {
	sort { join('.',@{$a->[0]}) cmp join('.', @{$b->[0]}) } @_
    }
};

=head2 @array = $cfg->flatten()

=head2 @array = $cfg->flatten(sort => $sort)    

Returns a I<flattened> representation of the configuration, as a
list of pairs B<[ $path, $value ]>, where B<$path> is a reference
to the variable pathname, and B<$value> is a
B<Config::AST::Node::Value> object.

The I<$sort> argument controls the ordering of the entries in the returned
B<@array>.  It is either a code reference suitable to pass to the Perl B<sort>
function, or one of the following constants:

=over 4

=item NO_SORT

Don't sort the array.  Statements will be placed in an apparently random
order.

=item SORT_NATURAL

Preserve relative positions of the statements.  Entries in the array will
be in the same order as they appeared in the configuration file.  This is
the default.

=item SORT_PATH

Sort by pathname.

=back

These constants are not exported by default.  You can either import the
ones you need, or use the B<:sort> keyword to import them all, e.g.:

    use Config::AST::Node qw(:sort);
    @array = $node->flatten(sort => SORT_PATH);
    
=cut

sub flatten {
    my $self = shift;
    local %_ = @_;
    my $sort = delete($_{sort}) || SORT_NATURAL;
    my @ar;
    my $i;
    
    croak "unrecognized keyword arguments: ". join(',', keys %_)
	if keys %_;

    push @ar, [ [], $self ];
    foreach my $elt (@ar) {
	next if $elt->[1]->is_value;
	while (my ($kw, $val) = each %{$elt->[1]->subtree}) {
	    push @ar, [ [@{$elt->[0]}, $kw], $val ];
	}
    }

    croak "sort must be a coderef"
	unless ref($sort) eq 'CODE';

    shift @ar; # toss off first entry
    return &{$sort}(grep { $_->[1]->is_value } @ar);
}       

=head2 $cfg->canonical(%args)

Returns the canonical string representation of the configuration node.
For value nodes, canonical representation is:

    QVAR=VALUE

where QVAR is fully qualified variable name, and VALUE is the corresponding
value.

For sections, canonical representation is a list of canonical representations
of the underlying nodes, delimited by newlines (or another character - see the
description of the B<delim> argument, below). The list is sorted by QVAR in
ascending lexicographical order.

B<%args> are zero or more of the following keywords:

=over 4

=item B<delim =E<gt> >I<STR>

Use I<STR> to delimit statements, instead of the newline.

=item B<locus =E<gt> 1>     

Prefix each statement with its location.

=back    
    
=cut

sub canonical {
    my $self = shift;
    local %_ = @_;
    my $delim;
    unless (defined($delim = delete $_{delim})) {
	$delim = "\n";
    }
    my $prloc = delete $_{locus};
    carp "unrecognized parameters: " . join(', ', keys(%_)) if (keys(%_));
    
    return join $delim, map {
		             ($prloc ? '[' . $_->[1]->locus . ']: ' : '')
				 . join('.', map {
				                 if (/[\.="]/) {
						     s/\"/\\"/;
						     '"'.$_.'"'
						 } else {
						     $_
						 }
					     } @{$_->[0]})
			     . "="
			     . Data::Dumper->new([scalar $_->[1]->value])
			                   ->Useqq(1)
					   ->Terse(1)
					   ->Indent(0)
					   ->Dump
    } $self->flatten(sort => SORT_PATH);
}

use overload
    bool => sub { 1 },
    '""' => sub { shift->as_string },
    eq => sub {
	my ($self,$other) = @_;
	return $self->as_string eq $other
    };
	
=head1 SEE ALSO

B<Config::AST>,    
B<Config::AST::Node::Null>,
B<Config::AST::Node::Value>,
B<Config::AST::Node::Section>.
    
=cut    

1;

	

Return to:

Send suggestions and report system problems to the System administrator.