blib/lib/Config/Neat.pm untested

filter: Statementstmt 0.0% 0 / 199 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 39 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 368 CC 70 SCAR 77.0
CC
70
Cov
0%
parse
3192.0
append_text
56.0
end_of_param
42.0
CRAP 2215.5
1 =head1 NAME
2  
3 Config::Neat - Parse/render human-readable configuration files with inheritance and schema validation
4  
5 =head1 SYNOPSIS
6  
7 use Config::Neat;
8  
9 my $cfg = Config::Neat->new();
10 my $data = $cfg->parse_file('server.nconf');
11  
12 =head1 DESCRIPTION
13  
14 This module provides parsing capabilites for the Config::Neat configuration file
15 format (see the example below). This is a highly readable and clean format inspired
16 by [nginx configuration files](http://wiki.nginx.org/FullExample).
17 See L<https://github.com/iafan/Config-Neat/blob/master/sample/readme.nconf>
18 for the detailed file syntax specification.
19  
20 Sample configuration file (let's call it 'server.nconf'):
21  
22 # Server configuration
23  
24 host localhost
25 port 8080
26 use_ssl YES
27 supported_mime_types text/html text/css text/xml text/plain
28 image/gif image/jpeg image/png image/x-icon
29 application/x-javascript
30  
31 handler test1 {
32 url /test1
33 class MyApp::Test
34 }
35  
36 handler test2 {
37 url /test2
38 class MyApp::AnotherTest
39 }
40  
41 Sample usage:
42  
43 use Config::Neat;
44  
45 my $cfg = Config::Neat->new();
46 my $data = $cfg->parse_file('server.nconf');
47  
48 # now $data contains a parsed hash tree which you can examine
49  
50 # consider the sample configuration file above
51  
52 my $list = $data->{'server'}->{'supported_mime_types'};
53 #
54 # $list now is an array reference:
55 # ['text/html', 'text/css', ..., 'application/x-javascript']
56  
57 my $handlers = $data->{'handler'};
58 map {
59 print $_->{url}->as_string, ' maps to ', $_->{class}->as_string
60 } @$handlers;
61  
62 =head1 COPYRIGHT
63  
64 Copyright (C) 2012-2015 Igor Afanasyev <igor.afanasyev@gmail.com>
65  
66 =head1 SEE ALSO
67  
68 L<https://github.com/iafan/Config-Neat>
69  
70 =cut
71  
72 package Config::Neat;
73  
74 our $VERSION = '1.4';
75  
76 use strict;
77  
78 use Config::Neat::Array;
79 use Config::Neat::Util qw(is_neat_array new_ixhash get_next_auto_key read_file);
80 use Tie::IxHash;
81  
82 my $LINE_START = 0;
83 my $KEY = 1;
84 my $WHITESPACE = 2;
85 my $VALUE = 3;
86 my $LINE_COMMENT = 4;
87 my $BLOCK_COMMENT = 5;
88  
89 #
90 # Initialize object
91 #
92 sub new {
93 my ($class) = @_;
94  
95 my $self = {
96 cfg => {}
97 };
98  
99 bless $self, $class;
100 return $self;
101 }
102  
103 # Given a string representation of the config, returns a parsed tree
104 sub parse {
105 my ($self, $nconf) = @_;
106  
107 my $o = {
108 context => [new_ixhash],
109 context_data => [{}],
110 c => undef,
111  
112 pos => 0,
113  
114 key => '',
115 values => Config::Neat::Array->new(),
116 value => undef,
117 mode => $LINE_START,
118 previous_mode => $LINE_START,
119 was_backslash => undef,
120 was_slash => undef,
121 was_asterisk => undef,
122 first_value_pos => 0,
123 };
124  
125 my $in_raw_mode = undef;
126 my $line = 1;
127  
128 sub end_of_param {
129 my ($o, $no_default_param) = @_;
130  
131 if ($o->{key} ne '') {
132 push @{$o->{values}}, 'YES' if !$no_default_param && scalar(@{$o->{values}}) == 0;
133 my $current_ctx = $o->{context}->[$#{$o->{context}}];
134 my $data = $o->{context_data}->[$#{$o->{context_data}}];
135 if (exists $current_ctx->{$o->{key}}) {
136 $data->{is_array} = {} unless exists $data->{is_array};
137 if (!$data->{is_array}->{$o->{key}}) {
138 $current_ctx->{$o->{key}} = Config::Neat::Array->new([$current_ctx->{$o->{key}}]);
139 $data->{is_array}->{$o->{key}} = 1;
140 }
141 $current_ctx->{$o->{key}}->push($o->{values});
142 } else {
143 $current_ctx->{$o->{key}} = $o->{values};
144 }
145 $o->{values} = Config::Neat::Array->new();
146 $o->{key} = '';
147 }
148 }
149  
150 sub append_text {
151 my ($o, $text) = @_;
152  
153 if ($o->{mode} == $LINE_START) {
154 if (($o->{first_value_pos} > 0) and ($o->{pos} >= $o->{first_value_pos})) {
155 $o->{mode} = $VALUE;
156 } else {
157 end_of_param($o);
158 $o->{mode} = $KEY;
159 $o->{first_value_pos} = 0;
160 }
161 } elsif ($o->{mode} == $WHITESPACE) {
162 $o->{mode} = $VALUE;
163 if ($o->{first_value_pos} == 0) {
164 $o->{first_value_pos} = $o->{pos} - 1; # -1 to allow for non-hanging backtick before the first value
165 }
166 }
167  
168 if ($o->{mode} == $KEY) {
169 $o->{key} .= $text;
170 } elsif ($o->{mode} == $VALUE) {
171 $o->{value} .= $text;
172 } else {
173 die "Unexpected mode $o->{mode}";
174 }
175 }
176  
177 sub process_pending_chars {
178 my $o = shift;
179  
180 if ($o->{was_slash}) {
181 append_text($o, '/');
182 $o->{was_slash} = undef;
183 }
184  
185 if ($o->{was_backslash}) {
186 append_text($o, '\\');
187 $o->{was_backslash} = undef;
188 }
189 }
190  
191 sub process_char {
192 my $o = shift;
193  
194 process_pending_chars($o);
195  
196 append_text($o, $o->{c});
197 $o->{c} = undef;
198 }
199  
200 sub end_of_value {
201 my $o = shift;
202  
203 process_pending_chars($o);
204  
205 if (defined $o->{value}) {
206 push @{$o->{values}}, $o->{value};
207 $o->{value} = undef;
208 }
209 }
210  
211 for (my $i = 0, my $l = length($nconf); $i < $l; $i++) {
212 my $c = $o->{c} = substr($nconf, $i, 1);
213 $o->{pos}++;
214  
215 if ($c ne '/') {
216 $o->{was_asterisk} = undef;
217 }
218  
219 if ($c eq '{') {
220 next if ($o->{mode} == $LINE_COMMENT) or ($o->{mode} == $BLOCK_COMMENT);
221  
222 if ($in_raw_mode) {
223 process_char($o);
224 next;
225 }
226  
227 end_of_value($o);
228  
229 if (!$o->{key}) {
230 $o->{key} = get_next_auto_key($o->{context}->[$#{$o->{context}}]);
231 }
232  
233 my $old_values = $o->{values};
234 my $new_context = $o->{values} = new_ixhash;
235  
236 end_of_param($o, 1); # do not push a default parameter
237  
238 $o->{value} = undef;
239 $o->{mode} = $LINE_START;
240 $o->{first_value_pos} = 0;
241  
242 push @{$o->{context}}, $new_context;
243 push @{$o->{context_data}}, {};
244  
245 # any values preceding the block will be added into it with an empty key value
246 if (scalar(@{$old_values}) > 0) {
247 $new_context->{''} = $old_values;
248 }
249  
250 } elsif ($c eq '}') {
251 next if ($o->{mode} == $LINE_COMMENT) or ($o->{mode} == $BLOCK_COMMENT);
252  
253 if ($in_raw_mode) {
254 process_char($o);
255 next;
256 }
257  
258 end_of_value($o);
259 end_of_param($o);
260  
261 if (scalar(@{$o->{context}}) == 1) {
262 die "Unmatched closing bracket at config line $line position $o->{pos}";
263 }
264 pop @{$o->{context}};
265 pop @{$o->{context_data}};
266 $o->{mode} = $WHITESPACE;
267 $o->{key} = '';
268 $o->{values} = Config::Neat::Array->new();
269  
270 } elsif ($c eq '\\') {
271 next if ($o->{mode} == $LINE_COMMENT) or ($o->{mode} == $BLOCK_COMMENT);
272  
273 process_pending_chars($o);
274  
275 $o->{was_backslash} = 1; # do not print current slash, but wait for the next char
276 next;
277  
278 } elsif ($c eq '/') {
279 next if ($o->{mode} == $LINE_COMMENT);
280 next if (!$o->{was_asterisk} and $o->{mode} == $BLOCK_COMMENT);
281  
282 if ($in_raw_mode) {
283 process_char($o);
284 next;
285 }
286  
287 if ($o->{was_asterisk} and ($o->{mode} == $BLOCK_COMMENT)) {
288 $o->{mode} = $o->{previous_mode};
289 next;
290 }
291  
292 process_pending_chars($o);
293  
294 $o->{was_slash} = 1; # do not print current slash, but wait for the next char
295 next;
296  
297 } elsif ($c eq '*') {
298 next if ($o->{mode} == $LINE_COMMENT);
299  
300 if ($o->{mode} == $BLOCK_COMMENT) {
301 $o->{was_asterisk} = 1;
302 next;
303 } else {
304 if ($o->{was_slash}) {
305 $o->{was_slash} = undef;
306 $o->{previous_mode} = $o->{mode};
307 $o->{mode} = $BLOCK_COMMENT;
308 next;
309 }
310  
311 process_char($o);
312 }
313  
314 } elsif ($c eq '`') {
315 next if ($o->{mode} == $LINE_COMMENT) or ($o->{mode} == $BLOCK_COMMENT);
316  
317 if ($o->{was_backslash}) {
318 $o->{was_backslash} = undef;
319 process_char($o);
320 next;
321 }
322  
323 $o->{c} = '';
324 process_char($o);
325  
326 $in_raw_mode = !$in_raw_mode;
327  
328 } elsif (($c eq ' ') or ($c eq "\t")) {
329 if ($c eq "\t") {
330 warn "Tab symbol at config line $line position $o->{pos} (replace tabs with spaces to ensure proper parsing of multiline parameters)";
331 }
332  
333 next if ($o->{mode} == $LINE_COMMENT) or ($o->{mode} == $BLOCK_COMMENT);
334  
335 if ($in_raw_mode) {
336 process_char($o);
337 next;
338 }
339  
340 if ($o->{mode} == $KEY) {
341 $o->{mode} = $WHITESPACE;
342 } elsif ($o->{mode} == $VALUE) {
343 end_of_value($o);
344 $o->{mode} = $WHITESPACE;
345 }
346  
347 } elsif ($c eq "\r") {
348 next;
349  
350 } elsif ($c eq "\n") {
351 $line++;
352 $o->{pos} = 0;
353  
354 next if ($o->{mode} == $BLOCK_COMMENT);
355  
356 if ($in_raw_mode) {
357 process_char($o);
358 next;
359 }
360  
361 end_of_value($o);
362 $o->{mode} = $LINE_START;
363  
364 } elsif ($c eq "#") {
365 next if ($o->{mode} == $LINE_COMMENT) or ($o->{mode} == $BLOCK_COMMENT);
366  
367 if ($in_raw_mode) {
368 process_char($o);
369 next;
370 }
371  
372 if (($o->{mode} == $LINE_START) or ($o->{mode} == $WHITESPACE)) {
373 $o->{mode} = $LINE_COMMENT;
374 } else {
375 process_char($o);
376 }
377  
378 } else {
379 next if ($o->{mode} == $LINE_COMMENT) or ($o->{mode} == $BLOCK_COMMENT);
380  
381 process_char($o);
382 }
383  
384 $o->{was_asterisk} = undef;
385 }
386  
387 die "Unmatched backtick at config line $line position $o->{pos}" if $in_raw_mode;
388  
389 die "Missing closing bracket at config line $line position $o->{pos}" if @{$o->{context}} > 1;
390  
391 end_of_value($o);
392 end_of_param($o);
393  
394 return $self->{cfg} = $o->{context}->[0];
395 } # end sub
396  
397 # Given file name, will read this file in the specified mode (defaults to UTF-8) and parse it
398 sub parse_file {
399 my ($self, $filename, $binmode) = @_;
400 return $self->parse(read_file($filename, $binmode));
401 } # end sub
402  
403 1; # return true