diff options
Diffstat (limited to 'lib/Config/AST.pm')
-rw-r--r-- | lib/Config/AST.pm | 60 |
1 files changed, 44 insertions, 16 deletions
diff --git a/lib/Config/AST.pm b/lib/Config/AST.pm index 837bbac..fb10569 100644 --- a/lib/Config/AST.pm +++ b/lib/Config/AST.pm @@ -379,19 +379,39 @@ sub parse { croak "call to abstract method" } -=head2 $cfg->commit +=head2 $cfg->commit([%hash]) Must be called after B<parse> to finalize the parse tree. This function -does the actual syntax checking and applied default values to the statements -where such are defined. Returns true on success. +applies default values on settings where such are defined. + +Optional arguments control what steps are performed. + +=over 4 + +=item lint => 1 + +Forse syntax checking. This can be necessary if new nodes were added to +the tree after parsing. + +=item lexicon => I<$hashref> + +Override the lexicon used for syntax checking and default value processing. + +=back + +Returns true on success. =cut sub commit { - my ($self) = @_; - # FIXME - $self->fixup_tree($self->tree, $self->{_lexicon}) - if exists $self->{_lexicon}; + my ($self, %opts) = @_; + my $lint = delete $opts{lint}; + my $lexicon = delete $opts{lexicon} // $self->lexicon; + croak "unrecognized arguments" if keys(%opts); + if ($lexicon) { + $self->lint_subtree($lexicon, $self->tree) if $lint; + $self->fixup_tree($self->tree, $lexicon); + } return $self->{_error_count} == 0; } @@ -765,7 +785,11 @@ names are separated by dots. I.e., the following two calls are equivalent: $cfg->add_node(qw(core pidfile), $node) $cfg->add_node('core.pidfile', $node) - + +If the node already exists at B<$path>, new node is merged to it according +to the lexical rules. I.e., for scalar value, new node overwrites the old +one. For lists, it is appended to the list. + =cut sub add_node { @@ -891,18 +915,25 @@ sub add_node { Adds a statement node with the given B<$value> and B<$locus> in position, indicated by $path. +If the setting already exists at B<$path>, the new value is merged to it +according to the lexical rules. I.e., for scalars, B<$value> overwrites +prior setting. For lists, it is appended to the list. + =cut sub add_value { my ($self, $path, $value, $locus) = @_; $self->add_node($path, new Config::AST::Node::Value(value => $value, - locus => $locus)); + locus => $locus)); } =head2 $cfg->set(@path, $value) Sets the configuration variable B<@path> to B<$value>. +No syntax checking is performed. To enforce syntax checking use +B<add_value>. + =cut sub set { @@ -1169,9 +1200,10 @@ sub lint_subtree { } } -=head2 $cfg->lint(\%lex) +=head2 $cfg->lint([\%lex]) -Checks the syntax according to the keyword lexicon B<%lex>. On success, +Checks the syntax according to the keyword lexicon B<%lex> (or +B<$cfg-E<gt>lexicon>, if called without arguments). On success, applies eventual default values and returns true. On errors, reports them using B<error> and returns false. @@ -1183,11 +1215,7 @@ after calling B<parse>. sub lint { my ($self, $lexicon) = @_; - -# $synt->{'*'} = { section => { '*' => 1 }} ; - $self->lint_subtree($lexicon, $self->tree); - $self->fixup_tree($self->tree, $lexicon); - return $self->{_error_count} == 0; + return $self->commit(lint => 1, lexicon => $lexicon); } =head1 SEE ALSO |