mirror of
https://github.com/Instagram/LibCST.git
synced 2025-12-23 10:35:53 +00:00
- Now that we're exporting everything from the top-level libcst package, automodule makes a mess of things. - While it does create more boilerplate, using autoclass/autofunction instead of automodule gives us a lot more control over the order in which nodes are presented, so we can group them by various categories. This diff also exports BaseParenthesizableWhitespace, because nodes refer to it by type, and so it's needed for documentation purposes.
40 lines
1,018 B
ReStructuredText
40 lines
1,018 B
ReStructuredText
Parsing
|
|
=======
|
|
|
|
The parser functions accept source code and an optional configuration object,
|
|
and will generate :class:`libcst.CSTNode` objects.
|
|
|
|
:func:`.parse_module` is the most useful function here, since it accepts the
|
|
entire contents of a file and returns a new tree, but :func:`.parse_expression`
|
|
and :func:`.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
|