]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/parso/__init__.py
2 Parso is a Python parser that supports error recovery and round-trip parsing
3 for different Python versions (in multiple Python versions). Parso is also able
4 to list multiple syntax errors in your python file.
6 Parso has been battle-tested by jedi_. It was pulled out of jedi to be useful
7 for other projects as well.
9 Parso consists of a small API to parse Python and analyse the syntax tree.
11 .. _jedi: https://github.com/davidhalter/jedi
16 >>> module = parso.parse('hello + 1', version="3.9")
17 >>> expr = module.children[0]
19 PythonNode(arith_expr, [<Name: hello@1,0>, <Operator: +>, <Number: 1>])
20 >>> print(expr.get_code())
22 >>> name = expr.children[0]
30 To list multiple issues:
32 >>> grammar = parso.load_grammar()
33 >>> module = grammar.parse('foo +\nbar\ncontinue')
34 >>> error1, error2 = grammar.iter_errors(module)
36 'SyntaxError: invalid syntax'
38 "SyntaxError: 'continue' not properly in loop"
41 from parso
.parser
import ParserSyntaxError
42 from parso
.grammar
import Grammar
, load_grammar
43 from parso
.utils
import split_lines
, python_bytes_to_unicode
49 def parse(code
=None, **kwargs
):
51 A utility function to avoid loading grammars.
52 Params are documented in :py:meth:`parso.Grammar.parse`.
54 :param str version: The version used by :py:func:`parso.load_grammar`.
56 version
= kwargs
.pop('version', None)
57 grammar
= load_grammar(version
=version
)
58 return grammar
.parse(code
, **kwargs
)