Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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]

In reply to Re: Parsing bracket formatted file (updated) by choroba
in thread Parsing bracket formatted file by Stilgar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found