X-Git-Url: https://git.camperquake.de/gitweb.cgi?a=blobdiff_plain;f=videosite%2FJSArrayParser.pm;h=cadf8e8a681efc0c307793f74b16bcb96c87a5fe;hb=12db405291947c6b02c2555051c3dc52f3995cbc;hp=20e6e0ef7813cfb9cf679893b6e94cb065ba6bfb;hpb=7b6cd96a0ad9a3bed4f771df3cc3d541470427f8;p=videosite.git diff --git a/videosite/JSArrayParser.pm b/videosite/JSArrayParser.pm index 20e6e0e..cadf8e8 100644 --- a/videosite/JSArrayParser.pm +++ b/videosite/JSArrayParser.pm @@ -1,55 +1,56 @@ # -# A helper class for parsing textual JS hashes into perl -# hashes -# -# The parser is in jsarray.yp, to regenerate you'll need the Parse::YAPP -# package. Use 'yapp -m videosite::jsarray -s jsarray.yp' to regenerate +# This is a stub class for more complex JS*ArrayParser objects. +# It's new() method usually does _not_ return an object of +# type videosite::JSArrayParser, but rather a child object of itself +# which is able to do some actual work. The only time new() retuns +# an videosite::JSArrayParser onject is when no child objects are +# available. # package videosite::JSArrayParser; -use Parse::Lex; -use videosite::jsarray; use strict; -my @tokens = ( - COLON => '[:]', - RIGHTC => '[\}]', - LEFTC => '[\{]', - QUOTE => '[\"]', - COMMA => '[,]', - ID => '[\w_%\.\+-]+' -); - sub new { my $class = shift; - my $self = { - '_PARSER' => videosite::jsarray->new(), - '_LEXER' => Parse::Lex->new(@tokens), + my $self = {}; + + if ($class ne __PACKAGE__) { + # We were called from a child object. Return ourselves. + return bless($self, $class); + } + + # Try to find a child object which is available and return that. + # + # See if videosite::JSLexArrayParser is available + eval { + require videosite::JSLexArrayParser; }; + unless($@) { + # Available. Return a JSLexArrayParser object + return videosite::JSLexArrayParser->new(); + } + + # See if JSSimleArrayParser is available + eval { + require videosite::JSSimpleArrayParser; + }; + + unless ($@) { + # Available. Return a JSSimpleArrayParser object + return videosite::JSSimpleArrayParser->new(); + } + + # Nothing available. Return ourselves. return bless($self, $class); } sub parse { my $self = shift; - my $s = shift; - my @result; - my $l = $self->{'_LEXER'}; - - $l->from($s); - @result = $self->{'_PARSER'}->YYParse( - yylex => sub { - my $tok = $l->next(); - return ('', undef) unless $tok; - return ('', undef) if $l->eoi(); - return ($tok->name(), $tok->text()); - }, - yyerror => sub { - $_[0]->YYAbort(); - }, - yydebug => 0x0); - return $result[0]?{@{$result[0]}}:undef; + + # No functionality here + return undef; } 1;