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 | |
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.
-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 { | |||
23 | } | 23 | } |
24 | } | 24 | } |
25 | } | 25 | } |
26 | |||
27 | my $self = $class->SUPER::new(%_); | ||
26 | 28 | ||
27 | unless ($_{lexicon}) { | 29 | if (my $lex = delete $_{lexicon}) { |
30 | $self->lexicon($lex); | ||
31 | } else { | ||
32 | $self->lexicon({ '*' => '*' }); | ||
28 | my $subs = Class::Inspector->subclasses(__PACKAGE__); | 33 | my $subs = Class::Inspector->subclasses(__PACKAGE__); |
29 | if ($subs) { | 34 | if ($subs) { |
30 | $_{lexicon} = {}; | ||
31 | foreach my $c (@$subs) { | 35 | foreach my $c (@$subs) { |
32 | # print "LOADING FROM $c\n"; | 36 | if (my ($file, $line, $data) = $c->findsynt) { |
33 | if (my $s = loadsynt($c)) { | 37 | $self->loadsynt($file, $line, $data); |
34 | $_{lexicon} = { %{$_{lexicon}}, %$s }; | ||
35 | $loaded++; | ||
36 | } | 38 | } |
37 | last if $c eq $class; | 39 | last if $c eq $class; |
38 | } | 40 | } |
39 | delete $_{lexicon} unless keys %{$_{lexicon}}; | ||
40 | } | 41 | } |
41 | } | 42 | } |
42 | 43 | ||
43 | my $self = $class->SUPER::new(%_); | 44 | $self->init; |
44 | $self->init if $loaded; | ||
45 | if (@parseargs) { | 45 | if (@parseargs) { |
46 | $self->parse(@parseargs); | 46 | $self->parse(@parseargs); |
47 | $self->commit or croak "configuration failed"; | 47 | $self->commit or croak "configuration failed"; |
@@ -50,19 +50,13 @@ sub new { | |||
50 | return $self; | 50 | return $self; |
51 | } | 51 | } |
52 | 52 | ||
53 | sub init { | 53 | sub init {} |
54 | my $self = shift; | 54 | sub mangle {} |
55 | $self->{_inited} = 1; | ||
56 | } | ||
57 | |||
58 | sub inited { shift->{_inited} }; | ||
59 | 55 | ||
60 | sub commit { | 56 | sub commit { |
61 | my $self = shift; | 57 | my $self = shift; |
62 | my $res = $self->SUPER::commit; | 58 | my $res = $self->SUPER::commit; |
63 | if ($res && $self->inited && $self->can('mangle')) { | 59 | $self->mangle if $res; |
64 | $self->mangle; | ||
65 | } | ||
66 | return $res; | 60 | return $res; |
67 | } | 61 | } |
68 | 62 | ||
@@ -83,18 +77,18 @@ sub findsynt { | |||
83 | } | 77 | } |
84 | 78 | ||
85 | sub loadsynt { | 79 | sub loadsynt { |
86 | my ($class) = @_; | 80 | my ($self, $file, $line, $data) = @_; |
87 | if (my ($file, $line, $data) = findsynt($class)) { | 81 | open(my $fh, '<', \$data) |
88 | open(my $fh, '<', \$data) | 82 | or croak "can't open filehandle for data string"; |
89 | or croak "can't open filehandle for data string"; | 83 | $self->parse($file, |
90 | my $d = $class->new(filename => $file, | 84 | fh => $fh, |
91 | fh => $fh, | 85 | line => $line) |
92 | line => $line, | 86 | or croak "Failed to parse template at $file:$line"; |
93 | lexicon => { '*' => '*' }) | 87 | close $fh; |
94 | or croak "Failed to parse template at $file:$line"; | 88 | $self->lexicon({ |
95 | close $fh; | 89 | %{$self->lexicon // {}}, |
96 | $d->as_hash(sub { | 90 | %{$self->as_hash(sub { |
97 | my ($what, $name, $val) = @_; | 91 | my ($what, $name, $val) = @_; |
98 | $name = '*' if $name eq 'ANY'; | 92 | $name = '*' if $name eq 'ANY'; |
99 | if ($what eq 'section') { | 93 | if ($what eq 'section') { |
100 | $val->{section} = {}; | 94 | $val->{section} = {}; |
@@ -106,7 +100,7 @@ sub loadsynt { | |||
106 | my @words = parse_line('\s+', 0, $val); | 100 | my @words = parse_line('\s+', 0, $val); |
107 | my $ret = {}; | 101 | my $ret = {}; |
108 | $val = shift @words; | 102 | $val = shift @words; |
109 | 103 | ||
110 | if ($val eq 'STRING') { | 104 | if ($val eq 'STRING') { |
111 | # nothing | 105 | # nothing |
112 | } elsif ($val eq 'NUMBER') { | 106 | } elsif ($val eq 'NUMBER') { |
@@ -128,13 +122,12 @@ sub loadsynt { | |||
128 | if ($ret->{array}) { | 122 | if ($ret->{array}) { |
129 | $ret->{default} = [@words]; | 123 | $ret->{default} = [@words]; |
130 | } else { | 124 | } else { |
131 | $ret->{default} = join(' ', @words); | 125 | $ret->{default} = join(' ', @words); |
132 | } | 126 | } |
133 | } | 127 | } |
134 | ($name, $ret); | 128 | ($name, $ret); |
135 | } | 129 | } |
136 | })->{section}; | 130 | })->{section}}}); |
137 | } | ||
138 | } | 131 | } |
139 | 132 | ||
140 | 1; | 133 | 1; |