Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > c2f81a6c54063236c4053665dee6de85 > files > 13

perl-Parser-MGC-0.120.0-2.mga4.noarch.rpm

#!/usr/bin/perl

use strict;
use warnings;

package DictParser;
use base qw( Parser::MGC );

sub parse
{
   my $self = shift;

   $self->any_of(
      sub { $self->token_int },

      sub { $self->token_string },

      sub { $self->scope_of( "{",
               sub { $self->commit; $self->parse_dict },
            "}" );
      },
   );
}

sub parse_dict
{
   my $self = shift;

   my %ret;
   $self->list_of( ",", sub {
      my $key = $self->token_ident;

      $self->expect( ":" );

      $ret{$key} = $self->parse;
   } );

   return \%ret
}

use Data::Dumper;

if( !caller ) {
   my $parser = __PACKAGE__->new;

   while( defined( my $line = <STDIN> ) ) {
      my $ret = eval { $parser->from_string( $line ) };
      print $@ and next if $@;

      print Dumper( $ret );
   }
}

1;