blib/lib/BIND/Config/Parser.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 95 CC 2 SCAR 9.0
CC
2
Cov
0%
_recurse
6.0
new
2.0
parse_file
2.0
CRAP 2.5
1 package BIND::Config::Parser;
2  
3 # $Id: Parser.pm 35 2005-06-26 18:58:24Z $
4  
5 use warnings;
6 use strict;
7  
8 use Parse::RecDescent;
9  
10 use vars qw( $VERSION );
11  
12 $VERSION = '0.01';
13  
14 $::RD_AUTOACTION = q{ $item[1] };
15  
16 my $grammar = q{
17  
18 program:
19 <skip: qr{\s*
20 (?:(?://|\#)[^\n]*\n\s*|/\*(?:[^*]+|\*(?!/))*\*/\s*)*
21 }x> statement(s) eofile { $item[2] }
22  
23 statement:
24 simple | nested
25  
26 simple:
27 value(s) ';'
28  
29 nested:
30 value value(s?) '{' statement(s?) '}' ';'
31 { [ $item[1], $item[2], $item[4] ] }
32  
33 value:
34 /[\w.\/=-]+/ | /"[\w.\/ =-]+"/
35  
36 eofile:
37 /^\Z/
38 };
39  
40 sub new {
41 my $class = shift;
42  
43 my $self = {
44 '_open_block' => \&_handle_open_block,
45 '_close_block' => \&_handle_close_block,
46 '_statement' => \&_handle_statement,
47 };
48  
49 $self->{ '_parser' } = new Parse::RecDescent( $grammar )
50 || die "Bad grammar\n";
51  
52 bless $self, $class;
53  
54 return $self;
55 }
56  
57 sub parse_file
58 {
59 my $self = shift;
60  
61 my $namedconf = shift
62 || die "Missing named.conf argument\n";
63  
64 open NAMEDCONF, $namedconf
65 || die "Can't open '$namedconf': $!\n";
66 my $text = join( "", <NAMEDCONF> );
67 close NAMEDCONF;
68  
69 defined( my $tree = $self->{ '_parser' }->program( $text ) )
70 || die "Bad text\n";
71  
72 $self->_recurse( $tree );
73 }
74  
75 sub open_block_handler
76 {
77 my $self = shift;
78  
79 return $self->{ '_open_block' };
80 }
81  
82 sub set_open_block_handler
83 {
84 my $self = shift;
85  
86 $self->{ '_open_block' } = shift;
87 }
88  
89 sub close_block_handler
90 {
91 my $self = shift;
92  
93 return $self->{ '_close_block' };
94 }
95  
96 sub set_close_block_handler
97 {
98 my $self = shift;
99  
100 $self->{ '_close_block' } = shift;
101 }
102  
103 sub statement_handler
104 {
105 my $self = shift;
106  
107 return $self->{ '_statement' };
108 }
109  
110 sub set_statement_handler
111 {
112 my $self = shift;
113  
114 $self->{ '_statement' } = shift;
115 }
116  
117 sub _recurse
118 {
119 my $self = shift;
120 my $tree = shift;
121  
122 foreach my $node ( @{ $tree } ) {
123 if ( ref( $node->[-1] ) eq 'ARRAY' ) {
124  
125 # If the last child of the node is an array then the
126 # node must be a nested statement, so handle the
127 # opening line, recurse through the contents and
128 # close with the curly brace
129  
130 $self->open_block_handler->( $node->[0],
131 @{ $node->[1] } );
132 $self->_recurse( $node->[-1] );
133 $self->close_block_handler->();
134 } else {
135  
136 # Normal single-line statement
137  
138 $self->statement_handler->( @{ $node } );
139 }
140 }
141 }
142  
143 sub _handle_open_block {}
144 sub _handle_close_block {}
145 sub _handle_statement {}
146  
147 1;
148  
149 __END__