What tools exist for parsing Javascript and reading the results in Javascript or Ruby?
-
10-10-2019 - |
Pergunta
I'd like to do some code-analysis of Javascript. I'd prefer to operate on ASTs or S-Expressions. It's certainly possible that the V8 engine builds this, though I can't seem to find any documentation on how to access this information from a Javascript context.
I'd be perfectly happy to do my analysis in Ruby, though the only way I can think of doing that would be to write an entire grammar in Treetop or a similar parser-generator.
Solução
Check out UglifyJS: https://github.com/mishoo/UglifyJS
Outras dicas
If you want to use Ruby you don't have to implement it yourself because Aaron Patterson of nokogiri fame already did in rkelly.
Our DMS Software Reengineering Toolkit is capable of parsing ECMAScript (even embedded in HTML script tags or ONxxx attrbutes), and building ASTs. You can define your own analysis procedures, use source-level patterns to recognize code or transform the code; these manipulate the ASTs. After transformation, you can regenerate valid source text, including comments. You can export the ASTs to another language as XML if you want, but DMS contains so much useful machinery to support program analysis and transformation that I believe that temptation will go away when you look closely.
DMS works for a wide variety of other languages, too.