Youtube: Enhance error check in the login process
[videosite.git] / videosite / JSLexArrayParser.pm
1 #
2 # A helper class for parsing textual JSON structures into perl 
3 # structures
4 #
5 # The parser is in JSONNospace.yp, to regenerate you'll need the Parse::YAPP
6 # package. Use 'yapp -m videosite::JSONNospace -s JSONNospace.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::JSONNospace;
16 use Data::Dumper;
17 use strict;
18
19 my @tokens = (
20     COLON => ':',
21     QUOTE => '\"',
22     SINGLEQUOTE => '\\\'',
23     TRUE => 'true',
24     FALSE => 'false',
25     NULL => 'null',
26     QUADHEX => 'u[0-9a-fA-F]{4}',
27     INTEGER => '[0-9]+',
28     QUOTEDNORMAL => '[nr]',
29     SIMPLECHAR => '[-\w\._\?\+=\&\!%<>;]+',
30     BACKSLASH => '\\\\',
31     SLASH => '/',
32     COMMA => ',',
33     CURLYOPEN => '{',
34     CURLYCLOSE => '}',
35     SQUAREOPEN => '\[',
36     SQUARECLOSE => '\]',
37 );
38
39 sub new {
40     my $class = shift;
41     my $self = $class->SUPER::new();
42
43     $self->{'_PARSER'} = videosite::JSONNospace->new();
44     $self->{'_LEXER'} = Parse::Lex->new(@tokens);
45
46     return bless($self, $class);
47 }
48
49 sub parse {
50     my $self = shift;
51     my $s = shift;
52     my $result;
53     my $l = $self->{'_LEXER'};
54
55     $l->from($s);
56     $result = $self->{'_PARSER'}->YYParse(
57         yylex => sub {
58             my $tok = $l->next();
59             return ('', undef) unless $tok;
60             return ('', undef) if $l->eoi();
61             return ($tok->name(), $tok->text());
62         },
63         yyerror => sub {
64             $_[0]->YYAbort();
65         },
66         yydebug => 0x0);
67     return ref($result)?$result->[0]:$result;
68 }
69
70 1;