| 1 |
|Donate| |PyPI Version| |PyPI License| |PyPI Format| |PyPI Status| |
| 2 |
|
| 3 |
**Esprima** (`esprima.org <http://esprima.org>`__, BSD license) is a |
| 4 |
high performance, standard-compliant |
| 5 |
`ECMAScript <http://www.ecma-international.org/publications/standards/Ecma-262.htm>`__ |
| 6 |
parser officially written in ECMAScript (also popularly known as |
| 7 |
`JavaScript <https://en.wikipedia.org/wiki/JavaScript>`__) and ported to |
| 8 |
Python. Esprima is created and maintained by `Ariya |
| 9 |
Hidayat <https://twitter.com/ariyahidayat>`__, with the help of `many |
| 10 |
contributors <https://github.com/jquery/esprima/contributors>`__. |
| 11 |
|
| 12 |
Python port is a line-by-line manual translation and was created and is |
| 13 |
maintained by `German Mendez Bravo |
| 14 |
(Kronuz) <https://twitter.com/germbravo>`__. |
| 15 |
|
| 16 |
Features |
| 17 |
~~~~~~~~ |
| 18 |
|
| 19 |
- Full support for ECMAScript 2017 (`ECMA-262 8th |
| 20 |
Edition <http://www.ecma-international.org/publications/standards/Ecma-262.htm>`__) |
| 21 |
- Sensible `syntax tree |
| 22 |
format <https://github.com/estree/estree/blob/master/es5.md>`__ as |
| 23 |
standardized by `ESTree project <https://github.com/estree/estree>`__ |
| 24 |
- Experimental support for `JSX <https://facebook.github.io/jsx/>`__, a |
| 25 |
syntax extension for `React <https://facebook.github.io/react/>`__ |
| 26 |
- Optional tracking of syntax node location (index-based and |
| 27 |
line-column) |
| 28 |
- `Heavily tested <http://esprima.org/test/ci.html>`__ (~1500 `unit |
| 29 |
tests <https://github.com/jquery/esprima/tree/master/test/fixtures>`__ |
| 30 |
with `full code |
| 31 |
coverage <https://codecov.io/github/jquery/esprima>`__) |
| 32 |
|
| 33 |
Installation |
| 34 |
~~~~~~~~~~~~ |
| 35 |
|
| 36 |
.. code:: shell |
| 37 |
|
| 38 |
pip install esprima |
| 39 |
|
| 40 |
API |
| 41 |
~~~ |
| 42 |
|
| 43 |
Esprima can be used to perform `lexical |
| 44 |
analysis <https://en.wikipedia.org/wiki/Lexical_analysis>`__ |
| 45 |
(tokenization) or `syntactic |
| 46 |
analysis <https://en.wikipedia.org/wiki/Parsing>`__ (parsing) of a |
| 47 |
JavaScript program. |
| 48 |
|
| 49 |
A simple example: |
| 50 |
|
| 51 |
.. code:: javascript |
| 52 |
|
| 53 |
>>> import esprima |
| 54 |
>>> program = 'const answer = 42' |
| 55 |
|
| 56 |
>>> esprima.tokenize(program) |
| 57 |
[{ |
| 58 |
type: "Keyword", |
| 59 |
value: "const" |
| 60 |
}, { |
| 61 |
type: "Identifier", |
| 62 |
value: "answer" |
| 63 |
}, { |
| 64 |
type: "Punctuator", |
| 65 |
value: "=" |
| 66 |
}, { |
| 67 |
type: "Numeric", |
| 68 |
value: "42" |
| 69 |
}] |
| 70 |
|
| 71 |
>>> esprima.parseScript(program) |
| 72 |
{ |
| 73 |
body: [ |
| 74 |
{ |
| 75 |
kind: "const", |
| 76 |
declarations: [ |
| 77 |
{ |
| 78 |
init: { |
| 79 |
raw: "42", |
| 80 |
type: "Literal", |
| 81 |
value: 42 |
| 82 |
}, |
| 83 |
type: "VariableDeclarator", |
| 84 |
id: { |
| 85 |
type: "Identifier", |
| 86 |
name: "answer" |
| 87 |
} |
| 88 |
} |
| 89 |
], |
| 90 |
type: "VariableDeclaration" |
| 91 |
} |
| 92 |
], |
| 93 |
type: "Program", |
| 94 |
sourceType: "script" |
| 95 |
} |
| 96 |
|
| 97 |
For more information, please read the `complete |
| 98 |
documentation <http://esprima.org/doc>`__. |
| 99 |
|
| 100 |
.. |Donate| image:: https://img.shields.io/badge/Donate-PayPal-green.svg |
| 101 |
:target: https://www.paypal.me/Kronuz/25 |
| 102 |
.. |PyPI Version| image:: https://img.shields.io/pypi/v/esprima.svg |
| 103 |
:target: https://pypi.python.org/pypi/esprima |
| 104 |
.. |PyPI License| image:: https://img.shields.io/pypi/l/esprima.svg |
| 105 |
:target: https://pypi.python.org/pypi/esprima |
| 106 |
.. |PyPI Wheel| image:: https://img.shields.io/pypi/wheel/esprima.svg |
| 107 |
:target: https://pypi.python.org/pypi/esprima |
| 108 |
.. |PyPI Format| image:: https://img.shields.io/pypi/format/esprima.svg |
| 109 |
:target: https://pypi.python.org/pypi/esprima |
| 110 |
.. |PyPI Python Version| image:: https://img.shields.io/pypi/pyversions/esprima.svg |
| 111 |
:target: https://pypi.python.org/pypi/esprima |
| 112 |
.. |PyPI Implementation| image:: https://img.shields.io/pypi/implementation/esprima.svg |
| 113 |
:target: https://pypi.python.org/pypi/esprima |
| 114 |
.. |PyPI Status| image:: https://img.shields.io/pypi/status/esprima.svg |
| 115 |
:target: https://pypi.python.org/pypi/esprima |
| 116 |
.. |PyPI Downloads| image:: https://img.shields.io/pypi/dm/esprima.svg |
| 117 |
:target: https://pypi.python.org/pypi/esprima |