Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Parsing bracket formatted file (updated)

by choroba (Cardinal)
on Sep 24, 2022 at 11:20 UTC ( [id://11147105]=note: print w/replies, xml ) Need Help??


in reply to Parsing bracket formatted file

Use Marpa::R2 (or a similar module) to write a parser for the format.
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Marpa::R2; use Data::Dumper; my $input = q(sys ecm cloud-provider /Common/aws-ec2 { description "The aws-ec2 parameters" property-template { account { } availability-zone { valid-values { a b c d } } instance-type { valid-values { t2.micro t2.small t2.medium } } region { valid-values { us-east-1 us-west-1 } } } }); my $dsl = << '__DSL__'; lexeme default = latm => 1 :default ::= action => first Top ::= atom Attrs Struct action => top Attrs ::= atom Attrs action => merge | atom action => newlist Struct ::= ('{') Elements ('}') Elements ::= Element Elements action => merges | Element Element ::= atom Value action => struct | atom Struct action => struct Value ::= Struct | ('"') string ('"') | ('{ }') action => empty || ('{') List ('}') List ::= atom List action => merge | atom action => newlist :discard ~ [\s] string ~ [^"]+ atom ~ [^\s{}]+ __DSL__ sub top { +{ $_[1] => { attrs => $_[2], contents => $_[3] } } } sub first { $_[1] } sub empty { [] } sub newlist { [ $_[1] ] } sub merge { [ $_[1], @{ $_[2] } ] } sub struct { +{ $_[1] => $_[2] } } sub merges { +{ %{ $_[1] }, %{ $_[2] } } } my $grammar = 'Marpa::R2::Scanless::G'->new({ source => \$dsl }); my $recce = 'Marpa::R2::Scanless::R'->new({ grammar => $gram +mar, semantics_package => 'main +', }); $recce->read(\$input); use Data::Dumper; print Dumper($recce->value) =~ s/ / /gr;
Output:
$VAR1 = \{ 'sys' => { 'contents' => { 'property-template' => { 'account' => [], 'region' => { 'valid-values' => [ 'us-east-1', 'us-west-1' ] }, 'availability-zone' => { 'valid-values' => [ 'a', 'b', 'c', 'd' ] }, 'instance-type' => { 'valid-values' => [ 't2.micro', 't2.small', 't2.medium' ] } }, 'description' => 'The aws-ec2 parameters' }, 'attrs' => [ 'ecm', 'cloud-provider', '/Common/aws-ec2' ] } };

Update: Fixed the missing + in the top rule, compacted the output, reverted the order of the merge rule.

Update2: Added the default action.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2025-07-08 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.