[docs]classPyTranslationUnit:"""Represents a parsed translation unit for a single source file. Manages parsing, memory management, and provides convenient access to the underlying translation unit's resources at a higher level. """def__init__(self,lib:CDLL,tu_ptr:Any,path:Optional[str]=None)->None:self._tu_ptr=tu_ptrself._lib=libself._path=pathor"null"self._released=Falseifnotself._tu_ptr:raiseRuntimeError(f"Failed to parse translation unit from path: '{self._path}'. "f"The file may be missing, unreadable, or contain unrecoverable syntax errors.")
def__del__(self)->None:# Ensure resources are released when the instance is garbage collected.self.release()def__repr__(self)->str:returnf"PyTranslationUnit(path={self._path}, released={self._released})"
[docs]defnodes_count(self)->int:"""Gets the total number of AST nodes within this translation unit."""returnself._lib.getTranslationUnitNodesCount(self._tu_ptr)
[docs]deftokens_count(self)->int:"""Retrieves the count of tokens parsed in the translation unit."""returnself._lib.getTranslationUnitTokensCount(self._tu_ptr)
[docs]deferrors_count(self)->int:"""Retrieves the count of errors parsed in the translation unit."""returnself._lib.getTranslationUnitErrorsCount(self._tu_ptr)
[docs]defnodes(self)->list[PyASTNode]:"""A list of AST nodes parsed in the translation unit."""nodes=self._lib.getTranslationUnitNodes(self._tu_ptr).to_list(ASTNode)returnlist(map(lambdan:PyASTNode(self,n),nodes))
[docs]defroot_nodes(self)->list[PyASTNode]:"""The root AST nodes parsed in the translation unit."""root_nodes=[]index_list=self._lib.getTranslationUnitRootNodes(self._tu_ptr).to_list(c_uint32)forindexinindex_list:node=self._lib.getTranslationUnitNodeFromIndex(self._tu_ptr,index)root_nodes.append(PyASTNode(self,node))returnroot_nodes
[docs]deftokens(self)->list[ASTToken]:"""A list of AST tokens parsed in the translation unit."""returnself._lib.getTranslationUnitTokens(self._tu_ptr).to_list(ASTToken)
[docs]deferrors(self)->list[ErrorReport]:"""A list of ErrorReport instances for all errors encountered during parsing. Parsing continues despite errors, so this list may contain multiple reports."""returnself._lib.getTranslationUnitErrors(self._tu_ptr).to_list(ErrorReport)
[docs]defrelease(self)->None:"""Manually release TranslationUnit memory. Once completed, unit resources will be no longer available."""ifnotself._released:self._released=Trueifself._tu_ptr:self._lib.freeTranslationUnit(self._tu_ptr)
@propertydefsource(self)->str:"""Fetches the full source code as a decoded UTF-8 string."""returnself._lib.getTranslationUnitSource(self._tu_ptr).to_list(PyString)[0]@propertydefpath(self)->str:"""The original file path used for parsing."""returnself._path@propertydefreleased(self)->bool:"""Whether the translation unit has been released."""returnself._released@propertydefptr(self)->Any:"""Gets the low-level pointer to the translation unit."""returnself._tu_ptr@propertydeflib(self)->CDLL:"""Gets the low-level ctypes.CDLL handle for the native parser library."""returnself._lib