diff options
-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 { | |||
379 | croak "call to abstract method" | 379 | croak "call to abstract method" |
380 | } | 380 | } |
381 | 381 | ||
382 | =head2 $cfg->commit | 382 | =head2 $cfg->commit([%hash]) |
383 | 383 | ||
384 | Must be called after B<parse> to finalize the parse tree. This function | 384 | Must be called after B<parse> to finalize the parse tree. This function |
385 | does the actual syntax checking and applied default values to the statements | 385 | applies default values on settings where such are defined. |
386 | where such are defined. Returns true on success. | 386 | |
387 | Optional arguments control what steps are performed. | ||
388 | |||
389 | =over 4 | ||
390 | |||
391 | =item lint => 1 | ||
392 | |||
393 | Forse syntax checking. This can be necessary if new nodes were added to | ||
394 | the tree after parsing. | ||
395 | |||
396 | =item lexicon => I<$hashref> | ||
397 | |||
398 | Override the lexicon used for syntax checking and default value processing. | ||
399 | |||
400 | =back | ||
401 | |||
402 | Returns true on success. | ||
387 | 403 | ||
388 | =cut | 404 | =cut |
389 | 405 | ||
390 | sub commit { | 406 | sub commit { |
391 | my ($self) = @_; | 407 | my ($self, %opts) = @_; |
392 | # FIXME | 408 | my $lint = delete $opts{lint}; |
393 | $self->fixup_tree($self->tree, $self->{_lexicon}) | 409 | my $lexicon = delete $opts{lexicon} // $self->lexicon; |
394 | if exists $self->{_lexicon}; | 410 | croak "unrecognized arguments" if keys(%opts); |
411 | if ($lexicon) { | ||
412 | $self->lint_subtree($lexicon, $self->tree) if $lint; | ||
413 | $self->fixup_tree($self->tree, $lexicon); | ||
414 | } | ||
395 | return $self->{_error_count} == 0; | 415 | return $self->{_error_count} == 0; |
396 | } | 416 | } |
397 | 417 | ||
@@ -765,7 +785,11 @@ names are separated by dots. I.e., the following two calls are equivalent: | |||
765 | $cfg->add_node(qw(core pidfile), $node) | 785 | $cfg->add_node(qw(core pidfile), $node) |
766 | 786 | ||
767 | $cfg->add_node('core.pidfile', $node) | 787 | $cfg->add_node('core.pidfile', $node) |
768 | 788 | ||
789 | If the node already exists at B<$path>, new node is merged to it according | ||
790 | to the lexical rules. I.e., for scalar value, new node overwrites the old | ||
791 | one. For lists, it is appended to the list. | ||
792 | |||
769 | =cut | 793 | =cut |
770 | 794 | ||
771 | sub add_node { | 795 | sub add_node { |
@@ -891,18 +915,25 @@ sub add_node { | |||
891 | Adds a statement node with the given B<$value> and B<$locus> in position, | 915 | Adds a statement node with the given B<$value> and B<$locus> in position, |
892 | indicated by $path. | 916 | indicated by $path. |
893 | 917 | ||
918 | If the setting already exists at B<$path>, the new value is merged to it | ||
919 | according to the lexical rules. I.e., for scalars, B<$value> overwrites | ||
920 | prior setting. For lists, it is appended to the list. | ||
921 | |||
894 | =cut | 922 | =cut |
895 | 923 | ||
896 | sub add_value { | 924 | sub add_value { |
897 | my ($self, $path, $value, $locus) = @_; | 925 | my ($self, $path, $value, $locus) = @_; |
898 | $self->add_node($path, new Config::AST::Node::Value(value => $value, | 926 | $self->add_node($path, new Config::AST::Node::Value(value => $value, |
899 | locus => $locus)); | 927 | locus => $locus)); |
900 | } | 928 | } |
901 | 929 | ||
902 | =head2 $cfg->set(@path, $value) | 930 | =head2 $cfg->set(@path, $value) |
903 | 931 | ||
904 | Sets the configuration variable B<@path> to B<$value>. | 932 | Sets the configuration variable B<@path> to B<$value>. |
905 | 933 | ||
934 | No syntax checking is performed. To enforce syntax checking use | ||
935 | B<add_value>. | ||
936 | |||
906 | =cut | 937 | =cut |
907 | 938 | ||
908 | sub set { | 939 | sub set { |
@@ -1169,9 +1200,10 @@ sub lint_subtree { | |||
1169 | } | 1200 | } |
1170 | } | 1201 | } |
1171 | 1202 | ||
1172 | =head2 $cfg->lint(\%lex) | 1203 | =head2 $cfg->lint([\%lex]) |
1173 | 1204 | ||
1174 | Checks the syntax according to the keyword lexicon B<%lex>. On success, | 1205 | Checks the syntax according to the keyword lexicon B<%lex> (or |
1206 | B<$cfg-E<gt>lexicon>, if called without arguments). On success, | ||
1175 | applies eventual default values and returns true. On errors, reports | 1207 | applies eventual default values and returns true. On errors, reports |
1176 | them using B<error> and returns false. | 1208 | them using B<error> and returns false. |
1177 | 1209 | ||
@@ -1183,11 +1215,7 @@ after calling B<parse>. | |||
1183 | 1215 | ||
1184 | sub lint { | 1216 | sub lint { |
1185 | my ($self, $lexicon) = @_; | 1217 | my ($self, $lexicon) = @_; |
1186 | 1218 | return $self->commit(lint => 1, lexicon => $lexicon); | |
1187 | # $synt->{'*'} = { section => { '*' => 1 }} ; | ||
1188 | $self->lint_subtree($lexicon, $self->tree); | ||
1189 | $self->fixup_tree($self->tree, $lexicon); | ||
1190 | return $self->{_error_count} == 0; | ||
1191 | } | 1219 | } |
1192 | 1220 | ||
1193 | =head1 SEE ALSO | 1221 | =head1 SEE ALSO |