[docs]classPyString:"""A special type for GenericSlice to treat an entire array of bytes as a single py string."""
[docs]classTranslationUnit(ctypes.Structure):"""Represents a parsed translation unit from the Zig AST."""
[docs]classGenericSlice(ctypes.Structure):"""A generic container for FFI slices, representing a pointer and it's length."""_fields_=[("ptr",ctypes.c_void_p),("len",ctypes.c_size_t),]def__repr__(self)->str:returnf"GenericSlice(len={self.len}, ptr={self.ptr})"
[docs]defto_list(self,ctype:type,encoding="utf-8")->List:"""Return the slice as a Python list. If `ctype` is `PyString`, the entire underlying byte array is decoded into a single Python string and returned as a list with one element. For other ctypes, the slice is returned as a list of individual instances. May raise a EOFError if the slice is empty."""ifself.is_empty:raiseEOFError("Slice is empty.")ifctypeisPyString:buf=bytes((ctypes.c_char*self.len).from_address(self.ptr))return[buf.decode(encoding)]ptr=ctypes.cast(self.ptr,ctypes.POINTER(ctype))return[ptr[i]foriinrange(self.len)]
@propertydefis_empty(self)->bool:"""Whether the slice is empty."""returnself.ptrisNone
[docs]classASTNode(ctypes.Structure):"""Represents a node in the Zig AST."""_fields_=[("index",ctypes.c_int),("tag_index",ctypes.c_int),("main_token",ctypes.c_int),]def__repr__(self)->str:return(f"ASTNode("f"tag={self.tag}, "f"index={self.index}, "f"main_token={self.main_token})")@propertydeftag(self)->NodeTag:"""The node tag as a `NodeTag` enum."""returnNodeTag(self.tag_index)
[docs]classASTToken(ctypes.Structure):"""Represents a token in the Zig AST."""_fields_=[("tag_index",ctypes.c_int),("start",ctypes.c_int),]def__repr__(self)->str:returnf"ASTToken(tag={self.tag}, start={self.start})"@propertydeftag(self)->TokenTag:"""The token tag as a `TokenTag` enum."""returnTokenTag(self.tag_index)
[docs]classErrorReport(ctypes.Structure):"""Represents a parsing error."""_fields_=[("tag_index",ctypes.c_int),("is_note",ctypes.c_bool),("token_is_prev",ctypes.c_bool),("token_index",ctypes.c_int),]def__repr__(self)->str:return(f"ErrorData("f"tag={self.tag}, "f"is_note={self.is_note}, "f"token_is_prev={self.token_is_prev}, "f"token_index={self.token_index})")@propertydeftag(self)->ErrorTag:"""The error tag as an `ErrorTag` enum."""returnErrorTag(self.tag_index)
[docs]classNodeParam(ctypes.Structure):"""Represents a node parameter."""_fields_=[("name",GenericSlice),("type",ASTNode),("is_comptime",ctypes.c_bool)]def__repr__(self)->str:returnf"NodeParam(name={self.name}, type={self.type}, is_comptime={self.is_comptime})"