- New YouYube scheme, as some videos did not conform to
[videosite.git] / videosite / JSArrayParser.pm
1 #
2 # A helper class for parsing textual JS hashes into perl 
3 # hashes
4 #
5 # The parser is in jsarray.yp, to regenerate you'll need the Parse::YAPP
6 # package. Use 'yapp -m videosite::jsarray -s jsarray.yp' to regenerate
7 #
8
9 package videosite::JSArrayParser;
10
11 use Parse::Lex;
12 use videosite::jsarray;
13 use strict;
14
15 my @tokens = (
16     COLON  => '[:]',
17     RIGHTC => '[\}]',
18     LEFTC => '[\{]',
19     QUOTE => '[\"]',
20     COMMA => '[,]',
21     ID =>    '[\w_%\.\+-]+'
22 );
23
24 sub new {
25     my $class = shift;
26     my $self = {
27         '_PARSER' => videosite::jsarray->new(),
28         '_LEXER' => Parse::Lex->new(@tokens),
29     };
30
31     return bless($self, $class);
32 }
33
34 sub parse {
35     my $self = shift;
36     my $s = shift;
37     my @result;
38     my $l = $self->{'_LEXER'};
39
40     $l->from($s);
41     @result = $self->{'_PARSER'}->YYParse(
42         yylex => sub {
43             my $tok = $l->next();
44             return ('', undef) unless $tok;
45             return ('', undef) if $l->eoi();
46             return ($tok->name(), $tok->text());
47         },
48         yyerror => sub {
49             $_[0]->YYAbort();
50         },
51         yydebug => 0x0);
52     return $result[0]?{@{$result[0]}}:undef;
53 }
54
55 1;