you'll probably be pleasantly surprised by how easy it is to write a parser using a parser generator; unfortunately i don't know which one to recommend for js (i've used yacc, ocamlyacc, hammer, and a few i've written myself, but i don't recommend using the js one i wrote)
if you aren't pleasantly surprised, maybe try a different parser generator
if you aren't pleasantly surprised, maybe try a different parser generator