Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# .github/workflows/pytest-ci.yml

name: build

on:
push:
tags:
- "[0-9]+.[0-9]+"
- "[0-9]+.[0-9]+.[0-9]+"
pull_request:
paths:
- ".github/workflows/build.yml"
workflow_dispatch: {}

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: actions/setup-python@v4
with:
python-version: "3.9"
cache: "pip" # caching pip dependencies
- name: Install dependencies
run: |
pip install -r requirements.txt
python setup.py install
bash scripts/start.sh
7 changes: 0 additions & 7 deletions scripts/antlr4.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ set -o errexit

# antlr -Dlanguage=Python3 solidity-antlr4/Solidity.g4 -o src -visitor


git submodule sync --recursive && git submodule update --init --recursive
git submodule foreach --recursive git clean -ffdx
git submodule update --remote --rebase solidity-antlr4


bash solidity-antlr4/build.sh
sleep 1

[ ! -f ./solidity-antlr4/antlr4.jar ] && { echo "ANTLR4 Jar does not exist."; exit 1; }
Expand Down
38 changes: 38 additions & 0 deletions scripts/prebuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# shellcheck disable=SC2034

echo "$JAVA_HOME"


ANTLR_JAR="antlr4.jar"
ANTLR_JAR_URI="https://www.antlr.org/download/antlr-4.9-complete.jar"


GRAMMAR="Solidity"
START_RULE="sourceUnit"
TEST_FILE="test.sol"
ERROR_PATTERN="mismatched|extraneous"

function download_antlr4
{
if [[ ! -e "$ANTLR_JAR" ]]
then
curl -sL "${ANTLR_JAR_URI}" -o "${ANTLR_JAR}"
fi
}

echo "Downloading Antlr 4.9..."

download_antlr4

mkdir -p target/

echo "Creating parser"
(
java -jar $ANTLR_JAR $GRAMMAR.g4 -o src/
sleep 1
javac -classpath $ANTLR_JAR src/*.java -d target/
)

echo "Artifacts Generated"
exit 0
19 changes: 19 additions & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -o errexit

git submodule sync --recursive && git submodule update --init --recursive
git submodule foreach --recursive git clean -ffdx
git submodule update --remote --rebase solidity-antlr4
sleep 1

# Sanity Check that Submodules was successful
[ ! -f ./solidity-antlr4/scripts/build.sh ] && { echo "Solidity Antlr4 does not exist."; exit 1; }

cp scripts/build.sh solidity-antlr4/setup.sh
bash solidity-antlr4/setup.sh
sleep 1
bash scripts/anltr4.sh

echo "Solidity Antlr4.9 build complete!"

exit 0
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def read(fname):


version = "0.1.3"
name = "python-solidity-parser"
name = "solidity-parser"

setup(
name=name,
Expand All @@ -32,8 +32,8 @@ def read(fname):
),
license="MIT",
keywords=["solidity", "parser", "antlr"],
url="https://github.com/consensys/python-%s/" % name,
download_url="https://github.com/consensys/python-%s/tarball/v%s" % (name, version),
url="https://github.com/manifoldfinance/python-%s/" % name,
download_url="https://github.com/manifoldfinance/python-%s/tarball/v%s" % (name, version),
long_description=read("README.md") if os.path.isfile("README.md") else "",
long_description_content_type="text/markdown",
# python setup.py register -r https://testpypi.python.org/pypi
Expand Down
32 changes: 32 additions & 0 deletions solidity-antlr4/.github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Python package

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with ruff
run: |
# stop the build if there are Python syntax errors or undefined names
ruff --format=github --select=E9,F63,F7,F82 --target-version=py37 .
# default set of ruff rules with GitHub Annotations
ruff --format=github --target-version=py37 .
- name: Test with pytest
run: |
pytest
118 changes: 118 additions & 0 deletions solidity-antlr4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
.DS_Store
.env
3 changes: 3 additions & 0 deletions solidity-antlr4/.gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "solidity-antlr4"]
path = solidity-antlr4
url = https://github.com/solidity-parser/antlr.git
22 changes: 22 additions & 0 deletions solidity-antlr4/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2020 Consensys, Inc.
Copyright (c) 2023 Manifold Finance, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
81 changes: 81 additions & 0 deletions solidity-antlr4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# python-solidity-parser
An experimental Solidity parser for Python built on top of a robust ANTLR4 grammar.

**ⓘ** 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.



## Install

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

## HowTo

```python

import sys
import pprint

from solidity_parser import parser

sourceUnit = parser.parse_file(sys.argv[1], loc=False) # loc=True -> add location information to ast nodes
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

```python
# ... 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
```
1 change: 1 addition & 0 deletions solidity-antlr4/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
antlr4-python3-runtime==4.9.3
Loading