Parse JavaScript to instrument code

2024/10/14 13:26:06

I need to split a JavaScript file into single instructions. For example

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

has to be separated into three instructions. (assignment, function call and function definition).

Basically I need to instrument the code, injecting code between these instructions to perform checks. Splitting by ";" wouldn't obviously work because you can also end instructions with newlines and maybe I don't want to instrument code inside function and class definitions (I don't know yet). I took a course about grammars with flex/Bison but in this case the semantic action for this rule would be "print all the descendants in the parse tree and put my code at the end" which can't be done with basic Bison I think. How do I do this? I also need to split the code because I need to interface with Python with python-spidermonkey. Or... is there a library out there already which saves me from reinventing the wheel? It doesn't have to be in Python.

Answer

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

https://en.xdnf.cn/q/69411.html

Related Q&A

Converting all files (.jpg to .png) from a directory in Python

Im trying to convert all files from a directory from .jpg to .png. The name should remain the same, just the format would change.Ive been doing some researches and came to this:from PIL import Image im…

AssertionError: Gaps in blk ref_locs when unstack() dataframe

I am trying to unstack() data in a Pandas dataframe, but I keep getting this error, and Im not sure why. Here is my code so far with a sample of my data. My attempt to fix it was to remove all rows whe…

Python does not consider distutils.cfg

I have tried everything given and the tutorials all point in the same direction about using mingw as a compiler in python instead of visual c++.I do have visual c++ and mingw both. Problem started comi…

Is it possible to dynamically generate commands in Python Click

Im trying to generate click commands from a configuration file. Essentially, this pattern:import click@click.group() def main():passcommands = [foo, bar, baz] for c in commands:def _f():print("I a…

Different accuracy between python keras and keras in R

I build a image classification model in R by keras for R.Got about 98% accuracy, while got terrible accuracy in python.Keras version for R is 2.1.3, and 2.1.5 in pythonfollowing is the R model code:mod…

Named Entity Recognition in aspect-opinion extraction using dependency rule matching

Using Spacy, I extract aspect-opinion pairs from a text, based on the grammar rules that I defined. Rules are based on POS tags and dependency tags, which is obtained by token.pos_ and token.dep_. Belo…

Python Socket : AttributeError: __exit__

I try to run example from : https://docs.python.org/3/library/socketserver.html#socketserver-tcpserver-example in my laptop but it didnt work.Server :import socketserverclass MyTCPHandler(socketserver.…

How to save pygame Surface as an image to memory (and not to disk)

I am developing a time-critical app on a Raspberry PI, and I need to send an image over the wire. When my image is captured, I am doing like this:# pygame.camera.Camera captures images as a Surface pyg…

Plotting Precision-Recall curve when using cross-validation in scikit-learn

Im using cross-validation to evaluate the performance of a classifier with scikit-learn and I want to plot the Precision-Recall curve. I found an example on scikit-learn`s website to plot the PR curve …

The SECRET_KEY setting must not be empty || Available at Settings.py

I tried to find this bug, but dont know how to solve it.I kept getting error message "The SECRET_KEY setting must not be empty." when executing populate_rango.pyI have checked on settings.py …