{"id":15290543,"url":"https://github.com/consensys/python-solidity-parser","last_synced_at":"2025-03-22T14:04:37.703Z","repository":{"id":34267574,"uuid":"174017260","full_name":"Consensys/python-solidity-parser","owner":"Consensys","description":"An experimental Solidity parser for Python built on top of a robust ANTLR4 grammar 📚","archived":false,"fork":false,"pushed_at":"2024-06-24T13:23:30.000Z","size":234,"stargazers_count":148,"open_issues_count":8,"forks_count":38,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-03-14T16:04:00.163Z","etag":null,"topics":["antlr","parser","python","python3","solidity"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/solidity-parser/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Consensys.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-05T20:39:24.000Z","updated_at":"2025-02-10T12:54:29.000Z","dependencies_parsed_at":"2024-06-19T22:47:29.643Z","dependency_job_id":"7858c3cc-c538-4f59-8029-ef2a2395705a","html_url":"https://github.com/Consensys/python-solidity-parser","commit_stats":{"total_commits":38,"total_committers":4,"mean_commits":9.5,"dds":"0.10526315789473684","last_synced_commit":"5b0977c4a986f0177260f315e77d77d26a2c6510"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Consensys%2Fpython-solidity-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Consensys%2Fpython-solidity-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Consensys%2Fpython-solidity-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Consensys%2Fpython-solidity-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Consensys","download_url":"https://codeload.github.com/Consensys/python-solidity-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244966500,"owners_count":20539795,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["antlr","parser","python","python3","solidity"],"created_at":"2024-09-30T16:08:34.557Z","updated_at":"2025-03-22T14:04:37.671Z","avatar_url":"https://github.com/Consensys.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# python-solidity-parser\nAn experimental Solidity parser for Python built on top of a robust ANTLR4 grammar.\n\n**ⓘ** This is a **python3** port of the [javascript antlr parser](https://github.com/federicobond/solidity-parser-antlr) maintained by [@federicobond](https://github.com/federicobond/). Interfaces are intentionally following the javascript implementation and are therefore not pep8 compliant.\n\n\n\n## Install\n\n```\n#\u003e pip3 install solidity_parser\n#\u003e python3 -m solidity_parser \u003cparse|outline\u003e \u003cpath_to_contract.sol\u003e   # print parse tree or sourceUnit outline\n```\n\n## HowTo\n\n```python\n\nimport sys\nimport pprint\n\nfrom solidity_parser import parser\n\nsourceUnit = parser.parse_file(sys.argv[1], loc=False) # loc=True -\u003e add location information to ast nodes\npprint.pprint(sourceUnit)  \n# see output below\n\n```\n\noutput:\n````\n{'type': 'SourceUnit',\n 'children': [{'type': 'PragmaDirective',\n               'name': 'solidity',\n               'value': '^0.4.22'},\n              {'type': 'ContractDefinition'},\n               'baseContracts': [],\n               'kind': 'contract',\n               'name': 'SimpleAuction',\n               'subNodes': [{'initialValue': None,\n                             'type': 'StateVariableDeclaration',\n                             'variables': [{'expression': None,\n                                            'isDeclaredConst': False,\n                                            'isIndexed': False,\n                                            'isStateVar': True,\n                                            'name': 'beneficiary',\n                                            'type': 'VariableDeclaration',\n                                            'typeName': {'name': 'address',\n                                                         'type': 'ElementaryTypeName'},\n                                            'visibility': 'public'}]},\n...\n````\n\n### Nodes\n\nParse-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`.\n\n## Accessing AST items in an Object Oriented fashion\n\n```python\n# ... continuing from previous snippet\n\n# subparse into objects for nicer interfaces:\n# create a nested object structure from AST\nsourceUnitObject = parser.objectify(sourceUnit)\n\n# access imports, contracts, functions, ...  (see outline example in __main__.py)\nsourceUnitObject.imports  # []\nsourceUnitObject.pragmas  # []\nsourceUnitObject.contracts.keys()  # get all contract names\nsourceUnitObject.contracts[\"contractName\"].functions.keys()  # get all functions in contract: \"contractName\"\nsourceUnitObject.contracts[\"contractName\"].functions[\"myFunction\"].visibility  # get \"myFunction\"s visibility (or stateMutability)\n```\n\n\n## Generate the parser\n\nUpdate the grammar in `./solidity-antlr4/Solidity.g4` and run the antlr generator script to create the parser classes in `solidity_parser/solidity_antlr4`.\n```\n#\u003e bash script/antlr4.sh\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconsensys%2Fpython-solidity-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconsensys%2Fpython-solidity-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconsensys%2Fpython-solidity-parser/lists"}