Skip to content

Python

PythonParser

Bases: ParserBase

Python specific parser. Uses a generic grammar for multiple versions of python. Uses tree_sitter to get the AST

Source code in src/parser/languages/python.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class PythonParser(ParserBase, lang=Extension.python.name):
    """
    Python specific parser. Uses a generic grammar for multiple versions of python. Uses tree_sitter to get the AST
    """

    def __init__(self, library_path: Path | str):
        super().__init__(library_path)
        self.language: Language = Language(tsp.language())
        self.parser: Parser = Parser(self.language)
        self.identifiers_pattern: str = """
                                        ((identifier) @identifier)
                                        """
        self.identifiers_query = self.language.query(self.identifiers_pattern)

        self.keywords = set(keyword.kwlist)  # Use python's built in keyword list
        self.keywords.update(['self', 'cls'])