Doesn't seem to be under active development anymore but you could check out pynarcissus, http://code.google.com/p/pynarcissus/source/browse/trunk/jsparser.py
Seems like a binding to V8 (JavaScript interpreter in Google Chromium) is available also, http://www.advogato.org/article/985.html
Answer from basicxman on Stack Overflow
» pip install esprima
Doesn't seem to be under active development anymore but you could check out pynarcissus, http://code.google.com/p/pynarcissus/source/browse/trunk/jsparser.py
Seems like a binding to V8 (JavaScript interpreter in Google Chromium) is available also, http://www.advogato.org/article/985.html
There is one, of an unknown level of completeness, written in RPython (a subset of Python, that is to say, it runs as normal Python): https://bitbucket.org/pypy/lang-js/overview
» pip install javascripthon
I don't want to use the term concise/terse, because many people take that to be a synonym for clever/confusing.
But Python is said to be a very readable language, due to syntax, whitespace rules, lack of {} etc, and is said to be 'executable pseudocode'. ES6 was a huge advance for JS and I find it a great mix of power and expressiveness.
Python:
def square(n): return n*n square2 = lambda x: x**2 def adder(x): return lambda y: x + y
ES6:
function square(x) { return x*x }
const square2 = x => x**x
const adder = x => y => x + yMaybe its because I'm used to it, but I find the JS versions easier to read/understand. Interestingly, the regular function syntax in python isn't more verbose than the lambda unlike in JS. Is there any difference between the Python version of square and square2?
I know python has things like list comprehensions and probably other things I'm not aware of that make it more powerful, and I'm curious how the 2 compare today.
Nowadays, there is at least one better tool, called slimit:
SlimIt is a JavaScript minifier written in Python. It compiles JavaScript into more compact code so that it downloads and runs faster.
SlimIt also provides a library that includes a JavaScript parser, lexer, pretty printer and a tree visitor.
Demo:
Imagine we have the following javascript code:
$.ajax({
type: "POST",
url: 'http://www.example.com',
data: {
email: '[email protected]',
phone: '9999999999',
name: 'XYZ'
}
});
And now we need to get email, phone and name values from the data object.
The idea here would be to instantiate a slimit parser, visit all nodes, filter all assignments and put them into the dictionary:
from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor
data = """
$.ajax({
type: "POST",
url: 'http://www.example.com',
data: {
email: '[email protected]',
phone: '9999999999',
name: 'XYZ'
}
});
"""
parser = Parser()
tree = parser.parse(data)
fields = {getattr(node.left, 'value', ''): getattr(node.right, 'value', '')
for node in nodevisitor.visit(tree)
if isinstance(node, ast.Assign)}
print fields
It prints:
{'name': "'XYZ'",
'url': "'http://www.example.com'",
'type': '"POST"',
'phone': "'9999999999'",
'data': '',
'email': "'[email protected]'"}
ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages.
The ANTLR site provides many grammars, including one for JavaScript.
As it happens, there is a Python API available - so you can call the lexer (recognizer) generated from the grammar directly from Python (good luck).