summaryrefslogtreecommitdiffstats
path: root/third_party/python/esprima/README
blob: 442fbc7b11fa8d02fe0a49b88101d43301f5967f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|Donate| |PyPI Version| |PyPI License| |PyPI Format| |PyPI Status|

**Esprima** (`esprima.org <http://esprima.org>`__, BSD license) is a
high performance, standard-compliant
`ECMAScript <http://www.ecma-international.org/publications/standards/Ecma-262.htm>`__
parser officially written in ECMAScript (also popularly known as
`JavaScript <https://en.wikipedia.org/wiki/JavaScript>`__) and ported to
Python. Esprima is created and maintained by `Ariya
Hidayat <https://twitter.com/ariyahidayat>`__, with the help of `many
contributors <https://github.com/jquery/esprima/contributors>`__.

Python port is a line-by-line manual translation and was created and is
maintained by `German Mendez Bravo
(Kronuz) <https://twitter.com/germbravo>`__.

Features
~~~~~~~~

-  Full support for ECMAScript 2017 (`ECMA-262 8th
   Edition <http://www.ecma-international.org/publications/standards/Ecma-262.htm>`__)
-  Sensible `syntax tree
   format <https://github.com/estree/estree/blob/master/es5.md>`__ as
   standardized by `ESTree project <https://github.com/estree/estree>`__
-  Experimental support for `JSX <https://facebook.github.io/jsx/>`__, a
   syntax extension for `React <https://facebook.github.io/react/>`__
-  Optional tracking of syntax node location (index-based and
   line-column)
-  `Heavily tested <http://esprima.org/test/ci.html>`__ (~1500 `unit
   tests <https://github.com/jquery/esprima/tree/master/test/fixtures>`__
   with `full code
   coverage <https://codecov.io/github/jquery/esprima>`__)

Installation
~~~~~~~~~~~~

.. code:: shell

    pip install esprima

API
~~~

Esprima can be used to perform `lexical
analysis <https://en.wikipedia.org/wiki/Lexical_analysis>`__
(tokenization) or `syntactic
analysis <https://en.wikipedia.org/wiki/Parsing>`__ (parsing) of a
JavaScript program.

A simple example:

.. code:: javascript

    >>> import esprima
    >>> program = 'const answer = 42'

    >>> esprima.tokenize(program)
    [{
        type: "Keyword",
        value: "const"
    }, {
        type: "Identifier",
        value: "answer"
    }, {
        type: "Punctuator",
        value: "="
    }, {
        type: "Numeric",
        value: "42"
    }]

    >>> esprima.parseScript(program)
    {
        body: [
            {
                kind: "const",
                declarations: [
                    {
                        init: {
                            raw: "42",
                            type: "Literal",
                            value: 42
                        },
                        type: "VariableDeclarator",
                        id: {
                            type: "Identifier",
                            name: "answer"
                        }
                    }
                ],
                type: "VariableDeclaration"
            }
        ],
        type: "Program",
        sourceType: "script"
    }

For more information, please read the `complete
documentation <http://esprima.org/doc>`__.

.. |Donate| image:: https://img.shields.io/badge/Donate-PayPal-green.svg
   :target: https://www.paypal.me/Kronuz/25
.. |PyPI Version| image:: https://img.shields.io/pypi/v/esprima.svg
   :target: https://pypi.python.org/pypi/esprima
.. |PyPI License| image:: https://img.shields.io/pypi/l/esprima.svg
   :target: https://pypi.python.org/pypi/esprima
.. |PyPI Wheel| image:: https://img.shields.io/pypi/wheel/esprima.svg
   :target: https://pypi.python.org/pypi/esprima
.. |PyPI Format| image:: https://img.shields.io/pypi/format/esprima.svg
   :target: https://pypi.python.org/pypi/esprima
.. |PyPI Python Version| image:: https://img.shields.io/pypi/pyversions/esprima.svg
   :target: https://pypi.python.org/pypi/esprima
.. |PyPI Implementation| image:: https://img.shields.io/pypi/implementation/esprima.svg
   :target: https://pypi.python.org/pypi/esprima
.. |PyPI Status| image:: https://img.shields.io/pypi/status/esprima.svg
   :target: https://pypi.python.org/pypi/esprima
.. |PyPI Downloads| image:: https://img.shields.io/pypi/dm/esprima.svg
   :target: https://pypi.python.org/pypi/esprima