File: parser.rst

package info (click to toggle)
python-libcst 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,240 kB
  • sloc: python: 78,096; makefile: 15; sh: 2
file content (49 lines) | stat: -rw-r--r-- 1,252 bytes parent folder | download | duplicates (2)
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
Parsing
=======

The parser functions accept source code and an optional configuration object,
and will generate :class:`~libcst.CSTNode` objects.

:func:`~libcst.parse_module` is the most useful function here, since it accepts
the entire contents of a file and returns a new tree, but
:func:`~libcst.parse_expression` and :func:`~libcst.parse_statement` are useful
when inserting new nodes into the tree, because they're easier to use than the
equivalent node constructors.

>>> import libcst as cst
>>> cst.parse_expression("1 + 2")
BinaryOperation(
    left=Integer(
        value='1',
        lpar=[],
        rpar=[],
    ),
    operator=Add(
        whitespace_before=SimpleWhitespace(
            value=' ',
        ),
        whitespace_after=SimpleWhitespace(
            value=' ',
        ),
    ),
    right=Integer(
        value='2',
        lpar=[],
        rpar=[],
    ),
    lpar=[],
    rpar=[],
)


.. autofunction:: libcst.parse_module
.. autofunction:: libcst.parse_expression
.. autofunction:: libcst.parse_statement
.. autoclass:: libcst.PartialParserConfig

Syntax Errors
-------------

.. autoclass:: libcst.ParserSyntaxError
   :members: message, raw_line, raw_column, editor_line, editor_column
   :special-members: __str__