A tool for exploring the abstrax syntrax tree generated by solc.
Project description
solc-ast
A tool for exploring the Solidity abstrax syntrax tree as generated by the solc compiler.
Installation
You can install the latest release via pip
:
$ pip install py-solc-ast
Or clone the repo and use setuptools
:
$ python setup.py install
Usage
First, use py-solc-x to compile your contracts to the standard JSON output format.
>>> import json
>>> import solcx
>>> input_json = json.load(open('input.json'))
>>> output_json = solcx.compile_standard(input_json)
Next, import solcast
and initialize using from_standard_output_json
or from_standard_output
. This returns a list of SourceUnit
objects, which each represent the base node in a Solidity AST.
>>> import solcast
>>> nodes = solcast.from_standard_output(output_json)
From the initial objects, you can explore the AST:
>>> nodes
[<SourceUnit iterable object 'contracts/Token.sol'>]
>>> s = nodes[0]
>>> s
<SourceUnit iterable object 'contracts/Token.sol'>
>>> s.keys()
['children', 'contract_id', 'contracts', 'depth', 'keys', 'name', 'node_type', 'offset', 'parent', 'path', 'value']
>>> s.contracts
[<ContractDefinition iterable 'Token'>]
>>> s[0]
<ContractDefinition iterable 'Token'>
>>> s['Token']
<ContractDefinition iterable 'Token'>
>>> s['Token'].keys()
['children', 'contract_id', 'depth', 'functions', 'keys', 'name', 'node_class', 'node_type', 'offset', 'parent', 'value']
>>> s['Token'].functions
[<FunctionDefinition iterable '<constructor>'>, <FunctionDefinition iterable '<fallback>'>, <FunctionDefinition iterable 'balanceOf'>, <FunctionDefinition iterable 'allowance'>, <FunctionDefinition iterable 'approve'>, <FunctionDefinition iterable 'transfer'>, <FunctionDefinition iterable 'transferFrom'>]
>>> s['Token']['transfer']
<FunctionDefinition iterable 'transfer'>
>>> s['Token']['transfer'].statements
[<ExpressionStatement.FunctionCall 'require(balances[msg.sender] >= _value, Insufficient Balance)'>, <ExpressionStatement.Assignment iterable uint256 'balances[msg.sender] = balances[msg.sender].sub(_value)'>, <ExpressionStatement.Assignment iterable uint256 'balances[_to] = balances[_to].add(_value)'>, <EmitStatement.FunctionCall 'Transfer'>, <Return.Literal bool 'true'>]
Use the Node.children
and Node.parents
methods to access and filter related nodes:
>>> node = s['Token']['transfer']
>>> node.children(depth=1)
[<ExpressionStatement.FunctionCall 'require(balances[msg.sender] >= _value, Insufficient Balance)'>, <ExpressionStatement.Assignment iterable uint256 'balances[msg.sender] = balances[msg.sender].sub(_value)'>, <ExpressionStatement.Assignment iterable uint256 'balances[_to] = balances[_to].add(_value)'>, <EmitStatement.FunctionCall 'Transfer'>, <Return.Literal bool 'true'>]
>>> node.children(include_children=False, filters={'node_type': "FunctionCall", 'name': "require"})
[<ExpressionStatement.FunctionCall 'require(balances[msg.sender] >= _value, Insufficient Balance)'>]
>>> node.parents()
[<ContractDefinition iterable 'Token'>, <SourceUnit iterable object 'contracts/Token.sol'>]
Calling help
on either of these methods provides a more detailed explanation of their functionality.
Development
This project is still in development and should be considered an early alpha. All feedback and contributions are welcomed!
Not all nodes have been implemented yet. From any object, you can use the Node._unimplemented
method to get a list of keys that contain AST nodes that have not yet been included. The raw json data is stored at Node._node
.
>>> s['Token']['transfer']._unimplemented()
['parameters', 'returnParameters']
>>> s['Token']['transfer']._node['returnParameters']
{'id': 328, 'nodeType': 'ParameterList', 'parameters': [{'constant': False, 'id': 327, 'name': '', 'nodeType': 'VariableDeclaration', 'scope': 373, 'src': '1573:4:2', 'stateVariable': False, 'storageLocation': 'default', 'typeDescriptions': {'typeIdentifier': 't_bool', 'typeString': 'bool'}, 'typeName': {'id': 326, 'name': 'bool', 'nodeType': 'ElementaryTypeName', 'src': '1573:4:2', 'typeDescriptions': {'typeIdentifier': 't_bool', 'typeString': 'bool'}}, 'value': None, 'visibility': 'internal'}], 'src': '1572:6:2'}
See the Solidity documentation for information about the AST grammar.
License
This project is licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file py-solc-ast-0.1.2.tar.gz
.
File metadata
- Download URL: py-solc-ast-0.1.2.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb759d8de33b79cbc2d344a17fd8b4a07768230455ee820cf09a898ca067cd4f |
|
MD5 | 6f88460f9217b7563e01e3f718dcf791 |
|
BLAKE2b-256 | 0460bf09a6872ca7366b082645983df0d480b6819c281554db2dd7528af386a9 |
File details
Details for the file py_solc_ast-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: py_solc_ast-0.1.2-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3fdb06601f1480fa3366ab4b794380a6ef4714dea93c9d489c46f1a5d172537 |
|
MD5 | 80d8c2908dbc30aa287dc2fec5ddc141 |
|
BLAKE2b-256 | 96e5b68c1ddb77566390fe4a7b31b9f2e4119cbe832572fc9128ad3920fdf761 |