[NEWBIE] Creating a module for general configuration parsing.
[email protected] (Cameron Kerr) Tue, 9 Dec 2003 01:49:10 +1300
| Newsgroups | perl.recdescent |
|---|---|
| Message-ID | <[email protected]> |
Hello, I'm new to P::RD, and I'm writing my first Perl module, which
I'm calling Parse::CurlyBlock, and will release to CPAN when its mature.
Its for a genaral configuration parsing task, in the style of BIND
named.conf syntax, only a bit more genaral. Here are some examples.
#my $data = 'foo;';
#my $data = 'foo bar;';
#my $data = 'foo { baz; };';
#my $data = 'foo bar { baz; };';
#my $data = 'foo bar { baz "bux"; one two; };';
my $data = 'foo bar far { baz "bux"; one two; } { three four; };
alpha beta;';
#my $data = 'foo; bar;';
Statements are seperated by a ";". The genaral form of each statement is
word args(s?) block(s?). So all af the above are valid. The last one
consists of two statements, obviously. Blocks are surrounded by "{" and
"}". words can be quoted or bare.
Here is the grammar as it stands
my $grammar = q{
{ my @statements }
startrule : statement(s) eof
{
push @statements, \$item[1];
}
statement : word arg(s?) ";"
| word arg(s?) block(s?) ";"
| <error?>
{
my %result;
$result{'args'} = $item{'arg(s?)'};
$result{'blocks'} = $item{'block(s?)'};
$return = { $item{'word'} => \%result };
$return;
}
word : bareword
| quotedword
{ $return = $item[1] }
arg : word
{ $return = $item[1] }
block : "{" statement(s) "}"
{ $return = $item[2] }
eof : /^\Z/
{ $return = $item[1] }
bareword : /\w+/
{ $return = $item[1] }
quotedword : / $Regexp::Common::RE{quoted} /x
{ $return = $item[1] =~ m/^ ["'`] (.*) ["'`] $/x; }
};
I'm trying to get out a data structure such as the following.
my $data = 'foo bar far { baz "bux"; one two; } { three four; };
alpha beta;';
[ # List of statements
'foo' => { 'args' => [ 'bar', 'far' ]
{ 'blocks' => [ [ 'baz' => 'bux',
'one' => 'two' ],
[ 'three' => 'four' ] ] },
'alpha' => { 'args' => [ 'beta' ] }
]
I think it reaches a good balance of flexibility, and managability.
Anyway, I can't get the thing to work (obvious, since I'm writing to the
list). I'm having problems with the <error?> in the statement rule (or
rather, with the rule itself).
ERROR (line 1): Invalid statement: Was expecting ';' but found "{ baz
"bux"; one two; } { three four; };" instead
When I use Data::Dumper to dump the @statements in startrule, it also
shows a ';' as being entered into the structure.
$VAR1 = \[
{
'foo' => {
'blocks' => [
[
';',
';'
],
[
';'
]
],
'args' => [
'bar',
'far'
]
}
},
';'
];
I've read a couple of tutorials, and the perldoc for P::RD, but there
isn't much discussion that I've found on dealing with this sort of
parsing problem (largely bracketed expressions).
Also, startrule is always returning '1', according to Data::Dumper(
$parser->startrule( $content ) ); What should I be doing to remedy
that?
Many thanks for your time.
PS. Please be gentle. I flunked Theory of Computing ;^)
--
Cameron Kerr
[email protected] : http://nzgeeks.org/cameron/
Empowered by Perl!