def _build_vyper_ast_init_kwargs( source_code: str, node: python_ast.AST, vyper_class: vyper_ast.VyperNode, source_id: int, ) -> Generator: start = node.first_token.start if hasattr(node, 'first_token') else ( None, None) # type: ignore yield ('col_offset', start[1]) yield ('lineno', start[0]) yield ('node_id', node.node_id) # type: ignore yield ('source_code', source_code) end = node.last_token.end if hasattr(node, 'last_token') else ( None, None) # type: ignore yield ('end_lineno', end[0]) yield ('end_col_offset', end[1]) if hasattr(node, 'last_token'): start_pos = node.first_token.startpos # type: ignore end_pos = node.last_token.endpos # type: ignore yield ('src', f"{start_pos}:{end_pos-start_pos}:{source_id}") if isinstance(node, python_ast.ClassDef): yield ('class_type', node.class_type) # type: ignore for field_name in vyper_class.get_slots(): if hasattr(node, field_name): yield (field_name, parse_python_ast(source_code=source_code, node=getattr(node, field_name), source_id=source_id))
def to_python_ast(vyper_ast_node: vyper_ast.VyperNode) -> python_ast.AST: if isinstance(vyper_ast_node, list): return [to_python_ast(n) for n in vyper_ast_node] elif isinstance(vyper_ast_node, vyper_ast.VyperNode): class_name = vyper_ast_node.__class__.__name__ if hasattr(python_ast, class_name): py_klass = getattr(python_ast, class_name) return py_klass( **{ k: to_python_ast(getattr(vyper_ast_node, k, None)) for k in vyper_ast_node.get_slots() }) else: raise CompilerPanic( f'Unknown vyper AST class "{class_name}" provided.') else: return vyper_ast_node
def _ast_to_dict(node: vyper_ast.VyperNode) -> Generator: for f in node.get_slots(): if f not in DICT_AST_SKIPLIST: yield (f, ast_to_dict(getattr(node, f, None))) yield ('ast_type', node.__class__.__name__)