Skip to content
A Solidity parser for Python built on top of a robust ANTLR4 grammar πŸ“š
Python Shell
Branch: master
Clone or download
Latest commit c0db3cd Sep 11, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
samples sample: add userdefined parameter type Sep 8, 2019
scripts initial commit Mar 5, 2019
solidity-antlr4 @ edce5c4 initial commit Mar 5, 2019
solidity_parser Add typeName to the Parameter Nodes Sep 7, 2019
.gitignore initial commit Mar 5, 2019
.gitmodules initial commit Mar 5, 2019 prepare 0.0.5 May 29, 2019
requirements.txt initial commit Mar 5, 2019 prepae v0.0.6 Sep 10, 2019


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 implementation and are therefore not pep8 compliant.


#> pip3 install solidity_parser
#> python3 -m solidity_parser <parse|outline> <path_to_contract.sol>   # print parse tree or sourceUnit outline


import sys
import pprint

from solidity_parser import parser

sourceUnit = parser.parse_file(sys.argv[1])
# see output below


{'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'}]},


Parse-tree nodes can be accessed both like dictionaries or via object attributes. Nodes always carry a type field to hint the type of AST node. The start node is always of type sourceUnit.

Accessing AST items in an Object Oriented fashion

# ... continuing from previous snippet

# subparse into objects for nicer interfaces:
# create a nested object structure from AST
sourceUnitObject = parser.objectify(sourceUnit)

# access imports, contracts, functions, ...  (see outline example in
sourceUnitObject.imports  # []
sourceUnitObject.pragmas  # []
sourceUnitObject.contracts.keys()  # get all contract names
sourceUnitObject.contracts["contractName"].functions.keys()  # get all functions in contract: "contractName"
sourceUnitObject.contracts["contractName"].functions["myFunction"].visibility  # get "myFunction"s visibility (or stateMutability)

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/
You can’t perform that action at this time.