File: parse-bencode.pl

package info (click to toggle)
libparser-mgc-perl 0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 460 kB
  • sloc: perl: 1,881; makefile: 2; sh: 1
file content (89 lines) | stat: -rw-r--r-- 1,306 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl

use v5.14;
use warnings;

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

use Syntax::Keyword::Try;

# See also
#   https://en.wikipedia.org/wiki/Bencode

sub parse
{
   my $self = shift;

   $self->any_of(
      'parse_int',
      'parse_bytestring',
      'parse_list',
      'parse_dict',

      sub { $self->commit; $self->fail( "Expected int, bytestring, list or dict" ) },
   );
}

sub parse_int
{
   my $self = shift;

   $self->expect( 'i' );
   my $value = $self->expect( qr/-?\d+/ );
   $self->expect( 'e' );

   return $value;
}

sub parse_bytestring
{
   my $self = shift;

   my $len = $self->expect( qr/\d+/ );
   $self->expect( ':' );

   return $self->take( $len );
}

sub parse_list
{
   my $self = shift;

   $self->committed_scope_of(
      'l',
      sub { $self->sequence_of( 'parse' ) },
      'e'
   );
}

sub parse_dict
{
   my $self = shift;

   my $kvlist = $self->committed_scope_of(
      'd',
      sub { $self->sequence_of( 'parse' ) },
      'e'
   );

   return { @$kvlist };
}

use Data::Dumper;

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

   while( defined( my $line = <STDIN> ) ) {
      try {
         my $ret = $parser->from_string( $line );
         print Dumper( $ret );
      }
      catch ( $e ) {
         print $e;
      }
   }
}

1;