Incognito has asked for the wisdom of the Perl Monks concerning the following question:
So far, I have learned a lot from this site and playing around with grammars using Parse::RecDescent, but I haven't really moved forward with the problem at hand.
I have a JavaScript file, which may or may not have global vars at the top, middle and bottom of the page. This file also has function declarations, which may have local vars defined in them as well.
When I break this problem down, it's basically, (a) skip any code until you reach a function (b) return everything in the function (including the function name and braces and stuff) and (c) once out of the function, ignore everything until we get another function.
For me to return the entire function string contents, I'll have to make modifications to the productions like stuff_we_ignore, paren_statement and bracket_statement, so that they return stuff... but that means they'll return stuff when not found within a function. I can't seem to figure out how to write a grammar that returns stuff when in a function, and nothing otherwise...
I'm including the code I've written that returns the array of function names. There's got to be something simple that I'm missing that will let me do what I want...
#!/usr/bin/perl use strict; # Enforces safer, clearer code. use warnings; # Detects common programming errors use Time::HiRes qw(gettimeofday); use Parse::RecDescent; #use Data::Dumper; #--------------------------------------------------------------------- +- # Build the grammar. #--------------------------------------------------------------------- +- my ($grammar); my ($startCompile,$startCompile2) = gettimeofday; $startCompile += ($startCompile2/1000000); $grammar = q { statement: ( function_method (';')(?) { $return = $item +[1]; } | brace_statement (';')(?) { $return = $it +em[1]; } | stuff_we_ignore (';')(?) { $return = $it +em[1]; } )(s) function_method: 'function' identifier paren_statement brace_statement { $return = $item[2]; } brace_statement: '\{' statement '\}' { $return = $item[2]; } paren_statement: '(' statement ')' bracket_statement: '[' statement ']' stuff_we_ignore: ( paren_statement | bracket_statement | identifier | punctuators )(s?) { $return = ""; } identifier: /\w+/ punctuators: /[><=!~:&^%,\?\|\+\-\*\/\.]+/ }; #--------------------------------------------------------------------- +- # Grab the data and parse. #--------------------------------------------------------------------- +- my @localDeclaredVars = <DATA>; my $localDeclaredVar = join ' ', @localDeclaredVars; my $parser = new Parse::RecDescent ($grammar) or die "*** Bad grammar! +\n"; my $i = 1; my ($endCompile,$endCompile2) = gettimeofday; $endCompile += ($endCompile2/1000000); my $refParsedValues = $parser->statement($localDeclaredVar) || print " +*** $localDeclaredVar\n"; my ($parseEnd,$parseEnd2) = gettimeofday; $parseEnd += ($parseEnd2/1000000); #--------------------------------------------------------------------- +- # Flatten the array and print contents. #--------------------------------------------------------------------- +- #print Dumper(\@$refParsedValues); my (@flatarray); sub flatten_recurse { # Thanks Anomo map ref eq 'ARRAY' ? flatten_recurse(@$_) : $_, grep defined && le +ngth, @_; } @flatarray = flatten_recurse ($refParsedValues); print join "\n", @flatarray, "\n"; my ($appEnd,$appEnd2) = gettimeofday; $appEnd += ($appEnd2/1000000); print '-'x72 . "\n"; print "Compile Time: " . (sprintf "%2.3f", ($endCompile - $startCompil +e)) . " seconds\n"; print "Parse Time: " . (sprintf "%2.3f", ($parseEnd - $endCompile)) +. " seconds\n"; print "Flatten Time: " . (sprintf "%2.3f", ($appEnd - $parseEnd)) . " +seconds\n"; print "__________________________\n"; print "Total Time: " . (sprintf "%2.3f", ($appEnd - $startCompile)) +. " seconds\n"; #--------------------------------------------------------------------- +- # End of program. #--------------------------------------------------------------------- +- __END__ function functStart () {}; var g1, g2 = __QUOTE__; var g3 = 10000000; if (g1) { var XXXXXXX = __QUOTE__; } if ( ! defaultCookieCrumbNav ) { cookieCrumbNavBarHTML = __QUOTE__ ; } + else { function funct1 () { }; var xxx = __QUOTE__ ; } if (true == false) { alert(var1); } if (1) { if (1) { function funct2 (X) { x = funct3 (1,2); }; function +funct3 () { alert(1); } } } function funct4 (a,b) { alert (1,2,3,4); return (a + b); } function funct5 () { var aaa = 1; } var g4; var g7 = __QUOTE__; function funct6 () { var b = __REGEX__; c = __REGEX__; if (test333()) +{ return true; } } function funct7 () { var a = 111; } alert ( 3 ); funct5 ( funct6 ( funct2 () ) ); function functEnd () {}
functStart funct1 funct2 funct3 funct4 funct5 funct6 funct7 functEnd ---------------------------------------------------------------------- +-- Compile Time: 0.270 seconds Parse Time: 0.691 seconds Flatten Time: 0.000 seconds __________________________ Total Time: 0.961 seconds
function functStart () {}; function funct2 (X) { x = funct3 (1,2); }; function funct3 () { alert(1); } function funct4 (a,b) { alert (1,2,3,4); return (a + b); } function funct5 () { var aaa = 1; } function funct6 () { var b = __REGEX__; c = __REGEX__; if (test333()) +{ return true; } } function funct7 () { var a = 111; } function functEnd () {} ---------------------------------------------------------------------- +-- Compile Time: 0.000 seconds Parse Time: 0.000 seconds Flatten Time: 0.000 seconds __________________________ Total Time: 0.000 seconds
2002-03-13 Edit by Corion : Added READMORE tag
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Rec::Descent Woes - Parsing JavaScript Files and Other Issues
by hossman (Prior) on Mar 13, 2002 at 05:21 UTC | |
|
Re: Rec::Descent Woes - Parsing JavaScript Files and Other Issues
by jryan (Vicar) on Mar 13, 2002 at 03:46 UTC | |
|
Re: Rec::Descent Woes - Parsing JavaScript Files and Other Issues
by Anonymous Monk on Mar 13, 2002 at 03:07 UTC | |
|
Re: Rec::Descent Woes - Parsing JavaScript Files and Other Issues
by dmmiller2k (Chaplain) on Mar 13, 2002 at 17:03 UTC | |
|
Re: Rec::Descent Woes - Parsing JavaScript Files and Other Issues
by I0 (Priest) on Jun 26, 2002 at 23:59 UTC |