JavaScript parsing for tool code

I need to split the JavaScript file into separate instructions. for example

a = 2; foo() function bar() { b = 5; print("spam"); } 

must be divided into three teams. (purpose, function call and function definition).

Basically I need to use the code, entering the code between these instructions to perform the checks. Separation of ";" won't work, because you can also end instructions with new characters, and maybe I don't want to enter the code inside the functions and class definitions (I don't know yet). I took a course on grammars with flex / Bison , but in this case the semantic action for this rule is “print all descendants in the parse tree and put my code at the end”, which cannot be done with the base Bison, I think. How can I do it? I also need to split the code because I need to interact with Python using python-spidermonkey. Or ... is there a library that saves me from rethinking the wheel? It should not be in Python.

+7
javascript python parsing spidermonkey
source share
5 answers

Why not use a JavaScript parser? There are many, including the Python API for ANTLR and the Python wrapper around SpiderMonkey.

+4
source share

JavaScript is harder to parse; you need a full javascript parser. DMS Software Reengineering Toolkit can analyze the full JavaScript and create the corresponding AST . AST operators can then be used to navigate the tree to “split it up”. However, it’s even easier to apply source transformations to the source that are looking for a pattern of one syntax (JavaScript) and replace it with another. You can use such transformations to insert toolkits into code, rather than splitting code to do tricks to perform inserts. After the conversions are completed, DMS can restore the valid JavaScript code (complete with orignal comments if they are not affected).

+2
source share

Why not use an existing JavaScript interpreter like Rhino (Java) or python-spidermonkey (not sure if it is still alive)? It will analyze JS, and then you can examine the resulting parse tree. I'm not sure how easy it will be to recreate the source code, but it mostly depends on how readable the toolkit code should be. If no one is looking at him, just create a truly compact form.

pajamas may also be of interest; This is Python for the JavaScript transporter.

[EDIT] Although this does not solve your problem at a glance, you can use it for a different approach: instead of processing JavaScript, write your Python code instead (which can be easily measured, all the tools are already there), and then convert JavaScript result.

Finally, if you want to solve your problem in Python, but cannot find a parser: use the Java engine to add comments to the code, which you can search in Python to control the code.

0
source share

Why not try javascript beautifier?

For example http://jsbeautifier.org/

Or see the JavaScript shell code on the command line, which works on Windows and Linux

0
source share

Forget your parser. https://bitbucket.org/mvantellingen/pyjsparser is an excellent and complete parser. I fixed a couple of errors here: https://bitbucket.org/nullie/pyjsparser

0
source share

All Articles