If I may make a suggestion, as well as solve your problem as well, try to stick with using the @item array rather than the %item hash as the docs suggest, for speed reasons. I think it is easier to work with anyways. With that in mind, you can get rid of your trailing actions and do something like this:
BEGIN { $::RD_AUTOACTION = q { bless [@item[1..$#item]], 'Config::Yacp::Deparse::'.$item[0] }; }
And later:
my $tree = Config::Yapc->new('test.ini', $parser->file($text)); my $deparsed = $tree->deparse;
$tree will now look something like:
$VAR1 = bless({ text => bless ([ bless ([ bless ([ '[', 'value1', ']' ], 'Config::Yapc::Deparse::header'), [ bless ([ bless ([ 'key1', '=', 'value1' ], 'Config::Yapc::Deparse::pair') ], 'Config::Yapc::Deparse::assign'), bless ([ bless ([ 'key2', '=', 'value2' ], 'Config::Yapc::Deparse::pair'), bless ([ ';', 'comment2' ], 'Config::Yapc::Deparse::comment'), ], 'Config::Yapc::Deparse::assign'), bless ([ bless ([ 'key3', '=', 'value3' ], 'Config::Yapc::Deparse::pair') ], 'Config::Yapc::Deparse::assign') ] ], 'Config::Yapc::Deparse::section'), bless ([ bless ([ '[', 'value1', ']' ], 'Config::Yapc::Deparse::header'), [ bless ([ bless ([ 'key4', '=', 'value4' ], 'Config::Yapc::Deparse::pair') ], 'Config::Yapc::Deparse::assign'), bless ([ bless ([ 'key5', '=', 'value5' ], 'Config::Yapc::Deparse::pair') ], 'Config::Yapc::Deparse::assign'), bless ([ bless ([ 'key6', '=', 'value6' ], 'Config::Yapc::Deparse::pair'), bless ([ ';', 'comment6' ], 'Config::Yapc::Deparse::comment'), ], 'Config::Yapc::Deparse::assign') ] ], 'Config::Yapc::Deparse::section') ], 'Config::Yapc::Deparse::file'), name => 'text.ini' }, 'Config::Yacp' );
Or something similar, I just generated it by hand.
Then, you can define a bunch of similarly-named methods that correspond to your named rules, such as:
sub Config::Yacp::Deparse::file::deparse { ... } sub Config::Yacp::Deparse::section::deparse { ... } etc...
These methods would know how to deparse an object of its own type, calling the deparse methods of inner objects if needed. With this technique, it should be very simple to generate the type of structure that you want.
Parsing just doesn't get any easier than that. (-:
In reply to Re: P::RD action sequence
by jryan
in thread P::RD action sequence
by TStanley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |