Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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.


Comment on Re: Parse::JavaScript
Download Code
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.

    Regexes

    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.)

    -- 
    briac

      Very interesting code and discussion - but my internal parser parses

      bar++ foo;
      as
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://193095]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-08-31 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls