Package: py-solc-ast
solc-astA tool for exploring the Solidity abstrax syntrax tree as generated by thesolccompiler.InstallationYou can install the latest release viapip:$pipinstallpy-solc-astOr clone the repo and usesetuptools:$pythonsetup.pyinstallUsageFirst, usepy-solc-xto compile your contracts to thestandard JSON output format.>>>importjson>>>importsolcx>>>input_json=json.load(open('input.json'))>>>output_json=solcx.compile_standard(input_json)Next, importsolcastand initialize usingfrom_standard_output_jsonorfrom_standard_output. This returns a list ofSourceUnitobjects, which each represent the base AST node in a Solidity source file.>>>importsolcast>>>nodes=solcast.from_standard_output(output_json)>>>nodes[<SourceUnititerable'contracts/Token.sol'>,<SourceUnititerable'contracts/SafeMath.sol'>]You can also generate a singleSourceUnitdirectly from that source's AST:>>>importsolcast>>>node=solcast.from_ast(output_json["sources"]["contracts/Token.sol"]["ast"])>>>node<SourceUnititerable'contracts/Token.sol'>Interacting with NodesEach node has the following attributes:>>>node<FunctionDefinitioniterable'mul'>>>>node.depth# Number of nodes between this node and the SourceUnit2>>>node.offset# Absolute source offsets as a (start, stop) tuple(1693,2151)>>>node.contract_id# Contract ID as given by the standard compiler JSON2>>>node.fields# List of fields for this node['baseNodeType','documentation','id','implemented','kind','modifiers','name','nodeType','nodes','parameters','returnParameters','scope','src','stateMutability','superFunction','visibility']Fields mostly follow the expectedAST grammar. One notable difference:Blocknodes are omitted and the body of eachBlockis available within it's parent as the attributenodes. Nodes containing a body are iterable and can be accessed with list-like syntax. Additionally, any child node with anamefield is accessible using dict-like syntax.The following additional fields are also available:Most nodes have abaseNodeTypefield as defined ingrammar.pyContractDefinitionnodes havedependenciesandlibrariesfields that point to relatedContractDefitionnodesSome Examples:>>>source_node<SourceUnititerable'contracts/math/SafeMath.sol'>>>>source_node.keys()['absolutePath','children','contract_id','depth','exportedSymbols','id','is_child_of','is_parent_of','keys','nodeType','nodes','offset','parent','parents','src']>>>source_node.nodes[<PragmaDirectiveobject>,<ContractDefinitioniterable'SafeMath'>]>>>source_node[1]<ContractDefinitioniterable'SafeMath'>>>>source_node['SafeMath']<ContractDefinitioniterable'SafeMath'>>>>source_node['SafeMath'].keys()['baseContracts','children','contractDependencies','contractKind','contract_id','dependencies','depth','documentation','fullyImplemented','id','is_child_of','is_parent_of','keys','libraries','linearizedBaseContracts','name','nodeType','nodes','offset','parent','parents','scope','src']>>>source_node['SafeMath'].nodes[<FunctionDefinitioniterable'add'>,<FunctionDefinitioniterable'sub'>,<FunctionDefinitioniterable'mul'>,<FunctionDefinitioniterable'div'>,<FunctionDefinitioniterable'mod'>]>>>source_node['SafeMath']['mul']<FunctionDefinitioniterable'mul'>>>>source_node['SafeMath']['mul'][<IfStatementobject>,<VariableDeclarationStatementobject>,<FunctionCallobject>,<Returnobject>]Exploring the TreeTheNode.children()method is used to search and filter through child nodes of a given node. It takes any of the following keyword arguments:depth: Number of levels of children to traverse.0returns only this node.include_self: Includes this node in the results.include_parents: Includes nodes that match in the results, when they also have child nodes that match.include_children: If True, as soon as a match is found it's children will not be included in the search.required_offset: Only match nodes with a source offset that contains this offset.offset_limits: Only match nodes when their source offset is contained inside this source offset.filters: Dictionary of{'attribute': "value"}that children must match. Can also be given as a list of dicts, children that match any of the dicts will be returned.exclude_filter: Dictionary of{'attribute': "value"}that children cannot match.>>>node=s['Token']['transfer']>>>node.children(include_children=False,filters={'nodeType':"FunctionCall","expression.name":"require"})[<FunctionCall>]Node.parent()andNode.parents()are used to travel back up the tree. They take the following arguments:depth: Depth limit. If given as a negative value, it will be subtracted from this object's depth.filters: Dictionary of{'attribute': "value"}that parents must match.Node.parent()returns one result,Node.parents()returns a list of matches.>>>node.parents()[<ContractDefinitioniterable'Token'>,<SourceUnititerableobject'contracts/Token.sol'>]TestsTo run the test suite:$toxDevelopmentComments, questions, criticisms and pull requests are welcomed! Feel free to open an issue if you encounter a problem or would like to suggest a new feature.LicenseThis project is licensed under theMIT license.