Skip to content Skip to sidebar Skip to footer

Retrieve Javascript Comments In Javascript, Or, How Do I Parse Js In Js?

I am looking for a way to access javascript comments from some (other) javascript code. I plan on using this to display low level help information for elements on the page that cal

Solution 1:

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.

There's a JS parser generator called PEG.js that could do this fairly easy. The grammar could look like this:

{
var functions = {};
var buffer = '';
}

start
  =  unit* {returnfunctions;}

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

When you parse the following source with the generated parser:

/**
 * This function does foo.
 * Call it with bar.  Yadda yadda "groo".
 */functionfoo(x)
{
    ...
}

var s = " /* ... */ function notAFunction() {} ... ";

// function alsoNotAFunction() // { ... }functionwithoutMultiLineComment() {
}

var t = ' /* ... */ function notAFunction() {} ... ';

/**
 * BAR!
 * Call it?
 */functiondoc_way_above(x, y, z) {
    ...
}

// function done(){};

the start() function of the parser returns the following map:

{
   "foo": "/** * This function does foo. * Call it with bar. Yadda yadda \"groo\". */",
   "withoutMultiLineComment": null,
   "doc_way_above": "/** * BAR! * Call it? */"
}

I realize there's some gaps to be filled (like this.id = function() { ... }), but after reading the docs from PEG.js 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.

You can even test the grammar posted above online!

Solution 2:

You could use a unique string identifier at the beginning of every comment, and then using that unique identifier you could easily craft a regex to extract the comment.

Post a Comment for "Retrieve Javascript Comments In Javascript, Or, How Do I Parse Js In Js?"