Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Parsing a config file with braces and nested braces

by choroba (Cardinal)
on Jan 08, 2015 at 15:34 UTC ( [id://1112655]=note: print w/replies, xml ) Need Help??


in reply to Parsing a config file with braces and nested braces

I tried to implement a Marpa::R2 solution:
#!/usr/bin/perl use warnings; use strict; use Marpa::R2; use Data::Dumper; my $input = do { local $/; <DATA> }; my $grammar = << '__GRAMMAR__'; lexeme default = latm => 1 :start ::= List :default ::= action => ::first List ::= Hash+ action => list Hash ::= String '{' Pairs '}' action => hash Pairs ::= Pair+ action => list Pair ::= String Value ';' action => pair | Hash Value ::= Simple | Bracketed Bracketed ::= '[' String ']' action => second Simple ::= String String ~ [-a-zA-Z_0-9]+ whitespace ~ [\s] + :discard ~ whitespace __GRAMMAR__ sub hash { +{ $_[1] => $_[3] } } sub pair { +{ $_[1] => $_[2] } } sub second { [ @_[ 2 .. $#_-1 ] ] } sub list { shift; \@_ } my $parser = 'Marpa::R2::Scanless::G'->new({ source => \$grammar }); print Dumper $parser->parse(\$input, 'main', { trace_terminals => 1 }) +; __DATA__ bob { ed { larry { ...

Does the output satisfy you?

[ { 'bob' => [ { 'ed' => [ { 'larry' => [ { 'rule5' => [ { ' +option' => [ + { + 'disable-server-response-inspection' => 'no' + } + ] }, { ' +tag' => [ + 'some_tag' + ] }, { ' +from' => [ + 'prod-L3' + ] }, { ' +to' => [ + 'corp-L3' + ] }, { ' +source' => [ + 'any' + ] }, { ' +destination' => [ + 'any' + ] }, { ' +source-user' => [ + 'any' + ] }, { ' +category' => [ + 'any' + ] }, { ' +application' => [ + 'any' + ] }, { ' +service' => [ + 'any' + ] }, { ' +hip-profiles' => [ + 'any' + ] }, { ' +log-start' => 'no' }, { ' +log-end' => 'yes' }, { ' +negate-source' => 'no' }, { ' +negate-destination' => 'no' }, { ' +action' => 'allow' }, { ' +log-setting' => 'orion_log' } ] }, { 'rule6' => [ { ' +option' => [ + { + 'disable-server-response-inspection' => 'no' + } + ] }, { ' +tag' => [ + 'some_tag' + ] }, { ' +from' => [ + 'prod-L3' + ] }, { ' +to' => [ + 'corp-L3' + ] }, { ' +source' => [ + 'any' + ] }, { ' +destination' => [ + 'any' + ] }, { ' +source-user' => [ + 'any' + ] }, { ' +category' => [ + 'any' + ] }, { ' +application' => [ + 'any' + ] }, { ' +service' => [ + 'any' + ] }, { ' +hip-profiles' => [ + 'any' + ] }, { ' +log-start' => 'no' }, { ' +log-end' => 'yes' }, { ' +negate-source' => 'no' }, { ' +negate-destination' => 'no' }, { ' +action' => 'allow' }, { ' +log-setting' => 'orion_log' } ] } ] } ] } ] } ]
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1112655]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-10-09 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (45 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.