Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could create a little parser that does not parse the complete JS language, but only matches string literals, single- and multi-line comments and functions of course.</p> <p>There's a JS parser generator called <a href="http://pegjs.majda.cz/" rel="noreferrer">PEG.js</a> that could do this fairly easy. The grammar could look like this:</p> <pre><code>{ var functions = {}; var buffer = ''; } start = unit* {return functions;} unit = func / string / multi_line_comment / single_line_comment / any_char func = m:multi_line_comment spaces? "function" spaces id:identifier {functions[id] = m;} / "function" spaces id:identifier {functions[id] = null;} multi_line_comment = "/*" ( !{return buffer.match(/\*\//)} c:. {buffer += c;} )* { var temp = buffer; buffer = ''; return "/*" + temp.replace(/\s+/g, ' '); } single_line_comment = "//" [^\r\n]* identifier = a:([a-z] / [A-Z] / "_") b:([a-z] / [A-Z] / [0-9] /"_")* {return a + b.join("");} spaces = [ \t\r\n]+ {return "";} string = "\"" ("\\" . / [^"])* "\"" / "'" ("\\" . / [^'])* "'" any_char = . </code></pre> <p>When you parse the following source with the generated parser:</p> <pre><code>/** * This function does foo. * Call it with bar. Yadda yadda "groo". */ function foo(x) { ... } var s = " /* ... */ function notAFunction() {} ... "; // function alsoNotAFunction() // { ... } function withoutMultiLineComment() { } var t = ' /* ... */ function notAFunction() {} ... '; /** * BAR! * Call it? */ function doc_way_above(x, y, z) { ... } // function done(){}; </code></pre> <p>the <code>start()</code> function of the parser returns the following map:</p> <pre><code>{ "foo": "/** * This function does foo. * Call it with bar. Yadda yadda \"groo\". */", "withoutMultiLineComment": null, "doc_way_above": "/** * BAR! * Call it? */" } </code></pre> <p>I realize there's some gaps to be filled (like <code>this.id = function() { ... }</code>), but after reading <a href="http://pegjs.majda.cz/documentation" rel="noreferrer">the docs from PEG.js</a> a bit, that shouldn't be a big problem (assuming you know a little of parser generators). If it is a problem, post back and I'll add it to the grammar and explain a bit about what's happening in the grammar.</p> <p>You can even <a href="http://pegjs.majda.cz/online" rel="noreferrer">test the grammar</a> posted above online!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload