A Solidity parser for Python built on top of a robust ANTLR4 grammar
Project description
python-solidity-parser
A Solidity parser for Python built on top of a robust ANTLR4 grammar
ⓘ This is a python3 port of the javascript antlr parser maintained by @federicobond. Interfaces are intentionally following the javascript impelementation and therefore not pep8 compliant.
Install
#> pip3 install solidity_parser
#> python3 -m solidity_parser <path_to_contract.sol> # prettyprints tree
HowTo
import sys
import pprint
from solidity_parser import parser
sourceUnit = parser.parse_file(sys.argv[1])
pprint.pprint(sourceUnit)
output:
{'type': 'SourceUnit',
'children': [{'type': 'PragmaDirective',
'name': 'solidity',
'value': '^0.4.22'},
{'type': 'ContractDefinition'},
'baseContracts': [],
'kind': 'contract',
'name': 'SimpleAuction',
'subNodes': [{'initialValue': None,
'type': 'StateVariableDeclaration',
'variables': [{'expression': None,
'isDeclaredConst': False,
'isIndexed': False,
'isStateVar': True,
'name': 'beneficiary',
'type': 'VariableDeclaration',
'typeName': {'name': 'address',
'type': 'ElementaryTypeName'},
'visibility': 'public'}]},
...
Nodes
parse nodes can be accessed like dictionaries or object attributes. Nodes always carry a type
field to denote the type of information provided. The first node is of type sourceUnit
.
Generate the parser
Update the grammar in ./solidity-antlr4/Solidity.g4
and run the antlr generator script to create the parser classes in solidity_parser/solidity_antlr4
.
#> bash script/antlr4.sh
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for solidity_parser-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f24ac3380562d9bb4f2266a8a607ff8d48bd8c234193cab16ede1be24681402e |
|
MD5 | 3f3b54c9740d2699c05be09418a3ed86 |
|
BLAKE2b-256 | 19730725ef1e23f354e2742d54a8eddc54eb3f7bfa0503022ac681a04b61e09b |