add youtu.be short links
[videosite.git] / videosite / JSLexArrayParser.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::JSLexArrayParser;
10
11 use videosite::JSArrayParser;
12 @ISA = qw(videosite::JSArrayParser);
13
14 use Parse::Lex;
15 use videosite::jsarray;
16 use strict;
17
18 my @tokens = (
19     COLON  => '[:]',
20     RIGHTC => '[\}]',
21     LEFTC => '[\{]',
22     QUOTE => '[\"]',
23     COMMA => '[,]',
24     ID =>    '[\w_%\.\+-]+'
25 );
26
27 sub new {
28     my $class = shift;
29     my $self = $class->SUPER::new();
30
31     $self->{'_PARSER'} = videosite::jsarray->new();
32     $self->{'_LEXER'} = Parse::Lex->new(@tokens);
33
34     return bless($self, $class);
35 }
36
37 sub parse {
38     my $self = shift;
39     my $s = shift;
40     my @result;
41     my $l = $self->{'_LEXER'};
42
43     $l->from($s);
44     @result = $self->{'_PARSER'}->YYParse(
45         yylex => sub {
46             my $tok = $l->next();
47             return ('', undef) unless $tok;
48             return ('', undef) if $l->eoi();
49             return ($tok->name(), $tok->text());
50         },
51         yyerror => sub {
52             $_[0]->YYAbort();
53         },
54         yydebug => 0x0);
55     return $result[0]?{@{$result[0]}}:undef;
56 }
57
58 1;