I've never acctually used Parse::RecDescent, but I've written parses like this before. The first thing that jumps out at me is your handler for functions...
function_method: 'function' identifier paren_statement brace { $return = $item[2]; }
Assuming this does what I think it does, you're throwing away the function signature and body by only returning $item[2]. You might try this instead...
function_method: 'function' identifier paren_statement brace { $return = \@item; }
with your existing flatten_recurse method, I think that will do what you want (allthough you'll probably be mising the parens and braces since it looks like you you throw those out when you parse paren_statement and brace_statement)

The power of writting Parsers like this is that you can build your data structures as you parse the file. By flattening out the results from your parser, you're throwing away a lot of the functionality you could have. Considering your goal, you might want to make all of your handlers just return \@item, that should cause your parser to build "a complete parse tree" of your javascript files. Then you can walk it and do whatever you want with it.

Update: Don't try \@item. On a whim I installed Parse::RecDescent to see if I was right, and ran into a deep recursion problem. Evidently @item is a more complicated array then I thought (i figured it was just the individual tokens ... now i'm curios to go read the docs and understand how this module works).
Bottom line: dmmiller2k is probably right, your grammer seems very specific in some ways and very general in others -- you should try to make it more specific about the things you care about, and more general about the things you don't.


In reply to Re: Rec::Descent Woes - Parsing JavaScript Files and Other Issues by hossman
in thread Rec::Descent Woes - Parsing JavaScript Files and Other Issues by Incognito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.