diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-08-19 22:39:25 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-08-19 22:39:25 +0300 |
commit | 18455d3afab3365d522fb6fa85ca8e03d0a388e6 (patch) | |
tree | 31bb8de510ced1e5e8182daffc6dba75e69e7e42 /lib/Config | |
parent | 11e6beb1f44fb74dc11e79c3bda25aebfd80ae12 (diff) | |
download | config-parser-18455d3afab3365d522fb6fa85ca8e03d0a388e6.tar.gz config-parser-18455d3afab3365d522fb6fa85ca8e03d0a388e6.tar.bz2 |
Use the same instance for parsing both the syntax definition and configuration.
* lib/Config/Parser.pm (inited): Remove.
(loadsynt): Don't create a dedicated object for parsing the
syntax definition. Use the principal one instead.
Diffstat (limited to 'lib/Config')
-rw-r--r-- | lib/Config/Parser.pm | 61 |
1 files changed, 27 insertions, 34 deletions
diff --git a/lib/Config/Parser.pm b/lib/Config/Parser.pm index fed0100..0f0a1f0 100644 --- a/lib/Config/Parser.pm +++ b/lib/Config/Parser.pm @@ -23,25 +23,25 @@ sub new { } } } + + my $self = $class->SUPER::new(%_); - unless ($_{lexicon}) { + if (my $lex = delete $_{lexicon}) { + $self->lexicon($lex); + } else { + $self->lexicon({ '*' => '*' }); my $subs = Class::Inspector->subclasses(__PACKAGE__); if ($subs) { - $_{lexicon} = {}; foreach my $c (@$subs) { -# print "LOADING FROM $c\n"; - if (my $s = loadsynt($c)) { - $_{lexicon} = { %{$_{lexicon}}, %$s }; - $loaded++; + if (my ($file, $line, $data) = $c->findsynt) { + $self->loadsynt($file, $line, $data); } last if $c eq $class; } - delete $_{lexicon} unless keys %{$_{lexicon}}; } } - my $self = $class->SUPER::new(%_); - $self->init if $loaded; + $self->init; if (@parseargs) { $self->parse(@parseargs); $self->commit or croak "configuration failed"; @@ -50,19 +50,13 @@ sub new { return $self; } -sub init { - my $self = shift; - $self->{_inited} = 1; -} - -sub inited { shift->{_inited} }; +sub init {} +sub mangle {} sub commit { my $self = shift; my $res = $self->SUPER::commit; - if ($res && $self->inited && $self->can('mangle')) { - $self->mangle; - } + $self->mangle if $res; return $res; } @@ -83,18 +77,18 @@ sub findsynt { } sub loadsynt { - my ($class) = @_; - if (my ($file, $line, $data) = findsynt($class)) { - open(my $fh, '<', \$data) - or croak "can't open filehandle for data string"; - my $d = $class->new(filename => $file, - fh => $fh, - line => $line, - lexicon => { '*' => '*' }) - or croak "Failed to parse template at $file:$line"; - close $fh; - $d->as_hash(sub { - my ($what, $name, $val) = @_; + my ($self, $file, $line, $data) = @_; + open(my $fh, '<', \$data) + or croak "can't open filehandle for data string"; + $self->parse($file, + fh => $fh, + line => $line) + or croak "Failed to parse template at $file:$line"; + close $fh; + $self->lexicon({ + %{$self->lexicon // {}}, + %{$self->as_hash(sub { + my ($what, $name, $val) = @_; $name = '*' if $name eq 'ANY'; if ($what eq 'section') { $val->{section} = {}; @@ -106,7 +100,7 @@ sub loadsynt { my @words = parse_line('\s+', 0, $val); my $ret = {}; $val = shift @words; - + if ($val eq 'STRING') { # nothing } elsif ($val eq 'NUMBER') { @@ -128,13 +122,12 @@ sub loadsynt { if ($ret->{array}) { $ret->{default} = [@words]; } else { - $ret->{default} = join(' ', @words); + $ret->{default} = join(' ', @words); } } ($name, $ret); } - })->{section}; - } + })->{section}}}); } 1; |