Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Parse::JavaScript

by PodMaster (Abbot)
on Aug 27, 2002 at 07:36 UTC ( #193095=note: print w/replies, xml ) Need Help??

in reply to JavaScript::Parse::PurePerl (was Parse::JavaScript)

I would like it if you would add something like (i really like it when modules are examples of how to use themselves):
package main; unless(caller) { $\ = "\n\n#######\n\n"; my $js_parser = Parse::JavaScript->new(); print $js_parser->parse(<<'_EOJS_'); function foo(bar){ // JS Comment window.location = "http://www." + bar + '.com' } var quux = 68; var baz = quux++; _EOJS_ print $js_parser->parse(q[ while(1) { document.write(" die "); } ]); print $js_parser->parse(q[ do { document.write(" die "); } while (1); ]); print $js_parser->parse(q[ for(;;) { document.write(" die "); } ]); print $js_parser->parse(q[ document.write( /(\w+)/g.exec("foo bar") ); ]); print $js_parser->parse(' document.write( /[^\]\[](\w+)/g.exec("foo bar") ); '); print $js_parser->parse(q{ /* HERE GOES AN ERROR (good job) }); }
Good job (++).

Do you know of any valid javascript which might cause an error ? ( the examples above are all valid, and all validate until /* HERE GOES AN ERROR (good job))

** The Third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: Parse::JavaScript
by OeufMayo (Curate) on Aug 27, 2002 at 08:45 UTC

    Thanks for your input podmaster. Here are a few valid JavaScript samples that don't parse too well.


    var foo = 3 / 4 + 1 / 2;

    is parsed as :

    var foo = 3; / 4 + 1 /; 2;

    var foo = (3/4) + 1 / 2; is not parsed at all (though I suspect it could be fixed by adjusting the precedence)

    Elisions (though I have yet to see this construct in a real script)

    var foo = foo[ , , bar];

    Wrong parsing

    bar ++foo;

    Is parsed as:

    bar++; foo;

    Which is wrong. the parser should give an error (no line terminator allowed between 'bar' and '++') or be nice and get parse it as bar++;foo;

    The following code is found in one of the the mozilla/js test script, but it doesn't seem to be valid, according to the specs (and obviously, doesn't parse).

    try { //some stuff }catch (e if e == 3){ /* do stuff */ }

    There are probably some more bugs, but the regex/division confusion is by far the most annoying

    Update here is another major bugs due to the Optional Semicolon:

    return a + b

    Should be:

    return; a + b;

    But it is parsed as

    return a + b;

    This bug is mainly due to the fact that line terminators are silently skipped by the lexer

    Update 2002-09-01: I'm glad to say that all these bugs are now fixed in the current version of this node (except for the elision.)


      Very interesting code and discussion - but my internal parser parses

      bar++ foo;
      bar+ +foo;

      which would be instantly valid. But on the other side, I don't know much about JavaScript (and C-style) operator syntax, and maybe two pluses automatically mean postincrement / preincrement operator ...

      perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

      I apologise for ressurecting a zombie thread, but just wanted to provide this info just in case anyone else was looking at the parser.

      I had to make the following corrections to get the module to work
      line 1495 comment out the entire line
      # /* vi:set syn=yacc: */
      to elminate error "Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE  vi:set syn=yacc: */ at PurePerl.yp line 1495."
      And also line 1307 changed from
      my $temp_pos = pos($$input); $$input =~ s/\G\s*(\n)\s*([+-]{2})/$1;$2/sg; pos($$input) = $temp_pos;
      my $temp_pos = pos($$input); $temp_pos =~ s/\G\s*(\n)\s*([+-]{2})/$1;$2/sg; pos($$input) = $temp_pos;
      to eliminate error "Modification of a read-only value attempted at PurePerl.yp line 1307."
      Hope this is of assistance to someone.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://193095]
and a moth chases the moon...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2018-06-19 13:18 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.