summaryrefslogtreecommitdiff
path: root/lib/Config/AST/Root.pm
blob: e0a6da88586a03ed7cc20c8594d73e5fc6e6c574 (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
# This file is part of Config::AST                            -*- perl -*-
# Copyright (C) 2017-2019 Sergey Poznyakoff <gray@gnu.org>
#
# Config::AST is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# Config::AST is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Config::AST.  If not, see <http://www.gnu.org/licenses/>.

package Config::AST::Root;
use strict;
use warnings;

=head1 NAME

Config::AST::Root - root of the abstract syntax tree

=head1 DESCRIPTION

An auxiliary class representing the root of the abstract syntax tree.
It is necessary because the tree itself forms a circular structure
(due to the B<root> attribute of B<Config::AST::Node::Section>). Without
this intermediate class (if B<root> pointed to B<Config::AST> itself),
the structure would have never been destroyed, because each element
would remain referenced at least once.

=head1 CONSTRUCTOR

=head2 $obj = new($ci)

I<$ci> is one to enable case-insensitive keyword lookup, and 0 otherwise.

=cut

sub new {
    my ($class, $ci) = @_;
    bless { _ci => $ci }, $class;
}

=head1 METHODS

=head2 $s = $r->mangle_key($name)

Converts the string I<$name> to a form suitable for lookups, in accordance
with the _ci attribute.

=cut

sub mangle_key {
    my ($self, $key) = @_;
    $self->{_ci} ? lc($key) : $key;
}

=head2 $r->reset

Destroys the underlying syntax tree.

=cut

sub reset { delete shift->{_tree} }


=head2 $t = $r->tree

Returns the root node of the tree, initializing it if necessary.

=cut

sub tree {
    my $self = shift;

    return $self->{_tree} //=
	        new Config::AST::Node::Section($self,
	                                       locus => new Text::Locus);
}

=head2 $bool = $r->empty

Returns true if the tree is empty.

=cut

sub empty {
    my $self = shift;
    return !($self->{_tree} && $self->{_tree}->keys > 0);
}

=head1 SEE ALSO

L<Config::AST>.

=cut

1;

Return to:

Send suggestions and report system problems to the System administrator.