Add _selftest() function and test script to verify Grabber functionality
[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 %params = @_;
42     my $self = $class->SUPER::new();
43
44     $self->{'_PARSER'} = videosite::JSONNospace->new();
45     $self->{'_LEXER'} = Parse::Lex->new(@tokens);
46     $self->{'_PARAMS'} = \%params;
47
48     return bless($self, $class);
49 }
50
51 sub parse {
52     my $self = shift;
53     my $s = shift;
54     my $result;
55     my $l = $self->{'_LEXER'};
56
57     $l->from($s);
58     $result = $self->{'_PARSER'}->YYParse(
59         yylex => sub {
60             my $tok = $l->next();
61             return ('', undef) unless $tok;
62             return ('', undef) if $l->eoi();
63             print STDERR $tok->text(), "\n" if (exists($self->{'_PARAMS'}->{'debug'}) and ($self->{'_PARAMS'}->{'debug'} > 0));
64             return ($tok->name(), $tok->text());
65         },
66         yyerror => sub {
67             $_[0]->YYAbort();
68         },
69         yydebug => (exists($self->{'_PARAMS'}->{'debug'})?$self->{'_PARAMS'}->{'debug'}:0x0));
70     return ref($result)?$result->[0]:$result;
71 }
72
73 1;