Skip to main content

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

Install

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

HowTo

import sys
import pprint

from solidity_parser import parser

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

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-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 __main__.py)
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/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

solidity-parser-0.0.5.tar.gz (58.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

solidity_parser-0.0.5-py3.7.egg (151.5 kB view details)

Uploaded Egg

solidity_parser-0.0.5-py2-none-any.whl (69.4 kB view details)

Uploaded Python 2

File details

Details for the file solidity-parser-0.0.5.tar.gz.

File metadata

  • Download URL: solidity-parser-0.0.5.tar.gz
  • Upload date:
  • Size: 58.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for solidity-parser-0.0.5.tar.gz
Algorithm Hash digest
SHA256 6cd66fc2d4124e86805f8068107ce026a33aad45c416d115625f317ef969fd24
MD5 90a01de6212b047aa4a7a2fcd80e490e
BLAKE2b-256 087f3ffbd60d75be48c24f3ec1b0d6c2ce8388f01d0af31663a47460f7ea2e11

See more details on using hashes here.

File details

Details for the file solidity_parser-0.0.5-py3.7.egg.

File metadata

  • Download URL: solidity_parser-0.0.5-py3.7.egg
  • Upload date:
  • Size: 151.5 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for solidity_parser-0.0.5-py3.7.egg
Algorithm Hash digest
SHA256 0fb3c3590ae2d184002af0471c8d43b633c4132b6d4b2e4547f86d00129190c8
MD5 5a3a918f3789344e6511772956e647c7
BLAKE2b-256 b98cbe2b5134d2e7c6d79427307bd812bfdb6e17ffb5f0c4ad3bdbce6546cb38

See more details on using hashes here.

File details

Details for the file solidity_parser-0.0.5-py2-none-any.whl.

File metadata

  • Download URL: solidity_parser-0.0.5-py2-none-any.whl
  • Upload date:
  • Size: 69.4 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for solidity_parser-0.0.5-py2-none-any.whl
Algorithm Hash digest
SHA256 a11390310fa2a8828d736406201002ac47d32e9d94d13d23f50cd8a6290f6f77
MD5 e36fd7ee9b0efc6652f4e12e59fe7f11
BLAKE2b-256 4c5059978fbd70dea164a1eb3fa9eac214ded1d04964e7cf3792159f48184f6c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page